-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnode_helper.js
79 lines (66 loc) · 2.8 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
// ====================================================
// MMM-ArduPort Copyright(C) 2019 Furkan Türkal
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
// and you are welcome to redistribute it under certain conditions; See
// file LICENSE, which is part of this source code package, for details.
// ====================================================
'use strict';
const NodeHelper = require('node_helper');
const {PythonShell} = require('python-shell');
var pythonStarted = false;
module.exports = NodeHelper.create({
consolePrefix: '[MMM-ArduPort]:: ',
start: function() {
console.log(this.consolePrefix + "Starting node_helper for module [" + this.name + "]");
this.initialized = false;
},
python_start: function () {
const self = this;
const pyshell = new PythonShell('modules/' + this.name + '/arduport/arduport.py', { mode: 'json', args: [JSON.stringify(this.config)]});
pyshell.on('message', function (message) {
console.log(message);
if (message.hasOwnProperty('debug')){
console.log(this.consolePrefix + "[" + self.name + "] " + message.debug);
}
if (message.hasOwnProperty('status')){
console.log(message.status);
self.sendSocketNotification('status', {action: "status", name: message.status.name, data: message.status.data});
}
if (message.hasOwnProperty('sensor')){
if(self.initialized){
self.sendData(message);
}
}
});
pyshell.end(function (err) {
if (err) throw err;
console.log("[" + self.name + "] " + 'finished running...');
self.sendSocketNotification('error', 'pyshell-throw');
});
},
sendData: function(message){
const self = this;
var value;
for(var i in this.config.sensors){
value = null;
var sensor = this.config.sensors.find(x => x.name === message.sensor.name);
if(sensor){
value = message.sensor.data;
} else {
console.error(self.consolePrefix + 'Error: Incoming Sensor ' + this.config.sensors[i].name + ' not configured in config.js!');
}
sensor.value = value;
}
self.sendSocketNotification('sensor', this.config.sensors);
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'CONFIG') {
this.config = payload;
} else if (notification === "INITIALIZE" && this.config !== null){
this.python_start();
self.sendSocketNotification('status', {action: "status", name: "initialized"});
this.initialized = true;
}
}
});