From 714871df91bf376c1bbd567a0e77b15e31ae0c1b Mon Sep 17 00:00:00 2001 From: khavinshankar Date: Tue, 3 Oct 2023 13:57:11 +0530 Subject: [PATCH 1/2] lock camera movements when ocr is taking an image --- src/automation/autoDataExtractor.js | 13 +++++---- src/utils/CameraUtils.js | 44 +++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/automation/autoDataExtractor.js b/src/automation/autoDataExtractor.js index 26bab46..95a9d31 100644 --- a/src/automation/autoDataExtractor.js +++ b/src/automation/autoDataExtractor.js @@ -1,11 +1,9 @@ - import { CameraUtils } from "../utils/CameraUtils.js"; import { downloadImage } from "./helper/downloadImageWithDigestRouter.js"; -import axios from 'axios'; +import axios from "axios"; import path from "path"; -import fs from 'fs'; -import FormData from 'form-data'; -import { getMonitorCoordinates } from "./helper/getMonitorCoordinates.js"; +import fs from "fs"; +import FormData from "form-data"; import * as dotenv from "dotenv"; import { saveOCRImages, waitBeforeOCRCapture } from "../utils/configs.js"; @@ -77,6 +75,8 @@ const extractData = async (camParams, monitorPreset = { x: 0, y: 0, z: 0 }) => { console.log("Moving to coordinates: ", monitorPreset); await CameraUtils.absoluteMove({ camParams, ...monitorPreset }); + CameraUtils.lockCamera(cameraParams.hostname); + // TODO: replace timeout with a better solution await new Promise((resolve) => setTimeout(resolve, waitBeforeOCRCapture * 1000) @@ -84,6 +84,8 @@ const extractData = async (camParams, monitorPreset = { x: 0, y: 0, z: 0 }) => { const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams }); + CameraUtils.unlockCamera(cameraParams.hostname); + const fileName = "image-" + new Date().getTime() + ".jpeg"; const imagePath = path.resolve("images", fileName); await downloadImage( @@ -149,4 +151,3 @@ export const updateObservationAuto = async (cameraParams, monitorPreset) => { }; } }; - diff --git a/src/utils/CameraUtils.js b/src/utils/CameraUtils.js index 0603c45..dc17fa4 100644 --- a/src/utils/CameraUtils.js +++ b/src/utils/CameraUtils.js @@ -2,13 +2,29 @@ import * as onvif from "onvif"; const Cam = onvif.Cam; +const cameraLock = {}; + export class CameraUtils { - constructor() { } + constructor() {} + + static lockCamera = (hostname) => { + cameraLock[hostname] = true; + }; + + static unlockCamera = (hostname) => { + cameraLock[hostname] = false; + }; static gotoPreset = async ({ camParams, preset }) => new Promise((resolve, reject) => { new Cam(camParams, function (err) { - if (err) return reject(err); + if (err) { + return reject(err); + } + + if (cameraLock[camParams.hostname]) { + return reject({ error: "Camera is locked" }); + } this.gotoPreset({ preset }, (data) => resolve(data)); }); @@ -42,7 +58,14 @@ export class CameraUtils { static absoluteMove = async ({ camParams, x, y, zoom }) => new Promise((resolve, reject) => { new Cam(camParams, function (err) { - if (err) return reject(err); + if (err) { + return reject(err); + } + + if (cameraLock[camParams.hostname]) { + return reject({ error: "Camera is locked" }); + } + try { const result = this.absoluteMove({ x, y, zoom }); resolve(result); @@ -55,7 +78,14 @@ export class CameraUtils { static relativeMove = async ({ camParams, x, y, zoom }) => new Promise((resolve, reject) => { new Cam(camParams, function (err) { - if (err) return reject(err); + if (err) { + return reject(err); + } + + if (cameraLock[camParams.hostname]) { + return reject({ error: "Camera is locked" }); + } + try { const result = this.relativeMove({ x, y, zoom }); resolve(result); @@ -70,7 +100,7 @@ export class CameraUtils { new Cam(camParams, function (err) { if (err) return reject(err); try { - const result = this.setPreset({ presetName }, () => { }); + const result = this.setPreset({ presetName }, () => {}); resolve(result); } catch (error) { reject(error); @@ -92,8 +122,4 @@ export class CameraUtils { } }); }); - - - - } From 8b11caccfc5802b14be85b389294821bdc5f648c Mon Sep 17 00:00:00 2001 From: khavinshankar Date: Tue, 3 Oct 2023 14:11:50 +0530 Subject: [PATCH 2/2] unlock camera on error --- src/automation/autoDataExtractor.js | 95 ++++++++++++++++------------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/src/automation/autoDataExtractor.js b/src/automation/autoDataExtractor.js index 95a9d31..be4234e 100644 --- a/src/automation/autoDataExtractor.js +++ b/src/automation/autoDataExtractor.js @@ -72,50 +72,63 @@ const getSanitizedData = (data) => { }; const extractData = async (camParams, monitorPreset = { x: 0, y: 0, z: 0 }) => { - console.log("Moving to coordinates: ", monitorPreset); - await CameraUtils.absoluteMove({ camParams, ...monitorPreset }); - - CameraUtils.lockCamera(cameraParams.hostname); - - // TODO: replace timeout with a better solution - await new Promise((resolve) => - setTimeout(resolve, waitBeforeOCRCapture * 1000) - ); - - const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams }); + try { + console.log("Moving to coordinates: ", monitorPreset); + await CameraUtils.absoluteMove({ camParams, ...monitorPreset }); + + CameraUtils.lockCamera(cameraParams.hostname); + + // TODO: replace timeout with a better solution + await new Promise((resolve) => + setTimeout(resolve, waitBeforeOCRCapture * 1000) + ); + + const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams }); + + CameraUtils.unlockCamera(cameraParams.hostname); + + const fileName = "image-" + new Date().getTime() + ".jpeg"; + const imagePath = path.resolve("images", fileName); + await downloadImage( + snapshotUrl.uri, + imagePath, + camParams.username, + camParams.password + ); + // const testImg = path.resolve("images", "test.png") + + // POST request with image to ocr + const bodyFormData = new FormData(); + bodyFormData.append("image", fs.createReadStream(imagePath)); + + const response = await axios.post(OCR_URL, bodyFormData, { + headers: { + ...bodyFormData.getHeaders(), + }, + }); - CameraUtils.unlockCamera(cameraParams.hostname); + if (!saveOCRImages) { + fs.unlink(imagePath, (err) => { + if (err) { + // TODO: Critical logger setup + console.error(err); + } + }); + } - const fileName = "image-" + new Date().getTime() + ".jpeg"; - const imagePath = path.resolve("images", fileName); - await downloadImage( - snapshotUrl.uri, - imagePath, - camParams.username, - camParams.password - ); - // const testImg = path.resolve("images", "test.png") - - // POST request with image to ocr - const bodyFormData = new FormData(); - bodyFormData.append("image", fs.createReadStream(imagePath)); - - const response = await axios.post(OCR_URL, bodyFormData, { - headers: { - ...bodyFormData.getHeaders(), - }, - }); - - if (!saveOCRImages) { - fs.unlink(imagePath, (err) => { - if (err) { - // TODO: Critical logger setup - console.error(err); - } - }); + return getSanitizedData(response.data.data); + } catch (err) { + console.log("Error in extractData: ", err); + CameraUtils.unlockCamera(cameraParams.hostname); + return { + spo2: null, + ventilator_spo2: null, + resp: null, + pulse: null, + temperature: null, + bp: null, + }; } - - return getSanitizedData(response.data.data); }; const _getCamParams = (params) => {