From 81e1c3de70e9bc3df9056ea93e62969eff48a29d Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Sun, 7 Aug 2016 11:19:33 +0200 Subject: [PATCH] Prevent multiple simultanious recordings per camera --- capture.js | 21 +++++++++++++++++++-- index.js | 20 +++++++++++++++----- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/capture.js b/capture.js index 5d2a1b8..da33f67 100644 --- a/capture.js +++ b/capture.js @@ -3,6 +3,9 @@ var stream = require('stream'); var logger = require('./logger'); +var runningCams = {}; + + function runCommand(command, displayName, callback) { var wait = function() @@ -83,11 +86,23 @@ module.exports = start: function(camId, cam, now) { + if (runningCams[camId] === true) + return false; + + + runningCams[camId] = true; + var processor = new (require('./processor-' + cam.processor))(camId, cam, now); var duringCommandsDone = false; var queueAfterCommand = false; + var unlockCam = function() + { + runningCams[camId] = false; + }; + + runCommands(cam.before, 'before', function() { processor.on('start', function() @@ -97,7 +112,7 @@ module.exports = // Check if the processor has already finished and the // 'after' commands should be run immediately. if (queueAfterCommand) - runCommands(cam.after, 'after', function() { }); + runCommands(cam.after, 'after', unlockCam); else duringCommandsDone = true; }); @@ -109,12 +124,14 @@ module.exports = // to be less than the duration of the 'during' commands, // queue it up. if (duringCommandsDone) - runCommands(cam.after, 'after', function() { }); + runCommands(cam.after, 'after', unlockCam); else queueAfterCommand = true; }); processor.run(); }); + + return true; } }; \ No newline at end of file diff --git a/index.js b/index.js index 2263a48..04ca470 100644 --- a/index.js +++ b/index.js @@ -102,17 +102,24 @@ app.get('/capture', function(req, res) { var now = moment(); var cams = []; + var alreadyRunning = []; for (var camId in config.cams) { if (config.cams.hasOwnProperty(camId)) { - cams.push(camId); - capture.start(camId, config.cams[camId], now); + if (capture.start(camId, config.cams[camId], now)) + cams.push(camId); + else + alreadyRunning.push(camId); } } - logger.info('Started capture for: ' + cams.join(', ')); + if (cams.length > 0) + logger.info('Started capture for: ' + cams.join(', ')); + + if (alreadyRunning.length > 0) + logger.info('Capture already running for: ' + alreadyRunning.join(', ')); res.send(JSON.stringify(cams)); } @@ -131,9 +138,12 @@ app.get('/capture/:camId', function(req, res) if (config.cams.hasOwnProperty(camId)) { - capture.start(camId, config.cams[camId], moment()); + if (capture.start(camId, config.cams[camId], moment())) + logger.info('Started capture for: ' + camId); + else + logger.info('Capture already running for: ' + camId); + - logger.info('Started capture for: ' + camId); res.send(JSON.stringify([camId])); } else