forked from prysme01/MMM-Jeedom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
70 lines (63 loc) · 1.65 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
var NodeHelper = require("node_helper");
const https = require('https');
const http = require('http');
module.exports = NodeHelper.create({
start: function () {
},
reload: function (refConfig) {
var self = this;
var i = 1;
var ids = "[";
for (var c in refConfig.sensors) {
var sensor = refConfig.sensors[c];
ids = ids + '"' + sensor.idx + '"';
if (i < refConfig.sensors.length) {
ids = ids + ',';
}
i++;
}
var postData = '{"jsonrpc": "2.0", "id": "1000", "method": "cmd::execCmd", "params": {"apikey": "';
postData = postData + refConfig.jeedomAPIKey + '", "id": ' + ids + ']}}';
//modif AGP
console.log(postData);
var options = {
hostname: refConfig.jeedomURL,
port: refConfig.jeedomPORT,
path: refConfig.jeedomAPIPath,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
console.log(options)
var protocol = refConfig.jeedomHTTPS ? https : http;
var req = protocol.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (chunk) => {
try {
self.sendSocketNotification("RELOAD_DONE", JSON.parse(chunk));
}
catch (e) {
console.log(`Jeedom parsing :`, e.message)
}
});
res.on('end', () => {
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
});
// write data to request body
req.write(postData);
req.end();
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'RELOAD') {
for (var c in payload.sensors) {
var sensor = payload.sensors[c];
}
this.reload(payload);
}
}
});