A client side library that provides a WebSocket abstraction to the KNoT Cloud for Node.js and browser applications.
npm install --save @cesarbr/knot-cloud-websocketKNoTCloudWebSocket connects to <protocol>://<hostname>:<port>/<pathname> using ID and token as credentials. Replace this address with your protocol adapter instance and the credentials with valid ones.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.register({
id: '6e5a681b2ae7be40',
type: 'knot:thing',
name: 'Door Lock',
});
});
client.on('registered', (thing) => {
console.log('Registered', thing);
client.close();
});
client.connect();Create a client object that will connect to a KNoT Cloud protocol adapter instance.
optionsObject JSON object with connection details.protocolString (Optional) Either'ws'or'wss'. Default:'wss'.hostnameString KNoT Cloud protocol adapter instance host name.portNumber (Optional) KNoT Cloud protocol adapter instance port. Default: 443.pathnameString (Optional) Path name on the server.idString Device ID.tokenString Device token.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});Connects to the protocol adapter instance. Receives the 'ready' message when succeeds and 'error' otherwise.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
console.log('Connection established');
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
});
client.connect();Closes the current connection.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
console.log('Connection established');
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Registers a new device. Receives the 'registered' message when succeeds and 'error' otherwise.
Users can create 'knot:gateway', 'knot:app' and 'knot:thing'. Gateways ('knot:gateway') can create 'knot:thing'.
propertiesObject JSON object with device detailstypeString Device type. One of:'knot:gateway','knot:app'or'knot:thing'.nameString (Optional) Human readable name for your device.idString Device ID. Required whentypeis'knot:thing', for other types it is automatically generated.activeBoolean (Optional) Whether the gateway being created is active. Only used whentypeis'knot:gateway'. Default:false.
deviceObject JSON object containing device details after creation on cloud.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.register({
id: '6e5a681b2ae7be40',
type: 'knot:thing',
name: 'Door Lock',
});
});
client.on('registered', (thing) => {
console.log(thing);
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();
// { type: 'knot:thing',
// metadata: { name: 'Door Lock' },
// knot:
// { gateways: [ '78159106-41ca-4022-95e8-2511695ce64c' ],
// id: '6e5a681b2ae7be40' },
// token: '40ad864d503488eda9b629825876d46cb1356bdf' }Removes a device from the cloud. Receives the 'unregistered' message when succeeds and 'error' otherwise.
idString Device ID.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.unregister('6e5a681b2ae7be40');
});
client.on('unregistered', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Lists the devices registered on cloud. If a query is specified, only the devices that match such query will be returned. Receives the 'devices' message when succeeds and 'error' otherwise.
queryObject (Optional) Search query, written using MongoDB query format.
devicesArray Set of devices that match the constraint specified onquery.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.getDevices({
type: 'knot:thing',
});
});
client.on('devices', (devices) => {
console.log(devices);
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();
// [ { type: 'knot:thing',
// metadata: { name: 'Door Lock' },
// knot:
// { gateways: [ '78159106-41ca-4022-95e8-2511695ce64c' ],
// id: '6e5a681b2ae7be40' } } ]Creates a session token for a device. Receives the 'created' message when succeeds and 'error' otherwise.
idString Device ID.
tokenString New token for the specified device.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.createSessionToken('6e5a681b2ae7be40');
});
client.on('created', (token) => {
console.log(token);
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();
// 'a0ab6f486633ddc87dceecc98e88d7ffee60a402'Revokes a device session token. Receives the 'revoked' message when succeeds and 'error' otherwise.
idString Device ID.tokenString Existing session token for the specified device.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.revokeSessionToken('6e5a681b2ae7be40', 'a0ab6f486633ddc87dceecc98e88d7ffee60a402');
});
client.on('revoked', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Updates the thing schema. Receives the 'updated' message when succeeds and 'error' otherwise.
schemaArray An array of objects in the following format:sensorIdNumber Sensor ID. Value between 0 and the maximum number of sensors defined for that thing.typeIdNumber Sensor type, e.g. whether it is a presence sensor or distance sensor.valueTypeNumber Value type, e.g. whether it is an integer, a floating-point number, etc.unitNumber Sensor unit.nameString Sensor name.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '6e5a681b2ae7be40',
token: 'a0ab6f486633ddc87dceecc98e88d7ffee60a402',
});
client.on('ready', () => {
client.updateSchema([
{
sensorId: 253,
typeId: 0xFFF1,
valueType: 3,
unit: 0,
name: 'Lock'
}
]);
});
client.on('updated', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Activates a gateway. Receives the 'activated' message when succeeds and 'error' otherwise.
idString Device ID.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.activate('871a6907-45c0-4557-b783-6224f3de92e7');
});
client.on('activated', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Updates the device metadata. Receives the 'updated' message when succeeds and 'error' otherwise.
idString Device ID.metadataAny Device metadata.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.updateMetadata('6e5a681b2ae7be40', {
room: {
name: 'Lula Cardoso Ayres',
location: 'Tiradentes'
}
});
});
client.on('updated', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Publishes data. Receives the 'published' message when succeeds and 'error' otherwise.
sensorIdNumber Sensor ID.valueNumber Sensor value.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '6e5a681b2ae7be40',
token: 'a0ab6f486633ddc87dceecc98e88d7ffee60a402',
});
client.on('ready', () => {
client.publishData(253, true);
});
client.on('published', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Requests a thing to send its current data items values. Receives the 'sent' message when succeeds and 'error' otherwise.
idString Device ID.sensorIdsArray Array of sensor IDs.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.getData('6e5a681b2ae7be40', [253]);
});
client.on('data', (data) => {
console.log(JSON.stringify(data, null, 2));
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Requests a thing to update its data items with the values passed as arguments. Receives the 'sent' message when succeeds and 'error' otherwise.
idString Device ID.dataArray Data items to be sent, each one formed by:sensorIdNumber Sensor ID.valueString|Boolean|Number Sensor value. Strings must be Base64 encoded.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('ready', () => {
client.setData('6e5a681b2ae7be40', [{ sensorId: 253, value: false }]);
});
client.on('sent', () => {
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Registers an event handler. See next section for details on events.
nameString Event namehandlerFunction Event handler.
const KNoTCloudWebSocket = require('@cesarbr/knot-cloud-websocket');
const client = new KNoTCloudWebSocket({
protocol: 'wss',
hostname: 'ws.knot.cloud',
port: 443,
pathname: '/',
id: '78159106-41ca-4022-95e8-2511695ce64c',
token: 'd5265dbc4576a88f8654a8fc2c4d46a6d7b85574',
});
client.on('registered', (message) => {
console.log('Who?', message.from);
console.log('What?', message.payload);
client.close();
});
client.on('error', (err) => {
console.error(err);
console.log('Connection refused');
client.close();
});
client.connect();Events can be listened to by registering a handler with on(). The handler will receive an object in the following format:
fromString ID of the device generating the event.payloadAny (Optional) Event-defined payload.
Triggered when a device is registered on the cloud. Only apps ('knot:app') and users receive this event.
An object containing the registered device.
{
from: '78159106-41ca-4022-95e8-2511695ce64c',
payload: {
type: 'knot:thing',
metadata: { name: 'Door Lock' },
knot: {
gateways: [ '78159106-41ca-4022-95e8-2511695ce64c' ],
id: '6e5a681b2ae7be40',
},
},
}Triggered when a device is unregistered from the cloud. Only apps ('knot:app') and users receive this event.
No payload. The ID of the unregistered device will come in the from field.
{
from: '6e5a681b2ae7be40'
}Triggered when a device publishes data items. Only apps 'knot:app' and users receive this event.
An object in the following format:
sensorIdNumber Sensor ID. Value between 0 and the maximum number of sensors defined for that thing.valueString|Boolean|Number Sensor value. Strings must be Base64 encoded.
{
from: '6e5a681b2ae7be40',
payload: {
sensorId: 253,
value: true,
},
}Triggered when a device of type 'knot:app' sends a command. Currently supported commands are getData and setData. Only things ('knot:thing') receive this event.
An object in the following format:
nameString Command name.argsAny (Optional) Command-defined arguments.
{
from: '78159106-41ca-4022-95e8-2511695ce64c',
payload: {
name: 'getData',
args: [253],
},
}