-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
33 lines (28 loc) · 959 Bytes
/
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
var mqtt = require("mqtt");
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function() {
this.config = false;
},
launch: function (config) {
this.config = config;
console.log(this.name + ": Starting node helper, connecting to MQTT");
this.mqtt = mqtt.connect(this.config.mqttServer, {
username: this.config.mqttUser,
password: this.config.mqttPassword
});
},
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
console.log(this.name + ": Received config from frontend");
this.launch(payload);
} else if (notification === this.config.notificationType) {
if (this.config) {
console.log(this.name + ": Received configured notification with payload: ", payload);
this.mqtt.publish(this.config.mqttTopic, "" + payload);
} else {
console.error(this.name + ": Received configured payload before configuration!");
}
}
},
});