forked from JosefJantzen/MMM-AlexaControl
/
node_helper.js
262 lines (231 loc) · 9.39 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Magic Mirror
* Node Helper: MMM-AlexaControl
*
* By JoChef2
* MIT Licensed.
*/
var NodeHelper = require("node_helper");
var FauxMo = require('node-fauxmo');
var pm2 = require('pm2');
const exec = require("child_process").exec;
module.exports = NodeHelper.create({
start: function () {
console.log('MMM-AlexaControl helper, started...');
this.config = null;
this.setDevicesCounter = 0; // Counter for only one node_helper start look line 225
},
setDevices: function(){
_this = this;
cD = this.customDevices(this.config.devices);
nD = this.notificationDevices(cD, this.config.notifications);
pD = this.pageDevices(nD);
mD = this.menuDevices(pD);
//console.log(mD);
fauxMoPages = new FauxMo(mD); // creates fauxmo devices
},
customDevices : function(customD){ // creates your custom devices from config
_this = this
for(i = 0; i < Object.keys(customD.devices).length; i++){
if(customD.devices[i].port === undefined){
customD.devices[i].port = _this.config.startPort + 30 + i;
}
customD.devices[i].handler = new Function('action', customD.devices[i].handler)
}
return customD;
},
notificationDevices: function(notificationD, notifications){ // creates your notification devices
_this = this
counter = 0 + Object.keys(notificationD.devices).length
_this.n = []
for(i = 0; i < Object.keys(notifications).length; i++){
device = {}
device.name = notifications[i].name
if(notifications[i].port === undefined){
device.port = _this.config.startPort + 50 + i
}else{
device.port = notifications[i].port
}
if(notifications[i].OnOff){
_this.n[i] = notifications[i].notification
device.handler = new Function('action', 'if(action === 1){_this.sendSocketNotification("CUSTOM",' + JSON.stringify(_this.n[i][0]) + ')}else{_this.sendSocketNotification("CUSTOM",' + JSON.stringify(_this.n[i][1]) + ')}')
}else{
_this.n[i] = notifications[i].notification
device.handler = new Function('action', '_this.sendSocketNotification("CUSTOM",' + JSON.stringify(_this.n[i]) + ')')
}
notificationD.devices[i + counter] = device
}
return notificationD;
},
pageDevices: function(pageD){ // creates your page devices
_this = this;
counter = 0 + Object.keys(pageD.devices).length
this.pPort = _this.config.startPort
if(_this.config.pages > 0){
for(i = 0; i < _this.config.pages; i++){
device = {}
device.name = _this.translations["page"] + (i + 1)
device.port = this.pPort - 100
device.handler = new Function('action', `_this.sendSocketNotification("PAGE_CHANGED", ` + i +`)`)
pageD.devices[i + counter] = device
this.pPort++
}
}
return pageD
},
menuDevices: function(menuD){ // create your devices to control the Mirror and Pi
_this = this;
var opts = { timeout: 8000 };
counter = 0 + Object.keys(menuD.devices).length
if(this.config.refresh){
device = {}
device.name = _this.translations["refresh"]
device.port = _this.config.startPort
device.handler = function(action) {_this.sendSocketNotification("ACTION", "refresh")}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
if(this.config.restart){ // only with PM2
device = {}
device.name = _this.translations["restart"]
device.port = _this.config.startPort
device.handler = function(action) {
pm2.connect((err) => {
if (err) {
console.error(err);
}
console.log("Restarting PM2 process: " + _this.config.pm2ProcessName);
pm2.restart(_this.config.pm2ProcessName, function(err, apps) {
pm2.disconnect();
if (err) { console.log(err); }
});
});
}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
if(this.config.stop){ // only with PM2
device = {}
device.name = _this.translations["stop"]
device.port = _this.config.startPort
device.handler = function(action) {
pm2.connect((err) => {
if (err) {
console.error(err);
}
console.log("Stopping PM2 process: " + _this.config.pm2ProcessName);
pm2.stop(_this.config.pm2ProcessName, function(err, apps) {
pm2.disconnect();
if (err) { console.log(err); }
});
});
}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
if(this.config.reboot){ //reboot the pi
device = {}
device.name = _this.translations["reboot"]
device.port = _this.config.startPort
device.handler = function(action) {
exec("sudo shutdown -r now", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
if(this.config.shutdown){ // shutdwon the pi
device = {}
device.name = _this.translations["shutdown"]
device.port = _this.config.startPort
device.handler = function(action) {
exec("sudo shutdown -h now", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
/**
* for me worked only vcgencmd display_power 0 and vcgencmd display_power 1
* probably for you work tvservice --off and tvservice --preferred
* test it in terminal if you aren't sure
*/
if(this.config.monitorToggle){
device = {}
device.name = _this.translations["monitor"]
device.port = _this.config.startPort
if(this.config.vcgencmd){
device.handler = function(action) {
if(action === 1){
exec("vcgencmd display_power 1", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}if(action === 0){
exec("vcgencmd display_power 0", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}
}
}else{
device.handler = function(action) {
if(action === 1){
exec("tvservice --preferred", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}if(action === 0){
exec("tvservice --off", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}
}
}
menuD.devices[counter] = device;
counter++;
}
_this.config.startPort++
return menuD;
},
monitorOff: function(){
var opts = { timeout: 8000 };
if(this.config.vcgencmd){
exec("vcgencmd display_power 0", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}else{
exec("tvservice --off", opts, (error, stdout, stderr) => {
_this.checkForExecError(error, stdout, stderr);
});
}
},
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;
},
socketNotificationReceived: function(notification, payload) {
if(notification === "SET_DEVICE"){ // set all your devices
this.config = payload;
if(this.setDevicesCounter === 0){ // set your devices only the first time a
this.setDevices(); // notification is received
this.setDevicesCounter++;
}
}
if(notification === "TRANSLATIONS"){ // get translations and put them in an array
this.translations = [];
this.translations = payload;
}
}
});