2019-12-05 07:25:13 +00:00
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const spawn = require('child_process').spawn
|
2019-12-08 05:53:17 +00:00
|
|
|
try{
|
|
|
|
fs.unlinkSync('/home/Shinobi/test.log')
|
|
|
|
}catch(err){
|
|
|
|
|
2019-12-05 12:41:36 +00:00
|
|
|
}
|
2019-12-05 07:25:13 +00:00
|
|
|
process.send = process.send || function () {};
|
|
|
|
// [CTRL] + [C] = exit
|
|
|
|
process.on('SIGINT', function() {
|
|
|
|
cameraProcess.kill('SIGTERM')
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!process.argv[2] || !process.argv[3]){
|
|
|
|
return writeToStderr('Missing FFMPEG Command String or no command operator')
|
|
|
|
}
|
|
|
|
var jsonData = JSON.parse(fs.readFileSync(process.argv[3],'utf8'))
|
|
|
|
const ffmpegAbsolutePath = process.argv[2].trim()
|
|
|
|
const ffmpegCommandString = jsonData.cmd
|
2019-12-06 03:45:24 +00:00
|
|
|
const rawMonitorConfig = jsonData.rawMonitorConfig
|
2019-12-05 07:25:13 +00:00
|
|
|
const stdioPipes = jsonData.pipes || []
|
|
|
|
var newPipes = []
|
|
|
|
var stdioWriters = [];
|
2019-12-05 12:41:36 +00:00
|
|
|
|
2019-12-08 05:53:17 +00:00
|
|
|
var writeToStderr = function(text){
|
|
|
|
if(!stdioWriters[2]){
|
|
|
|
fs.appendFileSync('/home/Shinobi/test.log',text + '\n','utf8')
|
|
|
|
}else{
|
|
|
|
stdioWriters[2].write(Buffer.from(`${text}`, 'utf8' ))
|
|
|
|
// stdioWriters[2].write(Buffer.from(`${new Error('writeToStderr').stack}`, 'utf8' ))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process.on('uncaughtException', function (err) {
|
|
|
|
writeToStderr('Uncaught Exception occured!');
|
|
|
|
writeToStderr(err.stack);
|
|
|
|
});
|
|
|
|
|
2019-12-05 07:25:13 +00:00
|
|
|
for(var i=0; i < stdioPipes; i++){
|
|
|
|
switch(i){
|
|
|
|
case 3:
|
2019-12-07 04:09:09 +00:00
|
|
|
stdioWriters[i] = fs.createWriteStream(null, {fd: i});
|
2019-12-08 05:53:17 +00:00
|
|
|
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
|
|
|
|
newPipes[i] = 'pipe'
|
2019-12-06 03:45:24 +00:00
|
|
|
}else{
|
2019-12-08 05:53:17 +00:00
|
|
|
newPipes[i] = stdioWriters[i]
|
2019-12-06 03:45:24 +00:00
|
|
|
}
|
2019-12-05 07:25:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stdioWriters[i] = fs.createWriteStream(null, {fd: i});
|
2019-12-08 05:53:17 +00:00
|
|
|
newPipes[i] = stdioWriters[i]
|
2019-12-05 07:25:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var cameraProcess = spawn(ffmpegAbsolutePath,ffmpegCommandString,{detached: true,stdio:newPipes})
|
2019-12-05 14:12:29 +00:00
|
|
|
cameraProcess.on('close',()=>{
|
|
|
|
process.exit();
|
|
|
|
})
|
2019-12-08 08:00:56 +00:00
|
|
|
writeToStderr('Thread Opening')
|
|
|
|
|
2019-12-08 05:53:17 +00:00
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
|
|
|
|
try{
|
|
|
|
const attachPamDetector = require(__dirname + '/detector.js')(jsonData,stdioWriters[3])
|
|
|
|
attachPamDetector(cameraProcess)
|
|
|
|
}catch(err){
|
|
|
|
writeToStderr(err.stack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},3000)
|