diff --git a/libs/events/utils.js b/libs/events/utils.js index a130bb40..a140d319 100644 --- a/libs/events/utils.js +++ b/libs/events/utils.js @@ -1,3 +1,4 @@ +const fs = require('fs').promises; const moment = require('moment'); const execSync = require('child_process').execSync; const exec = require('child_process').exec; @@ -16,6 +17,26 @@ module.exports = (s,config,lang,app,io) => { const { moveCameraPtzToMatrix } = require('../control/ptz.js')(s,config,lang) + async function saveImageFromEvent(options,frameBuffer){ + const monitorId = options.mid || options.id + const groupKey = options.ke + 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 + }) + } const countObjects = async (event) => { const matrices = event.details.matrices const eventsCounted = s.group[event.ke].activeMonitors[event.id].eventsCounted || {} @@ -322,6 +343,14 @@ module.exports = (s,config,lang,app,io) => { runMultiTrigger(monitorConfig,eventDetails, d, triggerEvent) } //save this detection result in SQL, only coords. not image. + if(d.frame){ + saveImageFromEvent({ + ke: d.ke, + mid: d.id, + time: eventTime, + matrices: eventDetails.matrices || [], + },d.frame) + } if(forceSave || (filter.save && monitorDetails.detector_save === '1')){ s.knexQuery({ action: "insert", diff --git a/libs/notifications/discordBot.js b/libs/notifications/discordBot.js index 480e2087..8b158a50 100644 --- a/libs/notifications/discordBot.js +++ b/libs/notifications/discordBot.js @@ -86,11 +86,14 @@ module.exports = function(s,config,lang){ ],d.ke) }) } - const {screenShot, isStaticFile} = await s.getRawSnapshotFromMonitor(monitorConfig,{ - secondsInward: monitorConfig.details.snap_seconds_inward - }) - if(screenShot){ + d.screenshotBuffer = d.screenshotBuffer || d.frame + if(!d.screenshotBuffer){ + const { screenShot, isStaticFile } = await s.getRawSnapshotFromMonitor(monitorConfig,{ + secondsInward: monitorConfig.details.snap_seconds_inward + }) d.screenshotBuffer = screenShot + } + if(d.screenshotBuffer){ sendMessage({ author: { name: s.group[d.ke].rawMonitorConfigurations[d.id].name, @@ -106,7 +109,7 @@ module.exports = function(s,config,lang){ } },[ { - attachment: screenShot, + attachment: d.screenshotBuffer, name: d.screenshotName+'.jpg' } ],d.ke) diff --git a/libs/notifications/email.js b/libs/notifications/email.js index 8ac3980f..e6e51ef5 100644 --- a/libs/notifications/email.js +++ b/libs/notifications/email.js @@ -177,6 +177,7 @@ module.exports = function(s,config,lang){ }) }) } + d.screenshotBuffer = d.screenshotBuffer || d.frame if(!d.screenshotBuffer){ const {screenShot, isStaticFile} = await s.getRawSnapshotFromMonitor(monitorConfig,{ secondsInward: monitorConfig.details.snap_seconds_inward diff --git a/libs/notifications/telegram.js b/libs/notifications/telegram.js index cd9b85b5..35af8f69 100644 --- a/libs/notifications/telegram.js +++ b/libs/notifications/telegram.js @@ -71,18 +71,21 @@ module.exports = function(s,config,lang){ ],d.ke) }) } - const {screenShot, isStaticFile} = await s.getRawSnapshotFromMonitor(monitorConfig,{ - secondsInward: monitorConfig.details.snap_seconds_inward - }) - if(screenShot){ + d.screenshotBuffer = d.screenshotBuffer || d.frame + if(!d.screenshotBuffer){ + const { screenShot, isStaticFile } = await s.getRawSnapshotFromMonitor(monitorConfig,{ + secondsInward: monitorConfig.details.snap_seconds_inward + }) d.screenshotBuffer = screenShot + } + if(d.screenshotBuffer){ sendMessage({ title: lang.Event+' - '+d.screenshotName, description: lang.EventText1+' '+d.currentTimestamp, },[ { type: 'photo', - attachment: screenShot, + attachment: d.screenshotBuffer, name: d.screenshotName+'.jpg' } ],d.ke) diff --git a/libs/timelapse.js b/libs/timelapse.js index 44ca2dd4..4cdd42e0 100644 --- a/libs/timelapse.js +++ b/libs/timelapse.js @@ -13,16 +13,16 @@ module.exports = function(s,config,lang,app,io){ return s.dir.videos+e.ke+'/'+e.id+'_timelapse/'; } } - s.createTimelapseFrameAndInsert = function(e,location,filename){ + s.createTimelapseFrameAndInsert = function(e,location,filename,eventTime,frameDetails){ //e = monitor object //location = file location var filePath = location + filename var fileStats = fs.statSync(filePath) - var details = {} + var details = Object.assign({},frameDetails || {}) if(e.details && e.details.dir && e.details.dir !== ''){ details.dir = e.details.dir } - var timeNow = new Date() + var timeNow = eventTime || new Date() var queryInfo = { ke: e.ke, mid: e.id, @@ -344,7 +344,7 @@ module.exports = function(s,config,lang,app,io){ }else{ s.closeJsonResponse(res,{ok: false, msg: lang[`Nothing exists`]}) } - }) + }) } } if(timelapseFramesCache[cacheKey]){