node-red-contrib-mobius-flow-thingsboard
Version:
Node-RED nodes to work with MOBiUSFlow and ThingsBoard.io
153 lines (145 loc) • 6.08 kB
JavaScript
/*
Copyright (c) 2017, IAconnects Technology Limited
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be
used to endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const thingsboard_http_client_1 = require("../thingsboard-drivers/thingsboard-http-client");
const thingsboard_mqtt_client_1 = require("../thingsboard-drivers/thingsboard-mqtt-client");
module.exports = (RED) => {
RED.nodes.registerType('mobius thingsboard connection', function (configuration) {
RED.nodes.createNode(this, configuration);
const node = this;
const users = {};
let mqttConnected = false;
const globalContext = node.context().global;
const registeredObjects = [];
const httpClient = new thingsboard_http_client_1.ThingsboardHttpClient(configuration.site, configuration.username, configuration.password, configuration.local === 'local' ? true : false);
const mqttClient = new thingsboard_mqtt_client_1.ThingsboardMqttClient(configuration.site, configuration.gatewayId, configuration.gatewayAccessToken, configuration.local === 'local' ? true : false);
node.on('close', (done) => {
httpClient.closeHttpConnection();
done();
});
node.getHttpClient = () => {
return httpClient;
};
node.getMqttClient = () => {
return mqttClient;
};
/**
* Register a new Thingsboard node with this config node
* @param thingsboardNode The new node to register
*/
node.register = (thingsboardNode) => {
users[thingsboardNode.id] = thingsboardNode;
if (Object.keys(users).length === 1) {
mqttClient.connect()
.catch((err) => {
node.error(`Failed to connect to Thingsboard: ${err.message}`);
});
}
if (mqttConnected) {
users[thingsboardNode.id].status({
fill: 'green',
shape: 'dot',
text: 'Connected',
});
}
};
/**
* Deregister a previously registered Thingsboard node
* @param thingsboardNode The node to deregister
* @param done A callback to be run by the node after it has been deregistered
*/
node.deregister = (thingsboardNode, done) => {
delete users[thingsboardNode.id];
if (Object.keys(users).length === 0) {
mqttClient.disconnect();
}
done();
};
/**
* Clear the registered objects list
*/
node.clearRegisteredObjects = () => {
registeredObjects.splice(0);
};
/**
* Register a Mobius object
* @param uri The object uri
*/
node.registerObject = (uri) => {
if (!registeredObjects.includes(uri)) {
registeredObjects.push(uri);
}
};
/**
* Get the full registered objects list
*/
node.getRegisteredObjects = () => {
return registeredObjects;
};
/**
* Check if an object is registered
* @param uri The object uri
*/
node.objectIsRegistered = (uri) => {
return registeredObjects.includes(uri);
};
// ***** MQTT CONNECTION FUNCTIONS *****/
mqttClient.on('connecting', () => {
mqttConnected = false;
for (const id in users) {
if (users.hasOwnProperty(id)) {
users[id].status({
fill: 'yellow',
shape: 'dot',
text: 'Connecting',
});
}
}
});
mqttClient.on('disconnected', () => {
mqttConnected = false;
for (const id in users) {
if (users.hasOwnProperty(id)) {
users[id].status({
fill: 'red',
shape: 'ring',
text: 'Disconnected',
});
}
}
});
mqttClient.on('connected', () => {
mqttConnected = true;
for (const id in users) {
if (users.hasOwnProperty(id)) {
users[id].status({
fill: 'green',
shape: 'dot',
text: 'Connected',
});
}
}
});
});
};
;