Prevent multiple simultanious recordings per camera

This commit is contained in:
Mark van Renswoude 2016-08-07 11:19:33 +02:00
parent 55fb51ae0c
commit 81e1c3de70
2 changed files with 34 additions and 7 deletions

View File

@ -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;
}
};

View File

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