From 83750bb20ef5ca7d2f55b99572d98a4c5f224a9d Mon Sep 17 00:00:00 2001 From: Moe Date: Mon, 4 Jul 2022 20:29:26 -0700 Subject: [PATCH] add timelapseVideoBuild to Websocket API --- libs/timelapse.js | 78 ++++++++++++++++++++-------- web/assets/js/bs5.timelapseViewer.js | 31 ++++++----- 2 files changed, 76 insertions(+), 33 deletions(-) diff --git a/libs/timelapse.js b/libs/timelapse.js index 3783a701..970b1f54 100644 --- a/libs/timelapse.js +++ b/libs/timelapse.js @@ -400,6 +400,35 @@ module.exports = function(s,config,lang,app,io){ response.msg = `${lang.Building}... ${lang['Please Wait...']}` return response } + function initiateTimelapseVideoBuild({ + groupKey, + monitorId, + framesPerSecond, + framesPosted, + }){ + return new Promise((resolve,reject) => { + let response = {ok: false} + const frames = [] + var n = 0 + framesPosted.forEach((frame) => { + var firstParam = [['ke','=',groupKey],['mid','=',monitorId],['filename','=',frame.filename]] + if(n !== 0)firstParam[0] = (['or']).concat(firstParam[0]) + frames.push(...firstParam) + ++n + }) + s.knexQuery({ + action: "select", + columns: "*", + table: "Timelapse Frames", + where: frames + },async (err,r) => { + if(r.length > 0){ + response = await createVideoFromTimelapse(r.reverse(),framesPerSecond) + } + resolve(response) + }) + }) + } // Web Paths // // // // // /** @@ -453,6 +482,8 @@ module.exports = function(s,config,lang,app,io){ ], function (req,res){ res.setHeader('Content-Type', 'application/json'); s.auth(req.params,function(user){ + const groupKey = req.params.ke + const monitorId = req.params.id var hasRestrictions = user.details.sub && user.details.allmonitors !== '1' if( user.permissions.watch_videos==="0" || @@ -472,28 +503,14 @@ module.exports = function(s,config,lang,app,io){ }) return } + const framesPerSecond = s.getPostData(req, 'fps') const framesPosted = s.getPostData(req, 'frames', true) || [] - const frames = [] - var n = 0 - framesPosted.forEach((frame) => { - var firstParam = [['ke','=',req.params.ke],['mid','=',req.params.id],['filename','=',frame.filename]] - if(n !== 0)firstParam[0] = (['or']).concat(firstParam[0]) - frames.push(...firstParam) - ++n - }) - s.knexQuery({ - action: "select", - columns: "*", - table: "Timelapse Frames", - where: frames - },async (err,r) => { - if(r.length === 0){ - s.closeJsonResponse(res,{ - ok: false - }) - return - } - const buildResponse = await createVideoFromTimelapse(r.reverse(),s.getPostData(req, 'fps')) + initiateTimelapseVideoBuild({ + groupKey, + monitorId, + framesPosted, + framesPerSecond, + }).then((buildResponse) => { s.closeJsonResponse(res,buildResponse) }) },res,req); @@ -597,6 +614,25 @@ module.exports = function(s,config,lang,app,io){ }) },res,req); }); + s.onOtherWebSocketMessages((d,connection) => { + switch(d.f){ + case'timelapseVideoBuild': + initiateTimelapseVideoBuild({ + groupKey: d.ke, + monitorId: d.mid, + framesPosted: d.frames, + framesPerSecond: d.fps, + }).then((buildResponse) => { + s.tx({ + f: 'timelapse_build_requested', + ke: d.ke, + mid: d.mid, + buildResponse: buildResponse, + },'GRP_'+d.ke); + }) + break; + } + }) var buildTimelapseVideos = function(){ var dateNow = new Date() var hoursNow = dateNow.getHours() diff --git a/web/assets/js/bs5.timelapseViewer.js b/web/assets/js/bs5.timelapseViewer.js index 25e652ea..9d5e26c1 100644 --- a/web/assets/js/bs5.timelapseViewer.js +++ b/web/assets/js/bs5.timelapseViewer.js @@ -211,24 +211,19 @@ $(document).ready(function(e){ var startDate = dateRange.startDate var endDate = dateRange.endDate var selectedMonitor = monitorsList.val() - var parsedFrames = JSON.stringify(currentPlaylistArray.map(function(frame){ + window.askedForTimelapseVideoBuild = true + var parsedFrames = currentPlaylistArray.map(function(frame){ return { mid: frame.mid, ke: frame.ke, filename: frame.filename, } - })); - $.post(apiBaseUrl + '/timelapseBuildVideo/' + $user.ke + '/' + selectedMonitor,{ - fps: fps, + }); + mainSocket.f({ + f: 'timelapseVideoBuild', + mid: selectedMonitor, frames: parsedFrames, - },function(response){ - setDownloadButtonLabel(response.msg, '') - new PNotify({ - title: lang['Timelapse Frames Video'], - text: response.msg, - type: response.fileExists ? 'success' : 'info' - }) - if(response.fileExists)downloadTimelapseVideo(response); + fps: fps, }) }) function isElementVisible (el) { @@ -300,6 +295,18 @@ $(document).ready(function(e){ } onWebSocketEvent(function(data){ switch(data.f){ + case'timelapse_build_requested': + console.log(data) + var response = data.buildResponse; + setDownloadButtonLabel(response.msg, '') + new PNotify({ + title: lang['Timelapse Frames Video'], + text: response.msg, + type: response.fileExists ? 'success' : 'info' + }); + if(response.fileExists && window.askedForTimelapseVideoBuild)downloadTimelapseVideo(response); + window.askedForTimelapseVideoBuild = false + break; case'fileBin_item_added': var saveBuiltVideo = dashboardOptions().switches.timelapseSaveBuiltVideo let statusText = `${lang['Done!']}`