pi-rfid-jukebox/player.js

158 lines
3.3 KiB
JavaScript

"use strict";
const config = require('./config.json')
const glob = require('glob');
const views = require('./jukebox/views')(__dirname + '/templates/');
const express = require('express');
const morgan = require('morgan')
const winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)()
]
});
logger.level = config.debug ? 'debug' : 'info';
const MediaPlayer = require('./jukebox/media-player')(config, logger);
const TagReader = require('./jukebox/tag-reader')(config, logger);
var app = express();
var server = require('http').createServer();
const WebSocket = require('ws');
const WebSocketServer = WebSocket.Server;
var wss = new WebSocketServer({server: server});
var play_log = [];
function exitHandler(options, err) {
logger.debug('closing child tasks');
if (err) logger.debug(err.stack);
TagReader.kill();
MediaPlayer.kill();
if (options.exit) {
process.exit();
}
};
process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
TagReader.on('message', (data) => {
processLine(data);
});
if (config.debug) {
MediaPlayer.on('message', (data) => {
logger.debug("mediaplayer send message", { line: data });
});
}
var last_tag = null;
var debounce_until = 0;
function lookup(tag, cb) {
if (tag === config.stop_id) {
return cb('STOP');
}
if (tag === config.pause_id) {
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: views.log.render({ play_log: play_log }),
tag: last_tag
};
wss.broadcast(JSON.stringify(data));
}
function processLine(data) {
if (Date.now() / 1000 < debounce_until) {
return;
}
last_tag = String(data).trim();
logger.debug("got tag from tag reader", { tag: last_tag });
// min 1 sec debounce
debounce_until = Date.now() / 1000 + 1;
lookup(last_tag, function(action, wait_sec) {
wait_sec = wait_sec || 1;
debounce_until = Date.now() / 1000 + wait_sec;
if (action) {
logger.info("sending action to media player", { action: action, tag: last_tag });
MediaPlayer.send(action + "\n");
}
});
}
app.use(morgan('dev'))
app.use(express.static(__dirname + '/static'))
app.get('/', function (req, res, next) {
try {
var index = views.index.render({
last_tag: last_tag
, config: config
, play_log: play_log
});
var html = views.base.render({
title: 'Home'
, content: index
})
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) {
logger.info('websocket client connected');
ws.on('close', function () {
logger.info('websocket client disconnected');
});
});
server.on('request', app);
server.listen(config.port, function () {
logger.info('express listening on http://localhost:' + config.port)
})
// vim:ts=2 sw=2 et: