knot-cloud
v1.0.1
Published
KNoT Cloud library for Node.js
Readme
KNoT Cloud library for Node.js
A client side library that provides access to the KNoT Cloud for Node.js applications.
Getting started
Install
npm install --save knot-cloudQuickstart
KNoTCloud connects to http://host:port using the UUID and token as credentials, respectively. Replace this address with your cloud instance and the credentials with valid ones.
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
try {
await cloud.connect();
const devices = cloud.getDevices();
console.log(devices);
} catch (err) {
console.error(err);
}
await cloud.close();
}
main();Methods
constructor(host, port, uuid, token)
Create a client object that will connect to a KNoT Cloud instance.
Arguments
hostString KNoT Cloud instance host name.portNumber KNoT Cloud instance port.uuidString User UUID.tokenString User token.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);connect(): Promise<Void>
Connects to the KNoT Cloud instance.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
}
main();close(): Promise<Void>
Closes the current connection.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.close();
}
main();getDevices(): Promise<Array>
Gets the devices associated to the connected user.
Result
devicesArray devices registered on the cloud or an empty array. Each device is an object in the following format:idString device ID (KNoT ID).nameString device name.onlineBoolean whether this device is online or not.schemaArray schema items, each one formed by:sensor_idNumber sensor ID.value_typeNumber semantic value type (voltage, current, temperature, etc).unitNumber sensor unit (V, A, W, W, etc).type_idNumber data value type (boolean, integer, etc).nameString sensor name.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
console.log(await cloud.getDevices());
await cloud.close();
}
main();
// [ { online: true,
// name: 'Door lock',
// id: '7e133545550e496a',
// schema: [ [Object], [Object] ] } ]getDevice(id): Promise<Object>
Gets the device identified by id associated to the connected user.
Argument
idString device ID (KNoT ID).
Result
deviceObject device as described in ingetDevices()
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
console.log(await cloud.getDevice('7e133545550e496a'));
await cloud.close();
}
main();
// { online: true,
// name: 'Door lock',
// id: '7e133545550e496a',
// schema: [ { sensor_id: 1,
// value_type: 3,
// unit: 0,
// type_id: 65521,
// name: 'Lock' },
// { sensor_id: 2,
// value_type: 1,
// unit: 2,
// type_id: 9,
// name: 'Card reader' } ] }getData(id[,limit, start, finish]): Promise<Array>
Gets the last 10 data items published by the device identified by id.
Arguments
idString device ID (KNoT ID).limitString (Optional) the maximum number of data that you want, default=10(the value*returns all data)startString (Optional) the start date that you want your set of data (format=YYYY/MM/DD HH:MM)finishString (Optional) the finish date that you want your set of data (format=YYYY/MM/DD HH:MM)
Result
data_itemsArray data items published by the device or an empty array. Each data item is an object in the following format:dataObject data published by the device, in the following format:sensor_idNumber sensor ID.valueString|Boolean|Number value published.
timestampDate moment of publication.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
console.log(await cloud.getData('7e133545550e496a'));
await cloud.close();
}
main();
// [ { data: { sensor_id: 2, value: 0 },
// timestamp: '2018-08-25T05:29:43.519Z' },
// { data: { sensor_id: 1, value: true },
// timestamp: '2018-08-25T05:29:43.520Z' },
// ... ]setData(id, data): Promise<Void>
Sets values to sensors.
Arguments
idString device ID (KNoT ID).dataArray array of sensor ID-value pairs, as follows:sensorIdNumber sensor ID.valueString|Boolean|Number value to attribute to the sensor. Strings must be Base64 encoded.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.setData('7e133545550e496a', [{ sensorId: 1, value: false }]);
await cloud.close();
}
main();requestData(id, sensors): Promise<Void>
Requests the device to publish its current value a set of sensors. The value can be retrieved using getData() or by listening to device updates.
Arguments
idString device ID (KNoT ID).sensorsArray an array of sensor IDs (Number).
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.requestData('7e133545550e496a', [1]);
await cloud.close();
}
main();setMetadata(id, metadata): Promise<Void>
Sets the device metadata. It will update the entire object with the contents of metadata. To update a single item inside metadata, fetch it first, using getDevice().
Arguments
idString device ID (KNoT ID).metadataAny device metadata.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.setMetadata('7e133545550e496a', { room: { name: 'Lula Cardoso Ayres', location: 'Tiradentes' } });
await cloud.close();
}
main();subscribe(id): Promise<Void>
Subscribes to data published by a device identified by id. To listen to the publish events, register a callback with on().
Argument
idString device ID (KNoT ID).
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.subscribe('7e133545550e496a');
await cloud.close();
}
main();on(callback)
Registers a callback to receive device updates after subcribe(). A single callback will receive the updates for all devices the user is subscribed to.
Argument
callbackFunction callback called with device updates. Receives:eventObject published event, object in the following format:sourceString device ID (KNoT ID).dataObject data published by the device, in the following format:sensor_idNumber sensor ID.valueString|Boolean|Number value published.
timestampDate moment of publication.
Example
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);
async function main() {
await cloud.connect();
await cloud.subscribe('7e133545550e496a');
cloud.on((data) => {
console.log(data);
});
}
main();
// { data: { sensor_id: 2, value: 21 },
// timestamp: '2018-08-25T17:46:41.337Z',
// source: '7e133545550e496a' }