-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
128 lines (116 loc) · 4.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
// ====================================================
// 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");
module.exports = NodeHelper.create({
consolePrefix: "[MMM-Live-Stream-TV]:: ",
start: function() {
const {exec} = require('child_process');
// Spin up HLS proxy server to get around CORS/Origin, Referer HTTP request headers
exec('./modules/MMM-Live-Stream-TV/node_modules/@warren-bank/hls-proxy/hls-proxy/bin/hlsd.js --port "8181"', (error, stdout, stderr) => {
if (error) {
console.error(`node subprocess error.message: ${error.message}`);
console.error(`node subprocess error: ${error}`);
return;
}
if (stderr) {
console.error(`node subprocess stderr: ${stderr}`);
return;
}
console.log(`node subprocess stdout: ${stdout}`);
})
console.log(this.consolePrefix + "Starting node_helper for module [" + this.name + "]");
this.initialized = false;
// setting an autoplaying boolean so the first channel is left alone on the first run through
this.autoplaying = false;
// setting a blank interval for later
this.interval = null;
// setting the current channel
this.currentSlideshowChannel = null;
},
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;
console.log(this.config.sensors)
var sensor = this.config.sensors.find(x => x.name === message.sensor.name);
if(sensor){
value = message.sensor.data;
console.log(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);
},
changeChannel: function() {
const self = this;
if (!self.autoplaying) {
//this allows the first run-through to leave the channel alone so it doesn't skip from the start channel immedietely
self.autoplaying = true;
} else {
// if its reached the last channel set it back to 0 and update DOM
if (this.currentSlideshowChannel >= 9) {
self.currentSlideshowChannel = 0;
console.log(this.currentSlideshowChannel);
this.sendSocketNotification("slideshow", this.currentSlideshowChannel);
}
//otherwise change the channel up one
else {
this.currentSlideshowChannel++;
this.sendSocketNotification("slideshow", this.currentSlideshowChannel);
}
}
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === "CONFIG") {
this.config = payload;
this.currentSlideshowChannel = this.config.currentChannel;
}
else if (notification === "INITIALIZE" && this.config !== null){
this.python_start();
self.sendSocketNotification("status", {action: "status", name: "initialized"});
this.initialized = true;
}
else if (notification === "SLIDESHOW" && this.config !== null) {
console.log("*************************")
// console.log(this.currentChannel);
this.interval = setInterval((self.changeChannel).bind(this), this.config.slideshowInterval);
}
else if (notification === "STATIC" && this.config !== null) {
self.sendSocketNotification("static", this.currentChannel);
}
}
});