2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 10:00:28 +02:00
|
|
|
"use strict";
|
2015-06-03 08:54:43 +02:00
|
|
|
var mqtt = require("mqtt");
|
|
|
|
var util = require("util");
|
2014-11-04 22:56:15 +01:00
|
|
|
var isUtf8 = require('is-utf8');
|
2014-05-29 10:00:28 +02:00
|
|
|
|
2015-06-03 08:54:43 +02:00
|
|
|
function matchTopic(ts,t) {
|
|
|
|
if (ts == "#") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var re = new RegExp("^"+ts.replace(/([\[\]\?\(\)\\\\$\^\*\.|])/g,"\\$1").replace(/\+/g,"[^/]+").replace(/\/#$/,"(\/.*)?")+"$");
|
|
|
|
return re.test(t);
|
|
|
|
}
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function MQTTBrokerNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2015-06-03 08:54:43 +02:00
|
|
|
|
|
|
|
// Configuration options passed by Node Red
|
2014-05-04 00:32:04 +02:00
|
|
|
this.broker = n.broker;
|
|
|
|
this.port = n.port;
|
|
|
|
this.clientid = n.clientid;
|
2015-09-01 23:30:15 +02:00
|
|
|
this.usetls = n.usetls;
|
2018-01-11 22:22:02 +01:00
|
|
|
this.usews = n.usews;
|
2015-09-01 23:30:15 +02:00
|
|
|
this.verifyservercert = n.verifyservercert;
|
2015-06-03 08:54:43 +02:00
|
|
|
this.compatmode = n.compatmode;
|
2015-09-01 23:30:15 +02:00
|
|
|
this.keepalive = n.keepalive;
|
|
|
|
this.cleansession = n.cleansession;
|
2015-06-03 08:54:43 +02:00
|
|
|
|
|
|
|
// Config node state
|
|
|
|
this.brokerurl = "";
|
|
|
|
this.connected = false;
|
|
|
|
this.connecting = false;
|
2015-12-23 00:25:29 +01:00
|
|
|
this.closing = false;
|
2015-06-03 08:54:43 +02:00
|
|
|
this.options = {};
|
|
|
|
this.queue = [];
|
|
|
|
this.subscriptions = {};
|
|
|
|
|
2015-09-02 12:18:59 +02:00
|
|
|
if (n.birthTopic) {
|
|
|
|
this.birthMessage = {
|
|
|
|
topic: n.birthTopic,
|
|
|
|
payload: n.birthPayload || "",
|
|
|
|
qos: Number(n.birthQos||0),
|
|
|
|
retain: n.birthRetain=="true"|| n.birthRetain===true
|
|
|
|
};
|
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
|
2014-07-18 15:47:58 +02:00
|
|
|
if (this.credentials) {
|
|
|
|
this.username = this.credentials.user;
|
|
|
|
this.password = this.credentials.password;
|
2014-05-29 10:00:28 +02:00
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
|
|
|
|
// If the config node is missing certain options (it was probably deployed prior to an update to the node code),
|
|
|
|
// select/generate sensible options for the new fields
|
2017-03-10 21:12:52 +01:00
|
|
|
if (typeof this.usetls === 'undefined') {
|
2015-09-01 23:30:15 +02:00
|
|
|
this.usetls = false;
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
2018-01-11 22:22:02 +01:00
|
|
|
if (typeof this.usews === 'undefined') {
|
|
|
|
this.usews = false;
|
|
|
|
}
|
2017-03-10 21:12:52 +01:00
|
|
|
if (typeof this.compatmode === 'undefined') {
|
2015-06-03 08:54:43 +02:00
|
|
|
this.compatmode = true;
|
|
|
|
}
|
2017-03-10 21:12:52 +01:00
|
|
|
if (typeof this.verifyservercert === 'undefined') {
|
2015-09-01 23:30:15 +02:00
|
|
|
this.verifyservercert = false;
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
2017-03-10 21:12:52 +01:00
|
|
|
if (typeof this.keepalive === 'undefined') {
|
2016-02-19 23:52:43 +01:00
|
|
|
this.keepalive = 60;
|
2015-09-01 23:30:15 +02:00
|
|
|
} else if (typeof this.keepalive === 'string') {
|
|
|
|
this.keepalive = Number(this.keepalive);
|
|
|
|
}
|
|
|
|
if (typeof this.cleansession === 'undefined') {
|
|
|
|
this.cleansession = true;
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the URL to pass in to the MQTT.js library
|
2016-07-26 22:33:00 +02:00
|
|
|
if (this.brokerurl === "") {
|
2018-01-11 23:46:18 +01:00
|
|
|
// if the broker may be ws:// or wss:// or even tcp://
|
2017-04-12 19:31:49 +02:00
|
|
|
if (this.broker.indexOf("://") > -1) {
|
|
|
|
this.brokerurl = this.broker;
|
2015-06-03 08:54:43 +02:00
|
|
|
} else {
|
2017-04-12 19:31:49 +02:00
|
|
|
// construct the std mqtt:// url
|
2018-01-11 22:22:02 +01:00
|
|
|
if (this.usetls) {
|
|
|
|
this.brokerurl="mqtts://";
|
|
|
|
} else {
|
2017-04-12 19:31:49 +02:00
|
|
|
this.brokerurl="mqtt://";
|
|
|
|
}
|
|
|
|
if (this.broker !== "") {
|
|
|
|
this.brokerurl = this.brokerurl+this.broker+":";
|
|
|
|
// port now defaults to 1883 if unset.
|
|
|
|
if (!this.port){
|
|
|
|
this.brokerurl = this.brokerurl+"1883";
|
|
|
|
} else {
|
|
|
|
this.brokerurl = this.brokerurl+this.port;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.brokerurl = this.brokerurl+"localhost:1883";
|
2018-01-11 22:22:02 +01:00
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 23:30:15 +02:00
|
|
|
if (!this.cleansession && !this.clientid) {
|
|
|
|
this.cleansession = true;
|
|
|
|
this.warn(RED._("mqtt.errors.nonclean-missingclientid"));
|
|
|
|
}
|
|
|
|
|
2015-06-03 08:54:43 +02:00
|
|
|
// Build options for passing to the MQTT.js API
|
|
|
|
this.options.clientId = this.clientid || 'mqtt_' + (1+Math.random()*4294967295).toString(16);
|
|
|
|
this.options.username = this.username;
|
|
|
|
this.options.password = this.password;
|
2015-09-01 23:30:15 +02:00
|
|
|
this.options.keepalive = this.keepalive;
|
2015-09-01 23:58:26 +02:00
|
|
|
this.options.clean = this.cleansession;
|
2015-09-01 23:30:15 +02:00
|
|
|
this.options.reconnectPeriod = RED.settings.mqttReconnectTime||5000;
|
2017-03-10 21:12:52 +01:00
|
|
|
if (this.compatmode == "true" || this.compatmode === true) {
|
2015-06-03 08:54:43 +02:00
|
|
|
this.options.protocolId = 'MQIsdp';
|
|
|
|
this.options.protocolVersion = 3;
|
|
|
|
}
|
2016-04-27 13:31:54 +02:00
|
|
|
if (this.usetls && n.tls) {
|
|
|
|
var tlsNode = RED.nodes.getNode(n.tls);
|
|
|
|
if (tlsNode) {
|
|
|
|
tlsNode.addTLSOptions(this.options);
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
console.log(this.brokerurl,this.options);
|
|
|
|
|
2016-04-27 13:31:54 +02:00
|
|
|
// If there's no rejectUnauthorized already, then this could be an
|
|
|
|
// old config where this option was provided on the broker node and
|
|
|
|
// not the tls node
|
|
|
|
if (typeof this.options.rejectUnauthorized === 'undefined') {
|
|
|
|
this.options.rejectUnauthorized = (this.verifyservercert == "true" || this.verifyservercert === true);
|
|
|
|
}
|
2015-09-01 23:30:15 +02:00
|
|
|
|
|
|
|
if (n.willTopic) {
|
|
|
|
this.options.will = {
|
|
|
|
topic: n.willTopic,
|
|
|
|
payload: n.willPayload || "",
|
|
|
|
qos: Number(n.willQos||0),
|
|
|
|
retain: n.willRetain=="true"|| n.willRetain===true
|
|
|
|
};
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define functions called by MQTT in and out nodes
|
|
|
|
var node = this;
|
2015-11-17 23:19:56 +01:00
|
|
|
this.users = {};
|
|
|
|
|
2017-03-10 21:12:52 +01:00
|
|
|
this.register = function(mqttNode) {
|
2015-11-17 23:19:56 +01:00
|
|
|
node.users[mqttNode.id] = mqttNode;
|
|
|
|
if (Object.keys(node.users).length === 1) {
|
|
|
|
node.connect();
|
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
};
|
|
|
|
|
2017-03-10 21:12:52 +01:00
|
|
|
this.deregister = function(mqttNode,done) {
|
2015-11-17 23:19:56 +01:00
|
|
|
delete node.users[mqttNode.id];
|
2015-12-23 00:25:29 +01:00
|
|
|
if (node.closing) {
|
|
|
|
return done();
|
|
|
|
}
|
2015-11-17 23:19:56 +01:00
|
|
|
if (Object.keys(node.users).length === 0) {
|
2016-04-18 14:54:05 +02:00
|
|
|
if (node.client && node.client.connected) {
|
2015-12-23 00:25:29 +01:00
|
|
|
return node.client.end(done);
|
2016-04-18 14:54:05 +02:00
|
|
|
} else {
|
|
|
|
node.client.end();
|
2016-07-26 22:33:00 +02:00
|
|
|
return done();
|
2015-12-23 00:25:29 +01:00
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
2015-12-23 00:25:29 +01:00
|
|
|
done();
|
2015-06-03 08:54:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
this.connect = function () {
|
2017-09-20 11:30:07 +02:00
|
|
|
console.log("CONNECT");
|
2015-06-03 08:54:43 +02:00
|
|
|
if (!node.connected && !node.connecting) {
|
|
|
|
node.connecting = true;
|
|
|
|
node.client = mqtt.connect(node.brokerurl ,node.options);
|
2015-11-17 23:19:56 +01:00
|
|
|
node.client.setMaxListeners(0);
|
2015-06-03 08:54:43 +02:00
|
|
|
// Register successful connect or reconnect handler
|
|
|
|
node.client.on('connect', function () {
|
2015-11-17 23:19:56 +01:00
|
|
|
node.connecting = false;
|
2015-06-03 08:54:43 +02:00
|
|
|
node.connected = true;
|
2015-09-01 23:30:15 +02:00
|
|
|
node.log(RED._("mqtt.state.connected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
2015-11-17 23:19:56 +01:00
|
|
|
for (var id in node.users) {
|
|
|
|
if (node.users.hasOwnProperty(id)) {
|
2016-09-19 10:36:38 +02:00
|
|
|
node.users[id].status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2015-11-17 23:19:56 +01:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
// Remove any existing listeners before resubscribing to avoid duplicates in the event of a re-connection
|
|
|
|
node.client.removeAllListeners('message');
|
|
|
|
|
|
|
|
// Re-subscribe to stored topics
|
|
|
|
for (var s in node.subscriptions) {
|
2016-07-26 22:33:00 +02:00
|
|
|
if (node.subscriptions.hasOwnProperty(s)) {
|
|
|
|
var topic = s;
|
|
|
|
var qos = 0;
|
|
|
|
for (var r in node.subscriptions[s]) {
|
|
|
|
if (node.subscriptions[s].hasOwnProperty(r)) {
|
|
|
|
qos = Math.max(qos,node.subscriptions[s][r].qos);
|
|
|
|
node.client.on('message',node.subscriptions[s][r].handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var options = {qos: qos};
|
|
|
|
node.client.subscribe(topic, options);
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:18:59 +02:00
|
|
|
// Send any birth message
|
|
|
|
if (node.birthMessage) {
|
|
|
|
node.publish(node.birthMessage);
|
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
});
|
2016-02-19 23:52:43 +01:00
|
|
|
node.client.on("reconnect", function() {
|
|
|
|
for (var id in node.users) {
|
|
|
|
if (node.users.hasOwnProperty(id)) {
|
2016-09-19 10:36:38 +02:00
|
|
|
node.users[id].status({fill:"yellow",shape:"ring",text:"node-red:common.status.connecting"});
|
2016-02-19 23:52:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-06-03 08:54:43 +02:00
|
|
|
// Register disconnect handlers
|
|
|
|
node.client.on('close', function () {
|
2015-09-01 23:30:15 +02:00
|
|
|
if (node.connected) {
|
2015-06-03 08:54:43 +02:00
|
|
|
node.connected = false;
|
2015-09-01 23:30:15 +02:00
|
|
|
node.log(RED._("mqtt.state.disconnected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
2015-11-17 23:19:56 +01:00
|
|
|
for (var id in node.users) {
|
|
|
|
if (node.users.hasOwnProperty(id)) {
|
2016-09-19 10:36:38 +02:00
|
|
|
node.users[id].status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
2015-11-17 23:19:56 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 23:30:15 +02:00
|
|
|
} else if (node.connecting) {
|
|
|
|
node.log(RED._("mqtt.state.connect-failed",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register connect error handler
|
|
|
|
node.client.on('error', function (error) {
|
|
|
|
if (node.connecting) {
|
|
|
|
node.client.end();
|
|
|
|
node.connecting = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.subscribe = function (topic,qos,callback,ref) {
|
|
|
|
ref = ref||0;
|
|
|
|
node.subscriptions[topic] = node.subscriptions[topic]||{};
|
|
|
|
var sub = {
|
|
|
|
topic:topic,
|
|
|
|
qos:qos,
|
|
|
|
handler:function(mtopic,mpayload, mpacket) {
|
|
|
|
if (matchTopic(topic,mtopic)) {
|
|
|
|
callback(mtopic,mpayload, mpacket);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ref: ref
|
|
|
|
};
|
|
|
|
node.subscriptions[topic][ref] = sub;
|
|
|
|
if (node.connected) {
|
|
|
|
node.client.on('message',sub.handler);
|
|
|
|
var options = {};
|
|
|
|
options.qos = qos;
|
|
|
|
node.client.subscribe(topic, options);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.unsubscribe = function (topic, ref) {
|
|
|
|
ref = ref||0;
|
|
|
|
var sub = node.subscriptions[topic];
|
|
|
|
if (sub) {
|
|
|
|
if (sub[ref]) {
|
|
|
|
node.client.removeListener('message',sub[ref].handler);
|
|
|
|
delete sub[ref];
|
|
|
|
}
|
2016-07-26 22:33:00 +02:00
|
|
|
if (Object.keys(sub).length === 0) {
|
2015-06-03 08:54:43 +02:00
|
|
|
delete node.subscriptions[topic];
|
2017-03-10 21:12:52 +01:00
|
|
|
if (node.connected) {
|
2015-06-03 08:54:43 +02:00
|
|
|
node.client.unsubscribe(topic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.publish = function (msg) {
|
|
|
|
if (node.connected) {
|
2018-01-13 23:53:58 +01:00
|
|
|
if (msg.payload === null || msg.payload === undefined) {
|
|
|
|
msg.payload = "";
|
|
|
|
} else if (!Buffer.isBuffer(msg.payload)) {
|
2015-06-03 08:54:43 +02:00
|
|
|
if (typeof msg.payload === "object") {
|
|
|
|
msg.payload = JSON.stringify(msg.payload);
|
|
|
|
} else if (typeof msg.payload !== "string") {
|
|
|
|
msg.payload = "" + msg.payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
qos: msg.qos || 0,
|
|
|
|
retain: msg.retain || false
|
|
|
|
};
|
2017-03-10 21:12:52 +01:00
|
|
|
node.client.publish(msg.topic, msg.payload, options, function(err) {return});
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-17 23:19:56 +01:00
|
|
|
this.on('close', function(done) {
|
2015-12-23 00:25:29 +01:00
|
|
|
this.closing = true;
|
2015-06-03 08:54:43 +02:00
|
|
|
if (this.connected) {
|
2015-12-23 00:25:29 +01:00
|
|
|
this.client.once('close', function() {
|
2015-11-17 23:19:56 +01:00
|
|
|
done();
|
2015-06-03 08:54:43 +02:00
|
|
|
});
|
|
|
|
this.client.end();
|
2017-03-10 21:12:52 +01:00
|
|
|
} else if (this.connecting || node.client.reconnecting) {
|
2016-04-27 13:31:54 +02:00
|
|
|
node.client.end();
|
|
|
|
done();
|
2015-06-03 08:54:43 +02:00
|
|
|
} else {
|
2015-11-17 23:19:56 +01:00
|
|
|
done();
|
2015-06-03 08:54:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-25 23:50:08 +01:00
|
|
|
}
|
2015-06-03 08:54:43 +02:00
|
|
|
|
2014-07-18 15:47:58 +02:00
|
|
|
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode,{
|
|
|
|
credentials: {
|
|
|
|
user: {type:"text"},
|
|
|
|
password: {type: "password"}
|
2013-11-25 23:50:08 +01:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-05-29 10:00:28 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function MQTTInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.topic = n.topic;
|
2016-06-20 22:35:43 +02:00
|
|
|
this.qos = parseInt(n.qos);
|
|
|
|
if (isNaN(this.qos) || this.qos < 0 || this.qos > 2) {
|
|
|
|
this.qos = 2;
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
this.broker = n.broker;
|
2015-06-03 08:54:43 +02:00
|
|
|
this.brokerConn = RED.nodes.getNode(this.broker);
|
2016-02-10 23:38:59 +01:00
|
|
|
if (!/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/.test(this.topic)) {
|
|
|
|
return this.warn(RED._("mqtt.errors.invalid-topic"));
|
|
|
|
}
|
2015-11-17 23:19:56 +01:00
|
|
|
var node = this;
|
2015-06-03 08:54:43 +02:00
|
|
|
if (this.brokerConn) {
|
2016-09-19 10:36:38 +02:00
|
|
|
this.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
2015-03-16 18:07:46 +01:00
|
|
|
if (this.topic) {
|
2015-12-23 00:25:29 +01:00
|
|
|
node.brokerConn.register(this);
|
2016-04-10 19:51:45 +02:00
|
|
|
this.brokerConn.subscribe(this.topic,this.qos,function(topic,payload,packet) {
|
2015-03-16 18:07:46 +01:00
|
|
|
if (isUtf8(payload)) { payload = payload.toString(); }
|
2015-06-03 08:54:43 +02:00
|
|
|
var msg = {topic:topic,payload:payload, qos: packet.qos, retain: packet.retain};
|
|
|
|
if ((node.brokerConn.broker === "localhost")||(node.brokerConn.broker === "127.0.0.1")) {
|
2015-03-16 18:07:46 +01:00
|
|
|
msg._topic = topic;
|
|
|
|
}
|
|
|
|
node.send(msg);
|
2015-04-09 21:10:34 +02:00
|
|
|
}, this.id);
|
2015-06-03 08:54:43 +02:00
|
|
|
if (this.brokerConn.connected) {
|
2016-09-19 10:36:38 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2015-04-09 21:10:34 +02:00
|
|
|
}
|
2015-03-16 18:07:46 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-05-10 22:47:22 +02:00
|
|
|
this.error(RED._("mqtt.errors.not-defined"));
|
2015-03-16 18:07:46 +01:00
|
|
|
}
|
2015-12-23 00:25:29 +01:00
|
|
|
this.on('close', function(done) {
|
2015-11-17 23:19:56 +01:00
|
|
|
if (node.brokerConn) {
|
|
|
|
node.brokerConn.unsubscribe(node.topic,node.id);
|
2015-12-23 00:25:29 +01:00
|
|
|
node.brokerConn.deregister(node,done);
|
2015-11-17 23:19:56 +01:00
|
|
|
}
|
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-05-10 22:47:22 +02:00
|
|
|
this.error(RED._("mqtt.errors.missing-config"));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("mqtt in",MQTTInNode);
|
2014-05-29 10:00:28 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function MQTTOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.topic = n.topic;
|
2014-08-22 13:42:07 +02:00
|
|
|
this.qos = n.qos || null;
|
|
|
|
this.retain = n.retain;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.broker = n.broker;
|
2015-06-03 08:54:43 +02:00
|
|
|
this.brokerConn = RED.nodes.getNode(this.broker);
|
2015-11-17 23:19:56 +01:00
|
|
|
var node = this;
|
2014-05-29 10:00:28 +02:00
|
|
|
|
2015-06-03 08:54:43 +02:00
|
|
|
if (this.brokerConn) {
|
2016-09-19 10:36:38 +02:00
|
|
|
this.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
2014-05-04 00:32:04 +02:00
|
|
|
this.on("input",function(msg) {
|
2014-09-08 22:10:06 +02:00
|
|
|
if (msg.qos) {
|
|
|
|
msg.qos = parseInt(msg.qos);
|
|
|
|
if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) {
|
|
|
|
msg.qos = null;
|
2014-08-22 13:42:07 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-09-08 22:10:06 +02:00
|
|
|
msg.qos = Number(node.qos || msg.qos || 0);
|
|
|
|
msg.retain = node.retain || msg.retain || false;
|
|
|
|
msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false;
|
|
|
|
if (node.topic) {
|
|
|
|
msg.topic = node.topic;
|
|
|
|
}
|
2015-04-14 19:39:42 +02:00
|
|
|
if ( msg.hasOwnProperty("payload")) {
|
|
|
|
if (msg.hasOwnProperty("topic") && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist
|
2015-06-03 08:54:43 +02:00
|
|
|
this.brokerConn.publish(msg); // send the message
|
2015-04-14 19:39:42 +02:00
|
|
|
}
|
2015-05-10 22:47:22 +02:00
|
|
|
else { node.warn(RED._("mqtt.errors.invalid-topic")); }
|
2014-09-08 22:10:06 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2015-06-03 08:54:43 +02:00
|
|
|
if (this.brokerConn.connected) {
|
2016-09-19 10:36:38 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2015-04-27 21:08:00 +02:00
|
|
|
}
|
2015-11-17 23:19:56 +01:00
|
|
|
node.brokerConn.register(node);
|
2015-12-23 00:25:29 +01:00
|
|
|
this.on('close', function(done) {
|
|
|
|
node.brokerConn.deregister(node,done);
|
2015-11-17 23:19:56 +01:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
} else {
|
2015-05-10 22:47:22 +02:00
|
|
|
this.error(RED._("mqtt.errors.missing-config"));
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("mqtt out",MQTTOutNode);
|
2015-06-03 08:54:43 +02:00
|
|
|
};
|