Class: VirtualDevice

iotcs.device.VirtualDevice(endpointId, deviceModel, client)

new VirtualDevice(endpointId, deviceModel, client)

VirtualDevice is a representation of a device model implemented by an endpoint. A device model is a specification of the attributes, formats, and resources available on the endpoint.

This VirtualDevice API is specific to the device client. This implements the alerts defined in the device model and can be used for raising alerts to be sent to the server for the device. Also it has action handlers for actions that come as requests from the server side.

A device model can be obtained by it's afferent urn with the DirectlyConnectedDevice if it is registered on the cloud.

The VirtualDevice has the attributes, actions and alerts 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.temperature.onError = function (errorTuple);
device.temperature.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.maxThreshold.onChange = function (changeTuple);
where changeTuple is an object of the form {attribute: ... , newValue: ... , oldValue: ...}. To tell the cloud that the attribute update has failed an error must be thrown in the onChange function, otherwise the library will send an OK response message to the cloud.

Monitor a specific action that was requested from the server:
device.reset.onAction = function (value);
where value is an optional parameter given if the action has parameters defined in the device model. To tell the cloud that an action has failed an error must be thrown in the onAction function, otherwise the library will send an OK response message to the cloud.

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: ...}]. To tell the cloud that the attribute update has failed an error must be thrown in the onChange function, otherwise the library will send an OK response message to the cloud.

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: ...}.

Raising alerts:
let alert = device.createAlert('urn:com:oracle:iot:device:temperature_sensor:too_hot');
alert.fields.temp = 100;
alert.fields.maxThreshold = 90;
alert.raise();
If an alert was not sent the error is handled by the device.onError handler where errorTuple has the following structure:
{attributes: ... , errorResponse: ...}
where attributes are the alerts that failed with fields already set, so the alert can be retried only by raising them.

Sending custom data fields:
let data = device.createData('urn:com:oracle:iot:device:motion_sensor:rfid_detected');
data.fields.detecting_motion = true;
data.submit();
If the custom data fields were not sent, the error is handled by the device.onError handler where errorTuple has the following structure:
{attributes: ... , errorResponse: ...}
where attributes are the Data objects that failed to be sent with fields already set, so the Data objects can be retried only by sending them.

A VirtualDevice can also be created with the appropriate parameters from the DirectlyConnectedDevice.

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.device.DirectlyConnectedDevice The device client used as message dispatcher for this virtual device.
See:
  • iotcs.device.DirectlyConnectedDevice#getDeviceModel
  • iotcs.device.DirectlyConnectedDevice#createVirtualDevice

Extends

Methods

(static) createAlert(formatUrn) → {iotcs.device.Alert}

This method returns an Alert object created based on the format given as parameter. An Alert object can be used to send alerts to the server by calling the raise method, after all mandatory fields of the alert are set.
Parameters:
Name Type Description
formatUrn string the urn format of the alert spec as defined in the device model that this virtual device represents
Returns:
The Alert instance
Type
iotcs.device.Alert

(static) createData(formatUrn) → {iotcs.device.Data}

This method returns a Data object created based on the format given as parameter. A Data object can be used to send custom data fields to the server by calling the submit method, after all mandatory fields of the data object are set.
Parameters:
Name Type Description
formatUrn string the urn format of the custom data spec as defined in the device model that this virtual device represents
Returns:
The Data instance
Type
iotcs.device.Data

(static) offer(attributeName, value)

Offer to set the value of an attribute. The attribute value is set depending upon any policy that may have been configured for the attribute. If there is no policy for the given attribute, offer behaves as if the set method were called. The value is validated according to the constraints in the device model. If the value is not valid, an Error is thrown.
Parameters:
Name Type Description
attributeName string The name of an attribute from the device type model.
value any The value to set.

Type Definitions

onErrorCallback(error)

Callback for iotcs.device.VirtualDevice.onError with the error.
Parameters:
Name Type Description
error string The error when sending this Alert.

Home