implement always locked time
This commit is contained in:
parent
4c6399f0c3
commit
8e73fd012c
17
packages/server/src/middlewares/TimeLockMiddleware.ts
Normal file
17
packages/server/src/middlewares/TimeLockMiddleware.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getDoorSettingTimeLock } from "../util/EnvConfigUtil";
|
||||
import { IDoorStatus } from "../types/IDoorStatus";
|
||||
|
||||
export const TimeLockVerify: RequestHandler = async (req, res, next) => {
|
||||
const timeLock = getDoorSettingTimeLock(req.params.id);
|
||||
|
||||
const timeHr = (new Date()).getHours();
|
||||
|
||||
|
||||
if (timeHr >= timeLock[0] || timeHr <= timeLock[1]) {
|
||||
res.status(401).json({ status: IDoorStatus.TIME_LOCK, msg: 'Sorry! This door is locked at this hour, try again later' });
|
||||
return;
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
@ -4,16 +4,18 @@ import { doorStatusKey } from "../types/RedisKeys";
|
||||
import { HandleAuthMode } from "../middlewares/DoorAuthModes";
|
||||
import { getDoorSettingNumber, getDoorSettingString } from "../util/EnvConfigUtil";
|
||||
import { IDoorConfig } from "../types/IDoorConfig";
|
||||
import { TimeLockVerify } from "../middlewares/TimeLockMiddleware";
|
||||
import { IDoorStatus } from "../types/IDoorStatus";
|
||||
|
||||
const router = express.Router();
|
||||
const client = await getRedisClient();
|
||||
|
||||
router.get('/:id/status', async(req, res) => {
|
||||
router.get('/:id/status', TimeLockVerify, async(req, res) => {
|
||||
const isOpen = await client.get(doorStatusKey(req.params.id));
|
||||
|
||||
if (isOpen) {
|
||||
const fingerprint = JSON.parse(isOpen);
|
||||
res.status(200).json({ status: 'open', fingerprint });
|
||||
res.status(200).json({ status: IDoorStatus.OPEN, fingerprint });
|
||||
|
||||
if (getDoorSettingString(req.params.id, IDoorConfig.CLOSE_AFTER_POLL)) {
|
||||
await client.remove(doorStatusKey(req.params.id));
|
||||
@ -21,7 +23,7 @@ router.get('/:id/status', async(req, res) => {
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(401).json({ status: 'closed' });
|
||||
res.status(401).json({ status: IDoorStatus.CLOSED });
|
||||
});
|
||||
|
||||
router.delete('/:id/status', async(req, res) => {
|
||||
|
||||
@ -3,4 +3,5 @@ export enum IDoorConfig {
|
||||
OPEN_TIMEOUT = "OPEN_TIMEOUT",
|
||||
FIXED_PIN = "FIXED_PIN",
|
||||
CLOSE_AFTER_POLL="CLOSE_AFTER_POLL",
|
||||
ALWAYS_LOCKED_TIME="ALWAYS_LOCKED_TIME",
|
||||
}
|
||||
5
packages/server/src/types/IDoorStatus.ts
Normal file
5
packages/server/src/types/IDoorStatus.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export enum IDoorStatus {
|
||||
OPEN="OPEN",
|
||||
CLOSED="CLOSED",
|
||||
TIME_LOCK="TIME_LOCK"
|
||||
}
|
||||
@ -21,4 +21,19 @@ export const getDoorSettingString = (door: string, setting: IDoorConfig): string
|
||||
|
||||
export const getDoorSettingNumber = (door: string, setting: IDoorConfig): number => {
|
||||
return parseInt(getDoorSettingString(door, setting) || "0");
|
||||
};
|
||||
};
|
||||
|
||||
export const getDoorSettingTimeLock = (door: string): number[] => {
|
||||
const config = getDoorSettingString(door, IDoorConfig.ALWAYS_LOCKED_TIME);
|
||||
|
||||
if (config) {
|
||||
try {
|
||||
return config.split(',').map(n => parseInt(n));
|
||||
} catch (e) {
|
||||
console.warn(`Config ${IDoorConfig.ALWAYS_LOCKED_TIME} for door ${door} is invalid`);
|
||||
}
|
||||
}
|
||||
|
||||
// never locked (always -1 < hr < 25)
|
||||
return [25, -1];
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user