2021-05-20 03:18:19 +00:00
|
|
|
const fs = require('fs').promises;
|
2021-01-02 06:17:37 +00:00
|
|
|
const moment = require('moment');
|
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
const exec = require('child_process').exec;
|
|
|
|
const spawn = require('child_process').spawn;
|
|
|
|
const request = require('request');
|
2021-05-21 02:10:31 +00:00
|
|
|
const imageSaveEventLock = {};
|
2021-01-02 06:17:37 +00:00
|
|
|
// Matrix In Region Libs >
|
|
|
|
const SAT = require('sat')
|
|
|
|
const V = SAT.Vector;
|
|
|
|
const P = SAT.Polygon;
|
|
|
|
const B = SAT.Box;
|
|
|
|
// Matrix In Region Libs />
|
|
|
|
module.exports = (s,config,lang,app,io) => {
|
2021-11-02 01:07:42 +00:00
|
|
|
// Event Filters >
|
|
|
|
const acceptableOperators = ['indexOf','!indexOf','===','!==','>=','>','<','<=']
|
|
|
|
// Event Filters />
|
2021-01-02 18:49:05 +00:00
|
|
|
const {
|
|
|
|
splitForFFPMEG
|
|
|
|
} = require('../ffmpeg/utils.js')(s,config,lang)
|
|
|
|
const {
|
|
|
|
moveCameraPtzToMatrix
|
|
|
|
} = require('../control/ptz.js')(s,config,lang)
|
2021-06-21 23:52:07 +00:00
|
|
|
const {
|
|
|
|
cutVideoLength
|
|
|
|
} = require('../video/utils.js')(s,config,lang)
|
2021-11-03 01:32:24 +00:00
|
|
|
const {
|
|
|
|
isEven
|
|
|
|
} = require('../basic/utils.js')(s,config,lang)
|
2021-05-20 03:18:19 +00:00
|
|
|
async function saveImageFromEvent(options,frameBuffer){
|
|
|
|
const monitorId = options.mid || options.id
|
|
|
|
const groupKey = options.ke
|
2021-05-21 02:10:31 +00:00
|
|
|
if(imageSaveEventLock[groupKey + monitorId])return;
|
2021-05-20 03:18:19 +00:00
|
|
|
const eventTime = options.time
|
|
|
|
const objectsFound = options.matrices
|
|
|
|
const monitorConfig = Object.assign({id: monitorId},s.group[groupKey].rawMonitorConfigurations[monitorId])
|
|
|
|
const timelapseRecordingDirectory = s.getTimelapseFrameDirectory({mid: monitorId, ke: groupKey})
|
|
|
|
const currentDate = s.formattedTime(eventTime,'YYYY-MM-DD')
|
|
|
|
const filename = s.formattedTime(eventTime) + '.jpg'
|
|
|
|
const location = timelapseRecordingDirectory + currentDate + '/'
|
|
|
|
try{
|
|
|
|
await fs.stat(location)
|
|
|
|
}catch(err){
|
|
|
|
await fs.mkdir(location)
|
|
|
|
}
|
|
|
|
await fs.writeFile(location + filename,frameBuffer)
|
|
|
|
s.createTimelapseFrameAndInsert(monitorConfig,location,filename,eventTime,{
|
|
|
|
objects: objectsFound
|
|
|
|
})
|
2021-05-21 02:10:31 +00:00
|
|
|
imageSaveEventLock[groupKey + monitorId] = setTimeout(function(){
|
|
|
|
delete(imageSaveEventLock[groupKey + monitorId])
|
|
|
|
},1000)
|
2021-05-20 03:18:19 +00:00
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
const countObjects = async (event) => {
|
|
|
|
const matrices = event.details.matrices
|
|
|
|
const eventsCounted = s.group[event.ke].activeMonitors[event.id].eventsCounted || {}
|
|
|
|
if(matrices){
|
|
|
|
matrices.forEach((matrix)=>{
|
|
|
|
const id = matrix.tag
|
|
|
|
if(!eventsCounted[id])eventsCounted[id] = {times: [], count: {}, tag: matrix.tag}
|
|
|
|
if(!isNaN(matrix.id))eventsCounted[id].count[matrix.id] = 1
|
|
|
|
eventsCounted[id].times.push(new Date().getTime())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return eventsCounted
|
|
|
|
}
|
2021-01-02 18:30:20 +00:00
|
|
|
const addEventDetailsToString = (eventData,string,addOps) => {
|
|
|
|
//d = event data
|
|
|
|
if(!addOps)addOps = {}
|
|
|
|
var newString = string + ''
|
|
|
|
var d = Object.assign(eventData,addOps)
|
|
|
|
var detailString = s.stringJSON(d.details)
|
|
|
|
newString = newString
|
|
|
|
.replace(/{{TIME}}/g,d.currentTimestamp)
|
|
|
|
.replace(/{{REGION_NAME}}/g,d.details.name)
|
|
|
|
.replace(/{{SNAP_PATH}}/g,s.dir.streams+d.ke+'/'+d.id+'/s.jpg')
|
|
|
|
.replace(/{{MONITOR_ID}}/g,d.id)
|
|
|
|
.replace(/{{MONITOR_NAME}}/g,s.group[d.ke].rawMonitorConfigurations[d.id].name)
|
|
|
|
.replace(/{{GROUP_KEY}}/g,d.ke)
|
|
|
|
.replace(/{{DETAILS}}/g,detailString)
|
|
|
|
if(d.details.confidence){
|
|
|
|
newString = newString
|
|
|
|
.replace(/{{CONFIDENCE}}/g,d.details.confidence)
|
|
|
|
}
|
|
|
|
if(newString.includes("REASON")) {
|
|
|
|
if(d.details.reason) {
|
|
|
|
newString = newString
|
|
|
|
.replace(/{{REASON}}/g, d.details.reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newString
|
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
const isAtleastOneMatrixInRegion = function(regions,matrices,callback){
|
|
|
|
var regionPolys = []
|
|
|
|
var matrixPoints = []
|
|
|
|
regions.forEach(function(region,n){
|
|
|
|
var polyPoints = []
|
|
|
|
region.points.forEach(function(point){
|
|
|
|
polyPoints.push(new V(parseInt(point[0]),parseInt(point[1])))
|
|
|
|
})
|
|
|
|
regionPolys[n] = new P(new V(0,0), polyPoints)
|
|
|
|
})
|
|
|
|
var collisions = []
|
|
|
|
var foundInRegion = false
|
|
|
|
matrices.forEach(function(matrix){
|
|
|
|
var matrixPoly = new B(new V(matrix.x, matrix.y), matrix.width, matrix.height).toPolygon()
|
|
|
|
regionPolys.forEach(function(region,n){
|
|
|
|
var response = new SAT.Response()
|
|
|
|
var collided = SAT.testPolygonPolygon(matrixPoly, region, response)
|
|
|
|
if(collided === true){
|
|
|
|
collisions.push({
|
|
|
|
matrix: matrix,
|
|
|
|
region: regions[n]
|
|
|
|
})
|
|
|
|
foundInRegion = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if(callback)callback(foundInRegion,collisions)
|
|
|
|
return foundInRegion
|
|
|
|
}
|
|
|
|
const scanMatricesforCollisions = function(region,matrices){
|
|
|
|
var matrixPoints = []
|
|
|
|
var collisions = []
|
|
|
|
if (!region || !matrices){
|
|
|
|
if(callback)callback(collisions)
|
|
|
|
return collisions
|
|
|
|
}
|
|
|
|
var polyPoints = []
|
|
|
|
region.points.forEach(function(point){
|
|
|
|
polyPoints.push(new V(parseInt(point[0]),parseInt(point[1])))
|
|
|
|
})
|
|
|
|
var regionPoly = new P(new V(0,0), polyPoints)
|
|
|
|
matrices.forEach(function(matrix){
|
|
|
|
if (matrix){
|
|
|
|
var matrixPoly = new B(new V(matrix.x, matrix.y), matrix.width, matrix.height).toPolygon()
|
|
|
|
var response = new SAT.Response()
|
|
|
|
var collided = SAT.testPolygonPolygon(matrixPoly, regionPoly, response)
|
|
|
|
if(collided === true){
|
|
|
|
collisions.push(matrix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return collisions
|
|
|
|
}
|
|
|
|
const getLargestMatrix = (matrices) => {
|
|
|
|
var largestMatrix = {width: 0, height: 0}
|
|
|
|
matrices.forEach((matrix) => {
|
|
|
|
if(matrix.width > largestMatrix.width && matrix.height > largestMatrix.height)largestMatrix = matrix
|
|
|
|
})
|
|
|
|
return largestMatrix.x ? largestMatrix : null
|
|
|
|
}
|
|
|
|
const addToEventCounter = (eventData) => {
|
|
|
|
const eventsCounted = s.group[eventData.ke].activeMonitors[eventData.id].detector_motion_count
|
|
|
|
s.debugLog(`addToEventCounter`,eventData,eventsCounted.length)
|
|
|
|
eventsCounted.push(eventData)
|
|
|
|
}
|
|
|
|
const clearEventCounter = (groupKey,monitorId) => {
|
|
|
|
s.group[eventData.ke].activeMonitors[eventData.id].detector_motion_count = []
|
|
|
|
}
|
|
|
|
const getEventsCounted = (groupKey,monitorId) => {
|
|
|
|
return s.group[eventData.ke].activeMonitors[eventData.id].detector_motion_count.length
|
|
|
|
}
|
2021-02-27 16:39:17 +00:00
|
|
|
const hasMatrices = (eventDetails) => {
|
2021-04-30 00:58:28 +00:00
|
|
|
return (eventDetails.matrices && eventDetails.matrices.length > 0) && eventDetails.reason !== 'motion'
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
|
|
|
const checkEventFilters = (d,monitorDetails,filter) => {
|
2021-01-17 14:38:08 +00:00
|
|
|
const eventDetails = d.details
|
2021-01-02 06:17:37 +00:00
|
|
|
if(
|
|
|
|
monitorDetails.use_detector_filters === '1' &&
|
2021-04-30 00:58:28 +00:00
|
|
|
((monitorDetails.use_detector_filters_object === '1' && d.details.matrices && d.details.reason !== 'motion') ||
|
2021-01-02 06:17:37 +00:00
|
|
|
monitorDetails.use_detector_filters_object !== '1')
|
|
|
|
){
|
|
|
|
const parseValue = function(key,val){
|
|
|
|
var newVal
|
|
|
|
switch(val){
|
|
|
|
case'':
|
|
|
|
newVal = filter[key]
|
|
|
|
break;
|
|
|
|
case'0':
|
|
|
|
newVal = false
|
|
|
|
break;
|
|
|
|
case'1':
|
|
|
|
newVal = true
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newVal = val
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return newVal
|
|
|
|
}
|
|
|
|
const filters = monitorDetails.detector_filters
|
|
|
|
Object.keys(filters).forEach(function(key){
|
|
|
|
var conditionChain = {}
|
|
|
|
var dFilter = filters[key]
|
2021-10-20 02:39:28 +00:00
|
|
|
if(dFilter.enabled === '0')return;
|
2021-11-03 01:32:24 +00:00
|
|
|
var numberOfOpenAndCloseBrackets = 0
|
2021-01-02 06:17:37 +00:00
|
|
|
dFilter.where.forEach(function(condition,place){
|
2021-11-03 01:32:24 +00:00
|
|
|
const hasOpenBracket = condition.openBracket === '1';
|
|
|
|
const hasCloseBracket = condition.closeBracket === '1';
|
|
|
|
conditionChain[place] = {
|
|
|
|
ok: false,
|
|
|
|
next: condition.p4,
|
|
|
|
matrixCount: 0,
|
|
|
|
openBracket: hasOpenBracket,
|
|
|
|
closeBracket: hasCloseBracket,
|
|
|
|
}
|
|
|
|
if(hasOpenBracket)++numberOfOpenAndCloseBrackets;
|
|
|
|
if(hasCloseBracket)++numberOfOpenAndCloseBrackets;
|
2021-01-02 06:17:37 +00:00
|
|
|
if(d.details.matrices)conditionChain[place].matrixCount = d.details.matrices.length
|
|
|
|
var modifyFilters = function(toCheck,matrixPosition){
|
|
|
|
var param = toCheck[condition.p1]
|
|
|
|
var pass = function(){
|
|
|
|
if(matrixPosition && dFilter.actions.halt === '1'){
|
|
|
|
delete(d.details.matrices[matrixPosition])
|
|
|
|
}else{
|
|
|
|
conditionChain[place].ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(condition.p2){
|
|
|
|
case'indexOf':
|
|
|
|
if(param.indexOf(condition.p3) > -1){
|
|
|
|
pass()
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case'!indexOf':
|
|
|
|
if(param.indexOf(condition.p3) === -1){
|
|
|
|
pass()
|
|
|
|
}
|
|
|
|
break;
|
2021-11-02 01:07:42 +00:00
|
|
|
case'===':
|
|
|
|
case'!==':
|
|
|
|
case'>=':
|
|
|
|
case'>':
|
|
|
|
case'<':
|
|
|
|
case'<=':
|
2021-01-02 06:17:37 +00:00
|
|
|
if(eval('param '+condition.p2+' "'+condition.p3.replace(/"/g,'\\"')+'"')){
|
|
|
|
pass()
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch(condition.p1){
|
|
|
|
case'tag':
|
|
|
|
case'x':
|
|
|
|
case'y':
|
|
|
|
case'height':
|
|
|
|
case'width':
|
|
|
|
case'confidence':
|
|
|
|
if(d.details.matrices){
|
|
|
|
d.details.matrices.forEach(function(matrix,position){
|
|
|
|
modifyFilters(matrix,position)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case'time':
|
|
|
|
var timeNow = new Date()
|
|
|
|
var timeCondition = new Date()
|
|
|
|
var doAtTime = condition.p3.split(':')
|
|
|
|
var atHour = parseInt(doAtTime[0]) - 1
|
|
|
|
var atHourNow = timeNow.getHours()
|
|
|
|
var atMinuteNow = timeNow.getMinutes()
|
|
|
|
var atSecondNow = timeNow.getSeconds()
|
|
|
|
if(atHour){
|
|
|
|
var atMinute = parseInt(doAtTime[1]) - 1 || timeNow.getMinutes()
|
|
|
|
var atSecond = parseInt(doAtTime[2]) - 1 || timeNow.getSeconds()
|
|
|
|
var nowAddedInSeconds = atHourNow * 60 * 60 + atMinuteNow * 60 + atSecondNow
|
|
|
|
var conditionAddedInSeconds = atHour * 60 * 60 + atMinute * 60 + atSecond
|
2021-11-02 01:07:42 +00:00
|
|
|
if(acceptableOperators.indexOf(condition.p2) > -1 && eval('nowAddedInSeconds '+condition.p2+' conditionAddedInSeconds')){
|
2021-01-02 06:17:37 +00:00
|
|
|
conditionChain[place].ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
modifyFilters(d.details)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
var conditionArray = Object.values(conditionChain)
|
2021-11-03 01:32:24 +00:00
|
|
|
var validationString = []
|
|
|
|
var allowBrackets = false;
|
|
|
|
if (numberOfOpenAndCloseBrackets === 0 || isEven(numberOfOpenAndCloseBrackets)){
|
|
|
|
allowBrackets = true;
|
|
|
|
}else{
|
|
|
|
s.userLog(d,{type:lang["Event Filter Error"],msg:lang.eventFilterErrorBrackets})
|
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
conditionArray.forEach(function(condition,number){
|
2021-11-03 01:32:24 +00:00
|
|
|
validationString.push(`${allowBrackets && condition.openBracket ? '(' : ''}${condition.ok}${allowBrackets && condition.closeBracket ? ')' : ''}`);
|
2021-01-02 06:17:37 +00:00
|
|
|
if(conditionArray.length-1 !== number){
|
2021-11-03 01:32:24 +00:00
|
|
|
validationString.push(condition.next)
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
|
|
|
})
|
2021-11-03 01:32:24 +00:00
|
|
|
if(eval(validationString.join(' '))){
|
2021-01-02 06:17:37 +00:00
|
|
|
if(dFilter.actions.halt !== '1'){
|
|
|
|
delete(dFilter.actions.halt)
|
|
|
|
Object.keys(dFilter.actions).forEach(function(key){
|
|
|
|
var value = dFilter.actions[key]
|
|
|
|
filter[key] = parseValue(key,value)
|
|
|
|
})
|
2021-10-20 02:39:28 +00:00
|
|
|
if(dFilter.actions.record === '1'){
|
|
|
|
filter.forceRecord = true
|
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
}else{
|
|
|
|
filter.halt = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-04-30 00:58:28 +00:00
|
|
|
if(d.details.matrices && d.details.matrices.length === 0 && d.details.reason !== 'motion' || filter.halt === true){
|
2021-01-02 06:17:37 +00:00
|
|
|
return false
|
2021-02-27 16:39:17 +00:00
|
|
|
}else if(hasMatrices(d.details)){
|
2021-01-02 06:17:37 +00:00
|
|
|
var reviewedMatrix = []
|
|
|
|
d.details.matrices.forEach(function(matrix){
|
|
|
|
if(matrix)reviewedMatrix.push(matrix)
|
|
|
|
})
|
|
|
|
d.details.matrices = reviewedMatrix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check modified indifference
|
|
|
|
if(
|
2021-05-19 06:09:04 +00:00
|
|
|
filter.indifference &&
|
2021-01-02 06:17:37 +00:00
|
|
|
eventDetails.confidence < parseFloat(filter.indifference)
|
|
|
|
){
|
|
|
|
// fails indifference check for modified indifference
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
const checkMotionLock = (eventData,monitorDetails) => {
|
|
|
|
if(s.group[eventData.ke].activeMonitors[eventData.id].motion_lock){
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var detector_lock_timeout
|
|
|
|
if(!monitorDetails.detector_lock_timeout||monitorDetails.detector_lock_timeout===''){
|
|
|
|
detector_lock_timeout = 2000
|
|
|
|
}
|
|
|
|
detector_lock_timeout = parseFloat(monitorDetails.detector_lock_timeout);
|
|
|
|
if(!s.group[eventData.ke].activeMonitors[eventData.id].detector_lock_timeout){
|
|
|
|
s.group[eventData.ke].activeMonitors[eventData.id].detector_lock_timeout=setTimeout(function(){
|
|
|
|
clearTimeout(s.group[eventData.ke].activeMonitors[eventData.id].detector_lock_timeout)
|
|
|
|
delete(s.group[eventData.ke].activeMonitors[eventData.id].detector_lock_timeout)
|
|
|
|
},detector_lock_timeout)
|
|
|
|
}else{
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-01-06 12:54:56 +00:00
|
|
|
const runMultiTrigger = (monitorConfig,eventDetails, d, triggerEvent) => {
|
2021-01-02 06:17:37 +00:00
|
|
|
s.getCamerasForMultiTrigger(monitorConfig).forEach(function(monitor){
|
|
|
|
if(monitor.mid !== d.id){
|
2021-01-06 12:54:56 +00:00
|
|
|
triggerEvent({
|
2021-01-02 06:17:37 +00:00
|
|
|
id: monitor.mid,
|
|
|
|
ke: monitor.ke,
|
|
|
|
details: {
|
|
|
|
confidence: 100,
|
|
|
|
name: "multiTrigger",
|
|
|
|
plug: eventDetails.plug,
|
|
|
|
reason: eventDetails.reason
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-02-27 16:39:17 +00:00
|
|
|
const checkForObjectsInRegions = (monitorConfig,eventDetails,filter,d,didCountingAlready) => {
|
2021-01-02 18:30:20 +00:00
|
|
|
const monitorDetails = monitorConfig.details
|
2021-02-27 16:39:17 +00:00
|
|
|
if(hasMatrices(eventDetails) && monitorDetails.detector_obj_region === '1'){
|
2021-01-02 18:30:20 +00:00
|
|
|
var regions = s.group[monitorConfig.ke].activeMonitors[monitorConfig.mid].parsedObjects.cords
|
|
|
|
var isMatrixInRegions = isAtleastOneMatrixInRegion(regions,eventDetails.matrices)
|
|
|
|
if(isMatrixInRegions){
|
|
|
|
s.debugLog('Matrix in region!')
|
|
|
|
if(filter.countObjects && monitorDetails.detector_obj_count === '1' && monitorDetails.detector_obj_count_in_region === '1' && !didCountingAlready){
|
|
|
|
countObjects(d)
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-01-06 12:54:56 +00:00
|
|
|
const runEventExecutions = async (eventTime,monitorConfig,eventDetails,forceSave,filter,d, triggerEvent) => {
|
2021-01-02 06:17:37 +00:00
|
|
|
const monitorDetails = monitorConfig.details
|
2021-01-02 16:47:59 +00:00
|
|
|
const detailString = JSON.stringify(eventDetails)
|
2021-09-30 22:49:34 +00:00
|
|
|
if(monitorDetails.detector_ptz_follow === '1'){
|
|
|
|
moveCameraPtzToMatrix(d,monitorDetails.detector_ptz_follow_target)
|
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
if(monitorDetails.det_multi_trig === '1'){
|
2021-01-06 12:54:56 +00:00
|
|
|
runMultiTrigger(monitorConfig,eventDetails, d, triggerEvent)
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
|
|
|
//save this detection result in SQL, only coords. not image.
|
2021-05-20 03:18:19 +00:00
|
|
|
if(d.frame){
|
|
|
|
saveImageFromEvent({
|
|
|
|
ke: d.ke,
|
|
|
|
mid: d.id,
|
|
|
|
time: eventTime,
|
|
|
|
matrices: eventDetails.matrices || [],
|
|
|
|
},d.frame)
|
|
|
|
}
|
2021-10-29 04:00:52 +00:00
|
|
|
if(forceSave || (filter.save || monitorDetails.detector_save === '1')){
|
2021-01-02 06:17:37 +00:00
|
|
|
s.knexQuery({
|
|
|
|
action: "insert",
|
|
|
|
table: "Events",
|
|
|
|
insert: {
|
|
|
|
ke: d.ke,
|
|
|
|
mid: d.id,
|
|
|
|
details: detailString,
|
|
|
|
time: eventTime,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if(monitorDetails.detector === '1' && monitorDetails.detector_notrigger === '1'){
|
|
|
|
s.setNoEventsDetector(monitorConfig)
|
|
|
|
}
|
|
|
|
var detector_timeout
|
|
|
|
if(!monitorDetails.detector_timeout||monitorDetails.detector_timeout===''){
|
|
|
|
detector_timeout = 10
|
|
|
|
}else{
|
|
|
|
detector_timeout = parseFloat(monitorDetails.detector_timeout)
|
|
|
|
}
|
|
|
|
if(
|
2021-10-20 02:39:28 +00:00
|
|
|
(filter.forceRecord || (filter.record && monitorDetails.detector_trigger === '1')) &&
|
2021-01-02 06:17:37 +00:00
|
|
|
monitorConfig.mode === 'start' &&
|
|
|
|
(monitorDetails.detector_record_method === 'sip' || monitorDetails.detector_record_method === 'hot')
|
|
|
|
){
|
2021-01-02 18:30:20 +00:00
|
|
|
createEventBasedRecording(d,moment(eventTime).subtract(5,'seconds').format('YYYY-MM-DDTHH-mm-ss'))
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
|
|
|
d.currentTime = eventTime
|
|
|
|
d.currentTimestamp = s.timeObject(d.currentTime).format()
|
|
|
|
d.screenshotName = eventDetails.reason + '_'+(monitorConfig.name.replace(/[^\w\s]/gi,''))+'_'+d.id+'_'+d.ke+'_'+s.formattedTime()
|
|
|
|
d.screenshotBuffer = null
|
|
|
|
|
|
|
|
if(filter.webhook && monitorDetails.detector_webhook === '1' && !s.group[d.ke].activeMonitors[d.id].detector_webhook){
|
|
|
|
s.group[d.ke].activeMonitors[d.id].detector_webhook = s.createTimeout('detector_webhook',s.group[d.ke].activeMonitors[d.id],monitorDetails.detector_webhook_timeout,10)
|
2021-01-02 18:30:20 +00:00
|
|
|
var detector_webhook_url = addEventDetailsToString(d,monitorDetails.detector_webhook_url)
|
2021-01-02 06:17:37 +00:00
|
|
|
var webhookMethod = monitorDetails.detector_webhook_method
|
|
|
|
if(!webhookMethod || webhookMethod === '')webhookMethod = 'GET'
|
|
|
|
request(detector_webhook_url,{method: webhookMethod,encoding:null},function(err,data){
|
|
|
|
if(err){
|
|
|
|
s.userLog(d,{type:lang["Event Webhook Error"],msg:{error:err,data:data}})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filter.command && monitorDetails.detector_command_enable === '1' && !s.group[d.ke].activeMonitors[d.id].detector_command){
|
|
|
|
s.group[d.ke].activeMonitors[d.id].detector_command = s.createTimeout('detector_command',s.group[d.ke].activeMonitors[d.id],monitorDetails.detector_command_timeout,10)
|
2021-01-02 18:30:20 +00:00
|
|
|
var detector_command = addEventDetailsToString(d,monitorDetails.detector_command)
|
2021-01-02 06:17:37 +00:00
|
|
|
if(detector_command === '')return
|
|
|
|
exec(detector_command,{detached: true},function(err){
|
|
|
|
if(err)s.debugLog(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < s.onEventTriggerExtensions.length; i++) {
|
|
|
|
const extender = s.onEventTriggerExtensions[i]
|
|
|
|
await extender(d,filter)
|
|
|
|
}
|
|
|
|
}
|
2021-06-02 20:11:36 +00:00
|
|
|
const getEventBasedRecordingUponCompletion = function(options){
|
|
|
|
const response = {ok: true}
|
|
|
|
return new Promise((resolve,reject) => {
|
|
|
|
const groupKey = options.ke
|
|
|
|
const monitorId = options.mid
|
|
|
|
const activeMonitor = s.group[groupKey].activeMonitors[monitorId]
|
2021-06-14 18:28:27 +00:00
|
|
|
if(activeMonitor && activeMonitor.eventBasedRecording && activeMonitor.eventBasedRecording.process){
|
|
|
|
const eventBasedRecording = activeMonitor.eventBasedRecording
|
2021-06-02 20:11:36 +00:00
|
|
|
const monitorConfig = s.group[groupKey].rawMonitorConfigurations[monitorId]
|
2021-06-21 23:52:07 +00:00
|
|
|
const videoLength = monitorConfig.details.detector_send_video_length
|
2021-06-02 20:11:36 +00:00
|
|
|
const recordingDirectory = s.getVideoDirectory(monitorConfig)
|
|
|
|
const fileTime = eventBasedRecording.lastFileTime
|
|
|
|
const filename = `${fileTime}.mp4`
|
|
|
|
response.filename = `${filename}`
|
|
|
|
response.filePath = `${recordingDirectory}${filename}`
|
|
|
|
eventBasedRecording.process.on('close',function(){
|
2021-06-21 23:52:07 +00:00
|
|
|
setTimeout(async () => {
|
|
|
|
if(!isNaN(videoLength)){
|
|
|
|
const cutResponse = await cutVideoLength({
|
|
|
|
ke: groupKey,
|
|
|
|
mid: monitorId,
|
|
|
|
filePath: response.filePath,
|
|
|
|
cutLength: parseInt(videoLength),
|
|
|
|
})
|
|
|
|
response.filename = cutResponse.filename
|
|
|
|
response.filePath = cutResponse.filePath
|
|
|
|
}
|
2021-06-02 20:11:36 +00:00
|
|
|
resolve(response)
|
|
|
|
},1000)
|
|
|
|
})
|
|
|
|
}else{
|
|
|
|
resolve(response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-02 18:30:20 +00:00
|
|
|
const createEventBasedRecording = function(d,fileTime){
|
|
|
|
if(!fileTime)fileTime = s.formattedTime()
|
|
|
|
const logTitleText = lang["Traditional Recording"]
|
|
|
|
const activeMonitor = s.group[d.ke].activeMonitors[d.id]
|
|
|
|
const monitorConfig = s.group[d.ke].rawMonitorConfigurations[d.id]
|
2021-01-02 06:17:37 +00:00
|
|
|
const monitorDetails = monitorConfig.details
|
2021-01-02 18:30:20 +00:00
|
|
|
if(monitorDetails.detector !== '1'){
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var detector_timeout
|
|
|
|
if(!monitorDetails.detector_timeout||monitorDetails.detector_timeout===''){
|
|
|
|
detector_timeout = 10
|
|
|
|
}else{
|
|
|
|
detector_timeout = parseFloat(monitorDetails.detector_timeout)
|
|
|
|
}
|
|
|
|
if(monitorDetails.watchdog_reset === '1' || !activeMonitor.eventBasedRecording.timeout){
|
|
|
|
clearTimeout(activeMonitor.eventBasedRecording.timeout)
|
|
|
|
activeMonitor.eventBasedRecording.timeout = setTimeout(function(){
|
|
|
|
activeMonitor.eventBasedRecording.allowEnd = true
|
|
|
|
activeMonitor.eventBasedRecording.process.stdin.setEncoding('utf8')
|
|
|
|
activeMonitor.eventBasedRecording.process.stdin.write('q')
|
|
|
|
activeMonitor.eventBasedRecording.process.kill('SIGINT')
|
|
|
|
delete(activeMonitor.eventBasedRecording.timeout)
|
|
|
|
},detector_timeout * 1000 * 60)
|
|
|
|
}
|
|
|
|
if(!activeMonitor.eventBasedRecording.process){
|
|
|
|
activeMonitor.eventBasedRecording.allowEnd = false;
|
2021-06-02 20:11:36 +00:00
|
|
|
activeMonitor.eventBasedRecording.lastFileTime = `${fileTime}`;
|
2021-01-02 18:30:20 +00:00
|
|
|
const runRecord = function(){
|
|
|
|
var ffmpegError = ''
|
|
|
|
var error
|
|
|
|
var filename = fileTime + '.mp4'
|
|
|
|
s.userLog(d,{
|
|
|
|
type: logTitleText,
|
|
|
|
msg: lang["Started"]
|
|
|
|
})
|
|
|
|
//-t 00:'+s.timeObject(new Date(detector_timeout * 1000 * 60)).format('mm:ss')+'
|
|
|
|
activeMonitor.eventBasedRecording.process = spawn(
|
|
|
|
config.ffmpegDir,
|
2021-01-02 18:49:05 +00:00
|
|
|
splitForFFPMEG(('-loglevel warning -analyzeduration 1000000 -probesize 1000000 -re -i "'+s.dir.streams+d.ke+'/'+d.id+'/detectorStream.m3u8" -movflags faststart+frag_keyframe+empty_moov -fflags +igndts -c:v copy -strftime 1 "'+s.getVideoDirectory(monitorConfig) + filename + '"'))
|
2021-01-02 18:30:20 +00:00
|
|
|
)
|
|
|
|
activeMonitor.eventBasedRecording.process.stderr.on('data',function(data){
|
|
|
|
s.userLog(d,{
|
|
|
|
type: logTitleText,
|
|
|
|
msg: data.toString()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
activeMonitor.eventBasedRecording.process.on('close',function(){
|
|
|
|
if(!activeMonitor.eventBasedRecording.allowEnd){
|
|
|
|
s.userLog(d,{
|
|
|
|
type: logTitleText,
|
|
|
|
msg: lang["Detector Recording Process Exited Prematurely. Restarting."]
|
|
|
|
})
|
|
|
|
runRecord()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.insertCompletedVideo(monitorConfig,{
|
|
|
|
file : filename,
|
|
|
|
})
|
|
|
|
s.userLog(d,{
|
|
|
|
type: logTitleText,
|
|
|
|
msg: lang["Detector Recording Complete"]
|
|
|
|
})
|
|
|
|
s.userLog(d,{
|
|
|
|
type: logTitleText,
|
|
|
|
msg: lang["Clear Recorder Process"]
|
|
|
|
})
|
|
|
|
delete(activeMonitor.eventBasedRecording.process)
|
|
|
|
clearTimeout(activeMonitor.eventBasedRecording.timeout)
|
|
|
|
delete(activeMonitor.eventBasedRecording.timeout)
|
|
|
|
clearTimeout(activeMonitor.recordingChecker)
|
|
|
|
})
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
2021-01-02 18:30:20 +00:00
|
|
|
runRecord()
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
2021-01-02 18:30:20 +00:00
|
|
|
}
|
|
|
|
const closeEventBasedRecording = function(e){
|
|
|
|
const activeMonitor = s.group[e.ke].activeMonitors[e.id]
|
|
|
|
if(activeMonitor.eventBasedRecording.process){
|
|
|
|
clearTimeout(activeMonitor.eventBasedRecording.timeout)
|
|
|
|
activeMonitor.eventBasedRecording.allowEnd = true
|
|
|
|
activeMonitor.eventBasedRecording.process.kill('SIGTERM')
|
|
|
|
}
|
|
|
|
// var stackedProcesses = s.group[e.ke].activeMonitors[e.id].eventBasedRecording.stackable
|
|
|
|
// Object.keys(stackedProcesses).forEach(function(key){
|
|
|
|
// var item = stackedProcesses[key]
|
|
|
|
// clearTimeout(item.timeout)
|
|
|
|
// item.allowEnd = true;
|
|
|
|
// item.process.kill('SIGTERM');
|
|
|
|
// })
|
|
|
|
}
|
|
|
|
const legacyFilterEvents = (x,d) => {
|
|
|
|
switch(x){
|
|
|
|
case'archive':
|
|
|
|
d.videos.forEach(function(v,n){
|
|
|
|
s.video('archive',v)
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
case'delete':
|
|
|
|
s.deleteListOfVideos(d.videos)
|
|
|
|
break;
|
|
|
|
case'execute':
|
|
|
|
exec(d.execute,{detached: true})
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s.onEventTriggerBeforeFilterExtensions.forEach(function(extender){
|
|
|
|
extender(x,d)
|
|
|
|
})
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
2021-04-09 21:10:31 +00:00
|
|
|
const sendFramesFromSecondaryOutput = (groupKey,monitorId,timeout) => {
|
|
|
|
const activeMonitor = s.group[groupKey].activeMonitors[monitorId]
|
|
|
|
const theEmitter = activeMonitor.secondaryDetectorOutput
|
|
|
|
if(!activeMonitor.sendingFromSecondaryDetectorOuput){
|
|
|
|
s.debugLog('start sending object frames',groupKey,monitorId)
|
|
|
|
theEmitter.on('data',activeMonitor.secondaryDetectorOuputContentWriter = (data) => {
|
|
|
|
s.ocvTx({
|
|
|
|
f : 'frame',
|
|
|
|
mon : s.group[groupKey].rawMonitorConfigurations[monitorId].details,
|
|
|
|
ke : groupKey,
|
|
|
|
id : monitorId,
|
|
|
|
time : s.formattedTime(),
|
|
|
|
frame : data
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
clearTimeout(activeMonitor.sendingFromSecondaryDetectorOuput)
|
|
|
|
activeMonitor.sendingFromSecondaryDetectorOuput = setTimeout(() => {
|
|
|
|
theEmitter.removeListener('data',activeMonitor.secondaryDetectorOuputContentWriter)
|
|
|
|
delete(activeMonitor.sendingFromSecondaryDetectorOuput)
|
|
|
|
},timeout || 5000)
|
|
|
|
}
|
2021-01-02 18:49:05 +00:00
|
|
|
const triggerEvent = async (d,forceSave) => {
|
|
|
|
var didCountingAlready = false
|
|
|
|
const filter = {
|
|
|
|
halt : false,
|
|
|
|
addToMotionCounter : true,
|
|
|
|
useLock : true,
|
2021-11-17 02:02:27 +00:00
|
|
|
save : false,
|
|
|
|
webhook : false,
|
|
|
|
command : false,
|
2021-12-04 01:52:40 +00:00
|
|
|
record : true,
|
2021-10-20 02:39:28 +00:00
|
|
|
forceRecord : false,
|
2021-01-02 18:49:05 +00:00
|
|
|
indifference : false,
|
2021-11-17 02:02:27 +00:00
|
|
|
countObjects : false
|
2021-01-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
if(!s.group[d.ke] || !s.group[d.ke].activeMonitors[d.id]){
|
|
|
|
return s.systemLog(lang['No Monitor Found, Ignoring Request'])
|
|
|
|
}
|
|
|
|
const monitorConfig = s.group[d.ke].rawMonitorConfigurations[d.id]
|
|
|
|
if(!monitorConfig){
|
|
|
|
return s.systemLog(lang['No Monitor Found, Ignoring Request'])
|
|
|
|
}
|
|
|
|
const monitorDetails = monitorConfig.details
|
|
|
|
s.onEventTriggerBeforeFilterExtensions.forEach(function(extender){
|
|
|
|
extender(d,filter)
|
|
|
|
})
|
2021-05-19 06:09:04 +00:00
|
|
|
const eventDetails = d.details
|
2021-01-02 18:49:05 +00:00
|
|
|
const passedEventFilters = checkEventFilters(d,monitorDetails,filter)
|
|
|
|
if(!passedEventFilters)return
|
|
|
|
const detailString = JSON.stringify(eventDetails)
|
|
|
|
const eventTime = new Date()
|
|
|
|
if(
|
|
|
|
filter.addToMotionCounter &&
|
|
|
|
filter.record &&
|
|
|
|
(
|
|
|
|
monitorConfig.mode === 'record' ||
|
|
|
|
monitorConfig.mode === 'start' &&
|
|
|
|
(
|
|
|
|
(
|
|
|
|
monitorDetails.detector_record_method === 'sip' &&
|
|
|
|
monitorDetails.detector_trigger === '1'
|
|
|
|
) ||
|
|
|
|
(
|
|
|
|
monitorDetails.detector_record_method === 'del' &&
|
|
|
|
monitorDetails.detector_delete_motionless_videos === '1'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
){
|
|
|
|
addToEventCounter(d)
|
|
|
|
}
|
|
|
|
if(
|
2021-11-17 02:02:27 +00:00
|
|
|
(filter.countObjects || monitorDetails.detector_obj_count === '1') &&
|
2021-01-02 18:49:05 +00:00
|
|
|
monitorDetails.detector_obj_count_in_region !== '1'
|
|
|
|
){
|
|
|
|
didCountingAlready = true
|
|
|
|
countObjects(d)
|
|
|
|
}
|
|
|
|
if(filter.useLock){
|
|
|
|
const passedMotionLock = checkMotionLock(d,monitorDetails)
|
|
|
|
if(!passedMotionLock)return
|
|
|
|
}
|
2021-02-27 16:39:17 +00:00
|
|
|
const passedObjectInRegionCheck = checkForObjectsInRegions(monitorConfig,eventDetails,filter,d,didCountingAlready)
|
2021-01-02 18:49:05 +00:00
|
|
|
if(!passedObjectInRegionCheck)return
|
|
|
|
|
|
|
|
//
|
2021-09-12 19:05:59 +00:00
|
|
|
d.doObjectDetection = (
|
|
|
|
eventDetails.reason !== 'object' &&
|
|
|
|
s.isAtleatOneDetectorPluginConnected &&
|
|
|
|
monitorDetails.detector_use_detect_object === '1' &&
|
|
|
|
monitorDetails.detector_use_motion === '1'
|
|
|
|
);
|
2021-01-02 18:49:05 +00:00
|
|
|
if(d.doObjectDetection === true){
|
2021-04-09 21:10:31 +00:00
|
|
|
sendFramesFromSecondaryOutput(d.ke,d.id)
|
2021-01-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
//
|
|
|
|
if(
|
|
|
|
monitorDetails.detector_use_motion === '0' ||
|
|
|
|
d.doObjectDetection !== true
|
|
|
|
){
|
2021-01-06 12:54:56 +00:00
|
|
|
runEventExecutions(eventTime,monitorConfig,eventDetails,forceSave,filter,d, triggerEvent)
|
2021-01-02 18:49:05 +00:00
|
|
|
}
|
|
|
|
//show client machines the event
|
|
|
|
s.tx({
|
|
|
|
f: 'detector_trigger',
|
|
|
|
id: d.id,
|
|
|
|
ke: d.ke,
|
|
|
|
details: eventDetails,
|
|
|
|
doObjectDetection: d.doObjectDetection
|
|
|
|
},`DETECTOR_${monitorConfig.ke}${monitorConfig.mid}`);
|
|
|
|
}
|
2021-01-02 06:17:37 +00:00
|
|
|
return {
|
|
|
|
countObjects: countObjects,
|
|
|
|
isAtleastOneMatrixInRegion: isAtleastOneMatrixInRegion,
|
|
|
|
scanMatricesforCollisions: scanMatricesforCollisions,
|
|
|
|
getLargestMatrix: getLargestMatrix,
|
|
|
|
addToEventCounter: addToEventCounter,
|
|
|
|
clearEventCounter: clearEventCounter,
|
|
|
|
getEventsCounted: getEventsCounted,
|
|
|
|
hasMatrices: hasMatrices,
|
|
|
|
checkEventFilters: checkEventFilters,
|
|
|
|
checkMotionLock: checkMotionLock,
|
|
|
|
runMultiTrigger: runMultiTrigger,
|
|
|
|
checkForObjectsInRegions: checkForObjectsInRegions,
|
|
|
|
runEventExecutions: runEventExecutions,
|
2021-01-02 18:30:20 +00:00
|
|
|
createEventBasedRecording: createEventBasedRecording,
|
|
|
|
closeEventBasedRecording: closeEventBasedRecording,
|
|
|
|
legacyFilterEvents: legacyFilterEvents,
|
2021-01-02 18:49:05 +00:00
|
|
|
triggerEvent: triggerEvent,
|
2021-03-27 20:04:01 +00:00
|
|
|
addEventDetailsToString: addEventDetailsToString,
|
2021-06-02 20:11:36 +00:00
|
|
|
getEventBasedRecordingUponCompletion: getEventBasedRecordingUponCompletion,
|
2021-01-02 06:17:37 +00:00
|
|
|
}
|
|
|
|
}
|