restserver-api
Example program This example shows basic restserver-api usage.
const restAPI = ; const Lwm2m = restAPI;const RESOURCE_TYPE encodeResource decodeResource = Lwm2mTLV; console;const service = ;const device = service 'urn:uuid:17200d69-ec9b-43b3-9051-73df2207907c'; console;const tlvBuffer = ; console;servicestart;
Classes
Constants
- TYPE
Represents LwM2M variable (identifier) types (OBJECT_INSTANCE, MULTIPLE_RESOURCE, RESOURCE_INSTANCE, RESOURCE).
- RESOURCE_TYPE
Represents resource type (NONE, BOOLEAN, INTEGER, FLOAT, STRING, OPAQUE).
Functions
- getDictionaryByValue(dictionaryList, keyName, value) ⇒
Object
|string
|number
Gets dictionary by given name of the key and value.
- encodeResourceValue(resource) ⇒
object
Encodes value of the resource.
- decodeResourceValue(buffer, resource) ⇒
Object
|string
|number
|boolean
Decodes value of the resource.
- encode(object) ⇒
object
Encodes ant type of instance (Object instance, multiple resources, resources instance, resource).
- encodeResourceInstance(resourceInstance) ⇒
object
Encodes resource instance to TLV buffer.
- encodeMultipleResourcesTLV(resources) ⇒
object
Encodes multiple resource values to TLV buffer.
- encodeResource(resource) ⇒
object
Encodes resource to TLV buffer.
- encodeObjectInstance(objectInstance) ⇒
object
Encodes LwM2M object instance to TLV buffer.
- encodeObject(object) ⇒
object
Encodes LwM2M object to TLV buffer.
- decode(buffer) ⇒
object
Decodes any TLV buffer.
- decodeResourceInstance(buffer, resources) ⇒
object
Decodes resource instance.
- decodeResourceInstanceValue(buffer, resourceInstance) ⇒
object
Decodes resource instance value
- decodeResource(buffer, resource) ⇒
object
Decodes resource.
- decodeObjectInstance(buffer, objectInstance) ⇒
object
Decodes object instance from TLV buffer.
- decodeObject(buffer, object) ⇒
object
Decodes LwM2M object to TLV buffer.
Device
This class represents device (endpoint).
Kind: global class
- Device
- new Device(service, id)
- .getObjects() ⇒
Promise
- .read(path, callback) ⇒
Promise
- .write(path, callback, payload, type) ⇒
Promise
- .execute(path, callback, payload, type) ⇒
Promise
- .observe(path, callback) ⇒
Promise
- .cancelObserve(path) ⇒
Promise
new Device(service, id)
Constructor initiliazes given service object, device's id and starts listening for events emited by service (when device registers, updates, deregisters, sends data), handles "async responses" and emits "register", "update", "deregister" events.
Param | Type | Description |
---|---|---|
service | object |
Service object |
id | string |
Endpoint id |
Example
const restAPI = ; const service = serviceOptions;const device = service 'deviceId';
Promise
device.getObjects() ⇒ Sends request to get all device's objects.
Kind: instance method of Device
Returns: Promise
- Promise object with device's objects
Example
device;
Promise
device.read(path, callback) ⇒ Sends request to read device's resource data.
Kind: instance method of Device
Returns: Promise
- Promise with async response id
Param | Type | Description |
---|---|---|
path | string |
Resource path |
callback | function |
Callback which will be called when async response is received |
Example
device;
Promise
device.write(path, callback, payload, type) ⇒ Sends request to write a value into device's resource.
Kind: instance method of Device
Returns: Promise
- Promise with async response id
Param | Type | Default | Description |
---|---|---|---|
path | string |
Resource path | |
callback | function |
Callback which will be called when async response is received | |
payload | buffer |
Data (optional) | |
type | string |
"application/vnd.oma.lwm2m+tlv" |
Content type (optional) |
Example
device;
Promise
device.execute(path, callback, payload, type) ⇒ Sends request to execute device's resource.
Kind: instance method of Device
Returns: Promise
- Promise with async response id
Param | Type | Default | Description |
---|---|---|---|
path | string |
Resource path | |
callback | function |
Callback which will be called when async response is received | |
payload | buffer |
Data (optional) | |
type | string |
"text/plain" |
Content type (optional) |
Example
device;
Promise
device.observe(path, callback) ⇒ Sends request to subscribe to resource.
Kind: instance method of Device
Returns: Promise
- Promise with async response id
Param | Type | Description |
---|---|---|
path | string |
Resource path |
callback | function |
Callback which will be called when async response is received |
Example
device;
Promise
device.cancelObserve(path) ⇒ Sends request to cancel subscriptions.
Kind: instance method of Device
Returns: Promise
- Promise with HTTP status code
Param | Type | Description |
---|---|---|
path | string |
Resource path |
Example
device;
Service
This class represents REST API service.
Kind: global class
- Service
- new Service(opts)
- .start(opts) ⇒
Promise
- .stop() ⇒
Promise
- .authenticate() ⇒
Promise
- .registerNotificationCallback() ⇒
Promise
- .deleteNotificationCallback() ⇒
Promise
- .checkNotificationCallback() ⇒
Promise
- .pullNotification() ⇒
Promise
- .getDevices() ⇒
Promise
- .getVersion() ⇒
Promise
- .get(path) ⇒
Promise
- .put(path, argument, type) ⇒
Promise
- .delete(path) ⇒
Promise
- .post(path, argument, type) ⇒
Promise
new Service(opts)
Initializes default configurations. Reconfigures with given options.
Param | Type | Description |
---|---|---|
opts | object |
Options object (optional) |
Example
const options = // REST server's address host: 'http://localhost:8888' // CA certificate ca: '' // authentication (true or false) authentication: false username: '' password: '' // notification polling (true or false) polling: false // time between each poll in miliseconds interval: 1234 // port for socket listener (not relevant if polling is enabled) port: 5728;options;
Promise
service.start(opts) ⇒ (Re)starts authentication, socket listener creation and notification callback registration or notification polling processes.
Kind: instance method of Service
Returns: Promise
- Promise which fulfills when service is started
Param | Type | Description |
---|---|---|
opts | object |
Options object (optional) |
Example
servicestart;
Example (Passing options object)
const options = // REST server's address host: 'http://localhost:8888' // CA certificate ca: '' // authentication (true or false) authentication: false username: '' password: '' // notification polling (true or false) polling: false // time between each poll in miliseconds interval: 1234 // port for socket listener (not relevant if polling is enabled) port: 5728;servicestartoptions;
Promise
service.stop() ⇒ Stops receiving and processing events Stops this service and all it's subservices that were started in start(). Cleans up resources
Kind: instance method of Service
Returns: Promise
- Promise which fulfills when service is stopped
Example
service;
Promise
service.authenticate() ⇒ Sends request to authenticate user.
Kind: instance method of Service
Returns: Promise
- Promise with authentication data (token and after what time it expires)
Example
service;
Promise
service.registerNotificationCallback() ⇒ Sends request to register notification callback.
Kind: instance method of Service
Returns: Promise
- Promise which fulfills when notification callback is registered
Example
service;
Promise
service.deleteNotificationCallback() ⇒ Sends request to delete notification callback.
Kind: instance method of Service
Returns: Promise
- Promise with HTTP status code
Example
service;
Promise
service.checkNotificationCallback() ⇒ Sends request to check whether or not notification callback is registered.
Kind: instance method of Service
Returns: Promise
- Promise with notification callback data
Example
service;
Promise
service.pullNotification() ⇒ Sends request to get pending/queued notifications.
Kind: instance method of Service
Returns: Promise
- Promise with notification data (registrations,
deregistrations, updates, async responses)
Example
service;
Promise
service.getDevices() ⇒ Sends request to get all registered endpoints.
Kind: instance method of Service
Returns: Promise
- Promise with a list of endpoints
Example
service;
Promise
service.getVersion() ⇒ Sends request to get REST server version.
Kind: instance method of Service
Returns: Promise
- Promise with REST server's version
Example
service;
Promise
service.get(path) ⇒ Performs GET requests with given path.
Kind: instance method of Service
Returns: Promise
- Promise with data and response object
Param | Type | Description |
---|---|---|
path | string |
Request path |
Example
service;
Promise
service.put(path, argument, type) ⇒ Performs PUT requests with given path, data and data type.
Kind: instance method of Service
Returns: Promise
- Promise with data and response object
Param | Type | Default | Description |
---|---|---|---|
path | string |
Request path | |
argument | object |
Data which will be sent (optional) | |
type | string |
"application/vnd.oma.lwm2m+tlv" |
Data type (optional) |
Promise
service.delete(path) ⇒ Performs DELETE requests with given path.
Kind: instance method of Service
Returns: Promise
- Promise with data and response object
Param | Type | Description |
---|---|---|
path | string |
Request path |
Promise
service.post(path, argument, type) ⇒ Performs POST requests with given path, data and data type.
Kind: instance method of Service
Returns: Promise
- Promise with data and response object
Param | Type | Default | Description |
---|---|---|---|
path | string |
Request path | |
argument | object |
Data which will be sent (optional) | |
type | string |
"application/vnd.oma.lwm2m+tlv" |
Data type (optional) |
TYPE
Represents LwM2M variable (identifier) types (OBJECT_INSTANCE, MULTIPLE_RESOURCE, RESOURCE_INSTANCE, RESOURCE).
RESOURCE_TYPE
Represents resource type (NONE, BOOLEAN, INTEGER, FLOAT, STRING, OPAQUE).
Object
| string
| number
getDictionaryByValue(dictionaryList, keyName, value) ⇒ Gets dictionary by given name of the key and value.
Kind: global function
Returns: Object
| string
| number
- dictionary
Param | Type | Description |
---|---|---|
dictionaryList | object |
Dictionary list. |
keyName | Object | string |
Name of the key |
value | Object | string | number |
Value |
object
encodeResourceValue(resource) ⇒ Encodes value of the resource.
Kind: global function
Returns: object
- buffer - Buffer of encoded value.
Param | Type | Description |
---|---|---|
resource | object |
Object which stores resource value and value's type. |
Example
const resource = type: TLVRESOURCE_TYPEINTEGER value: 1; const encodedValue = ;// encodedValue = <Buffer 01>
Object
| string
| number
| boolean
decodeResourceValue(buffer, resource) ⇒ Decodes value of the resource.
Kind: global function
Returns: Object
| string
| number
| boolean
- value - Decoded value in specified type
Param | Type | Description |
---|---|---|
buffer | object |
Buffer which will be decoded. |
resource | object |
Object which stores resource value's type. |
Example
const buffer = Buffer;const resource = type: TLVRESOURCE_TYPEINTEGER; const decodedValue = ;// decodedValue = 1
object
encode(object) ⇒ Encodes ant type of instance (Object instance, multiple resources, resources instance, resource).
Kind: global function
Returns: object
- buffer - encoded TLV buffer.
Param | Type | Description |
---|---|---|
object | object |
object which stores type, identifier and value. |
Example
const resource = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN value: true; const encoded = ;// encoded = <Buffer e1 16 da 01>
object
encodeResourceInstance(resourceInstance) ⇒ Encodes resource instance to TLV buffer.
Kind: global function
Returns: object
- buffer - Buffer in TLV format
Param | Type | Description |
---|---|---|
resourceInstance | object |
Object which stores resource identifier, value and it's type. |
Example
const resourceInstance = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN value: true; const encoded = ;// encoded = <Buffer e1 16 da 01>
object
encodeMultipleResourcesTLV(resources) ⇒ Encodes multiple resource values to TLV buffer.
Kind: global function
Returns: object
- buffer - TLV buffer.
Param | Type | Description |
---|---|---|
resources | object |
Object which stores identifier, resource type, and multiple values. |
Example
const resources = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN value: true false; const encoded = ;// encoded = <Buffer a6 16 da 41 00 01 41 01 00>
object
encodeResource(resource) ⇒ Encodes resource to TLV buffer.
Kind: global function
Returns: object
- buffer - TLV buffer.
Param | Type | Description |
---|---|---|
resource | object |
Object which stores resource identifier, type and value. |
Example
const resource = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN value: true; const encoded = ;// encoded = <Buffer e1 16 da 01>
object
encodeObjectInstance(objectInstance) ⇒ Encodes LwM2M object instance to TLV buffer.
Kind: global function
Returns: object
- buffer - TLV buffer.
Param | Type | Description |
---|---|---|
objectInstance | object |
LwM2M object. |
Example
const objectInstance = identifier: 0 resources: identifier: 5815 type: TLVRESOURCE_TYPEFLOAT value: 99999 ; const encoded = ;// encoded = <Buffer 07 00 e4 16 b7 44 79 ff 5c>
object
encodeObject(object) ⇒ Encodes LwM2M object to TLV buffer.
Kind: global function
Returns: object
- buffer - TLV buffer.
Param | Type | Description |
---|---|---|
object | object |
LwM2M object. |
Example
const object = identifier: 3305 objectInstances: identifier: 0 resources: identifier: 5815 type: TLVRESOURCE_TYPEFLOAT value: 99999 ; const encoded = ;// encoded = <Buffer 07 00 e4 16 b7 44 79 ff 5c>
object
decode(buffer) ⇒ Decodes any TLV buffer.
Kind: global function
Returns: object
- object - Decoded object.
Param | Type | Description |
---|---|---|
buffer | object |
encoded TLV buffer. |
Example
const buffer = Buffer; const decoded = TLV;// decoded = { type: 3, identifier: 5850, value: <Buffer 01>, tlvSize: 4 }
object
decodeResourceInstance(buffer, resources) ⇒ Decodes resource instance.
Kind: global function
Returns: object
- decodedResource - Object which stores resource identifier,
tlvSize resource type and value.
Param | Type | Description |
---|---|---|
buffer | object |
Resource instance TLV buffer. |
resources | object |
Object which stores resource identifier and resource type. |
Example
const buffer = Buffer;const resources = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN; const decoded = ;// decoded = { identifier: 5850, tlvSize: 4, type: TLV.RESOURCE_TYPE.BOOLEAN, value: true }
object
decodeResourceInstanceValue(buffer, resourceInstance) ⇒ Decodes resource instance value
Kind: global function
Returns: object
- decodedResourceValue - Decoded resource value
Param | Type | Description |
---|---|---|
buffer | object |
Resource instance value TLV buffer |
resourceInstance | object |
Object which stores resource type |
Example
const buffer = Buffer;const resourceInstance = type: TLVRESOURCE_TYPEINTEGER; const decoded = ;// decoded = 1
object
decodeResource(buffer, resource) ⇒ Decodes resource.
Kind: global function
Returns: object
- buffer - Decoded resource.
Param | Type | Description |
---|---|---|
buffer | object |
Resource TLV buffer |
resource | object |
Object which stores identifier and resource type. |
Example
const buffer = Buffer;const resource = identifier: 5850 type: TLVRESOURCE_TYPEBOOLEAN; const decoded = ;// decoded = { identifier: 5850, type: 1, value: true, tlvSize: 4 }
object
decodeObjectInstance(buffer, objectInstance) ⇒ Decodes object instance from TLV buffer.
Kind: global function
Returns: object
- object - Decoded object instance.
Param | Type | Description |
---|---|---|
buffer | object |
TLV buffer. |
objectInstance | object |
Object which stores object instance identifier and resources. |
Example
const buffer = Buffer;const objectInstance: identifier: 0 resources: identifier: 5815 type: TLVRESOURCE_TYPEFLOAT }; const decoded = ;// decoded = { identifier: 0, resources: [ { identifier: 5815, type: 3 } ] }
object
decodeObject(buffer, object) ⇒ Decodes LwM2M object to TLV buffer.
Kind: global function
Returns: object
- object - Decoded object.
Param | Type | Description |
---|---|---|
buffer | object |
TLV buffer. |
object | object |
Object which stores object instances with their resources. |
Example
const buffer = Buffer;const object = identifier: 3305 objectInstances: identifier: 0 resources: identifier: 5815 type: TLVRESOURCE_TYPEFLOAT ; const decoded = ;/*decoded = { identifier: 3305, objectInstances: [ { identifier: 0, resources: [ { identifier: 5815, type: 3, value: 999.989990234375, tlvSize: 7 } ] } ]}*/