A client side library that provides access to the KNoT Cloud for Node.js applications.
npm install --save knot-cloudKNoTCloud 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();Create a client object that will connect to a KNoT Cloud instance.
hostString KNoT Cloud instance host name.portNumber KNoT Cloud instance port.uuidString User UUID.tokenString User token.
const KNoTCloud = require('knot-cloud');
const cloud = new KNoTCloud(
'knot-test.cesar.org.br',
3000,
'78159106-41ca-4022-95e8-2511695ce64c',
'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
);Connects to the KNoT Cloud instance.
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();Closes the current connection.
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();Gets the devices associated to the connected user.
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.
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] ] } ]Gets the device identified by id associated to the connected user.
idString device ID (KNoT ID).
deviceObject device as described in ingetDevices()
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' } ] }Gets the last 10 data items published by the device identified by id.
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)
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.
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' },
// ... ]Sets values to sensors.
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.
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();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.
idString device ID (KNoT ID).sensorsArray an array of sensor IDs (Number).
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();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().
idString device ID (KNoT ID).metadataAny device metadata.
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();Subscribes to data published by a device identified by id. To listen to the publish events, register a callback with on().
idString device ID (KNoT ID).
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();Registers a callback to receive device updates after subcribe(). A single callback will receive the updates for all devices the user is subscribed to.
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.
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' }