1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Let MQTT input node receive binary packets

Try to auto select output type to be string or buffer to be backwards compatible
Fixes #435
This commit is contained in:
Dave C-J 2014-11-04 21:56:15 +00:00
parent 1153619a03
commit 273acc0ec4
2 changed files with 22 additions and 20 deletions

View File

@ -17,6 +17,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
var connectionPool = require("./lib/mqttConnectionPool"); var connectionPool = require("./lib/mqttConnectionPool");
var isUtf8 = require('is-utf8');
function MQTTBrokerNode(n) { function MQTTBrokerNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -45,8 +46,9 @@ module.exports = function(RED) {
this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
var node = this; var node = this;
this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) { this.client.subscribe(this.topic,2,function(topic,payload,qos,retain) {
if (isUtf8(payload)) { payload = payload.toString(); }
var msg = {topic:topic,payload:payload,qos:qos,retain:retain}; var msg = {topic:topic,payload:payload,qos:qos,retain:retain};
if ((node.brokerConfig.broker == "localhost")||(node.brokerConfig.broker == "127.0.0.1")) { if ((node.brokerConfig.broker === "localhost")||(node.brokerConfig.broker === "127.0.0.1")) {
msg._topic = topic; msg._topic = topic;
} }
node.send(msg); node.send(msg);
@ -78,7 +80,7 @@ module.exports = function(RED) {
this.brokerConfig = RED.nodes.getNode(this.broker); this.brokerConfig = RED.nodes.getNode(this.broker);
if (this.brokerConfig) { if (this.brokerConfig) {
this.status({fill:"red",shape:"ring",text:"disconnected"},true); this.status({fill:"red",shape:"ring",text:"disconnected"});
this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password); this.client = connectionPool.get(this.brokerConfig.broker,this.brokerConfig.port,this.brokerConfig.clientid,this.brokerConfig.username,this.brokerConfig.password);
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {

View File

@ -131,7 +131,7 @@ MQTTClient.prototype.connect = function(options) {
delete self.pendingSubscriptions[packet.messageId]; delete self.pendingSubscriptions[packet.messageId];
}); });
client.on('publish',function(packet) { client.on('publish',function(packet) {
self.lastInbound = (new Date()).getTime() self.lastInbound = (new Date()).getTime();
if (packet.qos < 2) { if (packet.qos < 2) {
var p = packet; var p = packet;
self.emit('message',p.topic,p.payload,p.qos,p.retain); self.emit('message',p.topic,p.payload,p.qos,p.retain);
@ -192,8 +192,9 @@ MQTTClient.prototype.subscribe = function(topic,qos) {
messageId: self._nextMessageId() messageId: self._nextMessageId()
}; };
this.pendingSubscriptions[options.messageId] = topic; this.pendingSubscriptions[options.messageId] = topic;
this.lastOutbound = (new Date()).getTime() this.lastOutbound = (new Date()).getTime();
self.client.subscribe(options); self.client.subscribe(options);
self.client.setPacketEncoding('binary');
} }
} }
MQTTClient.prototype.unsubscribe = function(topic) { MQTTClient.prototype.unsubscribe = function(topic) {
@ -251,4 +252,3 @@ module.exports.createClient = function(port,host) {
var mqtt_client = new MQTTClient(port,host); var mqtt_client = new MQTTClient(port,host);
return mqtt_client; return mqtt_client;
} }