forked from JonathanTreffler/Home-Assistant-remote-cec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (50 loc) · 1.54 KB
/
app.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
const http = require('http');
const url = require('url');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const hostname = '0.0.0.0';
const port = 8080;
const cecDeviceId = 0;
const server = http.createServer((req, res) => {
let urlParts = url.parse(req.url);
let route = urlParts.pathname;
if(route == "/shutdown") {
console.log("command recieved: shutdown");
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
exec("echo 'standby " + cecDeviceId + "' | cec-client -s -d 1")
.then(function() {
console.log("command success: shutdown");
res.write("success");
res.end('');
})
.catch(function() {
console.log("command failed: shutdown");
});
} else if(route == "/poweron") {
console.log("command recieved: poweron");
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
exec("echo 'on " + cecDeviceId + "' | cec-client -s -d 1")
.then(function() {
console.log("command success: poweron");
res.write("success");
res.end('');
})
.catch(function() {
console.log("command failed: poweron");
});
} else if(route == "/commands") {
exec("echo h | cec-client -s -d 1", (error, stdout, stderr) => {
res.write(stdout? stdout : stderr);
res.end('');
});
} else {
console.log("recieved unknown command");
res.statusCode = 400;
res.end('');
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});