Create modules for MediaPlayer and TagReader

This commit is contained in:
Annika Backstrom 2017-04-09 12:30:20 -04:00
parent 12529aeae8
commit b0f0694832
4 changed files with 57 additions and 46 deletions

View File

@ -4,17 +4,16 @@
// there enough commonalities to warrant a base class?
const spawn = require('child_process').spawn;
const EventEmitter = require('events').EventEmitter;
const TAGREADER = 'tagreader';
const MP3PLAYER = 'mpg321';
class ChildEmitter extends EventEmitter {
constructor(process) {
module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter {
constructor(command) {
super();
this.childProcess = process;
var cmd = command[0],
args = command.slice(1);
this.childProcess = spawn(cmd, args);
this.childProcess.on('error', function(err) {
this.emit('process_error', err);
@ -45,33 +44,4 @@ class ChildEmitter extends EventEmitter {
}
}
function launchChild(name, command) {
var cmd = command[0],
args = command.slice(1);
return spawn(cmd, args);
}
exports.init = function(config) {
// Set up tag reader
var tagreader = launchChild(TAGREADER, config.tag_reader);
exports.tagreader = new ChildEmitter(tagreader);
// set up mp3 player
var mp3player = launchChild(MP3PLAYER, config.mpg321);
exports.mp3player = new ChildEmitter(mp3player);
};
exports.exitHandler = function(options, err) {
console.log('[player] closing children');
if (err) console.log(err.stack);
module.exports.tagreader.kill();
module.exports.mp3player.kill();
if (options.exit) {
process.exit();
}
};
// vim:set ts=2 sw=2 et:

15
jukebox/media-player.js Normal file
View File

@ -0,0 +1,15 @@
"use strict";
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
class MediaPlayer extends ChildProcessEmitter {
constructor(config) {
super(config.mpg321);
}
}
module.exports = function(config) {
return new MediaPlayer(config);
};
// vim:set ts=2 sw=2 et:

15
jukebox/tag-reader.js Normal file
View File

@ -0,0 +1,15 @@
"use strict";
const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
class TagReader extends ChildProcessEmitter {
constructor(config) {
super(config.tag_reader);
}
}
module.exports = function(config) {
return new TagReader(config);
};
// vim:set ts=2 sw=2 et:

View File

@ -9,7 +9,8 @@ const views = require('./jukebox/views')(__dirname + '/templates/');
const express = require('express');
const logger = require('morgan')
const children = require('./jukebox/children');
const MediaPlayer = require('./jukebox/media-player')(config);
const TagReader = require('./jukebox/tag-reader')(config);
var app = express();
var server = require('http').createServer();
@ -20,10 +21,20 @@ var wss = new WebSocketServer({server: server});
var play_log = [];
children.init(config);
function exitHandler(options, err) {
console.log('[player] closing children');
if (err) console.log(err.stack);
process.on('exit', children.exitHandler.bind(null));
process.on('SIGINT', children.exitHandler.bind(null, {exit:true}));
TagReader.kill();
MediaPlayer.kill();
if (options.exit) {
process.exit();
}
};
process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
function setupChildLogging(name, child) {
child.on('process_error', (err) => {
@ -39,16 +50,16 @@ function setupChildLogging(name, child) {
});
}
setupChildLogging('tagreader', children.tagreader);
setupChildLogging('mp3player', children.mp3player);
setupChildLogging('tagreader', TagReader);
setupChildLogging('mediaplayer', MediaPlayer);
children.tagreader.on('message', (data) => {
TagReader.on('message', (data) => {
processLine(data);
});
if (config.debug) {
children.mp3player.on('message', (data) => {
console.log(`[mp3player] stdout: ${data}`);
MediaPlayer.on('message', (data) => {
console.log(`[mediaplayer] stdout: ${data}`);
});
}
@ -103,7 +114,7 @@ function processLine(data) {
debounce_until = Date.now() / 1000 + wait_sec;
if (action) {
console.log(`[action] [${action}] for [${last_tag}]`);
children.mp3player.send(action + "\n");
MediaPlayer.send(action + "\n");
}
});
}