@znti/dojot-web
v0.5.11
Published
Helper classes to integrate with [dojot](http://www.dojot.com.br/)'s services.
Downloads
7
Readme
dojot-web library
Helper classes to integrate with dojot's services.
This document describes its supported features, along with a basic usage example for each of those.
A sample of its usage can be found on the CLI tool - which uses this module underneath.
Installing
First of all, make sure to install the project package through npm.
npm install --save @znti/dojot-web
Once installed, simply import it as any other module;
const dojotLibrary = require('@znti/dojot-web');
Now all that is left to do is to initialize it with a valid dojot host address.
Initializing
This module is exported as a class. To use it, instantiate it from the required object.
const dojot = new dojotLibrary();
configure(dojotHost)
Initializes the client and setups a connection with the dojot server located on dojotHost
.
let dojotHost = 'http://localhost:8000';
dojot.configure(dojotHost).then(configuredClient => {
// The client is now pointing to the specified dojot host.
// All thats left is to provide some credentials.
}).catch(console.error);
Make sure to change your dojotHost
location, in case yours is not on localhost:8000
initializeWithCredentials(user, password)
Initializes the client based on its credentials. If left empty, the library assumes the default credentials located at configs.js and tries to authenticate with it.
configuredClient.initializeWithCredentials('admin', 'admin').then(initializedClient => {
// From here on, you can use the helpers this library has
let {Templates, Devices} = initializedClient;
}).catch(console.error);
initializeWithToken(authToken)
Initializes the client with a JWT previously generated by the dojot server. If you have a cached JWT, this is the best way to reuse it.
let authToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJZcVhJSmhOZ0psZFpOUTRYN3BFQkFCanMwNTJiM0lSTiIsImlhdCI6MTU0ODA4MzI3OSwiZXhwIjoxNTQ4MDgzNjk5LCJuYW1lIjoiQWRtaW4gKHN1cGVydXNlcikiLCJlbWFpbCI6ImFkbWluQG5vZW1haWwuY29tIiwicHJvZmlsZSI6ImFkbWluIiwiZ3JvdXBzIjpbMV0sInVzZXJpZCI6MSwianRpIjoiYjg5ZTQ5YWQ4MmUxOTY1YTNkZDE4OGE5NWQ5ZDQ1YjMiLCJzZXJ2aWNlIjoiYWRtaW4iLCJ1c2VybmFtZSI6ImFkbWluIn0.SnXBMGQWF99nCvmn8tH_mloreHA4NYT-S8hkjSo7-0g';
configuredClient.initializeWithAuthToken(authToken).then(initializedClient => {
// From here on, you can use the helpers this library has
let {Templates, Devices} = initializedClient;
}).catch(console.error);
Using the library
This section describes the helpers available and gives an overview of each supported feature they have.
getAuthToken()
Returns the curent jwt used for authentication.
Templates
get()
Lists all templates available at dojot.
Templates.get().then(templates => {
console.log(`Retrieved ${templates.length} templates`);
}).catch(console.error);
set(templateData)
Creates a new template based on data sent on templateData
.
Templates.set({
"label": "EquipmentName",
"attrs": [
{
"label": "serialCode",
"type": "dynamic",
"value_type": "string"
}
]
}).then(template => {
console.log('Created a new template');
}).catch(console.error);
delete(templateData)
Deletes the template identified on templateData
.
Templates.delete({
"label": "EquipmentName",
"attrs": [
{
"label": "serialCode",
"type": "dynamic",
"value_type": "string"
}
]
}).then(template => {
console.log('Removed template', template);
}).catch(console.error);
Devices
get(options)
Lists all devices available at dojot.
Devices.get().then(devices => {
console.log(`Retrieved ${devices.length} devices`);
}).catch(console.error);
options
It's an object detailing what kind of query must be performed. Currently supported options are:
Get data for a specific device
{
"deviceId": "abc123"
}
Brings the last n
entries for each device's dynamic attribute
{
"historySize": 5
}
Paginate the devices list
{
"pageSize": 5,
"pageNumber": 2
}
Filter by device label name (or partial name)
{
"labelContains": "TemperatureSensor"
}
Filter by one or more attribute values
{
"filter": {
"make": "TexasInstruments",
"type": "Sensor"
}
}
set(deviceData)
Creates a new device based on data sent on deviceData
Devices.set({
"label": "equipamentoDoJoao",
"templates": [
"12"
],
"attrs": [
{
"id": 76,
"label": "serialCode",
"static_value": "SC01",
"template_id": "12",
"type": "dynamic",
"value_type": "string"
}
]
}).then(device => {
console.log('Created a new device');
}).catch(console.error);
delete(deviceData)
Deletes the device identified on deviceData
.
Devices.delete({
"label": "equipamentoDoJoao",
"templates": [
"12"
],
"attrs": [
{
"id": 76,
"label": "serialCode",
"static_value": "SC01",
"template_id": "12",
"type": "dynamic",
"value_type": "string"
}
]
}).then(device => {
console.log('Removed device', device);
}).catch(console.error);
onDeviceData(function callback(deviceData))
Defines a handler for data sent to devices.
Devices.onDeviceData((data) => {
console.log('Got device message data:', data);
});
onDeviceChange(function callback(changeData))
Defines a handler for device changes.
Devices.onDeviceChange((data) => {
console.log('Got device change data:', data);
});
Users
get()
Lists existing users
Users.get().then(users => {
console.log(`Retrieved ${users.length} users`);
}).catch(console.error);
set(userData)
Created an user based on userData
Users.set({
"username": "user01",
"service": "admin",
"email": "[email protected]",
"name": "user01",
"profile": "admin"
}).then(user => {
console.log('Created user', user);
}).catch(console.error);