test(rfid): intial trials built
This commit is contained in:
51
server/services/rfid/controller/readTags.ts
Normal file
51
server/services/rfid/controller/readTags.ts
Normal 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
|
||||
};
|
||||
3
server/services/rfid/controller/shippedOutTags.ts
Normal file
3
server/services/rfid/controller/shippedOutTags.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* we will monitor shipped out pallets every hour if they get shipped out
|
||||
*/
|
||||
3
server/services/rfid/controller/station1.ts
Normal file
3
server/services/rfid/controller/station1.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* station 1 will just check for 10 tags
|
||||
*/
|
||||
4
server/services/rfid/controller/station2.ts
Normal file
4
server/services/rfid/controller/station2.ts
Normal 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
|
||||
*
|
||||
*/
|
||||
4
server/services/rfid/controller/station3.ts
Normal file
4
server/services/rfid/controller/station3.ts
Normal 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
|
||||
*/
|
||||
4
server/services/rfid/controller/station4.ts
Normal file
4
server/services/rfid/controller/station4.ts
Normal 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
|
||||
*/
|
||||
48
server/services/rfid/controller/tagData.ts
Normal file
48
server/services/rfid/controller/tagData.ts
Normal 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.");
|
||||
}
|
||||
}
|
||||
};
|
||||
18
server/services/rfid/rfidService.ts
Normal file
18
server/services/rfid/rfidService.ts
Normal 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;
|
||||
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