127 lines
2.7 KiB
JavaScript
127 lines
2.7 KiB
JavaScript
"use strict";
|
|
|
|
const config = require('./config.json')
|
|
|
|
const views = require('./jukebox/views')(__dirname + '/templates/');
|
|
const library = require('./jukebox/library');
|
|
|
|
const MediaLibraryFileBackend = require('./jukebox/library/file-backend')(config);
|
|
const MediaLibrary = new library.Library(config, MediaLibraryFileBackend);
|
|
|
|
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);
|
|
|
|
MediaPlayer.throttle();
|
|
|
|
var app = express();
|
|
var server = require('http').createServer();
|
|
|
|
const WebSocket = require('ws');
|
|
const WebSocketServer = WebSocket.Server;
|
|
var wss = new WebSocketServer({server: server});
|
|
|
|
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', tag => {
|
|
MediaLibrary.find(tag);
|
|
});
|
|
|
|
if (config.debug) {
|
|
MediaPlayer.on('message', line => {
|
|
logger.debug("mediaplayer sent message", { line: line });
|
|
});
|
|
}
|
|
|
|
MediaLibrary.on('play', event => {
|
|
var data = {
|
|
html: views.log.render({ play_log: MediaLibrary.getLog() }),
|
|
tag: MediaLibrary.getLastTag()
|
|
};
|
|
|
|
wss.broadcast(JSON.stringify(data));
|
|
});
|
|
|
|
MediaLibrary.on('play', event => {
|
|
if (event.tag === config.stop_id) {
|
|
MediaPlayer.stop();
|
|
}
|
|
|
|
if (event.tag === config.pause_id) {
|
|
MediaPlayer.pause();
|
|
}
|
|
|
|
if (event.path) {
|
|
MediaPlayer.playFile(event.path);
|
|
}
|
|
});
|
|
|
|
app.use(morgan('dev'))
|
|
app.use(express.static(__dirname + '/static'))
|
|
|
|
app.get('/', function (req, res, next) {
|
|
try {
|
|
var index = views.index.render({
|
|
last_tag: MediaLibrary.getLastTag()
|
|
, config: config
|
|
, play_log: MediaLibrary.getLog()
|
|
});
|
|
|
|
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:
|