test(rfid): intial trials built
This commit is contained in:
95
server/services/rfid/route/mgtEvents.ts
Normal file
95
server/services/rfid/route/mgtEvents.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
//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;
|
||||
91
server/services/rfid/route/tagInfo.ts
Normal file
91
server/services/rfid/route/tagInfo.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {ConsoleLogWriter} from "drizzle-orm";
|
||||
import {tagData} from "../controller/tagData.js";
|
||||
|
||||
// Define the response schema
|
||||
const responseSchema = z.object({
|
||||
success: z.boolean().openapi({example: true}),
|
||||
message: z.string().optional(),
|
||||
});
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
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: "/taginfo/{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");
|
||||
let tagdata: any = [];
|
||||
const body = await c.req.json();
|
||||
//console.log(`Tag: ${Buffer.from(body.idHex, "hex").toString("utf-8")}, ${body[key].data.idHex}`);
|
||||
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const tag = Buffer.from(body[i].data.idHex, "hex").toString("utf-8");
|
||||
if (tag.includes("ALPLA")) {
|
||||
tagdata = [
|
||||
...tagdata,
|
||||
{tagHex: body[i].data.idHex, reader: reader, tag: tag, timeStamp: body[i].timestamp},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
tagData(tagdata);
|
||||
|
||||
return c.json({success: true, message: `New info from ${reader}`}, 200);
|
||||
}
|
||||
);
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user