diff --git a/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts b/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts index 4173303..5efeded 100644 --- a/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts +++ b/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts @@ -107,7 +107,7 @@ export const labelerTagRead = async (tagData: any) => { // trigger the reader so we can get the label from the tag readers. setTimeout(async () => { await readTags("wrapper1"); - }, 2 * 1000); + }, 500); // half second delay on this guy } } }; diff --git a/server/services/ocp/controller/specialProcesses/dyco/plcTags/palletSendTag.ts b/server/services/ocp/controller/specialProcesses/dyco/plcTags/palletSendTag.ts index 0ce64a3..367583c 100644 --- a/server/services/ocp/controller/specialProcesses/dyco/plcTags/palletSendTag.ts +++ b/server/services/ocp/controller/specialProcesses/dyco/plcTags/palletSendTag.ts @@ -49,6 +49,8 @@ export const palletSendTag = async (tagData: any) => { Date.now() - tagTime <= 5000 && !tagData.value ) { - await pickedup({ runningNr: 1234, all: true, areaFrom: "wrapper_1" }); + setTimeout(() => { + pickedup({ runningNr: 1234, all: true, areaFrom: "wrapper_1" }); + }, 5000); } }; diff --git a/server/services/rfid/controller/noRead.ts b/server/services/rfid/controller/noRead.ts index db286c5..797348d 100644 --- a/server/services/rfid/controller/noRead.ts +++ b/server/services/rfid/controller/noRead.ts @@ -8,7 +8,22 @@ export const noRead = async (reader: string) => { createLog( "error", "rfid", - "rifd", + "rfid", `${reader} just had a no read please check for a tag and manually trigger a read.` ); }; + +// noReadTimer.js +let timeout: any = null; + +export const startNoReadTimer = (onTimeout: any, ms = 10000) => { + clearNoReadTimer(); // Always clear any previous timer + timeout = setTimeout(onTimeout, ms); +}; + +export const clearNoReadTimer = () => { + if (timeout) { + clearTimeout(timeout); + timeout = null; + } +}; diff --git a/server/services/rfid/controller/readTags.ts b/server/services/rfid/controller/readTags.ts index 9f594a5..91aad1c 100644 --- a/server/services/rfid/controller/readTags.ts +++ b/server/services/rfid/controller/readTags.ts @@ -3,6 +3,8 @@ import { createLog } from "../../logger/logger.js"; import { db } from "../../../../database/dbclient.js"; import { rfidReaders } from "../../../../database/schema/rfidReaders.js"; import { eq } from "drizzle-orm"; +import { noRead, startNoReadTimer } from "./noRead.js"; +import { badRead } from "./readerControl.js"; const authData = btoa("admin:Zebra123!"); let token: string; @@ -63,6 +65,11 @@ const startRead = async (reader: string, ip: string) => { //console.log(res.data); if (res.status === 200) { + // start the no read timer + startNoReadTimer(() => { + noRead(reader); + badRead(reader); + }); setTimeout(() => { stopRead(reader, ip); }, 5 * 1000); diff --git a/server/services/rfid/controller/readerControl.ts b/server/services/rfid/controller/readerControl.ts index b91c04e..4789976 100644 --- a/server/services/rfid/controller/readerControl.ts +++ b/server/services/rfid/controller/readerControl.ts @@ -48,7 +48,11 @@ export const badRead = async (reader: string) => { try { const badRead = await db .update(rfidReaders) - .set({ lastTrigger: sql`NOW()`, lastTriggerGood: false }) + .set({ + lastTrigger: sql`NOW()`, + lastTriggerGood: false, + lastTagScanned: null, + }) .where(eq(rfidReaders.reader, reader)); createLog( "info", diff --git a/server/services/rfid/controller/stations/wrappers.ts b/server/services/rfid/controller/stations/wrappers.ts index e87c765..756e362 100644 --- a/server/services/rfid/controller/stations/wrappers.ts +++ b/server/services/rfid/controller/stations/wrappers.ts @@ -11,12 +11,15 @@ 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"; +import { and, eq, gte, ne, sql } from "drizzle-orm"; import { rfidReaders } from "../../../../../database/schema/rfidReaders.js"; +import { shouldSkipByCooldown } from "../../utils/rateLimit.js"; export const wrapperStuff = async (tagData: any) => { console.log("WrapperTag", tagData[0].tag); + if (shouldSkipByCooldown("wrapperDouble Scan", 10)) return; + // update the reader with the last tag read. const { error } = await tryCatch( db .update(rfidReaders) @@ -53,8 +56,21 @@ export const wrapperStuff = async (tagData: any) => { monitorChecks(); } const { data: tagdata, error: tagError } = await tryCatch( - db.select().from(rfidTags).where(eq(rfidTags.tag, tagData[0].tag)) + db + .select() + .from(rfidTags) + .where( + and( + eq(rfidTags.tag, tagData[0].tag), + gte( + rfidTags.lastRead, + sql.raw(`NOW() - INTERVAL '6 hours'`) + ), + ne(rfidTags.lastareaIn, "wrapper1") + ) + ) ); + console.log("DB Data:", tagdata); if (tagError) { return { success: false, @@ -136,10 +152,12 @@ export const wrapperStuff = async (tagData: any) => { const genlabel = await labelingProcess({ line: lineNum.toString(), }); + + console.log(genlabel); if (genlabel?.success) { const createPrintData = { ...tagData[0], - runnungNr: parseInt(genlabel.data?.SSCC.slice(10, -1)), + runnungNumber: 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 b74d400..6d86ce8 100644 --- a/server/services/rfid/controller/tagData.ts +++ b/server/services/rfid/controller/tagData.ts @@ -9,6 +9,7 @@ export type TagData = { antenna: number; tagStrength: number; lastareaIn?: string; + runningNumber?: number; }; export const tagData = async (data: TagData[]) => { /** diff --git a/server/services/rfid/controller/tags/crudTag.ts b/server/services/rfid/controller/tags/crudTag.ts index 3106db5..882977b 100644 --- a/server/services/rfid/controller/tags/crudTag.ts +++ b/server/services/rfid/controller/tags/crudTag.ts @@ -26,6 +26,9 @@ export const tagStuff = async (tagData: TagData[]): Promise => { lastareaIn: tagData[i].reader, antenna: tagData[i].antenna, tagStrength: tagData[i].tagStrength, + runningNumber: tagData[i].runningNumber + ? tagData[i].runningNumber + : null, }; try { // insert the tag with the onConflict update the tag diff --git a/server/services/rfid/route/tagInfo.ts b/server/services/rfid/route/tagInfo.ts index 2bb74bb..30d2d8e 100644 --- a/server/services/rfid/route/tagInfo.ts +++ b/server/services/rfid/route/tagInfo.ts @@ -3,7 +3,7 @@ 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 { clearNoReadTimer, noRead } from "../controller/noRead.js"; import { badRead, goodRead } from "../controller/readerControl.js"; import { createLog } from "../../logger/logger.js"; import { tryCatch } from "../../../globalUtils/tryCatch.js"; @@ -51,10 +51,15 @@ app.openapi( 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)); + // console.log( + // "Raw value:", + // body[i].data.peakRssi, + // "Parsed:", + // parseInt(body[i].data.peakRssi) + // ); if ( tag.includes("ALPLA") && - parseInt(body[i].data.peakRssi) < -40 + parseInt(body[i].data.peakRssi) < -30 ) { tagdata = [ ...tagdata, @@ -71,8 +76,8 @@ app.openapi( } if (tagdata.length === 0) { - noRead(reader); - badRead(reader); + // noRead(reader); + // badRead(reader); return c.json( { success: false, message: `There were no tags scanned.` }, 200 @@ -80,6 +85,7 @@ app.openapi( } else { tagData(tagdata); goodRead(reader); + clearNoReadTimer(); return c.json( { success: true, message: `New info from ${reader}` }, diff --git a/server/services/rfid/utils/rateLimit.ts b/server/services/rfid/utils/rateLimit.ts new file mode 100644 index 0000000..67c4396 --- /dev/null +++ b/server/services/rfid/utils/rateLimit.ts @@ -0,0 +1,19 @@ +const lastRunMap = new Map(); + +/** + * Returns true if the function with a given key was called within the last `seconds`. + * @param {string} key - Unique key to track cooldown per function/context. + * @param {number} seconds - Cooldown in seconds. + * @returns {boolean} - true if should skip (still in cooldown), false if allowed. + */ +export function shouldSkipByCooldown(key: any, seconds: any) { + const now = Date.now(); + const cooldown = seconds * 1000; + + if (lastRunMap.has(key) && now - lastRunMap.get(key) < cooldown) { + return true; + } + + lastRunMap.set(key, now); + return false; +}