0.5.11 • Published 5 years ago

@znti/dojot-web v0.5.11

Weekly downloads
-
License
-
Repository
github
Last release
5 years ago

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": "user01@noemail.com",
	"name": "user01",
	"profile": "admin"
}).then(user => {
	console.log('Created user', user);
}).catch(console.error);

Resources

0.5.11

5 years ago

0.5.10

5 years ago

0.5.9

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

5 years ago

0.5.5

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago