Scriptel OmniScript Tutorial 05 - Handling Device Membership Changes

From Scriptel Wiki
Jump to navigation Jump to search

Video tutorial

Scriptel OmniScript Tutorial 05 - Handling Device Membership Changes

Instructions

In this video tutorial, we'll walk through how to handle device membership changes. For example, what happens if someone unplugs the device or if they plug a new device in? Let's find out!

Video instructions follow:

You'll need a couple tools.

  • You'll need a recent web-browser: Safari 6+, Chrome 16+, Explorer 10+, Firefox 11+, or Opera 12.10+.
  • Next you'll need a text editor: Notepad will work, but any text editor should work.
  • Make sure OmniScript is installed and running, and you have a ScripTouch capture device hooked up.

For this example, we're going to assume that you're going to be using OmniScript installed on your local computer.

Step 1: Creating the Example Files

We'll start by creating the files for the example.

  1. Open up a folder, create two files: an HTML file and a JS file.
  2. Open up the HTML file and make a skeleton document.
  3. Add the doctype tag because this is an HTML5 application.
  4. Then create the open and close html tags.
  5. Next, create the head and body tags.
  6. Give the document a title and create a script tag that references the new script file.
  7. Now let's add an onload handler to our document body that calls a function we'll create in the JavaScript file in just a moment.
  8. Save the document and open up the JavaScript file.
  9. Create the function we just referenced in the onload handler in the html document.
  10. Double-check and make sure the function matches the one we're calling.

Once that's done we can start writing code.

Step 2: Writing the Code

  1. Let's start by creating a websocket along with a callback for the "connection established," "connection closed," and "data received" events.
  2. We'll put console.log() statements in each of the callbacks so we can verify they're being called when we run the application.
  3. Now, just like in the previous examples, we'll add handlers to our on message callback.
  4. We'll start by handling the "connection open" just like before, where we open the first device on the server as soon as we connect.
  5. Now we'll add a handler for the "device open" response of it. When you receive this event, we'll set a variable "dev connected" to true.
  6. We'll need to define this at the top of our function.
  7. Now let's handle the "connection device membership change" event. This gets called when our OmniScript server gains or loses a ScripTouch device.
  8. In here, we'll check to make sure we're connected. If we're not and we get this event, we'll try to connect to the first available device.
  9. Now, we'll handle "device close" response objects.
  10. These are given to us when either we asked to close a device or the device we're using is disconnected from the system. If this happens, we'll try to reconnect it to another device on the system if we can.
  11. Let's not forget to define the devices array at the top of the function, since it may or may not be defined where to get our first "device closed" response.

Now let's try opening the application.

Step 3: Testing

  1. Save and open your HTML file on a web browser.
  2. If everything goes well, it should connect to the device connected to your computer.
  3. Try plugging and unplugging the device a few times. Each time your application should reconnect the device.