UNPKG

node-red-contrib-kolibri

Version:

Node-RED nodes for connecting to a Kolibri Broker (e.g. HMS Hub)

85 lines 3.16 kB
"use strict"; /* * Copyright 2021 HMS Industrial Networks AB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http: //www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const nodeInit = (RED) => { class KolibriInNode { constructor(config) { const lazyThis = this; RED.nodes.createNode(lazyThis, config); this.name = config.name; this.path = config.path; this.broker = config.broker; this.resume = config.resume; this.brokerConn = RED.nodes.getNode(config.broker); lazyThis.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.disconnected' }); if (!this.brokerConn) { lazyThis.error('Kolibri: missing broker configuration'); return; } if (!this.path) { lazyThis.error('Kolibri: path not defined for node ' + this.name); return; } this.brokerConn.register(this) .then(() => { this.brokerConn.addSubscribeListener(this.path, (path, ts, qual, value) => { const msg = { path: path, timestamp: ts, quality: qual, payload: value }; lazyThis.send(msg); }); }).then(() => { return this.brokerConn.subscribe({ path: this.path, resume: this.resume }); }) .then(() => { if (this.brokerConn.connected) { lazyThis.status({ fill: 'green', shape: 'dot', text: 'node-red:common.status.connected' }); } }).catch((e) => { lazyThis.error('Error during node creation: ' + e); }); lazyThis.on('close', (done) => { if (this.brokerConn) { lazyThis.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.disconnected' }); this.brokerConn.unsubscribe({ path: this.path }) .then(() => { return this.brokerConn.deregister(this); }); } done(); }); } } RED.nodes.registerType('kolibri-in', KolibriInNode); }; module.exports = nodeInit; module.exports = nodeInit; //# sourceMappingURL=kolibri-in.js.map