Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock camera movements when ocr is taking an image #103

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 57 additions & 43 deletions src/automation/autoDataExtractor.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -74,46 +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 });

// TODO: replace timeout with a better solution
await new Promise((resolve) =>
setTimeout(resolve, waitBeforeOCRCapture * 1000)
);
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(),
},
});

const snapshotUrl = await CameraUtils.getSnapshotUri({ camParams });
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) => {
Expand Down Expand Up @@ -149,4 +164,3 @@ export const updateObservationAuto = async (cameraParams, monitorPreset) => {
};
}
};

44 changes: 35 additions & 9 deletions src/utils/CameraUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -92,8 +122,4 @@ export class CameraUtils {
}
});
});




}
Loading