-
Notifications
You must be signed in to change notification settings - Fork 3
/
node_helper.js
106 lines (84 loc) · 3.68 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
const mqtt = require('mqtt');
const NodeHelper = require("node_helper");
const dateFormat = require('dateformat');
var node_helper_config;
module.exports = NodeHelper.create({
start: function() {
console.log(this.name + ': Starting node helper');
this.loaded = false;
},
makeServerKey: function(address, port, user){
return '' + address + ':' + (port | '1883' + user);
},
log: function(...args) {
if (node_helper_config.logging) {
console.log(args);
}
},
formatDateTime: function(secs) {
this.log("Secs", secs);
var epoch = new Date(0);
epoch.setSeconds(parseInt(secs));
this.log("Epoch", epoch);
var date = epoch.toISOString();
this.log("date", date);
this.log("toLocaleTime",epoch.toLocaleTimeString(node_helper_config.localeStr));
var dateStr = date.split('.')[0].split('T')[0]
return dateStr.split('-')[2]+'.'+dateStr.split('-')[1] + ' ' + epoch.toLocaleTimeString(node_helper_config.localeStr).split(':')[0]+":"+epoch.toLocaleTimeString(node_helper_config.localeStr).split(':')[1];
},
startClient: function(config) {
node_helper_config = config;
console.log(this.name + ': Starting client for: ', config.mqttServerAddress);
var self = this;
var serverKey = this.makeServerKey(config.mqttServerAddress, config.mqttServerPort, config.mqttServerUser);
var server = {}
server.serverKey = serverKey;
server.address = config.mqttServerAddress;
server.port = config.mqttServerPort;
server.options = {};
if(config.mqttServerUser) server.options.username = config.mqttServerUser;
if(config.mqttServerPassword) server.options.password = config.mqttServerPassword;
server.topics = config.mqttTopics;
var mqttServer = (server.address.match(/^mqtts?:\/\//) ? '' : 'mqtt://') + server.address;
if (server.port) {
mqttServer = mqttServer + ':' + server.port
}
console.log(self.name + ': Connecting to ' + mqttServer);
server.client = mqtt.connect(mqttServer, server.options);
server.client.on('error', function (err) {
console.log(self.name + ' ' + server.serverKey + ': Error: ' + err);
});
server.client.on('reconnect', function (err) {
server.value = 'reconnecting'; // Hmmm...
console.log(self.name + ': ' + server.serverKey + ' reconnecting');
});
server.client.on('connect', function (connack) {
console.log(self.name + ' connected to ' + mqttServer);
console.log(self.name + ': subscribing to ' + server.topics);
server.client.subscribe(server.topics);
});
server.client.on('message', function (topic, payload) {
self.log(self.name + ' ' + topic + payload.toString() );
var now = Date.now();
var nowStr = self.formatDateTime(now/1000);
self.log(self.name + ' ' + topic + now + ' ' + nowStr);
self.sendSocketNotification('MQTT_PAYLOAD', {
serverKey: server.serverKey,
topic: topic,
value: payload.toString(),
time: now,
timeStr: nowStr
});
});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
console.log(self.name + ' Notification received: ' + notification + ' ' + payload);
if (notification === 'MQTT_CONFIG') {
var config = payload;
console.log(self.name + ' MQTT_CONFIG: ' + config);
self.startClient(config);
self.loaded = true;
}
},
});