2016-07-10 17:34:49 +00:00
|
|
|
var http = require('http');
|
2016-07-16 09:35:15 +00:00
|
|
|
var stream = require('stream');
|
2016-08-07 09:02:21 +00:00
|
|
|
var logger = require('./logger');
|
2016-07-10 17:34:49 +00:00
|
|
|
|
|
|
|
|
2016-08-07 09:19:33 +00:00
|
|
|
var runningCams = {};
|
|
|
|
|
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
function runCommand(command, displayName, callback)
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-16 09:35:15 +00:00
|
|
|
var wait = function()
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-16 09:35:15 +00:00
|
|
|
if (command.wait)
|
|
|
|
{
|
|
|
|
setTimeout(callback, command.wait)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command.url)
|
|
|
|
{
|
2016-08-07 09:02:21 +00:00
|
|
|
logger.verbose('Running command: ' + (command.displayName ? command.displayName : displayName));
|
2016-07-16 09:35:15 +00:00
|
|
|
|
|
|
|
req = http.request(command.url, function(res)
|
|
|
|
{
|
|
|
|
res.resume();
|
|
|
|
wait();
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on('error', function(e)
|
|
|
|
{
|
2016-08-07 09:02:21 +00:00
|
|
|
logger.error(e);
|
2016-07-16 09:35:15 +00:00
|
|
|
wait();
|
|
|
|
});
|
2016-07-10 17:34:49 +00:00
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
req.end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wait();
|
|
|
|
}
|
2016-07-10 17:34:49 +00:00
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
|
|
|
|
function runCommands(commandArray, displayName, callback)
|
|
|
|
{
|
|
|
|
if (!commandArray || !commandArray.length)
|
|
|
|
{
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-10 17:34:49 +00:00
|
|
|
|
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
var commandIndex = 0;
|
|
|
|
|
|
|
|
(function runNextCommand()
|
|
|
|
{
|
|
|
|
if (commandIndex < commandArray.length)
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-16 09:35:15 +00:00
|
|
|
runCommand(commandArray[commandIndex], displayName + ' #' + (commandIndex + 1), function()
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-16 09:35:15 +00:00
|
|
|
runNextCommand();
|
|
|
|
});
|
|
|
|
|
|
|
|
commandIndex++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
callback();
|
|
|
|
})();
|
|
|
|
}
|
2016-07-10 17:34:49 +00:00
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
{
|
|
|
|
init: function(cams)
|
|
|
|
{
|
|
|
|
// Preload all modules to get error messages early
|
|
|
|
for (var camId in cams)
|
|
|
|
{
|
|
|
|
if (cams.hasOwnProperty(camId))
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-26 18:25:18 +00:00
|
|
|
require('./processor-' + cams[camId].processor);
|
2016-07-10 17:34:49 +00:00
|
|
|
}
|
2016-07-16 09:35:15 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
start: function(camId, cam, now)
|
|
|
|
{
|
2016-08-07 09:19:33 +00:00
|
|
|
if (runningCams[camId] === true)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
runningCams[camId] = true;
|
|
|
|
|
2016-07-26 18:25:18 +00:00
|
|
|
var processor = new (require('./processor-' + cam.processor))(camId, cam, now);
|
|
|
|
var duringCommandsDone = false;
|
|
|
|
var queueAfterCommand = false;
|
2016-07-10 17:34:49 +00:00
|
|
|
|
|
|
|
|
2016-08-07 09:19:33 +00:00
|
|
|
var unlockCam = function()
|
|
|
|
{
|
|
|
|
runningCams[camId] = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-07-16 09:35:15 +00:00
|
|
|
runCommands(cam.before, 'before', function()
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-26 18:25:18 +00:00
|
|
|
processor.on('start', function()
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-16 13:49:19 +00:00
|
|
|
runCommands(cam.during, 'during', function()
|
|
|
|
{
|
2016-07-26 18:25:18 +00:00
|
|
|
// Check if the processor has already finished and the
|
|
|
|
// 'after' commands should be run immediately.
|
|
|
|
if (queueAfterCommand)
|
2016-08-07 09:19:33 +00:00
|
|
|
runCommands(cam.after, 'after', unlockCam);
|
2016-07-16 13:49:19 +00:00
|
|
|
else
|
2016-07-26 18:25:18 +00:00
|
|
|
duringCommandsDone = true;
|
2016-07-16 09:35:15 +00:00
|
|
|
});
|
2016-07-10 17:34:49 +00:00
|
|
|
});
|
|
|
|
|
2016-07-26 18:25:18 +00:00
|
|
|
processor.on('end', function()
|
2016-07-10 17:34:49 +00:00
|
|
|
{
|
2016-07-26 18:25:18 +00:00
|
|
|
// If the stream is cut short, or the time is configured
|
|
|
|
// to be less than the duration of the 'during' commands,
|
|
|
|
// queue it up.
|
|
|
|
if (duringCommandsDone)
|
2016-08-07 09:19:33 +00:00
|
|
|
runCommands(cam.after, 'after', unlockCam);
|
2016-07-26 18:25:18 +00:00
|
|
|
else
|
|
|
|
queueAfterCommand = true;
|
2016-07-10 17:34:49 +00:00
|
|
|
});
|
|
|
|
|
2016-07-26 18:25:18 +00:00
|
|
|
processor.run();
|
2016-07-10 17:34:49 +00:00
|
|
|
});
|
2016-08-07 09:19:33 +00:00
|
|
|
|
|
|
|
return true;
|
2016-07-10 17:34:49 +00:00
|
|
|
}
|
|
|
|
};
|