WebSocket API JavaScript
Library
The WebSocket API Extension is discontinued. It is replaced by the JavaScript API Library. If you need assistance, please contact Technical Support.
The WebSocket API Extension comes packaged with a JavaScript library that allows web based applications to interact with the deviceWISE gateway's runtime process. The JavaScript library files are placed into the www/devicewise directory within the Staging Browser file system when the extension is installed.
Gateway HTTP Server
To support the JavaScript API calls into the gateway, the gateway's HTTP Server must be Started. The status of the gateway's HTTP Server can be viewed using the Workbench -> Administration -> HTTP Server tab. For more information, see HTTP Server.
Setup
To create a web page that makes use of the JavaScript API library, the following links must be included in the head of the main HTML page.
<script
src="devicewise/jquery-3.1.0.min.js"></script>
<script
src="devicewise/cookie.js"></script>
<script
src="devicewise/devicewiseUtil.js"></script>
<script
src="devicewise/devicewiseClient.js"></script>
<script
src="devicewise/devicewiseConstants.js"></script>
<script
src="devicewise/devicewiseDiagnostics.js"></script>
<script
src="devicewise/devicewiseVariable.js"></script>
<script
src="devicewise/devicewiseSQLite.js"></script>
<script
src="devicewise/devicewiseDevice.js"></script>
<script
src="devicewise/devicewiseTrigger.js"></script>
<script
src="devicewise/devicewiseProject.js"></script>
<script
src="devicewise/devicewiseChannel.js"></script>
<script
src="devicewise/devicewiseSystem.js"></script>
deviceWISE types
Datatype codes are accessed using the following definitions:
- devicewise.INT1
- devicewise.INT2
- devicewise.INT4
- devicewise.INT8
- devicewise.UINT1
- devicewise.UINT2
- devicewise.UINT4
- devicewise.UINT8
- devicewise.FLOAT4
- devicewise.FLOAT8
- devicewise.BOOL
- devicewise.STRING
deviceWISE driver code listing
The above list is incomplete and will not be updated to the latest driver codes. To find out the driver code of your device, use the devicewise.deviceInfo() function or export the device and view the type in the exported XML in the *.dwx file.
API functions
devicewise.connect(URL, callbackSuccess, callbackDisconnect, callbackError)
Create a WebSocket connection to the WebSocket API Extension proxy.
- URL - URL to connect to. The WebSocket proxy listens on port 9980. In most cases the WebSocket URL would be: "ws://_ipaddress_of_devicewise_gateway_:9980".
- callbackSuccess() - Callback function called upon successful authentication.
- callbackDisconnect() - Callback called in the event of a WebSocket disconnect.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.login(username, password, callbackSuccess, callbackError)
Login to deviceWISE gateway runtime.
Before you login, you will need to create a new user in
the gateway, using the Workbench. Use the Workbench ->
Administration -> Security tab to display the Security
panel, then select the Users tab.
Create a new User with the following credentials
- Name: websocketapi
- Password: 9cZbK9HFUtcTZY3Z
- Role: select one of the Roles that are defined on
the gateway.
For complete information on Users, Roles, and Policies, see: Security.
Once you've created the new user, you will be able to login with the following credentials:
- username - websocketapi
- password - wV7/btowyFzwgPlwhgPDV5jR1AaBVeoty9uQ3AjESxg=
- callbackSuccess() - Callback function called upon successful authentication.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.login2(username, password, callbackSuccess, callbackError)
Login to deviceWISE gateway runtime and persist session.
Before you login, you will need to create a new user in
the gateway, using the Workbench. Use the Workbench ->
Administration -> Security tab to display the Security
panel, then select the Users tab.
Create a new User with the following credentials
- Name: websocketapi
- Password: 9cZbK9HFUtcTZY3Z
- Role: select one of the Roles that are defined on
the gateway.
For complete information on Users, Roles, and Policies, see: Security.
Once you've created the new user, you will be able to login with the following credentials:
- username - websocketapi
- password - wV7/btowyFzwgPlwhgPDV5jR1AaBVeoty9uQ3AjESxg=
- callbackSuccess(token) - Callback function called upon successful authentication. Function returns token for session.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.loginToken(callbackSuccess, callbackError)
Attempt to resume existing session for client. Requires originally authenticating with "devicewise.login2()".
- callbackSuccess() - callback if existing session is resumed successfully
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.logout()
Logout from the deviceWISE gateway runtime. Closes the
WebSocket connection after logging out.
devicewise.read(device, variable, type, count, length, callbackSuccess, callbackError)
Read a variable.
- device - Device to be read from.
- variable - Variable to be read. Full path must be supplied (ex. folder1.folder2.variableName). If reading from arrays include the index you want to begin reading from (ex. arrayVariable[0]).
- type - deviceWISE data type.
- count - Number of elements to read from an array. Use a -1 for scalar reads.
- length - Length of the deviceWISE data type. Used for STRING data types. Use a -1 to inherit length for other types.
- callbackSuccess(data) - Callback function that returns data read.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.write(device, variable, type, count, length, data, callbackSuccess, callbackError)
Write to a variable.
- device - Device to write to.
- variable - Variable to be written. Full path must be supplied (ex. folder1.folder2.variableName). If writing to arrays include the index you want to begin writing from (ex. arrayVariable[0]).
- type - deviceWISE data type.
- count - Number of elements to write to an array. Use a -1 for scalar writes.
- length - Length of the deviceWISE data type. Used for STRING data types. Use a -1 to inherit length for other types.
- data - data to be written to the variable.
- callbackSuccess() - Callback function called upon successful completion.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.subscribe(device, variable, rate, type, count, length, callbackSuccess, callbackError, callbackPublishReceived)
Subscribe to a variable. Subscribing to multiple elements of an array will result in a subscription created for each element. The callbacks are shared for each element.
- device - Device containing the variable to subscribe to.
- variable - Variable to be subscribed to. Full path must be supplied (ex. folder1.folder2.variableName). If subscribing to arrays include the index you want to begin subscribing from (ex. arrayVariable[0]).
- rate - Poll rate, in seconds, for the subscription.
- type - deviceWISE data type.
- count - Number of elements to subscribe to for an array.
- length - Length of the deviceWISE data type. Used for STRING data types. Use a -1 to inherit length for other types.
- callbackSuccess(subscriptionID, variableName) - Callback function called upon successful completion. Returns the subscription's ID and variable name.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
- callbackPublishReceived(dataStructure) - Callback called when data publish is received for the given subscription. Returns a JSON object with the following structure:
{
device: device containing subscription,
variable: variable name,
count: element count,
length: data length,
type: datatype,
value: published value,
subId: subscription ID,
cbSuccess: success
callback,
cbError: error callback,
cbPublish: publish callback
}
devicewise.unsubscribe(subscriptionID, callbackSuccess, callbackError)
Unsubscribe from a variable.
- subscriptionID - ID of the subscription to unsubscribe from.
- callbackSuccess() - Callback called upon successful removal of the subscription.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.deviceList(callbackSuccess, callbackError)
Get a list of the defined devices on the deviceWISE gateway.
- callbackSuccess(devices) - Callback called upon reception of device list. Contains a list of devices. Each device is represented with a name, a type, and it's state.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Response
- Name - Name of Device
- Type - The device type from the deviceWISE driver code listing. The driver code listing is in the section, deviceWISE driver code listing.
- State:
- 1 - The device is Started.
- 2 - The device is Stopped.
- 3 - The device is Disabled.
- 4 - The device is Starting
- 5 - The device is Stopping
- 6 - Unknown
Example device list response:
[
{"name":"System
Monitor","type":"311","state":"2"},
{"name":"TestDevice","type":"9","state":"1"}
]
devicewise.triggerList(projectName, callbackSuccess, callbackError)
Get a list of triggers for a given project.
- projectName - Name of the project to get a list of triggers from.
- callbackSuccess(triggers) - Callback returning a list of triggers. Each trigger contains a name, a state, the number of successful executions, number in instances in progress, and the number of failure executions.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Response
- Name - The name of the trigger
- State:
- 1 - The trigger is Started.
- 2 - The trigger is Stopped.
- 3 - The trigger is Disabled.
- 4 - The device is Starting
- 5 - The device is Stopping
- 6 - Unknown
- Successes - The number of executions that ended in success.
- InProgress - The number of executions that are in progress.
- Failures - The number of executions that ended in failure.
Example trigger list response:
[
{"name":"TestTrigger1","state":"2","successes":"0","inProgress":"0","failures":"0"},
{"name":"TestTrigger2","state":"2","successes":"0","inProgress":"0","failures":"0"}
]
devicewise.projectList(callbackSuccess, callbackError)
Get a list of defined projects on the deviceWISE gateway.
- callbackSuccess(projects) - Callback returning a list of projects. Each project contains a name and a state.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Response
- Name - The name of the project
- State:
- 1 - The project is Started.
- 2 - The project is Stopped.
Example project list response:
[
{"name":"Project1","state":"2"},
{"name":"Project2","state":"2"}
]
devicewise.triggerStart(projectName, triggerName, callbackSuccess, callbackError)
Start a trigger.
- projectName - Name of the project containing the trigger.
- triggerName - Name of the trigger.
- callbackSuccess() - Callback called upon success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.triggerFire(projectName, triggerName, callbackSuccess, callbackError)
Fire a trigger.
- projectName - Name of the project containing the trigger.
- triggerName - Name of the trigger.
- callbackSuccess() - Callback called upon success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.triggerStop(projectName, triggerName, callbackSuccess, callbackError)
Stop a trigger.
- projectName - Name of the project containing the trigger.
- triggerName - Name of the trigger.
- callbackSuccess() - Callback called upon success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.projectStart(projectName, callbackSuccess, callbackError)
Start a project.
- projectName - Name of the project.
- callbackSuccess() - Callback called upon success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.projectStop(projectName, callbackSuccess, callbackError)
Stop a project.
- projectName - Name of the project.
- callbackSuccess() - Callback called upon success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.deviceInfo(device, callbackSuccess, callbackError)
Get device information.
- device - Name of the device to query for information.
- callbackSuccess(xmlString) - Callback containing xml string with device information.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Response
- Type - The device type from the deviceWISE driver code listing. The driver code listing is in the section, deviceWISE Driver Code Listings.
- Type_name - The device model name from the deviceWISE driver code listing.
- State:
- 1 - The device is Started.
- 2 - The device is Stopped.
- 3 - The device is Disabled.
- 4 - The device is Starting
- 5 - The device is Stopping
- 6 - Unknown
- Variable:
- Name - Variable name
- Type - Variable data type
- xdim - X dimensions (array spots)
- ydim - Y dimensions (2D array)
- zdim - Z dimensions (3D array)
Example device information XML response:
<?xml version="1.0" encoding="UTF-8"?>
<DeviceDefinition name="TestDevice" type="9"
type_name="Global Variables" state="1"
options="2104">
<Variables>
<VariableInfo name="boolArray" type="BOOL" xdim="3" options="7"/>
<VariableInfo name="bool" type="BOOL" options="7"/><VariableInfo name="int1" type="INT1" options="7"/>
<VariableInfo name="int2" type="INT2" options="7"/>
<VariableInfo name="int4" type="INT4" options="7"/>
<VariableInfo name="int8" type="INT8" options="7"/>
<VariableInfo name="uint1" type="UINT1" options="7"/>
<VariableInfo name="uint2" type="UINT2" options="7"/>
<VariableInfo name="uint4" type="UINT4" options="7"/>
<VariableInfo name="uint8" type="UINT8" options="7"/>
<VariableInfo name="float4" type="FLOAT4" options="7"/>
<VariableInfo name="float8" type="FLOAT8" options="7"/>
<VariableInfo name="string" type="STRING" length="32" options="7"/>
<VariableInfo name="boolMatrix" type="BOOL" xdim="100" options="7"/>
<VariableInfo name="int1Array" type="INT1" xdim="3" options="7"/>
<VariableInfo name="int2Array" type="INT2" xdim="3" options="7"/>
<VariableInfo name="int4Array" type="INT4" xdim="3" options="7"/>
<VariableInfo name="int8Array" type="INT8" xdim="3" options="7"/>
<VariableInfo name="uint1Array" type="UINT1" xdim="3" options="7"/>
<VariableInfo name="uint2Array" type="UINT2" xdim="3" options="7"/>
<VariableInfo name="uint4Array" type="UINT4" xdim="3" options="7"/>
<VariableInfo name="uint8Array" type="UINT8" xdim="3" options="7"/>
<VariableInfo name="float4Array" type="FLOAT4" xdim="3" options="7"/>
<VariableInfo name="float8Array" type="FLOAT8" xdim="3" options="7"/>
<VariableInfo name="stringArray" type="STRING" xdim="3" length="32" options="7"/>
</Variables><Structures/>
<MappedDescriptions/>
</DeviceDefinition>
devicewise.deviceStart(device, callbackSuccess, callbackError)
Start a device.
- device - Device to start.
- callbackSuccess(xmlString) - Callback containing xml string with device information.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.deviceStop(device, callbackSuccess, callbackError)
Stop a device.
- device - Name of the device to stop.
- callbackSuccess(xmlString) - Callback containing xml string with device information.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
devicewise.channelSubscribe(channel, callbackSuccess, callbackError, callbackPost)
Subscribe to a deviceWISE channel.
-
channel - channel to subscribe to.
- callbackSuccess() - Callback called on success.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
- callbackPost(data) - Callback called when a channel post is received. The response is an array of strings containing post specific information.
Response - Example: Subscribing to channel "TRIGGER"
-
Format – ("TRIGGER", LoadStatus, ProjectName, TriggerName, Status, State, User)
-
Load Status:
- 1 - Add
- 2 - Delete
- 3 - Edit
- 4 - State Change
- 5 - Load Status Changed
- Project Name – The name of the project
- Trigger Name – The name of the trigger
- Status:
- 1 - Loaded
- 2 - Unloaded
- 3 - Loading
- 4 - Unloading
- State:
- 1 - Started
- 2 - Stopped
- 3 - Disabled
- User – The last user of the trigger. This will only show when a user sets a state.
Log for starting and stopping trigger(bottom entry is most recent):
- Stopping (unloading &
started... stopped... unloaded &
stopped)
- TRIGGER,5,WebAPI,TestTrigger,2,2
Load Status Change, Unloaded, Stopped - TRIGGER,4,WebAPI,TestTrigger,2
Status Change, Stopped - TRIGGER,5,WebAPI,TestTrigger,4,1
Load Status Change, Unloading, Started
- TRIGGER,5,WebAPI,TestTrigger,2,2
- Starting (loading & stopped,
started, loaded & started)
- TRIGGER,5,WebAPI,TestTrigger,1,1
Load Status Change, Loaded, Started - TRIGGER,4,WebAPI,TestTrigger,1,admin
State Change, Started, admin - TRIGGER,5,WebAPI,TestTrigger,3,2
Load Status Change, Loading, Stopped
- TRIGGER,5,WebAPI,TestTrigger,1,1
devicewise.networkPing(IPAddress, numPings, callbackSuccess, callbackError)
Ping an IP address.
- IPAddress - IP address to ping.
- numPings - Number of pings to send.
- callbackSuccess(data) - Callback called on success. Returns array with response times for pings in milliseconds.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Response:
Array of response times in milliseconds
[ping1response, ping2response, ping3response, ping4response]
Example network ping response:
[2, 3, 2, 2]
devicewise.referenceList(sourceType, source, options, callbackSuccess, callbackError)
Get a reference list.
-
sourceType - The type of the object to get references for.
- source - The name of the object to get references for. Must be the same type as sourceType.
- Reference Direction:
- 0 - Forward - Referenced by source
- 1 - Backwards - References of source
- 2 - Both - All references of the source and by the source.
- callbackSuccess(data) - Callback called on success. Returns xml string containing reference information.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Returned XML
- crossreference name - Name of the source.
- referencedby name - Name of what's referencing the source.
- type - The type of reference.
Example reference list response:
<?xml version="1.0" encoding="UTF-8"?>
<crossreference name="TestDevice" type="device">
<referenceby name="WebAPI/TestTrigger" type="trigger">
<referenceby name="WebAPI" type="project"/>
</referenceby>
</crossreference>
This example is showing that TestDevice is a device that is
being referenced by the trigger WebAPI/TestTrigger as well
as the project WebAPI. This makes sense because TestDevice
is used by one trigger, WebAPI/TestTrigger, which is part
of the project WebAPI.
devicewise.SQLiteExec(query, callbackSuccess, callbackError)
Execute SQLite query.
- query - Query string to be executed.
- callbackSuccess(data) - Callback handling SQLite response. Response is an array of data whose structure is based on the query type.
- callbackError(errorCode) - Callback called if an error occurs. Returns errorCode.
Example SQLiteExec response 1
- Query: insert into SampleTable values ("samplevalue")
- Response: Affected Rows: 1
Example SQLiteExec response
2
- Query: SELECT * FROM SampleTable;
-
Response: 1,1,SampleTable;,SampleColumn,samplevalue
Error Codes
Error codes can be looked up from the deviceWISE Workbench by selecting the Help menu in the upper right hand corner, and then selecting Error Lookup.
Demo web application
The Demo Web App is a web browser based application that allows you to test all the WebSocket API Extension Javascript library functions.
The WebSocketAPI_Demo_Project_and_Device.dwx includes a sample deviceWISE project, trigger, and device for testing the demo web app.
The Demo Table can be imported if you want to test the SQLite library function.
To run the demo web application:
- Install the WebSocket API Extension
- Make sure the HTTP Server is in a Started status
- Import the demo project, trigger, and device contained in the export file: WebSocketAPI_Demo_Project_and_Device.dwx
- Import the demo SQLite table: Demo Table
- Extract the Demo Web App in the gateway's Staging Browser /www directory
- Point your Chrome browser to http://localhost:8080/API_Demo/index.html.
The demo web application user interface should look
similar to this: