test(rfid): intial trials built

This commit is contained in:
2025-03-13 21:37:04 -05:00
parent 0054c8f7d4
commit da04e9d35d
15 changed files with 386 additions and 1 deletions

View File

@@ -0,0 +1,51 @@
import axios from "axios";
import {createLog} from "../../logger/logger.js";
export const readTags = async (reader: string) => {
/**
* Start the read for x seconds then auto stop it
*/
let token: string;
const readers = [{reader: "reader1", readerIP: "10.10.1.222", lastHeartBeat: new Date()}];
// get the auth token
const ip = readers.find((r) => r.reader === reader)?.readerIP;
try {
const res = await axios.get(`https://${ip}/cloud/localRestLogin`, {
headers: {Authorization: `Basic ${btoa("admin:Zebra123!")}`},
});
token = res.data.message;
// start the read
try {
const res = await axios.put(
`https://${ip}/cloud/start`,
{},
{
headers: {Authorization: `Bearer ${token}`},
}
);
// stop after 5 seconds
try {
const res = await axios.put(
`https://${ip}/cloud/stop`,
{},
{
headers: {Authorization: `Bearer ${token}`},
}
);
} catch (error) {
createLog("error", "rfid", "rfid", `There was an error Stopping the read ${error}`);
}
} catch (error) {
createLog("error", "rfid", "rfid", `There was an error Starting the read ${error}`);
}
} catch (error) {
createLog("error", "rfid", "rfid", `There was an error Getting the token the read ${error}`);
}
// start the read
};

View File

@@ -0,0 +1,3 @@
/**
* we will monitor shipped out pallets every hour if they get shipped out
*/

View File

@@ -0,0 +1,3 @@
/**
* station 1 will just check for 10 tags
*/

View File

@@ -0,0 +1,4 @@
/**
* we will have 3 reader points here station2.1 will require 10 tags, 2.2 and 2.3 will require 1
*
*/

View File

@@ -0,0 +1,4 @@
/**
* Phase 1 we link the tag to the line only the line3.x where x is the line number
* Phase 2 we will generate a label to be reprinted at staion 4
*/

View File

@@ -0,0 +1,4 @@
/**
* Phase 1 we will just follow the logic of printing a label when we are told requested to based on this tag.
* Phase 2 we will just reprint the tag that was generated at the line
*/

View File

@@ -0,0 +1,48 @@
type TagData = {tagHex: string; reader: string; tag: string; timeStamp: Date};
export const tagData = async (data: TagData[]) => {
/**
* We will always update a tag
*/
// dyco wall makes sure we have 10 tags
const station1 = data.some((n) => n.reader.includes("reader1"));
// by the dyco infeed
const station2 = data.some((n) => n.reader.includes("station2"));
// at the end of each line
const station3 = data.some((n) => n.reader.includes("line3"));
// at the wrapper
const station4 = data.some((n) => n.reader.includes("wrapper"));
// station checks
if (station1 && data.length != 10) {
console.log(`There are only ${data.length}`);
}
if (station2) {
console.log(`There are only ${data.length}`);
}
if (station3) {
// make sure we only have one tag or dont update
if (data.length != 1) {
console.log(`There are ${data.length} tags, and ${data[0].reader} only allows 1 tag to create a label.`);
//throw new Error("There are more than 1 tag at this station and it is not allowed");
} else {
console.log("Generate the tag and link it to the tag.");
}
}
if (station4) {
if (data.length != 1) {
console.log(
`There are ${data.length} tags and this ${data[0].reader} only allows 1 tag to create a label.`
);
//throw new Error("There are more than 1 tag at this station and it is not allowed");
} else {
console.log("reprint the label linked to the tag.");
}
}
};

View File

@@ -0,0 +1,18 @@
import {OpenAPIHono} from "@hono/zod-openapi";
import mgtEvents from "./route/mgtEvents.js";
import tagInfo from "./route/tagInfo.js";
const app = new OpenAPIHono();
const routes = [
mgtEvents,
tagInfo,
// settings
] as const;
// app.route("/server", modules);
const appRoutes = routes.forEach((route) => {
app.route("/rfid", route);
});
export default app;

View 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;

View 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;

View File

@@ -8,7 +8,13 @@ import fs from "fs";
export const serversCheckPoint = async () => {
let servers: any;
fs.readFile("./data.json", "utf8", (err, data) => {
let filePath: string;
if (process.env.NODE_ENV === "development") {
filePath = "./server/services/server/utils/serverData.json";
} else {
filePath = "./dist/server/services/server/utils/serverData.json";
}
fs.readFile(filePath, "utf8", (err, data) => {
if (err) {
console.error("Error reading JSON file:", err);
return;

View File

@@ -18,6 +18,8 @@ export const initializeProdPool = async () => {
return {success: false, message: "The server is not installed."};
}
return;
// make sure the server is not set to localhost this will prevent some weird issues later but can be localhost on the dev
const serverLoc = await db.select().from(settings).where(eq(settings.name, "dbServer"));
if (serverLoc[0].value === "localhost" && process.env.NODE_ENV !== "development") {
@@ -44,6 +46,7 @@ export const initializeProdPool = async () => {
};
export const closePool = async () => {
return;
try {
await pool.close();
createLog("info", "lst", "sqlProd", "Connection pool closed");