feat(ocp): completly moved ocp to lst

This commit is contained in:
2025-04-06 07:48:05 -05:00
parent 95bebbde2b
commit 51cc4aa370
13 changed files with 448 additions and 96 deletions

View File

@@ -1,9 +1,14 @@
import { eq, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { printerData } from "../../../../database/schema/printers.js";
import { delay } from "../../../globalUtils/delay.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import { getLots } from "../controller/lots/lots.js";
import { getPrinters } from "../controller/printers/getPrinters.js";
import { createLog } from "../../logger/logger.js";
export const assignedPrinters = async () => {
createLog("info", "ocp", "ocp", "Lot assignment check");
const { data: l, error: lotError } = await tryCatch(getLots());
if (lotError) {
@@ -23,63 +28,44 @@ export const assignedPrinters = async () => {
};
}
const printers: any = print;
const lots: any = l;
const printers: any = print.data;
const lots: any = l.data;
for (let i = 0; i < printers.data.length; i++) {
for (let i = 0; i < printers.length; i++) {
// is the printer assinged in alplalabel online?
const assigned = lots.data.filter(
(p: any) => p.printerID === printers[i].humanReadableId
const assigned = lots.filter(
(p: any) => p.printerID === parseInt(printers[i].humanReadableId)
);
let assignedPrinter = false;
// if true update the assigned field to true
if (assigned.length >= 1) {
assignedPrinter = true;
}
// if the last time printed is greater than 5 min change the status to 10
// const lastTime = new Date(printers[i].lastTimePrinted);
// let status = printers[i].status;
// let statusText = printers[i].statusText;
// update the printer to set its assignment
const { data: p, error: pe } = await tryCatch(
db
.update(printerData)
.set({ assigned: assignedPrinter, upd_date: sql`NOW()` })
.where(
eq(printerData.humanReadableId, printers[i].humanReadableId)
)
);
// // states we consider idle
// const printerStateCheck = [1, 2, 3, 4, 5, 6, 10];
// if (
// differenceInMinutes(currentTime, lastTime) >= 60 &&
// printerStateCheck.includes(printers[i].status) &&
// printers[i].assigned
// ) {
// createLog(
// "printerState",
// "info",
// `${printers[i].name} has been idle for more than 60 min, doing a heartbeat check`
// );
// status = 11;
// statusText = "idle";
// // printerState({printerCheck: printers[i]});
// } else if (differenceInMinutes(currentTime, lastTime) > 5 && printers[i].status === 7) {
// createLog(
// "printerState",
// "info",
// `${printers[i].name} has been errored for more 5 min changing to idle until, until it gets checked at the hour heartbeat.`
// );
// status = 10;
// statusText = "idle";
// }
// const updatePrinter = await prisma.printers.update({
// where: {
// humanReadableId: printers[i].humanReadableId,
// },
// data: {
// assigned: assignedPrinter,
// // status,
// // statusText,
// upd_date: new Date(Date.now()).toISOString(),
// },
// });
if (pe) {
createLog(
"error",
"ocp",
"ocp",
`${printers[i].name} encountered an error updating: ${pe}`
);
return {
success: false,
message: "Error while updating the prints.",
data: pe,
};
}
//delaying 250ms
await delay(250);

View File

@@ -0,0 +1,24 @@
import { machineCheck } from "../../sqlServer/querys/ocp/machineId.js";
import { createLog } from "../../logger/logger.js";
import { query } from "../../sqlServer/prodSqlServer.js";
export const getMac = async (machine: string) => {
let updateQuery = machineCheck.replaceAll("[loc]", machine);
// create blank lots in case there is an error and dose not work
let mac = [];
try {
mac = await query(updateQuery, "Machine id check");
// console.log("Machine data", mac); // removed due to swr being activated
} catch (err) {
createLog(
"error",
"lst",
"ocp",
`Error with Machine id Check query: ${err}`
);
}
return mac;
};