2 * Copyright 2013,2014 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var connectionPool = require("./lib/mqttConnectionPool");
21 function MQTTBrokerNode(n) {
22 RED.nodes.createNode(this,n);
23 this.broker = n.broker;
25 this.clientid = n.clientid;
26 if (this.credentials) {
27 this.username = this.credentials.user;
28 this.password = this.credentials.password;
31 RED.nodes.registerType("mqtt-broker",MQTTBrokerNode,{
34 password: {type: "password"}
38 function MQTTInNode(n) {
39 RED.nodes.createNode(this,n);
41 this.broker = n.broker;
42 this.brokerConfig = RED.nodes.getNode(this.broker);
43 if (this.brokerConfig) {
44 this.status({fill:"red",shape:"ring",text:"disconnected"});
45 this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
47 this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) {
48 var msg = {topic:topic,payload:payload,qos:qos,retain:retain};
49 if ((node.brokerConfig.broker == "localhost")||(node.brokerConfig.broker == "127.0.0.1")) {
54 this.client.on("connectionlost",function() {
55 node.status({fill:"red",shape:"ring",text:"disconnected"});
57 this.client.on("connect",function() {
58 node.status({fill:"green",shape:"dot",text:"connected"});
60 this.client.connect();
62 this.error("missing broker configuration");
64 this.on('close', function() {
66 this.client.disconnect();
70 RED.nodes.registerType("mqtt in",MQTTInNode);
72 function MQTTOutNode(n) {
73 RED.nodes.createNode(this,n);
75 this.qos = n.qos || null;
76 this.retain = n.retain;
77 this.broker = n.broker;
78 this.brokerConfig = RED.nodes.getNode(this.broker);
80 if (this.brokerConfig) {
81 this.status({fill:"red",shape:"ring",text:"disconnected"},true);
82 this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
84 this.on("input",function(msg) {
86 msg.qos = parseInt(msg.qos);
87 if ((msg.qos !== 0) && (msg.qos !== 1) && (msg.qos !== 2)) {
91 msg.qos = Number(node.qos || msg.qos || 0);
92 msg.retain = node.retain || msg.retain || false;
93 msg.retain = ((msg.retain === true) || (msg.retain === "true")) || false;
95 msg.topic = node.topic;
97 if ((msg.hasOwnProperty("topic")) && (typeof msg.topic === "string") && (msg.topic !== "")) { // topic must exist
98 this.client.publish(msg); // send the message
100 else { node.warn("Invalid topic specified"); }
102 this.client.on("connectionlost",function() {
103 node.status({fill:"red",shape:"ring",text:"disconnected"});
105 this.client.on("connect",function() {
106 node.status({fill:"green",shape:"dot",text:"connected"});
108 this.client.connect();
110 this.error("missing broker configuration");
112 this.on('close', function() {
114 this.client.disconnect();
118 RED.nodes.registerType("mqtt out",MQTTOutNode);