feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* we will monitor shipped out pallets every hour if they get shipped out
|
||||
*/
|
||||
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* station 1 will just check for 10 tags
|
||||
*/
|
||||
@@ -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
|
||||
*
|
||||
*/
|
||||
68
lstV2/server/services/rfid/controller/stations/station3.ts
Normal file
68
lstV2/server/services/rfid/controller/stations/station3.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../../../../../database/dbclient.js";
|
||||
import { rfidReaders } from "../../../../../database/schema/rfidReaders.js";
|
||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||
import { createLog } from "../../../logger/logger.js";
|
||||
import type { TagData } from "../tagData.js";
|
||||
import { tagStuff } from "../tags/crudTag.js";
|
||||
|
||||
export const station3Tags = async (tagData: TagData[]) => {
|
||||
/**
|
||||
* Add the new tag to the reader so we know what was really here
|
||||
*/
|
||||
const { error } = await tryCatch(
|
||||
db
|
||||
.update(rfidReaders)
|
||||
.set({ lastTagScanned: tagData[0].tag })
|
||||
.where(eq(rfidReaders.reader, tagData[0].reader))
|
||||
);
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`${tagData[0].reader} encountered and error adding ${tagData[0].tag}.`
|
||||
);
|
||||
}
|
||||
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(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`There are ${tagData.length} tags, and ${tagData[0].reader} only allows 1 tag to create a label.`
|
||||
);
|
||||
// get tag data
|
||||
|
||||
for (let i = 0; i < tagData.length; i++) {
|
||||
const tag = {
|
||||
...tagData[i],
|
||||
runningNr: 0,
|
||||
lastareaIn: "NeedsChecked",
|
||||
};
|
||||
tagStuff([tag]);
|
||||
}
|
||||
} else {
|
||||
//console.log("Generate the label and link it to the tag.");
|
||||
const tag = { ...tagData[0], runningNr: 0 };
|
||||
const tagdata = await tagStuff([tag]);
|
||||
|
||||
createLog(
|
||||
"debug",
|
||||
"rfid",
|
||||
"rfid",
|
||||
"Generate a label and link it to this tag."
|
||||
);
|
||||
}
|
||||
};
|
||||
214
lstV2/server/services/rfid/controller/stations/wrappers.ts
Normal file
214
lstV2/server/services/rfid/controller/stations/wrappers.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
import os from "os";
|
||||
import { createLog } from "../../../logger/logger.js";
|
||||
import { labelingProcess } from "../../../ocp/controller/labeling/labelProcess.js";
|
||||
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 { and, eq, gte, ne, sql } from "drizzle-orm";
|
||||
import { rfidReaders } from "../../../../../database/schema/rfidReaders.js";
|
||||
import { shouldSkipByCooldown } from "../../utils/rateLimit.js";
|
||||
import { autoLabelCreated } from "../../../ocp/controller/labeling/labelRatio.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)
|
||||
.set({ lastTagScanned: tagData[0].tag })
|
||||
.where(eq(rfidReaders.reader, tagData[0].reader))
|
||||
);
|
||||
if (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`${tagData[0].reader} encountered and error adding ${tagData[0].tag}.`
|
||||
);
|
||||
}
|
||||
|
||||
if (tagData.length != 1) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`There are ${tagData.length} tags and this ${
|
||||
tagData[0].reader
|
||||
} only allows 1 tag to create a label: tag ${tagData
|
||||
.map((o: any) => `${o.tag}`)
|
||||
.join(",\n ")}`
|
||||
);
|
||||
const tag = { ...tagData[0], runningNr: 0 };
|
||||
//tagStuff([tag]);
|
||||
monitorChecks();
|
||||
return;
|
||||
} else {
|
||||
if (!tagData) {
|
||||
createLog("error", "rfid", "rfid", `No tagData was grabbed.`);
|
||||
monitorChecks();
|
||||
}
|
||||
const { data: tagdata, error: tagError } = await tryCatch(
|
||||
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,
|
||||
message: `There was an error getting the tag data for ${tagData[0].tag}.`,
|
||||
};
|
||||
}
|
||||
const checkTag: any = tagdata;
|
||||
|
||||
if (checkTag.length === 0) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`${tagData[0].tag} is not currently registered to a line please validate the pallet and manually print.`
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message:
|
||||
"There is no tag in the system please manually print..",
|
||||
};
|
||||
}
|
||||
|
||||
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.`
|
||||
);
|
||||
tagStuff(tagData);
|
||||
monitorChecks();
|
||||
return {
|
||||
success: false,
|
||||
message: `${tagData[0].tag}, Did not come from a line please check the pallet and manually print the label.`,
|
||||
};
|
||||
}
|
||||
if (lines[0]?.runningNumber) {
|
||||
createLog(
|
||||
"info",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`Reprint label ${lines[0]?.runningNumber}`
|
||||
);
|
||||
} else {
|
||||
createLog(
|
||||
"info",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`A new label will be created and linked to this ${tagData[0].tag} tag`
|
||||
);
|
||||
const lineNum = parseInt(
|
||||
checkTag[0]?.lastareaIn.replace("line3.", "")
|
||||
);
|
||||
createLog(
|
||||
"info",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`Line to be checked for printing: ${lineNum}`
|
||||
);
|
||||
const genlabel = await labelingProcess({
|
||||
line: lineNum.toString(),
|
||||
});
|
||||
autoLabelCreated();
|
||||
console.log(genlabel);
|
||||
if (genlabel?.success) {
|
||||
const createPrintData = {
|
||||
...tagData[0],
|
||||
runnungNumber: parseInt(genlabel.data?.SSCC.slice(10, -1)),
|
||||
};
|
||||
|
||||
tagStuff([createPrintData]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const monitorErrorTags: any = [];
|
||||
const monitorChecks = () => {
|
||||
/**
|
||||
* 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();
|
||||
monitorErrorTags.push({ timestamp: now });
|
||||
|
||||
// remove if creater than 1 hour
|
||||
const removalTiming = now - 1 * 60 * 60 * 1000; // 1 hour
|
||||
while (
|
||||
monitorErrorTags > 0 &&
|
||||
monitorErrorTags[0].timestamp < removalTiming
|
||||
) {
|
||||
monitorErrorTags.shift();
|
||||
}
|
||||
|
||||
if (monitorErrorTags > 10) {
|
||||
// send the email.
|
||||
const emailData = {
|
||||
email: "blake.matthes@alpla.com", // should be moved to the db so it can be reused.
|
||||
subject: `${os.hostname()} has had ${monitorErrorTags}.`,
|
||||
template: "toManyManualPrints",
|
||||
context: {
|
||||
count: monitorErrorTags,
|
||||
hours: "1",
|
||||
},
|
||||
};
|
||||
|
||||
sendEmail(emailData);
|
||||
}
|
||||
};
|
||||
|
||||
// if (
|
||||
// !Array.isArray(tagdata) &&
|
||||
// tagdata?.some((n: any) => n.lastareaIn.includes("line3"))
|
||||
// ) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "rfid",
|
||||
// "rfid",
|
||||
// `Data passed over is not an array.`
|
||||
// );
|
||||
// return;
|
||||
// }
|
||||
Reference in New Issue
Block a user