Add support for scripting directory

This commit is contained in:
Annika Backstrom 2019-12-14 16:19:06 -05:00
parent a4e4a119a2
commit 058f57919a
5 changed files with 51 additions and 5 deletions

View File

@ -50,5 +50,6 @@ The following keys are available:
(`-R`), reading commands from stdin. (`-R`), reading commands from stdin.
* `stop_id` -- id of the card that stops playback * `stop_id` -- id of the card that stops playback
* `pause_id` -- id of card that pauses playback * `pause_id` -- id of card that pauses playback
* `debug` -- true to enable some extra logging
[1]: https://sixohthree.com/ [1]: https://sixohthree.com/

View File

@ -4,11 +4,13 @@
"mpg321": ["/usr/bin/mpg321", "-R", "-"], "mpg321": ["/usr/bin/mpg321", "-R", "-"],
"media_path": "/path/to/media", "media_path": "/path/to/media",
"script_path": "/path/to/scripts",
"play_throttle": 5000, "play_throttle": 5000,
"pause_throttle": 2000, "pause_throttle": 2000,
"stop_throttle": 2000, "stop_throttle": 2000,
"unknown_throttle": 2000, "unknown_throttle": 2000,
"global_throttle": 2000,
"stop_id": "abcdef1234", "stop_id": "abcdef1234",
"pause_id": "567890abcd" "pause_id": "567890abcd"

31
jukebox/scripts.js Normal file
View File

@ -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:

View File

@ -1,7 +1,7 @@
{ {
"name": "pi-rfid-jukebox", "name": "pi-rfid-jukebox",
"version": "1.0.1", "version": "1.0.2",
"description": "", "description": "RFID Jukebox powered by Node.js and Python",
"main": "player.js", "main": "player.js",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -8,11 +8,15 @@ const library = require('./jukebox/library');
const MediaLibraryFileBackend = require('./jukebox/library/file-backend')(config); const MediaLibraryFileBackend = require('./jukebox/library/file-backend')(config);
const MediaLibrary = new library.Library(config, MediaLibraryFileBackend); const MediaLibrary = new library.Library(config, MediaLibraryFileBackend);
const ScriptRunner = require('./jukebox/scripts')(config);
const express = require('express'); const express = require('express');
const morgan = require('morgan') const morgan = require('morgan')
const { createLogger, format, transports } = require('winston'); const { createLogger, format, transports } = require('winston');
const { throttle } = require('throttle-debounce');
var logger = createLogger({ var logger = createLogger({
transports: [ transports: [
new transports.Console() new transports.Console()
@ -27,8 +31,6 @@ const play_log = require('./jukebox/library/play-log');
const PlayLog = new play_log.PlayLog(); const PlayLog = new play_log.PlayLog();
MediaPlayer.throttle();
var app = express(); var app = express();
var server = require('http').createServer(); var server = require('http').createServer();
@ -54,13 +56,23 @@ 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}));
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 => { TagReader.on('message', tag => {
if (tag == 'E1' || tag == 'E2') { if (tag == 'E1' || tag == 'E2') {
logger.debug("tagreader had rfid read error", { tag: tag }); logger.debug("tagreader had rfid read error", { tag: tag });
return; return;
} }
logger.debug("got tag from tagreader", { tag: tag }); logger.debug("got tag from tagreader", { tag: tag });
MediaLibrary.find(tag); throttledTag(tag);
}); });
if (config.debug) { if (config.debug) {