152 lines
3.4 KiB
JavaScript
152 lines
3.4 KiB
JavaScript
const glob = require('glob');
|
|
const spawn = require('child_process').spawn;
|
|
const tag_reader = spawn('/home/annika/rfid/run-tag-reader');
|
|
const player = spawn('/usr/bin/mpg321', ['-R', '-']);
|
|
|
|
const express = require('express');
|
|
const logger = require('morgan')
|
|
const pug = require('pug');
|
|
|
|
var app = express();
|
|
var server = require('http').createServer();
|
|
|
|
const WebSocket = require('ws');
|
|
const WebSocketServer = WebSocket.Server;
|
|
var wss = new WebSocketServer({server: server});
|
|
|
|
var template = pug.compileFile(__dirname + '/source/templates/homepage.pug'),
|
|
log_tpl = pug.compileFile(__dirname + '/source/templates/log.pug');
|
|
|
|
var play_log = [];
|
|
|
|
function exitHandler(options, err) {
|
|
console.log('[player] closing children');
|
|
if (err) console.log(err.stack);
|
|
tag_reader.kill();
|
|
player.kill();
|
|
if (options.exit) {
|
|
process.exit();
|
|
}
|
|
}
|
|
|
|
process.on('exit', exitHandler.bind(null));
|
|
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
|
|
|
|
tag_reader.on('error', function(err) {
|
|
console.log(`[tag_reader] error=${err}`);
|
|
});
|
|
|
|
player.on('error', function(err) {
|
|
console.log(`[mpg321] error=${err}`);
|
|
});
|
|
|
|
tag_reader.on('close', function(code, signal) {
|
|
console.log(`[tag_reader] process closed:code=${code} signal=${signal}`);
|
|
});
|
|
|
|
player.on('close', function(code, signal) {
|
|
console.log(`[mpg321] process closed:code=${code} signal=${signal}`);
|
|
});
|
|
|
|
// player.stdin.write("GAIN 100");
|
|
|
|
var last_tag = null;
|
|
var debounce_until = 0;
|
|
|
|
function lookup(tag, cb) {
|
|
if (tag === 'b9506555d9') {
|
|
return cb('STOP');
|
|
}
|
|
if (tag === 'aa89665510') {
|
|
return cb('PAUSE', 2);
|
|
}
|
|
|
|
glob('media/' + tag + ' - *.mp3', function(er, files) {
|
|
if (files.length > 0) {
|
|
appendLog(files[0]);
|
|
return cb("LOAD " + files[0], 5);
|
|
}
|
|
appendLog(tag);
|
|
});
|
|
}
|
|
|
|
function appendLog(message) {
|
|
if (play_log.length > 10) {
|
|
play_log.pop();
|
|
}
|
|
|
|
play_log.unshift(message);
|
|
|
|
var data = {
|
|
html: log_tpl({ play_log: play_log }),
|
|
tag: last_tag
|
|
};
|
|
|
|
wss.broadcast(JSON.stringify(data));
|
|
}
|
|
|
|
tag_reader.stdout.on('data', (data) => {
|
|
if (Date.now() / 1000 < debounce_until) {
|
|
return;
|
|
}
|
|
last_tag = String(data).trim();
|
|
console.log("[tag] " + last_tag);
|
|
lookup(last_tag, function(action, wait_sec) {
|
|
wait_sec = wait_sec || 1;
|
|
if (action) {
|
|
debounce_until = Date.now() / 1000 + wait_sec;
|
|
console.log(`[action] [${action}] for [${last_tag}]`);
|
|
player.stdin.write(action + "\n");
|
|
}
|
|
});
|
|
});
|
|
|
|
tag_reader.stderr.on('data', (data) => {
|
|
console.log(`[tag_reader:stderr] ${data}`);
|
|
});
|
|
|
|
player.stderr.on('data', (data) => {
|
|
if (String(data).substr(0, 3) === "@F ") {
|
|
return;
|
|
}
|
|
console.log(`[mpg321] stderr ${data}`);
|
|
});
|
|
|
|
app.use(logger('dev'))
|
|
app.use(express.static(__dirname + '/static'))
|
|
|
|
app.get('/', function (req, res, next) {
|
|
try {
|
|
var html = template({
|
|
title: 'Home',
|
|
last_tag: last_tag,
|
|
play_log: play_log
|
|
})
|
|
res.send(html)
|
|
} catch (e) {
|
|
next(e)
|
|
}
|
|
});
|
|
|
|
wss.broadcast = function broadcast(data) {
|
|
wss.clients.forEach(function each(client) {
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
client.send(data);
|
|
}
|
|
});
|
|
};
|
|
|
|
wss.on('connection', function (ws) {
|
|
console.log('[websocket] client connected');
|
|
ws.on('close', function () {
|
|
console.log('[websocket] client disconnected');
|
|
});
|
|
});
|
|
|
|
server.on('request', app);
|
|
|
|
var server_port = process.env.PORT || 80;
|
|
server.listen(server_port, function () {
|
|
console.log('[http] listening on http://localhost:' + server_port)
|
|
})
|