Device Module


Version Device 1.2.0

Table of Contents

Usage

To use the Device module, you can import it into your project from the Firebolt SDK:

import { Device } from '@firebolt-js/manage-sdk'

Overview

A module for querying about the device and it’s capabilities.

Methods

listen

To listen to a specific event pass the event name as the first parameter:

listen(event: string, callback: (data: any) => void): Promise<number>

Parameters:

Param Type Required Summary
event string Yes The event to listen for, see Events.
callback function Yes A function that will be invoked when the event occurs.

Promise resolution:

Type Description
number Listener ID to clear the callback method and stop receiving the event, e.g. Device.clear(id)

Callback parameters:

Param Type Required Summary
data any Yes The event data, which depends on which event is firing, see Events.

To listen to all events from this module pass only a callback, without specifying an event name:

listen(callback: (event: string, data: any) => void): Promise<number>

Parameters:

Param Type Required Summary
callback function Yes A function that will be invoked when the event occurs. The event data depends on which event is firing, see Events.

Callback parameters:

Param Type Required Summary
event string Yes The event that has occured listen for, see Events.
data any Yes The event data, which depends on which event is firing, see Events.

Promise resolution:

Type Description
number Listener ID to clear the callback method and stop receiving the event, e.g. Device.clear(id)

See Listening for events for more information and examples.

name

The human readable name of the device

To get the value of name call the method like this:

function name(): Promise<string>

Promise resolution:

Capabilities:

Role Capability
uses xrn:firebolt:capability:device:name

Examples

Default example #1

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let value = await Device.name()
console.log(value)

Value of value:

'Living Room'
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.name",
  "params": {}
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "Living Room"
}

Default example #2

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let value = await Device.name()
console.log(value)

Value of value:

'Living Room'
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.name",
  "params": {}
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "Kitchen"
}

To set the value of name call the method like this:

function name(value: string): Promise<void>

Parameters:

Param Type Required Description
value string true the device friendly-name

Promise resolution:

Examples

Default example #1

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let result = await Device.name('Living Room')
console.log(result)

Value of result:

null
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.setName",
  "params": {
    "value": "Living Room"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": null
}

Default example #2

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let result = await Device.name('Kitchen')
console.log(result)

Value of result:

null
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.setName",
  "params": {
    "value": "Kitchen"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": null
}

To subscribe to notifications when the value changes, call the method like this:

function name(callback: (value) => string): Promise<number>

Promise resolution:

number

Examples

Default example #1

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let listenerId = await name((value) => {
  console.log(value)
})
console.log(listenerId)

Value of value:

'Living Room'
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.onNameChanged",
  "params": {
    "listen": true
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "Living Room"
}

Default example #2

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let listenerId = await name((value) => {
  console.log(value)
})
console.log(listenerId)

Value of value:

'Living Room'
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.onNameChanged",
  "params": {
    "listen": true
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "Kitchen"
}

once

To listen to a single instance of a specific event pass the event name as the first parameter:

once(event: string, callback: (data: any) => void): Promise<number>

The once method will only pass the next instance of this event, and then dicard the listener you provided.

Parameters:

Param Type Required Summary
event string Yes The event to listen for, see Events.
callback function Yes A function that will be invoked when the event occurs.

Promise resolution:

Type Description
number Listener ID to clear the callback method and stop receiving the event, e.g. Device.clear(id)

Callback parameters:

Param Type Required Summary
data any Yes The event data, which depends on which event is firing, see Events.

To listen to the next instance only of any events from this module pass only a callback, without specifying an event name:

once(callback: (event: string, data: any) => void): Promise<number>

Parameters:

Param Type Required Summary
callback function Yes A function that will be invoked when the event occurs. The event data depends on which event is firing, see Events.

Callback parameters:

Param Type Required Summary
event string Yes The event that has occured listen for, see Events.
data any Yes The event data, which depends on which event is firing, see Events.

Promise resolution:

Type Description
number Listener ID to clear the callback method and stop receiving the event, e.g. Device.clear(id)

See Listening for events for more information and examples.

provision

Used by a distributor to push provision info to firebolt.

function provision(
  accountId: string,
  deviceId: string,
  distributorId: string,
): Promise<void>

Parameters:

Param Type Required Description
accountId string true The id of the account that is device is attached to in the back office.
deviceId string true The id of the device in the back office.
distributorId string false The id of the distributor in the back office.

Promise resolution:

Capabilities:

Role Capability
manages xrn:firebolt:capability:account:id
xrn:firebolt:capability:device:id
xrn:firebolt:capability:device:distributor

Examples

Default Example

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let result = await Device.provision('12345678910', '987654321111', null)
console.log(result)

Value of result:

null
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.provision",
  "params": {
    "accountId": "12345678910",
    "deviceId": "987654321111"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": null
}

With distributor id

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

let result = await Device.provision(
  '12345678910',
  '987654321111',
  'global_partner',
)
console.log(result)

Value of result:

null
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.provision",
  "params": {
    "accountId": "12345678910",
    "deviceId": "987654321111",
    "distributorId": "global_partner"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": null
}

Events

deviceNameChanged

function listen('deviceNameChanged', () => void): Promise<number>

See also: listen(), once(), clear().

Event value:

Capabilities:

Role Capability
uses xrn:firebolt:capability:device:name

Examples

Getting the device name

JavaScript:

import { Device } from '@firebolt-js/manage-sdk'

Device.listen('deviceNameChanged', (value) => {
  console.log(value)
})

Value of value:

'Living Room'
JSON-RPC:

Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "Device.onDeviceNameChanged",
  "params": {
    "listen": true
  }
}

Response:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": "Living Room"
}

nameChanged

See: name

Types