diff --git a/server/services/rfid/controller/noRead.ts b/server/services/rfid/controller/noRead.ts index 08c8734..db286c5 100644 --- a/server/services/rfid/controller/noRead.ts +++ b/server/services/rfid/controller/noRead.ts @@ -2,6 +2,13 @@ * For a no read we just want to put up a notification to the rfid dashboard stating this reader did not respond with any tag data. */ +import { createLog } from "../../logger/logger.js"; + export const noRead = async (reader: string) => { - console.log(`${reader} just had a no read please check for a tag and manually trigger a read.`); + createLog( + "error", + "rfid", + "rifd", + `${reader} just had a no read please check for a tag and manually trigger a read.` + ); }; diff --git a/server/services/rfid/controller/readTags.ts b/server/services/rfid/controller/readTags.ts index 8f45ee8..b0003dc 100644 --- a/server/services/rfid/controller/readTags.ts +++ b/server/services/rfid/controller/readTags.ts @@ -29,15 +29,18 @@ export const readTags = async (reader: string) => { // get the auth token ip = readers.find((r) => r.reader === reader)?.readerIP as string; + // start the read + startRead(); + + // start the read +}; +const getReaderToken = async () => { try { const res = await axios.get(`https://${ip}/cloud/localRestLogin`, { headers: { Authorization: `Basic ${authData}` }, }); token = res.data.message; - // start the read - startRead(); } catch (error: any) { - console.log(error); createLog( "error", "rfid", @@ -45,11 +48,10 @@ export const readTags = async (reader: string) => { `There was an error Getting the token the read: ${error.response?.data.message}` ); } - - // start the read }; const startRead = async () => { + await getReaderToken(); try { const res = await axios.put( `https://${ip}/cloud/start`, @@ -64,12 +66,12 @@ const startRead = async () => { if (res.status === 200) { setTimeout(() => { stopRead(); - }, 5 * 1000); + }, 2 * 1000); } // stop after 5 seconds } catch (error: any) { - if (error.response.data.code === 3) { + if (error.response?.data.code === 3) { stopRead(); setTimeout(() => { startRead(); @@ -79,11 +81,13 @@ const startRead = async () => { "error", "rfid", "rfid", - `There was an error Starting the read: ${error.response.data.message}` + `There was an error Starting the read: ${error.response?.data.message}` ); } }; -const stopRead = async () => { + +export const stopRead = async () => { + await getReaderToken(); try { const res = await axios.put( `https://${ip}/cloud/stop`, @@ -97,7 +101,7 @@ const stopRead = async () => { "error", "rfid", "rfid", - `There was an error Stopping the read: ${error.response.data.message}` + `There was an error Stopping the read: ${JSON.stringify(error)}` ); } }; diff --git a/server/services/rfid/controller/readerControl.ts b/server/services/rfid/controller/readerControl.ts index 7ecb1f4..b91c04e 100644 --- a/server/services/rfid/controller/readerControl.ts +++ b/server/services/rfid/controller/readerControl.ts @@ -2,10 +2,10 @@ * While in production we will monitor the readers if we have not gotten a heartbeat in the last 5 min we will send a reboot command along with an email. */ -import {eq, sql} from "drizzle-orm"; -import {db} from "../../../../database/dbclient.js"; -import {rfidReaders} from "../../../../database/schema/rfidReaders.js"; -import {createLog} from "../../logger/logger.js"; +import { eq, sql } from "drizzle-orm"; +import { db } from "../../../../database/dbclient.js"; +import { rfidReaders } from "../../../../database/schema/rfidReaders.js"; +import { createLog } from "../../logger/logger.js"; export const newHeartBeat = async (reader: string) => { /** @@ -14,14 +14,30 @@ export const newHeartBeat = async (reader: string) => { try { const heatBeat = await db - .update(rfidReaders) - .set({lastHeartBeat: sql`NOW()`}) - .where(eq(rfidReaders.reader, reader)); - createLog("info", "rfid", "rfid", `${reader} just updated its heatBeat.`); - return {success: true, message: `${reader} just updated its heatBeat.`}; + .update(rfidReaders) + .set({ lastHeartBeat: sql`NOW()` }) + .where(eq(rfidReaders.reader, reader)); + createLog( + "debug", + "rfid", + "rfid", + `${reader} just updated its heatBeat.` + ); + return { + success: true, + message: `${reader} just updated its heatBeat.`, + }; } catch (error) { - createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`); - return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`}; + createLog( + "error", + "rfid", + "rfid", + `${reader} encountered an error while updating the heatbeat, ${error}` + ); + return { + success: false, + message: `${reader} encountered an error while updating the heatbeat, ${error}`, + }; } }; @@ -31,14 +47,30 @@ export const badRead = async (reader: string) => { */ try { const badRead = await db - .update(rfidReaders) - .set({lastTrigger: sql`NOW()`, lastTriggerGood: false}) - .where(eq(rfidReaders.reader, reader)); - createLog("info", "rfid", "rfid", `${reader} just Triggered a bad read.`); - return {success: true, message: `${reader} just Triggered a bad read.`}; + .update(rfidReaders) + .set({ lastTrigger: sql`NOW()`, lastTriggerGood: false }) + .where(eq(rfidReaders.reader, reader)); + createLog( + "info", + "rfid", + "rfid", + `${reader} just Triggered a bad read.` + ); + return { + success: true, + message: `${reader} just Triggered a bad read.`, + }; } catch (error) { - createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`); - return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`}; + createLog( + "error", + "rfid", + "rfid", + `${reader} encountered an error while updating the heatbeat, ${error}` + ); + return { + success: false, + message: `${reader} encountered an error while updating the heatbeat, ${error}`, + }; } }; @@ -48,13 +80,29 @@ export const goodRead = async (reader: string) => { */ try { const goodRead = await db - .update(rfidReaders) - .set({lastTrigger: sql`NOW()`, lastTriggerGood: true}) - .where(eq(rfidReaders.reader, reader)); - createLog("info", "rfid", "rfid", `${reader} just Triggered a good read.`); - return {success: true, message: `${reader} just Triggered a good read.`}; + .update(rfidReaders) + .set({ lastTrigger: sql`NOW()`, lastTriggerGood: true }) + .where(eq(rfidReaders.reader, reader)); + createLog( + "info", + "rfid", + "rfid", + `${reader} just Triggered a good read.` + ); + return { + success: true, + message: `${reader} just Triggered a good read.`, + }; } catch (error) { - createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`); - return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`}; + createLog( + "error", + "rfid", + "rfid", + `${reader} encountered an error while updating the heatbeat, ${error}` + ); + return { + success: false, + message: `${reader} encountered an error while updating the heatbeat, ${error}`, + }; } }; diff --git a/server/services/rfid/controller/stations/station3.ts b/server/services/rfid/controller/stations/station3.ts index 6e94e83..d86bab0 100644 --- a/server/services/rfid/controller/stations/station3.ts +++ b/server/services/rfid/controller/stations/station3.ts @@ -8,6 +8,12 @@ import type { TagData } from "../tagData.js"; import { tagStuff } from "../tags/crudTag.js"; export const station3Tags = async (tagData: TagData[]) => { + createLog( + "info", + "rfid", + "rfid", + `${tagData[0].reader} has a ${tagData[0].tag} will post it :)` + ); // make sure we only have one tag or dont update if (tagData.length != 1) { createLog( diff --git a/server/services/rfid/controller/stations/wrappers.ts b/server/services/rfid/controller/stations/wrappers.ts index 895bc76..4062f0a 100644 --- a/server/services/rfid/controller/stations/wrappers.ts +++ b/server/services/rfid/controller/stations/wrappers.ts @@ -8,28 +8,14 @@ import { labelingProcess } from "../../../ocp/controller/labeling/labelProcess.j import type { TagData } from "../tagData.js"; import { tagStuff } from "../tags/crudTag.js"; import { sendEmail } from "../../../notifications/controller/sendMail.js"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import { db } from "../../../../../database/dbclient.js"; +import { rfidTags } from "../../../../../database/schema/rfidTags.js"; +import { eq } from "drizzle-orm"; -export const wrapperStuff = async (tagData: TagData[]) => { - if (tagData[0]?.lastareaIn === "NeedsChecked") { - createLog( - "error", - "rfid", - "rfid", - `The tags on this pallet need to be checked as it was flagged as more than 1 tag number. please validate and looks at both sides.` - ); +export const wrapperStuff = async (tagData: any) => { + console.log("WrapperTag", tagData[0].tag); - // just making sure we clear out the running number if one really came over. - for (let i = 0; i < tagData.length; i++) { - const tag = { ...tagData[i], runningNr: 0 }; - tagStuff([tag]); - } - monitorChecks(); - return { - success: false, - message: - "The tags on this pallet need to be checked as it was flagged as more than 1 tag number. please validate and looks at both sides.", - }; - } if (tagData.length != 1) { createLog( "error", @@ -38,39 +24,55 @@ export const wrapperStuff = async (tagData: TagData[]) => { `There are ${tagData.length} tags and this ${tagData[0].reader} only allows 1 tag to create a label.` ); const tag = { ...tagData[0], runningNr: 0 }; - tagStuff([tag]); + //tagStuff([tag]); monitorChecks(); } else { if (!tagData) { createLog("error", "rfid", "rfid", `No tagData was grabbed.`); monitorChecks(); } - const tagdata = await tagStuff(tagData); - /** - * we want to make sure this pallet came from a line as its last spot if not we need to have a manual check. - */ - - const lines = tagdata[0]?.lastareaIn.includes("line3"); - + const { data: tagdata, error: tagError } = await tryCatch( + db.select().from(rfidTags).where(eq(rfidTags.tag, tagData[0].tag)) + ); + if (tagError) { + return { + success: false, + message: `There was an error getting the tag data for ${tagData[0].tag}.`, + }; + } + const checkTag: any = tagdata; + if (checkTag[0]?.lastareaIn === "NeedsChecked") { + createLog( + "error", + "rfid", + "rfid", + `The tags on this pallet need to be checked as it was flagged as more than 1 tag number. please validate and looks at both sides.` + ); + for (let i = 0; i < tagData.length; i++) { + const tag = { ...tagData[i], runningNr: 0 }; + tagStuff([tag]); + } + monitorChecks(); + return { + success: false, + message: + "The tags on this pallet need to be checked as it was flagged as more than 1 tag number. please validate and looks at both sides.", + }; + } + const lines = checkTag[0]?.lastareaIn.includes("line3"); if (!lines) { createLog( "error", "rfid", "rfid", - `${tagdata[0].tag}, Did not come from a line please check the pallet and manually print the label.` + `${tagData[0].tag}, Did not come from a line please check the pallet and manually print the label.` ); monitorChecks(); - return { success: false, - message: `${tagdata[0].tag}, Did not come from a line please check the pallet and manually print the label.`, + message: `${tagData[0].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 if (lines[0].runningNumber) { createLog( "info", @@ -83,12 +85,10 @@ export const wrapperStuff = async (tagData: TagData[]) => { "info", "rfid", "rfid", - `A new label will be created and linked to this ${tagdata[0].tag} tag` + `A new label will be created and linked to this ${tagData[0].tag} tag` ); - - // get the actaul line number from the last area in const lineNum = parseInt( - tagdata[0]?.lastareaIn.repalceAll("line3", "") + checkTag[0]?.lastareaIn.repalceAll("line3", "") ); createLog( "info", @@ -96,17 +96,16 @@ export const wrapperStuff = async (tagData: TagData[]) => { "rfid", `Line to be checked for printing: ${lineNum}` ); - const genlabel = await labelingProcess({ line: lineNum.toString(), }); - if (genlabel?.success) { - // update the tag and add the label into it const createPrintData = { ...tagData[0], runnungNr: parseInt(genlabel.data?.SSCC.slice(10, -1)), }; + + tagStuff([createPrintData]); } } } diff --git a/server/services/rfid/controller/tagData.ts b/server/services/rfid/controller/tagData.ts index f6c5611..b74d400 100644 --- a/server/services/rfid/controller/tagData.ts +++ b/server/services/rfid/controller/tagData.ts @@ -15,6 +15,9 @@ export const tagData = async (data: TagData[]) => { * We will always update a tag */ + if (monitorTagsdata.includes(data[0].tag)) { + } + // dyco wall makes sure we have 10 tags const station1 = data.some((n) => n.reader.includes("reader1")); @@ -44,3 +47,21 @@ export const tagData = async (data: TagData[]) => { wrapperStuff(data); } }; + +const monitorTagsdata: any = []; +const monitorTags = (tag: string) => { + /** + * If we have to check more than 10 tags in an hour send an email to alert everyone. + */ + const now = new Date(Date.now()).getTime(); + monitorTagsdata.push({ timestamp: now, tag: tag }); + + // remove if creater than 1 hour + const removalTiming = now - 2 * 60 * 1000; // 2 min + while ( + monitorTagsdata > 0 && + monitorTagsdata[0].timestamp < removalTiming + ) { + monitorTagsdata.shift(); + } +}; diff --git a/server/services/rfid/controller/tags/manualTag.ts b/server/services/rfid/controller/tags/manualTag.ts index ff34ddd..39daf59 100644 --- a/server/services/rfid/controller/tags/manualTag.ts +++ b/server/services/rfid/controller/tags/manualTag.ts @@ -1,8 +1,12 @@ -import {db} from "../../../../../database/dbclient.js"; -import {rfidTags} from "../../../../../database/schema/rfidTags.js"; -import {createLog} from "../../../logger/logger.js"; +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) => { +export const manualTag = async ( + tag: string, + area: string, + runningNr?: any | null +) => { /** * we only allow tags to be manually added from the wrappers */ @@ -13,24 +17,40 @@ export const manualTag = async (tag: string, area: string) => { 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(), + 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.`); + const tag = await db + .insert(rfidTags) + .values(newTag) + .returning({ + tag: rfidTags.tag, + runningNumber: runningNr ? runningNr : 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}; + 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}; + return { + success: false, + message: `Error creating a new tag`, + data: error, + }; } }; diff --git a/server/services/rfid/route/mgtEvents.ts b/server/services/rfid/route/mgtEvents.ts index 4729615..36f5426 100644 --- a/server/services/rfid/route/mgtEvents.ts +++ b/server/services/rfid/route/mgtEvents.ts @@ -1,24 +1,24 @@ //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"; -import {responses} from "../../../globalUtils/routeDefs/responses.js"; -import {newHeartBeat} from "../controller/readerControl.js"; +import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi"; +import { readTags } from "../controller/readTags.js"; +import { createLog } from "../../logger/logger.js"; +import { responses } from "../../../globalUtils/routeDefs/responses.js"; +import { newHeartBeat } from "../controller/readerControl.js"; const app = new OpenAPIHono(); let lastGpiTimestamp = 0; const ParamsSchema = z.object({ reader: z - .string() - .min(3) - .openapi({ - param: { - name: "reader", - in: "path", - }, - example: "1212121", - }), + .string() + .min(3) + .openapi({ + param: { + name: "reader", + in: "path", + }, + example: "1212121", + }), }); app.openapi( @@ -33,31 +33,47 @@ app.openapi( responses: responses(), }), async (c) => { - const {reader} = c.req.valid("param"); + const { reader } = c.req.valid("param"); const body = await c.req.json(); if (body.type === "heartbeat") { const heart = await newHeartBeat(reader); - return c.json({success: heart.success, message: heart.message}, 200); + return c.json( + { success: heart.success, message: heart.message }, + 200 + ); } if (body.type === "gpi" && body.data.state === "HIGH") { const eventTimestamp = new Date(body.timestamp).getTime(); // Convert ISO timestamp to milliseconds - if (eventTimestamp - lastGpiTimestamp > 5 * 1000) { + if (eventTimestamp - lastGpiTimestamp > 30 * 1000) { // Check if it's been more than 2ms lastGpiTimestamp = eventTimestamp; // Update last seen timestamp - createLog("info", "rfid", "rfid", `${reader} is reading a tag.`); + createLog( + "info", + "rfid", + "rfid", + `${reader} is reading a tag.` + ); await readTags(reader); } else { - createLog("info", "rfid", "rfid", `A new trigger from ${reader} was to soon`); + createLog( + "info", + "rfid", + "rfid", + `A new trigger from ${reader} was to soon` + ); lastGpiTimestamp = eventTimestamp; } //console.log(body); } - return c.json({success: true, message: `New info from ${reader}`}, 200); + return c.json( + { success: true, message: `New info from ${reader}` }, + 200 + ); } ); diff --git a/server/services/rfid/route/tagInfo.ts b/server/services/rfid/route/tagInfo.ts index 0b4404d..dff9640 100644 --- a/server/services/rfid/route/tagInfo.ts +++ b/server/services/rfid/route/tagInfo.ts @@ -1,24 +1,27 @@ //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"; -import {responses} from "../../../globalUtils/routeDefs/responses.js"; -import {noRead} from "../controller/noRead.js"; -import {badRead, goodRead} from "../controller/readerControl.js"; +import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi"; +import { ConsoleLogWriter } from "drizzle-orm"; +import { tagData } from "../controller/tagData.js"; +import { responses } from "../../../globalUtils/routeDefs/responses.js"; +import { noRead } from "../controller/noRead.js"; +import { badRead, goodRead } from "../controller/readerControl.js"; +import { createLog } from "../../logger/logger.js"; +import { tryCatch } from "../../../globalUtils/tryCatch.js"; +import { stopRead } from "../controller/readTags.js"; const app = new OpenAPIHono(); const ParamsSchema = z.object({ reader: z - .string() - .min(3) - .openapi({ - param: { - name: "reader", - in: "path", - }, - example: "1212121", - }), + .string() + .min(3) + .openapi({ + param: { + name: "reader", + in: "path", + }, + example: "1212121", + }), }); app.openapi( @@ -33,15 +36,25 @@ app.openapi( responses: responses(), }), async (c) => { - const {reader} = c.req.valid("param"); + const { reader } = c.req.valid("param"); + createLog("info", "rfid", "rfid", `${reader} is sending us data.`); let tagdata: any = []; - const body = await c.req.json(); + const { data: body, error: bodyError } = await tryCatch(c.req.json()); + + if (bodyError) { + return c.json({ success: false, message: "missing data" }, 400); + } //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"); + const tag = Buffer.from(body[i].data.idHex, "hex").toString( + "utf-8" + ); //console.log("Raw value:", body[i].data.peakRssi, "Parsed:", parseInt(body[i].data.peakRssi)); - if (tag.includes("ALPLA") && parseInt(body[i].data.peakRssi) < -50) { + if ( + tag.includes("ALPLA") && + parseInt(body[i].data.peakRssi) < -50 + ) { tagdata = [ ...tagdata, { @@ -59,11 +72,18 @@ app.openapi( if (tagdata.length === 0) { noRead(reader); badRead(reader); - return c.json({success: false, message: `There were no tags scanned.`}, 200); + return c.json( + { success: false, message: `There were no tags scanned.` }, + 200 + ); } else { tagData(tagdata); goodRead(reader); - return c.json({success: true, message: `New info from ${reader}`}, 200); + + return c.json( + { success: true, message: `New info from ${reader}` }, + 200 + ); } } );