-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
136 lines (115 loc) · 2.98 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Magic Mirror
* node_helper.js
*
* By RaspiManu & ViatorisBaculum
* https://github.com/RaspiManu/MMM-FrameLight
* MPL-2.0 licensed
*/
"use strict";
let NodeHelper = require("node_helper");
const fs = require("fs");
const fetch = require("node-fetch");
let { PythonShell } = require("python-shell");
const pythonScriptName = "/MMM-FrameLight_LED_Control.py";
const configFile = "/presets/color_presets.JSON";
const options = {
mode: "binary",
pythonOptions: ["-u"]
};
let pyshell;
module.exports = NodeHelper.create({
start: function () {
this.started = true;
},
stop: function () {
this.sendSocketNotification("shuttingDown", true);
this.pythonStop();
},
/**
* initialize python shell
*/
pythonStart: function () {
pyshell = new PythonShell(this.path + pythonScriptName, options);
},
/**
* stop old shell, initialize new one and send payload to script
* @param {object} payload
*/
pythonSend: function (payload) {
this.pythonStop();
pyshell = new PythonShell(this.path + pythonScriptName, options);
pyshell.send(JSON.stringify(payload)).end(function (err, code, signal) {
if (err) throw err;
});
},
/**
* stop python shell
*/
pythonStop: function () {
pyshell.kill("SIGTERM");
},
/**
* receives socket from main module
* @param {string} notification name of the socket
* @param {string} payload could contain object or presets
*/
socketNotificationReceived: function (notification, payload) {
if (notification === "CONFIG") {
// set config from payload
if (!this.started) {
this.config = payload;
}
this.pythonStart();
}
const presetPath = this.path + configFile;
if (notification === "saveJSON") {
// save json preset file
fs.writeFile(presetPath, payload, function (err) {
if (err) return console.log(err);
});
}
/**
* writes into json when file exists | catches "no file" error and creates new json
*/
function createFileIfMissing() {
fs.promises.writeFile(presetPath, "", { flag: "w" }).catch((err) => console.log(err));
}
if (notification === "openJSON") {
// open json
fs.promises
.readFile(presetPath, "utf8")
.then((ledPresets) => {
if (ledPresets === "") {
// if file is empty create new json with given presets anyway
createFileIfMissing();
}
this.sendSocketNotification("ledPresets", ledPresets);
})
.catch((err) => {
if (err.code === "ENOENT")
// if error "no file" create new json
createFileIfMissing();
else console.log(err);
});
}
if (notification === "createJSON") {
// if creating file is explicitly demanded -> creating new file
createFileIfMissing();
}
if (notification === "sendToPy") {
// sends payload(sent object) to py
this.pythonSend(payload);
}
},
checkForExecError: function (error, stdout, stderr) {
if (stderr) {
console.log('stderr: "' + stderr + '"');
return 1;
}
if (error !== null) {
console.log("exec error: " + error);
return 1;
}
return 0;
}
});