-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio.js
106 lines (87 loc) · 2.71 KB
/
radio.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
const PORT = 1111;
const util = require('util');
const http = require('http');
const fileSystem = require('fs');
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const stations = [{"path":"/rcan","caption":"Radio-Canaada Premiere","url":"http://2qmtl0.akacast.akamaistream.net/7/953/177387/v1/rc.akacast.akamaistream.net/2QMTL0"},
{"path":"/npr", "caption":"Vermont Public Radio Classic","url":"http://vprclassical.streamguys.net/vprclassical64.aac"}];
var proc = null;
var omxplayer_output = null;
var volume = 100;
function handleRequest(request, response) {
var station = null;
var arrayLength = stations.length;
for (var i = 0; i < arrayLength; i++) {
if (request.url == stations[i].path) {
console.log(stations[i].path);
station = stations[i];
break;
}
}
if (request.url == "/stop" || station != null) {
exec("killall omxplayer.bin");
}
if (request.url == "/stop") {
returnJson('{}', response);
return;
}
if (request.url == "/up") {
if (volume < 100) {
volume += 10;
if (proc != null)
proc.stdin.write("+");
}
var toReturn = {'volume': volume};
returnJson(toReturn, response);
return;
}
if (request.url == "/down") {
if (volume > 0) {
volume -= 10;
if (proc != null)
proc.stdin.write("-");
}
var toReturn = {'volume': volume};
returnJson(toReturn, response);
return;
}
if (station != null) {
console.log(station.url);
proc = spawn('omxplayer', [station.url]);
proc.stdout.on('stdout', function (data) {
omxplayer_output = data;
console.log('stdout: '+data);
});
proc.stderr.on('data', function(data) {
console.log('stderr: '+data);
});
proc.on('close', function(code) {
console.log('child process exited');
});
// set initial volume
var volumeNotches = (100 - volume) / 10;
for (var i = 0 ; i < volumeNotches ; i++)
proc.stdin.write("-");
var toReturn = {'volume': volume};
returnJson(toReturn, response);
return;
}
if (request.url == "/radio-ui.js") {
response.end(fileSystem.readFileSync('radio-ui.js'));
return;
}
response.end(fileSystem.readFileSync('index.html'));
}
function returnJson (json, response) {
if (!(typeof json === "string" || json instanceof String))
json = JSON.stringify(json);
console.log (json); // todo logging
response.writeHead (200, {'Content-Type':'application/json', 'Content-Lenght':json.length});
response.end (json);
}
//Create a server
var server = http.createServer(handleRequest);
//Lets start our server
server.listen(PORT);
console.log('server is listening on '+PORT);