Twin class

A Device Twin is document describing the state of a device that is stored by an Azure IoT hub and is available even if the device is offline. It is built around 3 sections:

  • Tags: key/value pairs only accessible from the service side
  • Desired Properties: updated by a service and received by the device
  • Reported Properties: updated by the device and received by the service.

Note that although it is a possibility, desired and reported properties do not have to match and that the logic to sync these two collections, if necessary, is left to the user of the SDK.

For more information see Understanding Device Twins.

The recommended way to obtain a Twin for a specific device is to use the getTwin(string, HttpResponseCallback<Twin>) method.

Constructors

Twin(string | DeviceIdentity, Registry)

Instantiates a new Twin. The recommended way to get a new Twin object is to use the getTwin(string, HttpResponseCallback<Twin>) method.

Properties

deviceId

Unique identifier of the device identity associated with the twin, as it exists in the device identity registry.

etag

Tag used in optimistic concurrency to avoid multiple parallel editions of the device twin.

modelId

Model Id of the device identity associated with the twin, as it exists in the device identity registry.

moduleId

Module identifier for the module associated with the twin, as it exists in the device identity registry.

properties

The desired and reported properties dictionnaries (respectively in properties.desired and properties.reported).

tags

Collection of key/value pairs that is available only on the service side and can be used in queries to find specific devices.

Methods

get()
get(IncomingMessageCallback<Twin>)

Gets the latest version of this device twin from the IoT Hub service.

toJSON()
update(any)
update(any, IncomingMessageCallback<Twin>)

Update the device twin with the patch provided as argument.

Constructor Details

Twin(string | DeviceIdentity, Registry)

Instantiates a new Twin. The recommended way to get a new Twin object is to use the getTwin(string, HttpResponseCallback<Twin>) method.

new Twin(device: string | DeviceIdentity, registryClient: Registry)

Parameters

device

string | DeviceIdentity

A device identifier string or an object describing the device. If an Object, it must contain a deviceId property.

registryClient
Registry

The HTTP registry client used to execute REST API calls.

Property Details

deviceId

Unique identifier of the device identity associated with the twin, as it exists in the device identity registry.

deviceId: string

Property Value

string

etag

Tag used in optimistic concurrency to avoid multiple parallel editions of the device twin.

etag: string

Property Value

string

modelId

Model Id of the device identity associated with the twin, as it exists in the device identity registry.

modelId?: string

Property Value

string

moduleId

Module identifier for the module associated with the twin, as it exists in the device identity registry.

moduleId?: string

Property Value

string

properties

The desired and reported properties dictionnaries (respectively in properties.desired and properties.reported).

properties: { desired: {[key: string]: any}, reported: {[key: string]: any} }

Property Value

{ desired: {[key: string]: any}, reported: {[key: string]: any} }

tags

Collection of key/value pairs that is available only on the service side and can be used in queries to find specific devices.

tags: {[key: string]: string}

Property Value

{[key: string]: string}

Method Details

get()

function get(): Promise<ResultWithIncomingMessage<Twin>>

Returns

Promise<ResultWithIncomingMessage<Twin>>

get(IncomingMessageCallback<Twin>)

Gets the latest version of this device twin from the IoT Hub service.

function get(done?: IncomingMessageCallback<Twin>)

Parameters

done

IncomingMessageCallback<Twin>

The optional function to call when the operation is complete. done will be called with three arguments: an Error object (can be null), a Twin object representing the created device identity, and a transport-specific response object useful for logging or debugging.

toJSON()

function toJSON(): object

Returns

object

update(any)

function update(patch: any): Promise<ResultWithIncomingMessage<Twin>>

Parameters

patch

any

Returns

Promise<ResultWithIncomingMessage<Twin>>

update(any, IncomingMessageCallback<Twin>)

Update the device twin with the patch provided as argument.

function update(patch: any, done?: IncomingMessageCallback<Twin>)

Parameters

patch

any

Object containing the new values to apply to this device twin.

done

IncomingMessageCallback<Twin>

The optional function to call when the operation is complete. done will be called with three arguments: an Error object (can be null), a Twin object representing the created device identity, and a transport-specific response object useful for logging or debugging.