From 2ec67288e46e6844aff37c534fd3ffa05864d6f9 Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sun, 9 Apr 2017 15:08:32 -0400 Subject: [PATCH] Filter out @F messages from mpg321 --- bin/mpg321-dummy | 13 +++++++++---- config.json.vagrant | 2 +- jukebox/child-process.js | 18 +++++++++++++----- jukebox/media-player.js | 3 +++ player.js | 8 ++++---- 5 files changed, 30 insertions(+), 14 deletions(-) mode change 100755 => 100644 bin/mpg321-dummy diff --git a/bin/mpg321-dummy b/bin/mpg321-dummy old mode 100755 new mode 100644 index 050767f..84caed2 --- a/bin/mpg321-dummy +++ b/bin/mpg321-dummy @@ -1,5 +1,10 @@ -#!/bin/bash +var i = 1; -while read line ; do - echo "running $line" -done +setInterval(function() { + i += 1; + process.stderr.write(`@F ${i} 2 3 4\n`); +}, 100); + +process.stdin.on('data', function(data) { + process.stdout.write(`GOT MESSAGE ${data}`); +}); diff --git a/config.json.vagrant b/config.json.vagrant index 6d316a1..1dbaf8f 100644 --- a/config.json.vagrant +++ b/config.json.vagrant @@ -2,5 +2,5 @@ "port": "8080", "tag_reader": ["./bin/tag-reader-dummy"], - "mpg321": ["./bin/mpg321-dummy"] + "mpg321": ["node", "./bin/mpg321-dummy"] } diff --git a/jukebox/child-process.js b/jukebox/child-process.js index 873ae9c..3c0f23d 100644 --- a/jukebox/child-process.js +++ b/jukebox/child-process.js @@ -11,6 +11,7 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit super(); this.logger = logger; + this.stderrFilters = []; var cmd = command[0], args = command.slice(1); @@ -26,14 +27,21 @@ module.exports.ChildProcessEmitter = class ChildProcessEmitter extends EventEmit }); this.childProcess.stdout.on('data', (data) => { - var s = data.toString(), lines = s.split(/\n/g); - for (var i = 0, l = lines.length; i < l; i++) { - this.emit('message', lines[i]); - } + var lines = data.toString().split(/\n/g); + lines.map(line => line.trim()) + .filter(line => line.length) + .map(line => this.emit('message', line)); }); 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}); }); } diff --git a/jukebox/media-player.js b/jukebox/media-player.js index 7703b53..a8014ef 100644 --- a/jukebox/media-player.js +++ b/jukebox/media-player.js @@ -5,6 +5,9 @@ const ChildProcessEmitter = require('./child-process').ChildProcessEmitter; class MediaPlayer extends ChildProcessEmitter { constructor(config, logger) { super(config.mpg321, logger); + this.stderrFilters.push(line => { + return line.substr(0, 3) == '@F ' + }); } } diff --git a/player.js b/player.js index 6ed45d8..38f1525 100644 --- a/player.js +++ b/player.js @@ -45,13 +45,13 @@ function exitHandler(options, err) { process.on('exit', exitHandler.bind(null)); process.on('SIGINT', exitHandler.bind(null, {exit:true})); -TagReader.on('message', (data) => { +TagReader.on('message', data => { processLine(data); }); if (config.debug) { - MediaPlayer.on('message', (data) => { - logger.debug("mediaplayer send message", { line: data }); + MediaPlayer.on('message', line => { + logger.debug("mediaplayer sent message", { line: line }); }); } @@ -66,7 +66,7 @@ function lookup(tag, cb) { return cb('PAUSE', 2); } - glob('media/' + tag + ' - *.mp3', function(er, files) { + glob('media/' + tag + ' - *.mp3', (er, files) => { if (files.length > 0) { appendLog(files[0]); return cb("LOAD " + files[0], 5);