Allow plugin to send frame back to be re-used for save or notification

merge-requests/74/merge
Moe 2021-05-19 20:18:19 -07:00
parent 4819160bf5
commit f9c41c4fc7
5 changed files with 50 additions and 14 deletions

View File

@ -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",

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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]){