Skip to content

Commit

Permalink
Merge pull request #81 from coronasafe/develop
Browse files Browse the repository at this point in the history
Production Release
  • Loading branch information
mathew-alex authored Jul 30, 2023
2 parents 82d172b + b0b1703 commit a419708
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/Validators/cameraValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ export const baseGetCameraParamsValidators = [
.withMessage("port must be integer."),
];

export const camerasStatusBodyValidators = [
body().isArray().withMessage("body must be a valid array."),
body("*.hostname")
.exists({ checkFalsy: true })
.withMessage("hostname is required.")
.isString()
.withMessage("hostname must be string."),
body("*.username")
.exists({ checkFalsy: true })
.withMessage("username is required.")
.isString()
.withMessage("username must be string."),
body("*.password")
.exists({ checkFalsy: true })
.withMessage("password is required.")
.isString()
.withMessage("password must be string."),
body("*.port")
.exists({ checkFalsy: true })
.withMessage("port is required.")
.isInt()
.withMessage("port must be integer."),
];

export const gotoPresetValidator = [
...baseCameraParamsValidators,
body("preset")
Expand Down
97 changes: 97 additions & 0 deletions src/controller/CameraController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
import { CameraUtils } from "../utils/CameraUtils.js";
import { catchAsync } from "../utils/catchAsync.js";

var assets = [];
var statuses = [];
var fetchStatusesInterval = null;

const filterStatus = () => {
const MIN_IN_MS = 60000;
statuses = statuses.filter(
(status) => new Date() - new Date(status.time) <= 30 * MIN_IN_MS
);
};

const fetchCameraStatuses = async () => {
filterStatus();

const cameraStatuses = await Promise.all(
assets.map(async (camera) => {
try {
const camParams = CameraController._getCamParams(camera);
const status = await CameraUtils.getStatus({ camParams });

return {
deviceId: camera.hostname,
status: status?.error === "NO error" ? "up" : "down",
};
} catch (error) {
console.error(error);

return {
deviceId: camera.hostname,
status: "down",
};
}
})
);

statuses.push({
time: new Date().toISOString(),
status: cameraStatuses.reduce((acc, curr) => {
acc[curr.deviceId] = curr.status;
return acc;
}, {}),
});
};

export class CameraController {
static _getCamParams = (body) => {
const { hostname, username, password, port } = body;
Expand Down Expand Up @@ -140,6 +184,59 @@ export class CameraController {
res.send(status);
});

/**
* @swagger
* /cameras/status:
* post:
* summary: "Get status of cameras"
* tags:
* - status
* requestBody:
* content:
* application/json:
* schema:
* type: array
* items:
* type: object
* properties:
* hostname:
* type: string
* description: Device Id or device IP address
* port:
* type: number
* enum: [80, 443]
* username:
* type: string
* password:
* type: string
* responses:
* "200":
* description: Return camera statuses
* content:
* application/json:
* schema:
* type: object
* properties:
* time:
* type: string
* format: date-time
* status:
* type: object
* properties:
* device_id:
* type: string
* enum: [up, down]
*/
static getCameraStatuses = catchAsync(async (req, res) => {
assets = req.body;
await fetchCameraStatuses();

clearInterval(fetchStatusesInterval);
fetchStatusesInterval = setInterval(fetchCameraStatuses, 60 * 1000);

return res.json(statuses);
});

/**
* @swagger
* /absoluteMove:
Expand Down
23 changes: 20 additions & 3 deletions src/controller/ObservationController.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,13 @@ const updateObservationsToCare = async () => {
const filterStatusData = () => {
const MIN_IN_MS = 60000;
statusData = statusData.filter(
(status) => new Date() - status.time <= 30 * MIN_IN_MS
(status) => new Date() - new Date(status.time) <= 30 * MIN_IN_MS
);
};

const parseDataAsStatus = (data) => {
return {
time: new Date(),
time: new Date(new Date().setSeconds(0, 0)).toISOString(),

status: data.reduce((acc, device_observations) => {
device_observations.forEach((observation) => {
Expand All @@ -396,7 +396,24 @@ const parseDataAsStatus = (data) => {
const addStatusData = (data) => {
filterStatusData();

statusData.push(parseDataAsStatus(data));
const newStatus = parseDataAsStatus(data);

const index = statusData.findIndex(
(status) => status.time === newStatus.time
);

if (index === -1) {
statusData.push(newStatus);
return;
}

statusData[index] = {
time: newStatus.time,
status: {
...statusData[index].status,
...newStatus.status,
},
};
};

export class ObservationController {
Expand Down
7 changes: 7 additions & 0 deletions src/router/cameraRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { CameraController } from "../controller/CameraController.js";
import { validate } from "../middleware/validate.js";
import {
baseCameraParamsValidators,
camerasStatusBodyValidators,
setPresetValidators,
baseGetCameraParamsValidators,
camMoveValidator,
Expand All @@ -31,6 +32,12 @@ router.get(
CameraController.getStatus
);

router.post(
"/cameras/status",
validate(camerasStatusBodyValidators),
CameraController.getCameraStatuses
);

router.post(
"/gotoPreset",
validate(gotoPresetValidator),
Expand Down

0 comments on commit a419708

Please sign in to comment.