forked from SVendittelli/MMM-fitbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
57 lines (50 loc) · 1.53 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
/* Magic Mirror
* Node Helper: MMM-fitbit
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var PythonShell = require('python-shell');
module.exports = NodeHelper.create({
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'SET CREDS') {
console.log('Set credential request recieved.');
console.log(payload);
this.setCreds(payload.client_id,payload.client_secret);
};
if (notification === 'GET DATA') {
console.log('Initial run request recieved.');
this.getData();
};
},
setCreds: function (id,secret) {
var options = {
mode: 'json',
scriptPath: 'modules/MMM-fitbit/python',
args: [id, secret]
}
PythonShell.run('iniHandler.py', options, function (err, results) {
if (err) throw err;
// results is an array consisting of messages collected during execution
console.log('results: %j', results);
});
},
getData: function () {
const self = this;
const fileName = 'getData.py';
console.log('Running ' + fileName);
const fitbitPyShell = new PythonShell(fileName, {mode: 'json', scriptPath: 'modules/MMM-fitbit/python'});
fitbitPyShell.on('message', function (message) {
if (message['type'] == 'data') {
self.sendSocketNotification('DATA', message);
}
});
fitbitPyShell.end(function (err) {
if (err) throw err;
self.sendSocketNotification('UPDATE', 'Finished getting data');
console.log('Finished getting data');
});
},
});