refactor(rfid): refactored the way the wrapper works to indlude backup dyco plan
This commit is contained in:
@@ -2,65 +2,158 @@
|
|||||||
* Phase 1 we will just follow the logic of printing a label when we are told requested to based on this tag.
|
* 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
|
* 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 { createLog } from "../../../logger/logger.js";
|
||||||
|
import { labelingProcess } from "../../../ocp/controller/labeling/labelProcess.js";
|
||||||
import type { TagData } from "../tagData.js";
|
import type { TagData } from "../tagData.js";
|
||||||
import { tagStuff } from "../tags/crudTag.js";
|
import { tagStuff } from "../tags/crudTag.js";
|
||||||
|
import { sendEmail } from "../../../notifications/controller/sendMail.js";
|
||||||
|
|
||||||
export const wrapperStuff = async (tagData: TagData[]) => {
|
export const wrapperStuff = async (tagData: TagData[]) => {
|
||||||
if (tagData.length != 1) {
|
if (tagData[0]?.lastareaIn === "NeedsChecked") {
|
||||||
createLog(
|
createLog(
|
||||||
"error",
|
"error",
|
||||||
"rfid",
|
"rfid",
|
||||||
"rfid",
|
"rfid",
|
||||||
`There are ${tagData.length} tags and this ${tagData[0].reader} only allows 1 tag to create a label.`
|
`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.`
|
||||||
);
|
);
|
||||||
tagStuff(tagData);
|
|
||||||
} else {
|
// just making sure we clear out the running number if one really came over.
|
||||||
const tagdata = await tagStuff(tagData);
|
for (let i = 0; i < tagData.length; i++) {
|
||||||
if (!tagData) {
|
const tag = { ...tagData[i], runningNr: 0 };
|
||||||
createLog("error", "rfid", "rfid", `No tagData was grabbed.`);
|
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(
|
||||||
* we want to make sure this pallet came from a line as its last spot if not we need to have a manual check.
|
"error",
|
||||||
*/
|
"rfid",
|
||||||
if (
|
"rfid",
|
||||||
!Array.isArray(tagdata) &&
|
`There are ${tagData.length} tags and this ${tagData[0].reader} only allows 1 tag to create a label.`
|
||||||
tagdata?.some((n: any) => n.lastareaIn.includes("line3"))
|
);
|
||||||
) {
|
const tag = { ...tagData[0], runningNr: 0 };
|
||||||
createLog("error", "rfid", "rfid", `Data passed over is not an array.`);
|
tagStuff([tag]);
|
||||||
return;
|
monitorChecks();
|
||||||
}
|
|
||||||
const station3 = tagdata; //?.some((n: any) => n.lastareaIn.includes("line3"));
|
|
||||||
|
|
||||||
if (!station3) {
|
|
||||||
createLog(
|
|
||||||
"error",
|
|
||||||
"rfid",
|
|
||||||
"rfid",
|
|
||||||
`${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 (station3.runningNumber) {
|
|
||||||
createLog(
|
|
||||||
"info",
|
|
||||||
"rfid",
|
|
||||||
"rfid",
|
|
||||||
`Reprint label ${station3.runningNumber}`
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
createLog(
|
if (!tagData) {
|
||||||
"info",
|
createLog("error", "rfid", "rfid", `No tagData was grabbed.`);
|
||||||
"rfid",
|
monitorChecks();
|
||||||
"rfid",
|
}
|
||||||
`A new labels will be created and linked to this ${tagdata.tag} tag`
|
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");
|
||||||
|
|
||||||
|
if (!lines) {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"rfid",
|
||||||
|
"rfid",
|
||||||
|
`${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.`,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
"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`
|
||||||
|
);
|
||||||
|
|
||||||
|
// get the actaul line number from the last area in
|
||||||
|
const lineNum = parseInt(
|
||||||
|
tagdata[0]?.lastareaIn.repalceAll("line3", "")
|
||||||
|
);
|
||||||
|
createLog(
|
||||||
|
"info",
|
||||||
|
"rfid",
|
||||||
|
"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)),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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;
|
||||||
|
// }
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import {eq} from "drizzle-orm";
|
import { eq } from "drizzle-orm";
|
||||||
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 type {TagData} from "../tagData.js";
|
import type { TagData } from "../tagData.js";
|
||||||
import {createLog} from "../../../logger/logger.js";
|
import { createLog } from "../../../logger/logger.js";
|
||||||
|
|
||||||
type ReturnTag = {
|
type ReturnTag = {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
@@ -22,7 +22,7 @@ export const tagStuff = async (tagData: TagData[]): Promise<any> => {
|
|||||||
tagHex: tagData[i].tagHex,
|
tagHex: tagData[i].tagHex,
|
||||||
tag: tagData[i].tag,
|
tag: tagData[i].tag,
|
||||||
lastRead: new Date(tagData[i].timeStamp),
|
lastRead: new Date(tagData[i].timeStamp),
|
||||||
counts: [{area: tagData[i].reader, timesHere: 1}], //jsonb("counts").notNull(), //.default([{area: 1, timesHere: 5}]).notNull(),
|
counts: [{ area: tagData[i].reader, timesHere: 1 }], //jsonb("counts").notNull(), //.default([{area: 1, timesHere: 5}]).notNull(),
|
||||||
lastareaIn: tagData[i].reader,
|
lastareaIn: tagData[i].reader,
|
||||||
antenna: tagData[i].antenna,
|
antenna: tagData[i].antenna,
|
||||||
tagStrength: tagData[i].tagStrength,
|
tagStrength: tagData[i].tagStrength,
|
||||||
@@ -36,30 +36,37 @@ export const tagStuff = async (tagData: TagData[]): Promise<any> => {
|
|||||||
lastareaIn: rfidTags.lastareaIn,
|
lastareaIn: rfidTags.lastareaIn,
|
||||||
});
|
});
|
||||||
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
||||||
return {success: true, tag};
|
return { success: true, tag };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
||||||
return {success: false, error};
|
return { success: false, error };
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// update tag
|
// update tag
|
||||||
//console.log("Updating existing tag");
|
//console.log("Updating existing tag");
|
||||||
// make sure we actually have an array here
|
// make sure we actually have an array here
|
||||||
const countsArray = (tag[0]?.counts as {area: string; timesHere: number}[]) ?? [];
|
const countsArray =
|
||||||
|
(tag[0]?.counts as { area: string; timesHere: number }[]) ?? [];
|
||||||
|
|
||||||
// check if the reader exists on the array
|
// check if the reader exists on the array
|
||||||
const areaExists = countsArray.some((t) => t.area === tagData[0].reader);
|
const areaExists = countsArray.some(
|
||||||
|
(t) => t.area === tagData[0].reader
|
||||||
|
);
|
||||||
|
|
||||||
// run the update on the array
|
// run the update on the array
|
||||||
const updateCount = areaExists
|
const updateCount = areaExists
|
||||||
? countsArray.map((t) => {
|
? countsArray.map((t) => {
|
||||||
if (t.area === tagData[0].reader) {
|
if (t.area === tagData[0].reader) {
|
||||||
return {...t, timesHere: t.timesHere + 1};
|
return { ...t, timesHere: t.timesHere + 1 };
|
||||||
} else {
|
} else {
|
||||||
return {...t, area: tagData[i].reader, timesHere: 1};
|
return {
|
||||||
|
...t,
|
||||||
|
area: tagData[i].reader,
|
||||||
|
timesHere: 1,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
: [...countsArray, {area: tagData[i].reader, timesHere: 1}];
|
: [...countsArray, { area: tagData[i].reader, timesHere: 1 }];
|
||||||
|
|
||||||
const updateTag = {
|
const updateTag = {
|
||||||
lastRead: new Date(tagData[i].timeStamp),
|
lastRead: new Date(tagData[i].timeStamp),
|
||||||
@@ -70,17 +77,21 @@ export const tagStuff = async (tagData: TagData[]): Promise<any> => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await db.update(rfidTags).set(updateTag).where(eq(rfidTags.tagHex, tagData[0].tagHex)).returning({
|
await db
|
||||||
tag: rfidTags.tag,
|
.update(rfidTags)
|
||||||
runningNumber: rfidTags.runningNumber,
|
.set(updateTag)
|
||||||
counts: rfidTags.counts,
|
.where(eq(rfidTags.tagHex, tagData[0].tagHex))
|
||||||
lastareaIn: rfidTags.lastareaIn,
|
.returning({
|
||||||
});
|
tag: rfidTags.tag,
|
||||||
|
runningNumber: rfidTags.runningNumber,
|
||||||
|
counts: rfidTags.counts,
|
||||||
|
lastareaIn: rfidTags.lastareaIn,
|
||||||
|
});
|
||||||
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
||||||
return {success: true, tag};
|
return { success: true, tag };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
||||||
return {success: false, error};
|
return { success: false, error };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user