/** * Disconnect from the server * * @memberof MqttTransporter */ disconnect() { if (this.client) { return new this.broker.Promise(resolve => { this.client.end(false, resolve); this.client = null; }); } }
/** * Subscribe to a command * * @param {String} cmd * @param {String} nodeID * * @memberof MqttTransporter */ subscribe(cmd, nodeID) { return new this.broker.Promise((resolve, reject) => { const topic = this.getTopicName(cmd, nodeID); this.client.subscribe(topic, { qos: this.qos }, (err, granted) => { if (err) return reject(err); this.logger.debug("MQTT server granted", granted); resolve(); }); }); }
transporter._client.onCallbacks.connect(); // Trigger the `resolve` return p; });
transporter._client.onCallbacks.connect(); // Trigger the `resolve` return p; });
this._client = client; // For tests client.on("connect", () => { this.client = client; this.logger.info("MQTT client is connected."); client.on("error", (e) => { this.logger.error("MQTT error.", e.message); this.logger.debug(e); if (!client.connected) reject(e); }); client.on("reconnect", () => { this.logger.warn("MQTT client is reconnecting..."); }); client.on("message", (rawTopic, buf) => { const topic = rawTopic.substring(this.prefix.length + this.topicSeparator.length); const cmd = topic.split(this.topicSeparator)[0]; client.on("close", () => { this.connected = false; this.logger.warn("MQTT client is disconnected.");
ctx.mqttClient = new mqtt.Client(_ => websocket(host, options.wsOptions), options);
/** * Send data buffer. * * @param {String} topic * @param {Buffer} data * @param {Object} meta * * @returns {Promise} */ send(topic, data) { /* istanbul ignore next*/ if (!this.client) return; return new this.broker.Promise((resolve, reject) => { this.client.publish(topic, data, { qos: this.qos }, err => { /* istanbul ignore next*/ if (err) return reject(err); resolve(); }); }); }
this.client.unsubscribe('/'+this.appid+topic, function() { self.subscriptions.splice(self.subscriptions.indexOf('/'+self.appid+topic)); if (self.debugmode) console.log(self.subscriptions); if (typeof(callback) == 'function') callback(); });
function subscribetopic(payload){ node.subscribe('storm.dev/'+payload.newtopic+'/#', function (err) { if (err){ console.log('Error could not subscribe in '+payload.newtopic+': '+ err.toString()); } }); }
async function handleHostMonitoring(payload) { DEBUG('handle hostmonitoring'); const hostmonitoring = require(__dirname + '/storm_modules/system/hostmonitoring.js'); const taskData = await hostmonitoring.run(); const taskResult = { taskData: taskData }; DEBUG(taskResult); node.publish(`storm.dev/hostmonitoring/${payload.id}/${nodeOptions.nodeId}/results`, JSON.stringify(taskResult)); }
server.on('message', (topic, data) => { if (data instanceof Array) { data.forEach(function(subdata){ funcs.processJson(subdata,{}); }); } else { funcs.processJson(data,{}); } })
mqttClient.subscribe(topic, function (err, granted) { if (err) { console.error(err); return; } console.log('Subscribed to topic: ' + topic); });
transporter._client.onCallbacks.connect(); // Trigger the `resolve` return p; });