-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
151 lines (126 loc) · 5.44 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
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
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
// Magicmirror Modul - MMM-MQTTbind / node_helper.js
// by Jan Mittelstaedter
//
// MIT Licensed
//
// Please see: https://github.com/janm84/MMM-MQTTbind
//
// Inspired from MMM-MQTTbridge (MIT-License) - https://github.com/sergge1/MMM-MQTTbridge
const NodeHelper = require('node_helper');
const mqtt = require('mqtt');
module.exports = NodeHelper.create({
// Setting up some internal variables.
start: function () {
this.logMsg('module startet');
this.clients = [];
this.config = {};
this.initialstartup = 0;
},
// Unsubscribe and Clean up when MM gets stopped
stop: function() {
const self = this;
self.logMsg("INFO: Cleaning up...");
self.unsubscribe();
self.clients[self.config.server].end();
},
//Subscribe to the configured topics (options from mqtt.js)
subscribe: function() {
const self = this;
for (var i = 0; i < self.config.bindings.subscriptions.length; i++) {
if (!self.clients[self.config.server].connected) {
self.clients[self.config.server].subscribe(self.config.bindings.subscriptions[i].topic, self.config.bindings.subscriptions[i].options);
self.logMsg("LOG", "Subscripted to: " + self.config.bindings.subscriptions[i].topic);
}
}
},
// Unsubsribe from configured topics.
unsubscribe: function() {
const self = this;
for (var i = 0; i < self.config.bindings.subscriptions.length; i++) {
if (!self.clients[self.config.server].connected) {
self.clients[self.config.server].unsubscribe(self.config.bindings.subscriptions[i].topic);
self.logMsg("LOG", "Unsubscripted from: " + self.config.bindings.subscriptions[i].topic);
}
}
},
// connect the client to mqtt server
connect: function (config) {
var self = this;
var client;
self.config = config;
// checking if already connected or the client var is not initialized, setting up a new connection if not
if (typeof self.clients[self.config.server] === "undefined" || self.clients[self.config.server].connected == false) {
client = mqtt.connect(self.config.server, config.options);
self.clients[self.config.server] = client;
// Gets triggered on Error
self.clients[self.config.server].on('error', function (error) {
self.logMsg("ERROR", "(MQTT server: " + error);
self.sendSocketNotification("ERROR", { type: 'notification', title: '[MMM-MQTTbind]', message: 'Server: ' + error });
});
// Gets triggered when the connection could not get established.
self.clients[self.config.server].on('offline', function () {
self.logMsg("INFO", "Could not establish connection to MQTT Server");
self.sendSocketNotification("ERROR", { type: 'notification', title: '[MMM-MQTTbind]', message: "Could not establish connection to MQTT Server" });
client.end();
});
// Gets trigered on disconnect
self.clients[self.config.server].on('disconnect', function() {
self.logMsg("LOG", "Disconnected!");
});
// Gets triggered when the client is connected
self.clients[self.config.server].on('connect', function() {
self.logMsg("LOG", "Connected!");
self.sendSocketNotification('MQTT_BIND_CONNECTED', true); // Tell the main module that we are connected
});
// gets triggered on reconnect
self.clients[self.config.server].on('reconnect', function() {
self.logMsg("LOG", "Reconnect...");
});
// Gets triggered on every packet received by the client. Only for debbuging purposes
self.clients[self.config.server].on('packetreceive', function(packet) {
//self.logMsg("LOG", "PACKETRECEIVE - topic:" + packet.topic + " payload: " + packet.payload);
});
}
self.subscribe();
// Gets trggered when a MQTT is received. Forward the Message to the Mainmodule.
self.clients[self.config.server].on('message', function (topic, message, packet) {
self.logMsg("LOG", "MQTT message received - Topic: " + topic + ", Message: " + message.toString());
self.sendSocketNotification('MQTT_MESSAGE_RECEIVED', { 'topic': topic, 'message': message.toString() });
});
},
socketNotificationReceived: function (notification, payload) {
var self = this;
switch (notification) {
case 'MQTT_BIND_CONNECT':
this.connect(payload);
break;
case 'MQTT_MESSAGE_SEND':
if (typeof self.clients[self.config.server] !== "undefined") { // Triggered by Mainmodule if we should send a Msg to the MQTT server
self.clients[self.config.server].publish(payload.binding.topic, payload.message, payload.binding.options); // Options from mqtt.js
self.logMsg("LOG", "Published Topic: " + payload.binding.topic + " Value:" + payload.message);
};
break;
case 'LOG':
self.logMsg(payload.type, payload.message);
break;
}
},
// used for writing the logs from mainmodule and "this", if logging is enabled.
logMsg: function(type, message) {
var self = this;
switch(type) {
case 'INFO': // always gets logged
console.log("[MQTTbind]: " + message);
break;
case 'LOG': // only if enableLogging is true (used for debugging)
if(self.config.enableLogging == true) {
console.log("[MQTTbind]: " + message)
}
break;
case 'ERROR': // in case of an error
console.log("[MQTTbind] ERROR: " + message);
break;
}
}
});