From ac68b16bf676ffcdce17dada95431e8c2d91cdd9 Mon Sep 17 00:00:00 2001 From: Moe Date: Wed, 15 Mar 2023 11:47:32 -0700 Subject: [PATCH] Fix Triggering Record by Monitor Tags --- libs/events.js | 18 ++++++++++++++++++ libs/events/utils.js | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/libs/events.js b/libs/events.js index 38b9ea14..45896315 100644 --- a/libs/events.js +++ b/libs/events.js @@ -1,4 +1,22 @@ module.exports = function(s,config,lang){ require('./events/onvif.js')(s,config,lang) require('./events/noEventsDetector.js')(s,config,lang) + const { bindTagLegendForMonitors } = require('./events/utils.js')(s,config,lang) + s.onAccountSave(function(theGroup,formDetails,user){ + const groupKey = user.ke + bindTagLegendForMonitors(groupKey) + }) + s.onMonitorSave(function(monitorConfig){ + const groupKey = monitorConfig.ke + bindTagLegendForMonitors(groupKey) + }) + s.onMonitorStop(function(monitorConfig){ + const groupKey = monitorConfig.ke + bindTagLegendForMonitors(groupKey) + }) + s.onProcessReady(function(){ + Object.keys(s.group).forEach((groupKey) => { + bindTagLegendForMonitors(groupKey) + }) + }) } diff --git a/libs/events/utils.js b/libs/events/utils.js index 16994645..96edb25d 100644 --- a/libs/events/utils.js +++ b/libs/events/utils.js @@ -10,7 +10,7 @@ const V = SAT.Vector; const P = SAT.Polygon; const B = SAT.Box; // Matrix In Region Libs /> -module.exports = (s,config,lang,app,io) => { +module.exports = (s,config,lang) => { // Event Filters > const acceptableOperators = ['indexOf','!indexOf','===','!==','>=','>','<','<='] // Event Filters /> @@ -330,8 +330,8 @@ module.exports = (s,config,lang,app,io) => { } return true } - const runMultiEventBasedRecord = (monitorConfig, triggerTags, eventTime) => { - triggerTags.forEach(function(monitorId){ + const runMultiEventBasedRecord = (monitorConfig, monitorIdsToTrigger, eventTime) => { + monitorIdsToTrigger.forEach(function(monitorId){ const groupKey = monitorConfig.ke const monitor = s.group[groupKey].rawMonitorConfigurations[monitorId] if(monitorId !== monitorConfig.mid && monitor){ @@ -347,7 +347,35 @@ module.exports = (s,config,lang,app,io) => { } }) } + function bindTagLegendForMonitors(groupKey){ + const newTagLegend = {} + const theGroup = s.group[groupKey] + const monitorIds = Object.keys(theGroup.rawMonitorConfigurations) + monitorIds.forEach((monitorId) => { + const monitorConfig = theGroup.rawMonitorConfigurations[monitorId] + const theTags = monitorConfig.tags || '' + theTags.split(',').forEach((tag) => { + if(!tag)return; + if(!newTagLegend[tag])newTagLegend[tag] = {} + newTagLegend[tag][monitorId] = {} + }) + newTagLegend[tag] = Object.keys(newTagLegend[tag]) + }) + theGroup.tagLegend = newTagLegend + } + function findMonitorsAssociatedToTags(groupKey,triggerTags){ + const monitorsToTrigger = [] + const theGroup = s.group[groupKey] + triggerTags.forEach((tag) => { + const monitorIds = theGroup.tagLegend[tag] + monitorIds.forEach((monitorId) => { + if(monitorsToTrigger.indexOf(monitorId) === -1)monitorsToTrigger.push(monitorId) + }) + }) + return monitorsToTrigger + } const runEventExecutions = async (eventTime,monitorConfig,eventDetails,forceSave,filter,d, triggerEvent) => { + const groupKey = monitorConfig.ke const monitorDetails = monitorConfig.details const detailString = JSON.stringify(eventDetails) if(monitorDetails.detector_ptz_follow === '1'){ @@ -355,7 +383,8 @@ module.exports = (s,config,lang,app,io) => { } if(monitorDetails.det_trigger_tags){ const triggerTags = monitorDetails.det_trigger_tags.split(',') - runMultiEventBasedRecord(monitorConfig, triggerTags, eventTime) + const monitorIds = findMonitorsAssociatedToTags(groupKey, triggerTags) + runMultiEventBasedRecord(monitorConfig, monitorIds, eventTime) } //save this detection result in SQL, only coords. not image. if(d.frame){ @@ -779,7 +808,8 @@ module.exports = (s,config,lang,app,io) => { hasMatrices: hasMatrices, checkEventFilters: checkEventFilters, checkMotionLock: checkMotionLock, - runMultiEventBasedRecord: runMultiEventBasedRecord, + bindTagLegendForMonitors, + runMultiEventBasedRecord, runEventExecutions: runEventExecutions, createEventBasedRecording: createEventBasedRecording, closeEventBasedRecording: closeEventBasedRecording,