-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
63 lines (57 loc) · 1.92 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
var request = require("request");
var jp = require("jsonpath");
var NodeHelper = require("node_helper");
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node helper: " + this.name);
},
socketNotificationReceived: function (notification, payload) {
var self = this;
console.log("Notification: " + notification + " Payload: " + payload);
if (notification === "MMM_JSON_GET_REQUEST") {
request(payload.config.url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var jsonData = JSON.parse(body);
var responseObject = {
test: "test"
};
if (
payload.config.values == undefined ||
payload.config.values.length == 0
) {
// Values are not defined fetch first properties
var firstObject = jsonData;
if (Array.isArray(jsonData)) {
firstObject = jsonData[0];
}
responseObject = Object.keys(firstObject).map((prop) => {
return {
title: prop,
value: firstObject[prop]
};
});
} else {
// Values are defined, get what the user wants
responseObject = payload.config.values.map((val) => {
return {
...val,
value:
val.numberDevisor != undefined
? (
jp.query(jsonData, val.query)[0] / val.numberDevisor
).toFixed(3)
: jp.query(jsonData, val.query)[0]
};
});
}
self.sendSocketNotification("MMM_JSON_GET_RESPONSE", responseObject);
} else {
self.sendSocketNotification("MMM_JSON_GET_RESPONSE", {
error: true
});
console.error(error);
}
});
}
}
});