Skip to content

Commit

Permalink
added a route a get all camera statuses (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
khavinshankar authored Jul 28, 2023
1 parent 3c23f0e commit 9f9926d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 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
67 changes: 67 additions & 0 deletions src/controller/CameraController.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,73 @@ 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) => {
const cameras = req.body;

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

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

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

/**
* @swagger
* /absoluteMove:
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 9f9926d

Please sign in to comment.