From 21c374903b5cba56ced44b7eb2066ee902fc7bc3 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 17 Mar 2025 08:09:00 -0500 Subject: [PATCH] test(rfid): more work on the rfid service --- .../rfid/controller/stations/wrappers.ts | 3 ++ .../rfid/controller/tags/manualTag.ts | 36 +++++++++++++++++ server/services/rfid/route/manualTagEnter.ts | 39 +++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 server/services/rfid/controller/tags/manualTag.ts create mode 100644 server/services/rfid/route/manualTagEnter.ts diff --git a/server/services/rfid/controller/stations/wrappers.ts b/server/services/rfid/controller/stations/wrappers.ts index d4ad35e..08a8fba 100644 --- a/server/services/rfid/controller/stations/wrappers.ts +++ b/server/services/rfid/controller/stations/wrappers.ts @@ -31,6 +31,9 @@ export const wrapperStuff = async (tagData: TagData[]) => { "rfid", `${tagdata.tag}, Did not come from a line please check the pallet and manually print the label.` ); + + // when we manually run again we want to make sure we read from the 3rd antenna this way we do not get the wrong info. + // more testing will need to be done on this. } // check if a running number exists diff --git a/server/services/rfid/controller/tags/manualTag.ts b/server/services/rfid/controller/tags/manualTag.ts new file mode 100644 index 0000000..ff34ddd --- /dev/null +++ b/server/services/rfid/controller/tags/manualTag.ts @@ -0,0 +1,36 @@ +import {db} from "../../../../../database/dbclient.js"; +import {rfidTags} from "../../../../../database/schema/rfidTags.js"; +import {createLog} from "../../../logger/logger.js"; + +export const manualTag = async (tag: string, area: string) => { + /** + * we only allow tags to be manually added from the wrappers + */ + // create the proper string + const number = tag.toString().padStart(9, "0"); + const tagString = `ALPLA${number}`; + const newTag = { + tagHex: Buffer.from(tagString, "utf8").toString("hex").toUpperCase(), + tag: tagString, + lastRead: new Date(Date.now()), + counts: [{area: area, timesHere: 1}], //jsonb("counts").notNull(), //.default([{area: 1, timesHere: 5}]).notNull(), + lastareaIn: area, + antenna: 0, + tagStrength: -1, + }; + try { + // insert the tag with the onConflict update the tag + const tag = await db.insert(rfidTags).values(newTag).returning({ + tag: rfidTags.tag, + runningNumber: rfidTags.runningNumber, + counts: rfidTags.counts, + lastareaIn: rfidTags.lastareaIn, + }); + createLog("info", "rfid", "rfid", `Tags were just added, and label printed.`); + // do the label thing here + return {success: true, message: `${tagString} was just added, and label printed.`, data: tag}; + } catch (error) { + createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`); + return {success: false, message: `Error creating a new tag`, data: error}; + } +}; diff --git a/server/services/rfid/route/manualTagEnter.ts b/server/services/rfid/route/manualTagEnter.ts new file mode 100644 index 0000000..b11dc15 --- /dev/null +++ b/server/services/rfid/route/manualTagEnter.ts @@ -0,0 +1,39 @@ +//http://usday1vms006:4000/api/v1/zebra/wrapper1 +import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi"; +import {responses} from "../../../globalUtils/routeDefs/responses.js"; +import {authMiddleware} from "../../auth/middleware/authMiddleware.js"; +import {manualTag} from "../controller/tags/manualTag.js"; + +const app = new OpenAPIHono(); + +const RequestBody = z.object({ + tagNumber: z.string().openapi({example: "2541"}), +}); + +app.openapi( + createRoute({ + tags: ["rfid"], + summary: "Post a manual Tag", + method: "post", + path: "/manualtag", + middleware: authMiddleware, + request: { + body: {content: {"application/json": {schema: RequestBody}}}, + }, + responses: responses(), + }), + async (c) => { + const body = + (await c.req.json()) ?? + c.json({success: false, message: `Please check that you are sending over a json object`}, 400); + + try { + const tag = await manualTag(body.tag, body.area); + return c.json({success: tag.success, message: tag.message, data: tag.data}, 200); + } catch (error) { + return c.json({success: true, message: `There was an error with entering a new tag`, error}, 400); + } + } +); + +export default app;