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? // there enough commonalities to warrant a base class?
const spawn = require('child_process').spawn; const spawn = require('child_process').spawn;
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const TAGREADER = 'tagreader'; module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmitter {
const MP3PLAYER = 'mpg321'; constructor(command) {
class ChildEmitter extends EventEmitter {
constructor(process) {
super(); super();
this.childProcess = process; var cmd = command[0],
args = command.slice(1);
this.childProcess = spawn(cmd, args);
this.childProcess.on('error', function(err) { this.childProcess.on('error', function(err) {
this.emit('process_error', 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: // 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 express = require('express');
const logger = require('morgan') 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 app = express();
var server = require('http').createServer(); var server = require('http').createServer();
@ -20,10 +21,20 @@ var wss = new WebSocketServer({server: server});
var play_log = []; 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)); TagReader.kill();
process.on('SIGINT', children.exitHandler.bind(null, {exit:true})); 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) { function setupChildLogging(name, child) {
child.on('process_error', (err) => { child.on('process_error', (err) => {
@ -39,16 +50,16 @@ function setupChildLogging(name, child) {
}); });
} }
setupChildLogging('tagreader', children.tagreader); setupChildLogging('tagreader', TagReader);
setupChildLogging('mp3player', children.mp3player); setupChildLogging('mediaplayer', MediaPlayer);
children.tagreader.on('message', (data) => { TagReader.on('message', (data) => {
processLine(data); processLine(data);
}); });
if (config.debug) { if (config.debug) {
children.mp3player.on('message', (data) => { MediaPlayer.on('message', (data) => {
console.log(`[mp3player] stdout: ${data}`); console.log(`[mediaplayer] stdout: ${data}`);
}); });
} }
@ -103,7 +114,7 @@ function processLine(data) {
debounce_until = Date.now() / 1000 + wait_sec; debounce_until = Date.now() / 1000 + wait_sec;
if (action) { if (action) {
console.log(`[action] [${action}] for [${last_tag}]`); console.log(`[action] [${action}] for [${last_tag}]`);
children.mp3player.send(action + "\n"); MediaPlayer.send(action + "\n");
} }
}); });
} }