new VirtualDevice(endpointId, deviceModel, client)
The VirtualDevice API is specific to the enterprise client. Also it implements the device monitoring and control specific to the enterprise client and the call to an action method. Actions are defined in the device model.
A device model can be obtained by it's afferent urn with the EnterpriseClient if it is registered on the cloud.
The VirtualDevice has the attributes and actions of the device model as properties and it provides functionality to the device model in the following ways:
Get the value of an attribute:
let value = device.temperature.value;
Get the last known value of an attribute:
let lastValue = device.temperature.lastKnownValue;
Set the value of an attribute (with update on cloud and error callback handling):
device.maxThreshold.onError = function (errorTuple);
device.maxThreshold.value = 27;
where errorTuple is an object of the form
{attribute: ... , newValue: ... , tryValue: ... , errorResponse: ...}
.
The library will throw an error in the value to update is invalid
according to the device model.
Monitor a specific attribute for any value change (that comes from the cloud):
device.temperature.onChange = function (changeTuple);
where changeTuple is an object of the form
{attribute: ... , newValue: ... , oldValue: ...}
.
Monitor all attributes for any value change (that comes from the cloud):
device.onChange = function (changeTuple);
where changeTuple is an object with array type properties of the form
[{attribute: ... , newValue: ... , oldValue: ...}]
.
Monitor all update errors:
device.onError = function (errorTuple);
where errorTuple is an object with array type properties (besides errorResponse) of the form
{attributes: ... , newValues: ... , tryValues: ... , errorResponse: ...}
.
Monitor a specific alert format for any alerts that where generated:
device.tooHot.onAlerts = function (alerts);
where alerts is an array containing all the alerts generated of the specific format. An
alert is an object of the form:
{eventTime: ... , severity: ... , fields: {field1: value1, field2: value2 ... }}
.
The onAlerts can be set also by urn:
device['temperature:format:tooHot'].onAlerts = function (alerts);
Monitor all alerts generated for all formats:
device.onAlerts = function (alerts);
where alerts is an object containing all the alert formats as keys and each has as value the above described array:
{formatUrn1: [ ... ], formatUrn2: [ ... ], ... }
.
Monitor a specific custom message format for any messages that where generated:
device.rfidDetected.onData = function (data);
where data is an array containing all the custom data messages generated of the specific format. A
data object is an object of the form:
{eventTime: ... , severity: ... , fields: {field1: value1, field2: value2 ... }}
.
The onData can be set also by urn:
device['temperature:format:rfidDetected'].onData = function (data);
Monitor all custom data messages generated for all formats:
device.onData = function (data);
where data is an object containing all the custom formats as keys and each has as value the above described array:
{formatUrn1: [ ... ], formatUrn2: [ ... ], ... }
.
A VirtualDevice can also be created with the appropriate parameters from the EnterpriseClient.
Parameters:
Name | Type | Description |
---|---|---|
endpointId |
string | The endpoint id of this device. |
deviceModel |
object | The device model object holding the full description of that device model that this device implements. |
client |
iotcs.enterprise.EnterpriseClient | The enterprise client associated with the device application context. |
- See:
-
iotcs.enterprise.EnterpriseClient#getDeviceModel
iotcs.enterprise.EnterpriseClient#createVirtualDevice
Extends
Methods
(static) call(actionName, argopt)
The response from the cloud to the execution of the action can be retrieved by setting a callback
function to the onAction property of the action:
device.reset.onAction = function
(response);
device.call('reset');
where response is a JSON
representation of the response from the cloud if any.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
actionName |
string | The name of the action to execute. | |
arg |
object |
<optional> |
An optional unique argument to pass for action execution. This is specific to the action and description of it is provided in the device model. |