feat(ocp): completly moved ocp to lst
This commit is contained in:
13
server/globalUtils/timeZoneFix.ts
Normal file
13
server/globalUtils/timeZoneFix.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { addHours } from "date-fns";
|
||||||
|
|
||||||
|
export const timeZoneFix = () => {
|
||||||
|
/**
|
||||||
|
* Returns iso date based on current timezone.
|
||||||
|
*/
|
||||||
|
const rawDate = new Date(Date.now()).toISOString();
|
||||||
|
const offsetMinutes = new Date().getTimezoneOffset(); // in minutes
|
||||||
|
const offsetHours =
|
||||||
|
-offsetMinutes / 60 >= 0 ? offsetMinutes / 60 : -offsetMinutes / 60;
|
||||||
|
|
||||||
|
return addHours(rawDate, offsetHours).toISOString();
|
||||||
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { pino, type LogFn, type Logger } from "pino";
|
import { pino, type LogFn, type Logger } from "pino";
|
||||||
|
|
||||||
export let logLevel = process.env.LOGLEVEL || "info";
|
export let logLevel = process.env.LOG_LEVEL || "info";
|
||||||
|
|
||||||
const transport = pino.transport({
|
const transport = pino.transport({
|
||||||
targets: [
|
targets: [
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import comsumeMaterial from "./route/consumeMaterial.js";
|
|||||||
import returnMat from "./route/returnMaterial.js";
|
import returnMat from "./route/returnMaterial.js";
|
||||||
import createSiloAdjustment from "./route/siloAdjustments/createSiloAdjustment.js";
|
import createSiloAdjustment from "./route/siloAdjustments/createSiloAdjustment.js";
|
||||||
import postComment from "./route/siloAdjustments/postComment.js";
|
import postComment from "./route/siloAdjustments/postComment.js";
|
||||||
|
import getStockSilo from "./route/siloAdjustments/getStockData.js";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
@@ -13,6 +15,7 @@ const routes = [
|
|||||||
// silo
|
// silo
|
||||||
createSiloAdjustment,
|
createSiloAdjustment,
|
||||||
postComment,
|
postComment,
|
||||||
|
getStockSilo,
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
// app.route("/server", modules);
|
// app.route("/server", modules);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { settings } from "../../../../../database/schema/settings.js";
|
|||||||
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||||
import { createLog } from "../../../logger/logger.js";
|
import { createLog } from "../../../logger/logger.js";
|
||||||
import { getLots } from "../lots/lots.js";
|
import { getLots } from "../lots/lots.js";
|
||||||
import { getMac } from "../specialProcesses/utils/getMachineId.js";
|
|
||||||
import { billingCheck } from "../specialProcesses/billingCheck/billingCheck.js";
|
import { billingCheck } from "../specialProcesses/billingCheck/billingCheck.js";
|
||||||
import { isMainMatStaged } from "../materials/mainMaterial.js";
|
import { isMainMatStaged } from "../materials/mainMaterial.js";
|
||||||
import { firstLotLabel } from "../specialProcesses/lotChangeLabel/lotCHangeLabel.js";
|
import { firstLotLabel } from "../specialProcesses/lotChangeLabel/lotCHangeLabel.js";
|
||||||
@@ -12,9 +11,11 @@ import { createLabel } from "./createLabel.js";
|
|||||||
import { bookInLabel } from "./bookIn.js";
|
import { bookInLabel } from "./bookIn.js";
|
||||||
import { delieryInhouse } from "../specialProcesses/inhouse/inhouseDelivery.js";
|
import { delieryInhouse } from "../specialProcesses/inhouse/inhouseDelivery.js";
|
||||||
import { dualPrintingProcess } from "../specialProcesses/dualPrinting/dualPrinting.js";
|
import { dualPrintingProcess } from "../specialProcesses/dualPrinting/dualPrinting.js";
|
||||||
|
import { getMac } from "../../utils/getMachineId.js";
|
||||||
|
|
||||||
interface Printer {
|
interface Printer {
|
||||||
name: string;
|
name: string;
|
||||||
|
humanReadableId: string;
|
||||||
// Add any other expected properties
|
// Add any other expected properties
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +73,9 @@ export const labelingProcess = async ({
|
|||||||
if (printer) {
|
if (printer) {
|
||||||
// filter the lot based on the printerID
|
// filter the lot based on the printerID
|
||||||
// console.log(printer);
|
// console.log(printer);
|
||||||
filteredLot = lots.data.filter((l: any) => l.printerID === printer);
|
filteredLot = lots.data.filter(
|
||||||
|
(l: any) => l.printerID === parseInt(printer?.humanReadableId)
|
||||||
|
);
|
||||||
if (filteredLot.length === 0) {
|
if (filteredLot.length === 0) {
|
||||||
// console.log(`There is not a lot assigned to ${printer.name}`);
|
// console.log(`There is not a lot assigned to ${printer.name}`);
|
||||||
createLog(
|
createLog(
|
||||||
|
|||||||
@@ -1,20 +1,81 @@
|
|||||||
|
import { eq } from "drizzle-orm";
|
||||||
|
import { db } from "../../../../../database/dbclient.js";
|
||||||
|
import { settings } from "../../../../../database/schema/settings.js";
|
||||||
|
import { tryCatch } from "../../../../globalUtils/tryCatch.js";
|
||||||
|
import { createLog } from "../../../logger/logger.js";
|
||||||
import { getPrinters } from "./getPrinters.js";
|
import { getPrinters } from "./getPrinters.js";
|
||||||
|
import { printerStatus } from "./printerStatus.js";
|
||||||
|
|
||||||
export const printerCycle = async () => {
|
export const printerCycle = async () => {
|
||||||
/**
|
/**
|
||||||
* We will cycle through the printers to check there states.
|
* We will cycle through the printers to check there states.
|
||||||
*/
|
*/
|
||||||
let printers = await getPrinters();
|
|
||||||
/**
|
// get the printers
|
||||||
* if the last timeprinted would be greater than x well just change the status to idle and extended based on the 2 times.
|
const { data: s, error: se } = await tryCatch(
|
||||||
*
|
db.select().from(settings).where(eq(settings.name, "ocpCycleDelay"))
|
||||||
* to get a printer going again label will need to come from the front end as that will just unpause the printer and start the labeling, or the api for manual print
|
);
|
||||||
* well need to adjust this to actually print the label then unpause it.
|
if (se) {
|
||||||
*
|
createLog(
|
||||||
* it will be
|
"error",
|
||||||
*
|
"ocp",
|
||||||
* less than x since time printed run the printer status
|
"ocp",
|
||||||
* greater than x but less than y change the status to idle, but ping to make sure its online,
|
"There was an error getting the ocpCycleDelay."
|
||||||
* if greater than y change to extended idle but stil also ping to make sure its online.
|
);
|
||||||
*/
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Error getting printers.",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const ocpDelay: any = s;
|
||||||
|
|
||||||
|
// start the actual printer cycle
|
||||||
|
const actualPrinterCycle = setInterval(async () => {
|
||||||
|
const { data, error } = await tryCatch(getPrinters());
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
"There was an error getting the printers."
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Error getting printers.",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let printers: any = data.data;
|
||||||
|
|
||||||
|
// only keep the assigned ones
|
||||||
|
printers = printers.filter((p: any) => p.assigned === true);
|
||||||
|
|
||||||
|
// for autolabelers like dayton and MCD we want to ignore them from the loop as well.
|
||||||
|
printers = printers.filter(
|
||||||
|
(p: any) => p.name != "Autolabeler" && !p.remark.includes("ignore")
|
||||||
|
);
|
||||||
|
|
||||||
|
printers.forEach(async (p: any) => {
|
||||||
|
/**
|
||||||
|
* if the last timeprinted would be greater than x well just change the status to idle and extended based on the 2 times.
|
||||||
|
*
|
||||||
|
* to get a printer going again label will need to come from the front end as that will just unpause the printer and start the labeling, or the api for manual print
|
||||||
|
* well need to adjust this to actually print the label then unpause it.
|
||||||
|
*
|
||||||
|
* it will be
|
||||||
|
*
|
||||||
|
* less than x since time printed run the printer status
|
||||||
|
* greater than x but less than y change the status to idle, but ping to make sure its online,
|
||||||
|
* if greater than y change to extended idle but stil also ping to make sure its online.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ignore pdf printer as we want it here for testing purposes
|
||||||
|
if (p.name.toLowerCase() === "pdf24") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await printerStatus(p);
|
||||||
|
});
|
||||||
|
}, parseInt(ocpDelay[0]?.value) * 1000);
|
||||||
};
|
};
|
||||||
|
|||||||
61
server/services/ocp/controller/printers/printerStatUpdate.ts
Normal file
61
server/services/ocp/controller/printers/printerStatUpdate.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { eq, sql } from "drizzle-orm";
|
||||||
|
import { db } from "../../../../../database/dbclient.js";
|
||||||
|
import { printerData } from "../../../../../database/schema/printers.js";
|
||||||
|
import { createLog } from "../../../logger/logger.js";
|
||||||
|
|
||||||
|
export const printStatus = [
|
||||||
|
{ code: 1, text: "Printing" },
|
||||||
|
{ code: 2, text: "Pending labels" },
|
||||||
|
{ code: 3, text: "Printing to fast" },
|
||||||
|
{ code: 4, text: "Creating label" },
|
||||||
|
{ code: 6, text: "Printer Paused" },
|
||||||
|
{ code: 7, text: "Printer error" },
|
||||||
|
{
|
||||||
|
code: 8,
|
||||||
|
text: "First time printing",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export const printerUpdate = async (printer: any, status: any) => {
|
||||||
|
/**
|
||||||
|
* Updates the status of the printer.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// get current time
|
||||||
|
let pd = {};
|
||||||
|
const currentTime = sql`NOW()`;
|
||||||
|
|
||||||
|
if (status === 3) {
|
||||||
|
pd = {
|
||||||
|
status: status,
|
||||||
|
statusText: printStatus.filter((c) => c.code === status)[0]?.text,
|
||||||
|
};
|
||||||
|
} else if (status === 6) {
|
||||||
|
pd = {
|
||||||
|
status: status,
|
||||||
|
statusText: printStatus.filter((c) => c.code === status)[0]?.text,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
pd = {
|
||||||
|
lastTimePrinted: currentTime,
|
||||||
|
status: status,
|
||||||
|
statusText: printStatus.filter((c) => c.code === status)[0]?.text,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printer.humanReadableId) {
|
||||||
|
try {
|
||||||
|
await db
|
||||||
|
.update(printerData)
|
||||||
|
.set(pd)
|
||||||
|
.where(
|
||||||
|
eq(printerData.humanReadableId, printer.humanReadableId)
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
createLog("error", "ocp", "ocp", `Error updating printer state`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// console.log(printerUpdate.name);
|
||||||
|
return;
|
||||||
|
};
|
||||||
190
server/services/ocp/controller/printers/printerStatus.ts
Normal file
190
server/services/ocp/controller/printers/printerStatus.ts
Normal file
@@ -0,0 +1,190 @@
|
|||||||
|
import net from "net";
|
||||||
|
import { pausePrinter } from "../../utils/pausePrinter.js";
|
||||||
|
import { addHours, differenceInSeconds } from "date-fns";
|
||||||
|
import { printerUpdate } from "./printerStatUpdate.js";
|
||||||
|
import { createLog } from "../../../logger/logger.js";
|
||||||
|
import { unPausePrinter } from "../../utils/unpausePrinter.js";
|
||||||
|
|
||||||
|
import { labelingProcess } from "../labeling/labelProcess.js";
|
||||||
|
import { timeZoneFix } from "../../../../globalUtils/timeZoneFix.js";
|
||||||
|
|
||||||
|
let logLevel: string = process.env.LOG_LEVEL || "info";
|
||||||
|
let errorCheck = false;
|
||||||
|
export const printerStatus = async (p: any) => {
|
||||||
|
/**
|
||||||
|
* Checks each printer to see what the current state is
|
||||||
|
*/
|
||||||
|
createLog("debug", "ocp", "ocp", `Printer cycling`);
|
||||||
|
|
||||||
|
const printer = new net.Socket();
|
||||||
|
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
// connect to the printer, and check its status
|
||||||
|
printer.connect(p.port, p.ipAddress, async () => {
|
||||||
|
// write the message to the printer below gives us a feedback of the printer
|
||||||
|
printer.write("~HS");
|
||||||
|
});
|
||||||
|
|
||||||
|
// read the data from the printer
|
||||||
|
printer.on("data", async (data) => {
|
||||||
|
const res = data.toString();
|
||||||
|
|
||||||
|
// turn the data into an array to make it more easy to deal with
|
||||||
|
const tmp = res.split(",");
|
||||||
|
|
||||||
|
//--------------- time stuff-----------------------------------------------------------------
|
||||||
|
// get last time printed
|
||||||
|
const lastTime = new Date(p.lastTimePrinted).toISOString();
|
||||||
|
// console.log(lastTime);
|
||||||
|
|
||||||
|
// current time?
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* add the time zone to the settings db
|
||||||
|
*/
|
||||||
|
// const currentTime = addHours(
|
||||||
|
// new Date(Date.now()),
|
||||||
|
// -6
|
||||||
|
// ).toISOString();
|
||||||
|
|
||||||
|
const currentTime = timeZoneFix();
|
||||||
|
|
||||||
|
let timeBetween = 0;
|
||||||
|
// if this is our first time printing pause the printer to start the timer, else we just update the time between timer
|
||||||
|
if (lastTime === undefined) {
|
||||||
|
printer.end();
|
||||||
|
printerUpdate(p, 8);
|
||||||
|
pausePrinter(p);
|
||||||
|
resolve({ success: true, message: "First Time printing" });
|
||||||
|
} else {
|
||||||
|
timeBetween = differenceInSeconds(currentTime, lastTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- end time ---
|
||||||
|
|
||||||
|
// --- printer logic ---
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name}: timeBetween: ${timeBetween}, delay ${
|
||||||
|
p.printDelay || 90
|
||||||
|
}, ${currentTime}... ${lastTime}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (tmp[2] === "0" && tmp[4] !== "000") {
|
||||||
|
// unpaused and printing labels - reset timer
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`Unpaused and printing labels, time remaing ${differenceInSeconds(
|
||||||
|
p.printDelay || 90,
|
||||||
|
timeBetween
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// update last time printed in the array
|
||||||
|
printerUpdate(p, 1);
|
||||||
|
} else if (tmp[2] === "1" && tmp[4] !== "000") {
|
||||||
|
// was paused or label sent from somewhere else
|
||||||
|
createLog(
|
||||||
|
"info",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${
|
||||||
|
p.name
|
||||||
|
} paused to soon, unpausing, remaining time: ${differenceInSeconds(
|
||||||
|
p.printDelay || 90,
|
||||||
|
timeBetween
|
||||||
|
)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// reset the timer for this printer as well other labels shouldnt be sent but if we send them ok
|
||||||
|
printerUpdate(p, 2);
|
||||||
|
|
||||||
|
unPausePrinter(p);
|
||||||
|
} else if ((tmp[2] === "0" && timeBetween < p.printDelay) || 90) {
|
||||||
|
// was unpaused to soon so repause it
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name} Unpaused before the time allowed, time left ${
|
||||||
|
differenceInSeconds(p.printDelay || 90, timeBetween) //seconds
|
||||||
|
}`
|
||||||
|
);
|
||||||
|
|
||||||
|
printerUpdate(p, 3);
|
||||||
|
pausePrinter(p);
|
||||||
|
} else if ((tmp[2] === "0" && timeBetween > p.printDelay) || 90) {
|
||||||
|
// its been long enough we can print a label
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name} Allowed time passed and printing new label`
|
||||||
|
);
|
||||||
|
|
||||||
|
// update last time printed in the array
|
||||||
|
printerUpdate(p, 4);
|
||||||
|
|
||||||
|
// sending over for labeling.
|
||||||
|
labelingProcess({ printer: p });
|
||||||
|
} else if (tmp[2] === "0") {
|
||||||
|
// printer was unpaused for the first time or made it here
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name} Frist time printing`
|
||||||
|
);
|
||||||
|
|
||||||
|
// add the time and printer
|
||||||
|
printerUpdate(p, 4);
|
||||||
|
|
||||||
|
// sending over for labeling.
|
||||||
|
labelingProcess({ printer: p });
|
||||||
|
} else if (tmp[2] === "1") {
|
||||||
|
// printer is paused and waiting
|
||||||
|
createLog(
|
||||||
|
"debug",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name} paused and waiting`
|
||||||
|
);
|
||||||
|
|
||||||
|
printerUpdate(p, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
printer.end();
|
||||||
|
|
||||||
|
resolve({ success: true, message: "Print cycle completed." });
|
||||||
|
});
|
||||||
|
|
||||||
|
printer.on("error", async (error) => {
|
||||||
|
// just going to say theres an error with the printer
|
||||||
|
|
||||||
|
if (!errorCheck) {
|
||||||
|
createLog(
|
||||||
|
"error",
|
||||||
|
"ocp",
|
||||||
|
"ocp",
|
||||||
|
`${p.name} encountered an error: ${error}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await printerUpdate(p, 7);
|
||||||
|
errorCheck = true;
|
||||||
|
|
||||||
|
// send log data
|
||||||
|
// fake line
|
||||||
|
printer.end();
|
||||||
|
resolve({
|
||||||
|
success: false,
|
||||||
|
message: "There was an error with the printer.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -25,7 +25,7 @@ export const updatePrinters = async () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (prodError) {
|
if (prodError) {
|
||||||
console.log(prodError);
|
//console.log(prodError);
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
message: "there was an error getting the printers.",
|
message: "there was an error getting the printers.",
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ export const dualPrintingProcess = async (lotInfo: any) => {
|
|||||||
"info",
|
"info",
|
||||||
"ocp",
|
"ocp",
|
||||||
"ocp",
|
"ocp",
|
||||||
`Printing label for ${whatToPrint.MachineDescription}`
|
`Printing label for ${whatToPrint[0].MachineDescription}`
|
||||||
);
|
);
|
||||||
return whatToPrint;
|
return whatToPrint;
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,7 @@ export const dualPrintingProcess = async (lotInfo: any) => {
|
|||||||
"info",
|
"info",
|
||||||
"ocp",
|
"ocp",
|
||||||
"ocp",
|
"ocp",
|
||||||
`Printing label for ${whatToPrint.MachineDescription}`
|
`Printing label for ${whatToPrint[0].MachineDescription}`
|
||||||
);
|
);
|
||||||
return whatToPrint;
|
return whatToPrint;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import { dycoConnect } from "./controller/specialProcesses/dyco/plcConnection.js
|
|||||||
import dycoCon from "./routes/specialProcesses/dyco/connection.js";
|
import dycoCon from "./routes/specialProcesses/dyco/connection.js";
|
||||||
import dycoClose from "./routes/specialProcesses/dyco/closeConnection.js";
|
import dycoClose from "./routes/specialProcesses/dyco/closeConnection.js";
|
||||||
import manualprint from "./routes/labeling/manualPrint.js";
|
import manualprint from "./routes/labeling/manualPrint.js";
|
||||||
|
import { assignedPrinters } from "./utils/checkAssignments.js";
|
||||||
|
import { printerCycle } from "./controller/printers/printerCycle.js";
|
||||||
|
import { tryCatch } from "../../globalUtils/tryCatch.js";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
@@ -43,14 +46,34 @@ app.all("/ocp/*", (c) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the settings so we only run items when they are truly active
|
||||||
|
*/
|
||||||
|
const dycoActive = setting.filter((n) => n.name == "dycoConnect");
|
||||||
|
const ocpActive = setting.filter((n) => n.name === "ocpActive");
|
||||||
// run the printer update on restart just to keep everything good
|
// run the printer update on restart just to keep everything good
|
||||||
setTimeout(() => {
|
|
||||||
updatePrinters();
|
|
||||||
}, 3 * 1000);
|
|
||||||
|
|
||||||
// do the intnal connection to the dyco
|
// do the intnal connection to the dyco
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
dycoConnect();
|
if (dycoActive[0].value === "1") {
|
||||||
|
dycoConnect();
|
||||||
|
}
|
||||||
}, 3 * 1000);
|
}, 3 * 1000);
|
||||||
|
|
||||||
|
// check for printers being assigned
|
||||||
|
setInterval(() => {
|
||||||
|
if (ocpActive[0].value === "1") {
|
||||||
|
assignedPrinters();
|
||||||
|
}
|
||||||
|
}, 60 * 1000);
|
||||||
|
|
||||||
|
// start the printer process after everything else is up ad running
|
||||||
|
setTimeout(async () => {
|
||||||
|
if (ocpActive[0].value === "1") {
|
||||||
|
await updatePrinters();
|
||||||
|
await assignedPrinters();
|
||||||
|
printerCycle();
|
||||||
|
}
|
||||||
|
}, 10 * 1000);
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
@@ -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 { delay } from "../../../globalUtils/delay.js";
|
||||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||||
import { getLots } from "../controller/lots/lots.js";
|
import { getLots } from "../controller/lots/lots.js";
|
||||||
import { getPrinters } from "../controller/printers/getPrinters.js";
|
import { getPrinters } from "../controller/printers/getPrinters.js";
|
||||||
|
import { createLog } from "../../logger/logger.js";
|
||||||
|
|
||||||
export const assignedPrinters = async () => {
|
export const assignedPrinters = async () => {
|
||||||
|
createLog("info", "ocp", "ocp", "Lot assignment check");
|
||||||
const { data: l, error: lotError } = await tryCatch(getLots());
|
const { data: l, error: lotError } = await tryCatch(getLots());
|
||||||
|
|
||||||
if (lotError) {
|
if (lotError) {
|
||||||
@@ -23,63 +28,44 @@ export const assignedPrinters = async () => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const printers: any = print;
|
const printers: any = print.data;
|
||||||
const lots: any = l;
|
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?
|
// is the printer assinged in alplalabel online?
|
||||||
const assigned = lots.data.filter(
|
const assigned = lots.filter(
|
||||||
(p: any) => p.printerID === printers[i].humanReadableId
|
(p: any) => p.printerID === parseInt(printers[i].humanReadableId)
|
||||||
);
|
);
|
||||||
|
|
||||||
let assignedPrinter = false;
|
let assignedPrinter = false;
|
||||||
// if true update the assigned field to true
|
// if true update the assigned field to true
|
||||||
if (assigned.length >= 1) {
|
if (assigned.length >= 1) {
|
||||||
assignedPrinter = true;
|
assignedPrinter = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the last time printed is greater than 5 min change the status to 10
|
// update the printer to set its assignment
|
||||||
// const lastTime = new Date(printers[i].lastTimePrinted);
|
const { data: p, error: pe } = await tryCatch(
|
||||||
// let status = printers[i].status;
|
db
|
||||||
// let statusText = printers[i].statusText;
|
.update(printerData)
|
||||||
|
.set({ assigned: assignedPrinter, upd_date: sql`NOW()` })
|
||||||
|
.where(
|
||||||
|
eq(printerData.humanReadableId, printers[i].humanReadableId)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
// // states we consider idle
|
if (pe) {
|
||||||
// const printerStateCheck = [1, 2, 3, 4, 5, 6, 10];
|
createLog(
|
||||||
// if (
|
"error",
|
||||||
// differenceInMinutes(currentTime, lastTime) >= 60 &&
|
"ocp",
|
||||||
// printerStateCheck.includes(printers[i].status) &&
|
"ocp",
|
||||||
// printers[i].assigned
|
`${printers[i].name} encountered an error updating: ${pe}`
|
||||||
// ) {
|
);
|
||||||
// createLog(
|
return {
|
||||||
// "printerState",
|
success: false,
|
||||||
// "info",
|
message: "Error while updating the prints.",
|
||||||
// `${printers[i].name} has been idle for more than 60 min, doing a heartbeat check`
|
data: pe,
|
||||||
// );
|
};
|
||||||
|
}
|
||||||
// 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(),
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
//delaying 250ms
|
//delaying 250ms
|
||||||
await delay(250);
|
await delay(250);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { createLog } from "../../../../logger/logger.js";
|
import { machineCheck } from "../../sqlServer/querys/ocp/machineId.js";
|
||||||
import { query } from "../../../../sqlServer/prodSqlServer.js";
|
import { createLog } from "../../logger/logger.js";
|
||||||
import { machineCheck } from "../../../../sqlServer/querys/ocp/machineId.js";
|
import { query } from "../../sqlServer/prodSqlServer.js";
|
||||||
|
|
||||||
export const getMac = async (machine: string) => {
|
export const getMac = async (machine: string) => {
|
||||||
let updateQuery = machineCheck.replaceAll("[loc]", machine);
|
let updateQuery = machineCheck.replaceAll("[loc]", machine);
|
||||||
@@ -191,33 +191,45 @@ const newSettings = [
|
|||||||
serviceBelowsTo: "system",
|
serviceBelowsTo: "system",
|
||||||
roleToChange: "admin",
|
roleToChange: "admin",
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// ocp
|
||||||
|
{
|
||||||
|
name: "acpActive",
|
||||||
|
value: `1`,
|
||||||
|
description: "Are we pritning on demand?",
|
||||||
|
serviceBelowsTo: "ocp",
|
||||||
|
roleToChange: "admin",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "ocpCycleDelay",
|
||||||
|
value: `10`,
|
||||||
|
description: "How long between printer cycles do we want to monitor.",
|
||||||
|
serviceBelowsTo: "ocp",
|
||||||
|
roleToChange: "admin",
|
||||||
|
},
|
||||||
];
|
];
|
||||||
export const areSettingsIn = async () => {
|
export const areSettingsIn = async () => {
|
||||||
// get the roles
|
// get the roles
|
||||||
try {
|
try {
|
||||||
const settingsCheck = await db.select().from(settings);
|
try {
|
||||||
|
const newRole = await db
|
||||||
if (settingsCheck.length !== newSettings.length) {
|
.insert(settings)
|
||||||
try {
|
.values(newSettings)
|
||||||
const newRole = await db
|
.onConflictDoNothing() // this will only update the ones that are new :D
|
||||||
.insert(settings)
|
.returning({ name: settings.name });
|
||||||
.values(newSettings)
|
createLog(
|
||||||
.onConflictDoNothing() // this will only update the ones that are new :D
|
"info",
|
||||||
.returning({ name: settings.name });
|
"lst",
|
||||||
createLog(
|
"server",
|
||||||
"info",
|
"Settings were just added due to missing them on server startup"
|
||||||
"lst",
|
);
|
||||||
"server",
|
} catch (error) {
|
||||||
"Settings were just added due to missing them on server startup"
|
createLog(
|
||||||
);
|
"error",
|
||||||
} catch (error) {
|
"lst",
|
||||||
createLog(
|
"server",
|
||||||
"error",
|
"There was an error adding new roles to the db"
|
||||||
"lst",
|
);
|
||||||
"server",
|
|
||||||
"There was an error adding new roles to the db"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
createLog(
|
createLog(
|
||||||
|
|||||||
Reference in New Issue
Block a user