feat(rfid): more rfid topics while monitoring the system run

This commit is contained in:
2025-07-10 20:09:35 -05:00
parent 58711becf2
commit 6a082d9737
10 changed files with 87 additions and 12 deletions

View File

@@ -107,7 +107,7 @@ export const labelerTagRead = async (tagData: any) => {
// trigger the reader so we can get the label from the tag readers. // trigger the reader so we can get the label from the tag readers.
setTimeout(async () => { setTimeout(async () => {
await readTags("wrapper1"); await readTags("wrapper1");
}, 2 * 1000); }, 500); // half second delay on this guy
} }
} }
}; };

View File

@@ -49,6 +49,8 @@ export const palletSendTag = async (tagData: any) => {
Date.now() - tagTime <= 5000 && Date.now() - tagTime <= 5000 &&
!tagData.value !tagData.value
) { ) {
await pickedup({ runningNr: 1234, all: true, areaFrom: "wrapper_1" }); setTimeout(() => {
pickedup({ runningNr: 1234, all: true, areaFrom: "wrapper_1" });
}, 5000);
} }
}; };

View File

@@ -8,7 +8,22 @@ export const noRead = async (reader: string) => {
createLog( createLog(
"error", "error",
"rfid", "rfid",
"rifd", "rfid",
`${reader} just had a no read please check for a tag and manually trigger a read.` `${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;
}
};

View File

@@ -3,6 +3,8 @@ import { createLog } from "../../logger/logger.js";
import { db } from "../../../../database/dbclient.js"; import { db } from "../../../../database/dbclient.js";
import { rfidReaders } from "../../../../database/schema/rfidReaders.js"; import { rfidReaders } from "../../../../database/schema/rfidReaders.js";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { noRead, startNoReadTimer } from "./noRead.js";
import { badRead } from "./readerControl.js";
const authData = btoa("admin:Zebra123!"); const authData = btoa("admin:Zebra123!");
let token: string; let token: string;
@@ -63,6 +65,11 @@ const startRead = async (reader: string, ip: string) => {
//console.log(res.data); //console.log(res.data);
if (res.status === 200) { if (res.status === 200) {
// start the no read timer
startNoReadTimer(() => {
noRead(reader);
badRead(reader);
});
setTimeout(() => { setTimeout(() => {
stopRead(reader, ip); stopRead(reader, ip);
}, 5 * 1000); }, 5 * 1000);

View File

@@ -48,7 +48,11 @@ export const badRead = async (reader: string) => {
try { try {
const badRead = await db const badRead = await db
.update(rfidReaders) .update(rfidReaders)
.set({ lastTrigger: sql`NOW()`, lastTriggerGood: false }) .set({
lastTrigger: sql`NOW()`,
lastTriggerGood: false,
lastTagScanned: null,
})
.where(eq(rfidReaders.reader, reader)); .where(eq(rfidReaders.reader, reader));
createLog( createLog(
"info", "info",

View File

@@ -11,12 +11,15 @@ import { sendEmail } from "../../../notifications/controller/sendMail.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { rfidTags } from "../../../../../database/schema/rfidTags.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 { rfidReaders } from "../../../../../database/schema/rfidReaders.js";
import { shouldSkipByCooldown } from "../../utils/rateLimit.js";
export const wrapperStuff = async (tagData: any) => { export const wrapperStuff = async (tagData: any) => {
console.log("WrapperTag", tagData[0].tag); console.log("WrapperTag", tagData[0].tag);
if (shouldSkipByCooldown("wrapperDouble Scan", 10)) return;
// update the reader with the last tag read.
const { error } = await tryCatch( const { error } = await tryCatch(
db db
.update(rfidReaders) .update(rfidReaders)
@@ -53,8 +56,21 @@ export const wrapperStuff = async (tagData: any) => {
monitorChecks(); monitorChecks();
} }
const { data: tagdata, error: tagError } = await tryCatch( 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) { if (tagError) {
return { return {
success: false, success: false,
@@ -136,10 +152,12 @@ export const wrapperStuff = async (tagData: any) => {
const genlabel = await labelingProcess({ const genlabel = await labelingProcess({
line: lineNum.toString(), line: lineNum.toString(),
}); });
console.log(genlabel);
if (genlabel?.success) { if (genlabel?.success) {
const createPrintData = { const createPrintData = {
...tagData[0], ...tagData[0],
runnungNr: parseInt(genlabel.data?.SSCC.slice(10, -1)), runnungNumber: parseInt(genlabel.data?.SSCC.slice(10, -1)),
}; };
tagStuff([createPrintData]); tagStuff([createPrintData]);

View File

@@ -9,6 +9,7 @@ export type TagData = {
antenna: number; antenna: number;
tagStrength: number; tagStrength: number;
lastareaIn?: string; lastareaIn?: string;
runningNumber?: number;
}; };
export const tagData = async (data: TagData[]) => { export const tagData = async (data: TagData[]) => {
/** /**

View File

@@ -26,6 +26,9 @@ export const tagStuff = async (tagData: TagData[]): Promise<any> => {
lastareaIn: tagData[i].reader, lastareaIn: tagData[i].reader,
antenna: tagData[i].antenna, antenna: tagData[i].antenna,
tagStrength: tagData[i].tagStrength, tagStrength: tagData[i].tagStrength,
runningNumber: tagData[i].runningNumber
? tagData[i].runningNumber
: null,
}; };
try { try {
// insert the tag with the onConflict update the tag // insert the tag with the onConflict update the tag

View File

@@ -3,7 +3,7 @@ import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
import { ConsoleLogWriter } from "drizzle-orm"; import { ConsoleLogWriter } from "drizzle-orm";
import { tagData } from "../controller/tagData.js"; import { tagData } from "../controller/tagData.js";
import { responses } from "../../../globalUtils/routeDefs/responses.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 { badRead, goodRead } from "../controller/readerControl.js";
import { createLog } from "../../logger/logger.js"; import { createLog } from "../../logger/logger.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../globalUtils/tryCatch.js";
@@ -51,10 +51,15 @@ app.openapi(
const tag = Buffer.from(body[i].data.idHex, "hex").toString( const tag = Buffer.from(body[i].data.idHex, "hex").toString(
"utf-8" "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 ( if (
tag.includes("ALPLA") && tag.includes("ALPLA") &&
parseInt(body[i].data.peakRssi) < -40 parseInt(body[i].data.peakRssi) < -30
) { ) {
tagdata = [ tagdata = [
...tagdata, ...tagdata,
@@ -71,8 +76,8 @@ app.openapi(
} }
if (tagdata.length === 0) { if (tagdata.length === 0) {
noRead(reader); // noRead(reader);
badRead(reader); // badRead(reader);
return c.json( return c.json(
{ success: false, message: `There were no tags scanned.` }, { success: false, message: `There were no tags scanned.` },
200 200
@@ -80,6 +85,7 @@ app.openapi(
} else { } else {
tagData(tagdata); tagData(tagdata);
goodRead(reader); goodRead(reader);
clearNoReadTimer();
return c.json( return c.json(
{ success: true, message: `New info from ${reader}` }, { success: true, message: `New info from ${reader}` },

View File

@@ -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;
}