forked from ottopaulsen/MMM-MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
51 lines (44 loc) · 1.24 KB
/
node_helper.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
const mqttHelper = require("./mqtt_helper");
const NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
servers: [],
logging: false,
log: function (...args) {
if (this.logging) {
console.log(args);
}
},
start: function () {
console.log(this.name + ": Starting node helper");
this.loaded = false;
},
startTimeout: null,
socketNotificationReceived: function (notification, payload) {
const messageCallback = (key, topic, value) => {
this.log(
`Received message from ${key}: topic: ${topic}, message: ${value}`
);
this.sendSocketNotification("MQTT_PAYLOAD", {
serverKey: key,
topic: topic,
value: value,
time: Date.now()
});
};
if (notification === "MQTT_CONFIG") {
this.servers = mqttHelper.addServers(
this.servers,
payload.mqttServers,
this.name
);
this.logging = payload.logging;
// Start clients
// Allow 2 seconds for multiple instances to configure servers
clearTimeout(this.startTimeout);
this.startTimeout = setTimeout(() => {
mqttHelper.startClients(this.servers, messageCallback, this.name);
}, 2000);
this.loaded = true;
}
}
});