forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqlight.js
139 lines (126 loc) · 4.87 KB
/
mqlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module.exports = function(RED) {
"use strict";
var mqlight = require('mqlight');
var util = require("util");
function MQLightServiceNode(n) {
RED.nodes.createNode(this,n);
var id = "mqlight_" + (n.clientid ? n.clientid : (1+Math.random()*4294967295).toString(16));
var opts = {
service: n.service,
id: id
};
if (this.credentials) {
opts.user = this.credentials.user;
opts.password = this.credentials.password;
}
this.client = mqlight.createClient(opts, function(err) {
if (err) {
util.log('[mqlight] ['+id+'] not connected to service '+n.service);
}
else {
util.log('[mqlight] ['+id+'] connected to service '+n.service);
}
});
this.client.on("error", function(err) {
if (err) { util.log('[mqlight] ['+id+'] '+ err.toString()); }
});
}
RED.nodes.registerType("mqlight service",MQLightServiceNode,{
credentials: {
user: {type:"text"},
password: {type: "password"}
}
});
function MQLightIn(n) {
RED.nodes.createNode(this, n);
this.topic = n.topic || "";
this.share = n.share || null;
this.service = n.service;
this.serviceConfig = RED.nodes.getNode(this.service);
var node = this;
if (node.serviceConfig) {
if (node.serviceConfig.client) {
var recvClient = node.serviceConfig.client;
recvClient.on("error", function(err) {
if (err) { node.error(err.toString()); }
});
recvClient.on("started", function() {
recvClient.on("message", function(data, delivery) {
if (node.topic === delivery.destination.topicPattern) {
var msg = {
topic: delivery.message.topic,
payload: data,
_session: {
type: "mqlight",
id: recvClient.id
}
};
if (delivery.destination.share) {
msg.share = delivery.destination.share;
}
node.send(msg);
}
});
var subscribeCallback = function(err) {
if (err) {
node.error("Failed to subscribe: " + err);
}
else {
node.log("Subscribed to "+node.topic+(node.share?" ["+node.share+"]":""));
}
};
if (node.share) {
recvClient.subscribe(node.topic, node.share, subscribeCallback);
}
else {
recvClient.subscribe(node.topic, subscribeCallback);
}
});
recvClient.start();
node.on("close", function (done) {
recvClient.stop(done);
});
}
}
}
RED.nodes.registerType("mqlight in", MQLightIn);
function MQLightOut(n) {
RED.nodes.createNode(this, n);
this.topic = n.topic || "";
this.service = n.service;
this.serviceConfig = RED.nodes.getNode(this.service);
var node = this;
if (node.serviceConfig) {
if (node.serviceConfig.client) {
var sendClient = node.serviceConfig.client;
sendClient.on("error", function(err) {
if (err) { node.error(err.toString()); }
});
sendClient.on("started", function () {
node.on("input", function(msg) {
var topic = node.topic;
if (topic === "") {
if (msg.topic) {
topic = msg.topic;
}
else {
node.warn("No topic set in MQ Light out node");
return;
}
}
sendClient.send(topic, msg.payload, function(err) {
if (err) {
node.error(err,msg);
}
});
});
});
sendClient.start();
node.on("close", function (done) {
sendClient.stop(done);
});
}
}
}
RED.nodes.registerType("mqlight out", MQLightOut);
};