-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode_helper.js
74 lines (63 loc) · 2.67 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
/* Magic Mirror
* Module: MMM-MQTT
*
* By jupadin
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Log = require('../../js/logger.js');
const Subscriber = require('./subscriber.js');
module.exports = NodeHelper.create({
// Override start method.
start: function() {
// Log.log("Starting node helper for: " + this.name);
this.subscribers = {};
},
// Override socketNotificationReveived method.
socketNotificationReceived: function(notification, payload) {
if (notification == "ADD_BROKER") {
this.createSubscriber(payload.url, payload.port, payload.auth, payload.topics, payload.id);
}
},
/**
* Creates a subscriber for a new url, it it does not exist yet.
* Otherwise it reuses the existing one.
*
* @param {string} url The url of the broker
* @param {number} port The port of the broker
* @param {object} auth The object containing options for authentication against the broker
* @param {array} topics An array of topics to which the subscriber should subscribe to
* @param {string} identifier ID of the module
*/
createSubscriber: function(url, port, auth, topics, identifier) {
try {
new URL(url);
} catch (error) {
Log.error(this.name + ": MQTT Error. Malformed broker url: ", url, error);
this.sendSocketNotification("MQTT_ERROR", { error_type: "MODULE_ERROR_MALFORMED_URL" });
return;
}
let subscriber;
if (typeof this.subscribers[identifier + url] === "undefined") {
Log.log(this.name + ": Create new subscriber for url: " + url + " and topic(s): " + topics);
subscriber = new Subscriber(url, port, auth, topics);
// Set callback functions
subscriber.onConnect((packet) => {
Log.debug(this.name + ": Connected! (" + url + ", " + topics + ").");
});
subscriber.onMessage((topic, payload, packet) => {
Log.log(this.name + ": Receivied message. Topic: \"" + topic + "\", with payload: \"" + payload.toString() + "\"");
this.sendSocketNotification("DATA", {topic: topic, payload: payload.toString()});
});
subscriber.onError((error) => {
Log.debug(this.name + ": Error: " + error);
});
// Connect to server and subscribe to topics.
subscriber.connect();
this.subscribers[identifier + url] = subscriber;
} else {
Log.log(this.name + ": Use existing subscriber for url: " + url);
subscriber = this.subscribers[identifier + url];
}
},
});