feat(tag reading): more tag reading updates, with more contorl now

station 1 and 2 are still pending but didnt have enough tags at the office trial it
This commit is contained in:
2025-03-16 15:37:40 -05:00
parent 1a79a97929
commit cb59f58926
12 changed files with 277 additions and 107 deletions

View File

@@ -2,12 +2,8 @@
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
import {readTags} from "../controller/readTags.js";
import {createLog} from "../../logger/logger.js";
// Define the response schema
const responseSchema = z.object({
success: z.boolean().openapi({example: true}),
message: z.string().optional(),
});
import {responses} from "../../../globalUtils/routeDefs/responses.js";
import {newHeartBeat} from "../controller/readerControl.js";
const app = new OpenAPIHono();
let lastGpiTimestamp = 0;
@@ -28,64 +24,37 @@ const ParamsSchema = z.object({
app.openapi(
createRoute({
tags: ["rfid"],
summary: "Adds a new module",
summary: "Post info from the reader",
method: "post",
path: "/mgtevents/{reader}",
request: {
params: ParamsSchema,
},
responses: {
200: {
content: {
"application/json": {schema: responseSchema},
},
description: "Response message",
},
400: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
},
},
description: "Internal Server Error",
},
401: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Unauthenticated"})}),
},
},
description: "Unauthorized",
},
500: {
content: {
"application/json": {
schema: z.object({message: z.string().optional().openapi({example: "Internal Server error"})}),
},
},
description: "Internal Server Error",
},
},
responses: responses(),
}),
async (c) => {
const {reader} = c.req.valid("param");
const body = await c.req.json();
if (body.type === "heartbeat") {
console.log("Heartbeat");
const heart = await newHeartBeat(reader);
return c.json({success: heart.success, message: heart.message}, 200);
}
if (body.type === "gpi" && body.data.state === "HIGH") {
const eventTimestamp = new Date(body.timestamp).getTime(); // Convert ISO timestamp to milliseconds
if (eventTimestamp - lastGpiTimestamp > 10) {
if (eventTimestamp - lastGpiTimestamp > 5 * 1000) {
// Check if it's been more than 2ms
lastGpiTimestamp = eventTimestamp; // Update last seen timestamp
createLog("info", "rfid", "rfid", `${reader} is reading a tag.`);
readTags(reader);
await readTags(reader);
} else {
console.log("Duplicate GPI event ignored.");
createLog("info", "rfid", "rfid", `A new trigger from ${reader} was to soon`);
lastGpiTimestamp = eventTimestamp;
}
//console.log(body);
}
return c.json({success: true, message: `New info from ${reader}`}, 200);