forked from SVendittelli/MMM-fitbit
-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
82 lines (69 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Magic Mirror
* Node Helper: MMM-Fitbit2
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
const PythonShell = require("python-shell");
module.exports = NodeHelper.create({
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === "GET DATA") {
console.log("MMM-Fitbit2: " + payload.trigger + " request to get data received");
this.getData(payload.config);
}
},
getData: function (config) {
const self = this;
let fileName;
if (config.test) {
fileName = "test_data.py";
} else {
fileName = "get_data.py";
}
if (config.debug) {
console.log("MMM-Fitbit2: Data to receive: " + JSON.stringify(config));
}
console.log("MMM-Fitbit2: START " + fileName);
var pyArgs = []
if (config.debug) {
pyArgs.push("--debug")
}
if (config.test) {
pyArgs.push("--test")
}
pyArgs.push(config.credentials.clientId)
pyArgs.push(config.credentials.clientSecret)
pyArgs.push("--resources")
pyArgs = pyArgs.concat(config.resources)
if (config.debug) {
console.log("MMM-Fitbit2: " + JSON.stringify(pyArgs))
}
const fitbitPyShell = new PythonShell(
fileName, {
mode: "json",
scriptPath: "modules/MMM-Fitbit2/python",
pythonPath: "python3",
pythonOptions: ["-u"], // get print results in real-time
args: pyArgs
}
);
fitbitPyShell.on("message", function (message) {
if (config.debug) {
console.log("MMM-Fitbit2: Message received: " + JSON.stringify(message))
}
if (message.type == "data") {
message.clientId = config.credentials.clientId
self.sendSocketNotification("API_DATA_RECEIVED", message);
}
});
fitbitPyShell.end(function (err) {
if (err) {
throw err;
}
self.sendSocketNotification("UPDATE_VIEW", "Finished getting data from Fitbit API");
console.log("MMM-Fitbit2: END " + fileName);
});
},
});