Shinobi/libs/cameraThread/singleCamera.js

72 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-12-05 07:25:13 +00:00
const fs = require('fs')
const spawn = require('child_process').spawn
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
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
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:
stdioWriters[i] = fs.createWriteStream(null, {fd: i});
if(rawMonitorConfig.details.detector === '1' && rawMonitorConfig.details.detector_pam === '1'){
newPipes[i] = 'pipe'
}else{
newPipes[i] = stdioWriters[i]
}
2019-12-05 07:25:13 +00:00
break;
default:
stdioWriters[i] = fs.createWriteStream(null, {fd: i});
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')
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)