From 058f57919a16cb4179b32730ad2f27bde7b27b12 Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sat, 14 Dec 2019 16:19:06 -0500 Subject: [PATCH] Add support for scripting directory --- README.md | 1 + config.json.sample | 2 ++ jukebox/scripts.js | 31 +++++++++++++++++++++++++++++++ package.json | 4 ++-- player.js | 18 +++++++++++++++--- 5 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 jukebox/scripts.js diff --git a/README.md b/README.md index 5ed3758..bad9c78 100644 --- a/README.md +++ b/README.md @@ -50,5 +50,6 @@ The following keys are available: (`-R`), reading commands from stdin. * `stop_id` -- id of the card that stops playback * `pause_id` -- id of card that pauses playback +* `debug` -- true to enable some extra logging [1]: https://sixohthree.com/ diff --git a/config.json.sample b/config.json.sample index 88c4382..24d7961 100644 --- a/config.json.sample +++ b/config.json.sample @@ -4,11 +4,13 @@ "mpg321": ["/usr/bin/mpg321", "-R", "-"], "media_path": "/path/to/media", + "script_path": "/path/to/scripts", "play_throttle": 5000, "pause_throttle": 2000, "stop_throttle": 2000, "unknown_throttle": 2000, + "global_throttle": 2000, "stop_id": "abcdef1234", "pause_id": "567890abcd" diff --git a/jukebox/scripts.js b/jukebox/scripts.js new file mode 100644 index 0000000..ea819db --- /dev/null +++ b/jukebox/scripts.js @@ -0,0 +1,31 @@ +"use strict"; + +const { execFile } = require('child_process'); +const glob = require('glob'); +const util = require('util'); + +const globPromise = util.promisify(glob); + +class ScriptRunner { + constructor(config) { + this.config = config; + } + + find(tag) { + return globPromise(this.config.script_path + '/**/' + tag + '*.sh').then(files => { + return new Promise((resolve, reject) => { + if (files.length === 1) { + execFile(files[0]); + resolve(true); + } + resolve(false); + }); + }); + } +} + +module.exports = function(config) { + return new ScriptRunner(config); +}; + +// vim:ts=2 sw=2 et: diff --git a/package.json b/package.json index eb87f53..7441641 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "pi-rfid-jukebox", - "version": "1.0.1", - "description": "", + "version": "1.0.2", + "description": "RFID Jukebox powered by Node.js and Python", "main": "player.js", "repository": { "type": "git", diff --git a/player.js b/player.js index ef0a6eb..3cca474 100644 --- a/player.js +++ b/player.js @@ -8,11 +8,15 @@ const library = require('./jukebox/library'); const MediaLibraryFileBackend = require('./jukebox/library/file-backend')(config); const MediaLibrary = new library.Library(config, MediaLibraryFileBackend); +const ScriptRunner = require('./jukebox/scripts')(config); + const express = require('express'); const morgan = require('morgan') const { createLogger, format, transports } = require('winston'); +const { throttle } = require('throttle-debounce'); + var logger = createLogger({ transports: [ new transports.Console() @@ -27,8 +31,6 @@ const play_log = require('./jukebox/library/play-log'); const PlayLog = new play_log.PlayLog(); -MediaPlayer.throttle(); - var app = express(); var server = require('http').createServer(); @@ -54,13 +56,23 @@ function exitHandler(options, err) { process.on('exit', exitHandler.bind(null)); process.on('SIGINT', exitHandler.bind(null, {exit:true})); +const throttledTag = throttle(config.global_throttle, true, tag => { + ScriptRunner.find(tag).then((fulfilled) => { + if (false === fulfilled) { + MediaLibrary.find(tag) + } + }).catch(err => { + logger.error("got an error handling the tag: " + err); + }); +}); + TagReader.on('message', tag => { if (tag == 'E1' || tag == 'E2') { logger.debug("tagreader had rfid read error", { tag: tag }); return; } logger.debug("got tag from tagreader", { tag: tag }); - MediaLibrary.find(tag); + throttledTag(tag); }); if (config.debug) {