96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
|
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(),
|
|
});
|
|
|
|
const app = new OpenAPIHono();
|
|
let lastGpiTimestamp = 0;
|
|
|
|
const ParamsSchema = z.object({
|
|
reader: z
|
|
.string()
|
|
.min(3)
|
|
.openapi({
|
|
param: {
|
|
name: "id",
|
|
in: "path",
|
|
},
|
|
example: "1212121",
|
|
}),
|
|
});
|
|
|
|
app.openapi(
|
|
createRoute({
|
|
tags: ["server"],
|
|
summary: "Adds a new module",
|
|
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",
|
|
},
|
|
},
|
|
}),
|
|
async (c) => {
|
|
const {reader} = c.req.valid("param");
|
|
const body = await c.req.json();
|
|
|
|
if (body.type === "heartbeat") {
|
|
console.log("Heartbeat");
|
|
}
|
|
|
|
if (body.type === "gpi" && body.data.state === "HIGH") {
|
|
const eventTimestamp = new Date(body.timestamp).getTime(); // Convert ISO timestamp to milliseconds
|
|
|
|
if (eventTimestamp - lastGpiTimestamp > 10) {
|
|
// 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);
|
|
} else {
|
|
console.log("Duplicate GPI event ignored.");
|
|
}
|
|
}
|
|
|
|
return c.json({success: true, message: `New info from ${reader}`}, 200);
|
|
}
|
|
);
|
|
|
|
export default app;
|