-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
213 lines (198 loc) · 5.53 KB
/
server.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
const util = require("util");
var path = require("path");
var psTree = require("ps-tree");
const Gpio = require("onoff").Gpio;
const { exec } = require("child_process");
var fs = require("fs");
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const Parser = require("./parser.js");
const Buttons = [];
var State = {};
var BlockButton = false;
var StopMainFunction = false;
var PlayerTask = null;
var Volume = 500;
var killall = function (pid, signal, callback) {
signal = signal || "SIGKILL";
callback = callback || function () {};
var killTree = true;
if (killTree) {
psTree(pid, function (err, children) {
[pid]
.concat(
children.map(function (p) {
return p.PID;
})
)
.forEach(function (tpid) {
try {
process.kill(tpid, signal);
} catch (ex) {}
});
callback();
});
} else {
try {
process.kill(pid, signal);
} catch (ex) {}
callback();
}
};
function buttonBlock() {
BlockButton = true;
setTimeout(function () {
console.log("--------Button Released-----------");
BlockButton = false;
}, 500);
}
function OmxKill() {
return new Promise(function (resolve, reject) {
if (PlayerTask != null) {
buttonBlock();
console.log("Kill Player PID" + PlayerTask.pid);
//killed_uid = PlayerTask.pid;
//PlayerTask.stdin.write("q");
killall(PlayerTask.pid, "SIGKILL", function () {
resolve(true);
});
} else {
resolve(true);
}
});
}
async function OmxPlayFile(file, volume = Volume) {
return new Promise((resolve, reject) => {
if (!BlockButton) {
OmxKill().then((result) => {
console.log("Play Video: " + file);
State.file = file;
State.fileId = Parser.getIdByFile(file);
State.isPlaying = true;
PlayerTask = exec("omxplayer -o local --vol " + volume + " " + file);
PlayerTask.on("exit", (code) => {
console.log("child process exited with code " + code);
if (code == 0) {
State.isPlaying = false;
}
resolve(true);
//console.log(util.inspect(PlayerTask, { showHidden: false, depth: null }));
});
//return true;
});
} else {
console.log("Button still blocked");
}
});
}
async function OmxPlayFileLoop(file, volume = Volume) {
while (true) {
await OmxPlayFile(file, volume);
if (BlockButton) {
console.log("EXIT OMX LOOP");
break;
}
}
}
function StopMain() {
return new Promise((resolve, reject) => {
StopMainFunction = true;
resolve(true);
});
}
function StartMain() {
StopMainFunction = false;
MainFunction();
}
function MainFunction(mainFunction = Parser.getConfig().mainfunction) {
if (StopMainFunction || BlockButton) return;
console.log("-----START MAIN----");
//if (mainFunction != null) {
var customFunction = new AsyncFunction(mainFunction);
//customFunction = new AsyncFunction(customFunction);
var Config = new Parser.getConfig();
var getFileById = Parser.getFileById;
var getIdByFile = Parser.getIdByFile;
var RestartMain = MainFunction;
try {
customFunction.call({
OmxPlayFile,
OmxPlayFileLoop,
getFileById,
getIdByFile,
RestartMain,
StartMain,
StopMain,
OmxKill,
Config,
State,
});
//setTimeout(MainFunction(mainFunction), 5000);
} catch (e) {
console.log("Import Code Error ");
console.log(e);
}
/*} else {
console.log("No Main Function");
}*/
}
//ToDo: protect members (without status)
//Input: Trigger Object from Config Array
function attachButton(Trigger /*number, file, isrepeat = false, isdefault = false*/) {
Buttons[Trigger.gpio] = new Gpio(Trigger.gpio, "in", "falling", { debounceTimeout: 50 });
Buttons[Trigger.gpio].watch((err, value) => {
console.log("-----Button Trigger GPIO: " + Trigger.gpio + "------");
if (Trigger.customFunction != null) {
//StopMain();
var customFunction = new AsyncFunction(Trigger.customFunction);
var RestartMain = MainFunction;
var Config = new Parser.getConfig();
var getFileById = Parser.getFileById;
var getIdByFile = Parser.getIdByFile;
try {
customFunction.call({
OmxPlayFile,
OmxPlayFileLoop,
getFileById,
getIdByFile,
OmxKill,
RestartMain,
StartMain,
StopMain,
Config,
Trigger,
State,
});
} catch (e) {
console.log("Import Code Error");
console.log(e);
}
} else {
console.log("No Function for GPIO: " + Trigger.gpio);
}
});
}
Parser.init({ configpath: "./media/", configfile: "config_files.json" }).then(function () {
fs.watchFile(Parser.getConfigPath(), (curr, prev) => {
console.log("Restart from File Change: " + Parser.getConfigPath());
OmxKill().then(() => {
process.kill(process.pid, "SIGUSR2");
process.exit();
});
});
Volume = Parser.checkENV("VOLUME", 500);
Parser.parseConfig().then((Config) => {
//console.log("By ID XX " + Parser.getFileById(23));
MainFunction();
for (var i = 0; i < Config.trigger.length; i++) {
if (Config.trigger[i].gpio != undefined) {
console.log("----------NEW BUTTON GPIO: " + Config.trigger[i].gpio + "--------");
attachButton(Config.trigger[i]);
}
}
console.log("----------INIT DONE-----------------");
});
});
process.on("SIGINT", (_) => {
//led.unexport();
//button1.unexport();
});