Filter out @F messages from mpg321

This commit is contained in:
Annika Backstrom 2017-04-09 15:08:32 -04:00
parent 8ede18f300
commit 2ec67288e4
5 changed files with 30 additions and 14 deletions

13
bin/mpg321-dummy Executable file → Normal file
View File

@ -1,5 +1,10 @@
#!/bin/bash var i = 1;
while read line ; do setInterval(function() {
echo "running $line" i += 1;
done process.stderr.write(`@F ${i} 2 3 4\n`);
}, 100);
process.stdin.on('data', function(data) {
process.stdout.write(`GOT MESSAGE ${data}`);
});

View File

@ -2,5 +2,5 @@
"port": "8080", "port": "8080",
"tag_reader": ["./bin/tag-reader-dummy"], "tag_reader": ["./bin/tag-reader-dummy"],
"mpg321": ["./bin/mpg321-dummy"] "mpg321": ["node", "./bin/mpg321-dummy"]
} }

View File

@ -11,6 +11,7 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit
super(); super();
this.logger = logger; this.logger = logger;
this.stderrFilters = [];
var cmd = command[0], var cmd = command[0],
args = command.slice(1); args = command.slice(1);
@ -26,14 +27,21 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit
}); });
this.childProcess.stdout.on('data', (data) => { this.childProcess.stdout.on('data', (data) => {
var s = data.toString(), lines = s.split(/\n/g); var lines = data.toString().split(/\n/g);
for (var i = 0, l = lines.length; i < l; i++) { lines.map(line => line.trim())
this.emit('message', lines[i]); .filter(line => line.length)
} .map(line => this.emit('message', line));
}); });
this.childProcess.stderr.on('data', (data) => { this.childProcess.stderr.on('data', (data) => {
this.logger.error('process stderr', { class: this.constructor.name, stderr: String(data).trim()}); var line = data.toString().trim();
var filtered = this.stderrFilters.reduce((filter, filtered) => {
return filtered || filter(line);
}, false);
if (filtered) {
return;
}
this.logger.error('process stderr', { class: this.constructor.name, stderr: line});
}); });
} }

View File

@ -5,6 +5,9 @@ const ChildProcessEmitter = require('./child-process').ChildProcessEmitter;
class MediaPlayer extends ChildProcessEmitter { class MediaPlayer extends ChildProcessEmitter {
constructor(config, logger) { constructor(config, logger) {
super(config.mpg321, logger); super(config.mpg321, logger);
this.stderrFilters.push(line => {
return line.substr(0, 3) == '@F '
});
} }
} }

View File

@ -45,13 +45,13 @@ function exitHandler(options, err) {
process.on('exit', exitHandler.bind(null)); process.on('exit', exitHandler.bind(null));
process.on('SIGINT', exitHandler.bind(null, {exit:true})); process.on('SIGINT', exitHandler.bind(null, {exit:true}));
TagReader.on('message', (data) => { TagReader.on('message', data => {
processLine(data); processLine(data);
}); });
if (config.debug) { if (config.debug) {
MediaPlayer.on('message', (data) => { MediaPlayer.on('message', line => {
logger.debug("mediaplayer send message", { line: data }); logger.debug("mediaplayer sent message", { line: line });
}); });
} }
@ -66,7 +66,7 @@ function lookup(tag, cb) {
return cb('PAUSE', 2); return cb('PAUSE', 2);
} }
glob('media/' + tag + ' - *.mp3', function(er, files) { glob('media/' + tag + ' - *.mp3', (er, files) => {
if (files.length > 0) { if (files.length > 0) {
appendLog(files[0]); appendLog(files[0]);
return cb("LOAD " + files[0], 5); return cb("LOAD " + files[0], 5);