-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (77 loc) · 2.47 KB
/
index.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
var express = require('express');
var app = require('express')();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var fs = require('fs');
var fifo = process.env.EPHEMERAL + '/ctl';
server.listen(3000);
// Routing
app.use(express.static(process.env.PATIOBAR + '/views'));
function readCurrentSong() {
// var currentSong = fs.readFileSync(process.env.HOME + '/currentSong').toString()
var currentSong = fs.readFileSync(process.env.EPHEMERAL + '/currentSong').toString()
if (currentSong) {
var a = currentSong.split(',');
io.emit('start', { artist: a[0], title: a[1], album: a[2], coverArt: a[3], rating: a[4] });
}
}
function PidoraCTL(action) {
fs.open(fifo, 'w', 0644, function(error, fd) {
if (error) {
if (fd) {
fs.close(fd);
}
console.log('Error opening fifo: ' + error);
return;
}
buf = new Buffer(action);
fs.write(fd, buf, 0, action.length, null, function(error, written, buffer) {
if (fd) {
fs.close(fd);
}
if (error) {
console.log('Error writing to fifo: ' + error);
} else {
if (written == action.length) {
console.log(action + ' has been written successfully!');
} else {
console.log('Error: Only wrote ' + written + ' out of ' + action.length + ' bytes to fifo.');
}
}
});
});
}
function readStations() {
//var stations = fs.readFileSync(process.env.HOME + '/stationList').toString().split("\n");
var stations = fs.readFileSync(process.env.EPHEMERAL + '/stationList').toString().split("\n");
io.emit('stations', { stations: stations });
}
io.on('connection', function(socket) {
console.log('a user connected');
readCurrentSong();
readStations();
socket.on('action', function (data) {
var action = data.action.substring(0, 1)
PidoraCTL(action);
});
socket.on('changeStation', function (data) {
var stationId = data.stationId;
var cmd = 's' + stationId + '\n';
PidoraCTL(cmd);
});
app.post('/start', function(request, response){
artist = request.query.artist;
title = request.query.title;
album = request.query.album;
coverArt = request.query.coverArt;
rating = request.query.rating;
io.emit('start', { artist: artist, title: title, coverArt: coverArt, album: album, rating: rating });
readStations();
response.send(request.query);
});
app.post('/lovehate', function(request, response) {
rating = request.query.rating;
io.emit('lovehate', { rating: rating });
console.log(request.query);
});
});