refactor(printers): allowed for more logging to come over

This commit is contained in:
2025-10-28 07:42:19 -05:00
parent 8f22165951
commit 66ae443fb8

View File

@@ -1,285 +1,275 @@
import net from "net";
import { pausePrinter } from "../../utils/pausePrinter.js";
import { addHours, differenceInSeconds } from "date-fns"; import { addHours, differenceInSeconds } from "date-fns";
import { printerUpdate } from "./printerStatUpdate.js"; import net from "net";
import { timeZoneFix } from "../../../../globalUtils/timeZoneFix.js";
import { createLog } from "../../../logger/logger.js"; import { createLog } from "../../../logger/logger.js";
import { pausePrinter } from "../../utils/pausePrinter.js";
import { unPausePrinter } from "../../utils/unpausePrinter.js"; import { unPausePrinter } from "../../utils/unpausePrinter.js";
import { labelingProcess } from "../labeling/labelProcess.js"; import { labelingProcess } from "../labeling/labelProcess.js";
import { timeZoneFix } from "../../../../globalUtils/timeZoneFix.js";
import { autoLabelCreated } from "../labeling/labelRatio.js"; import { autoLabelCreated } from "../labeling/labelRatio.js";
import { printerUpdate } from "./printerStatUpdate.js";
let logLevel: string = process.env.LOG_LEVEL || "info"; let logLevel: string = process.env.LOG_LEVEL || "info";
let errorCheck = false; let errorCheck = false;
export const printerStatus = async (p: any) => { export const printerStatus = async (p: any) => {
/** /**
* Checks each printer to see what the current state is * Checks each printer to see what the current state is
*/ */
createLog("debug", "ocp", "ocp", `Printer cycling`); createLog("debug", "ocp", "ocp", `Printer cycling`);
const printer = new net.Socket(); const printer = new net.Socket();
return new Promise((resolve) => { return new Promise((resolve) => {
// connect to the printer, and check its status // connect to the printer, and check its status
printer.connect(p.port, p.ipAddress, async () => { printer.connect(p.port, p.ipAddress, async () => {
// write the message to the printer below gives us a feedback of the printer // write the message to the printer below gives us a feedback of the printer
printer.write("~HS"); printer.write("~HS");
}); });
// read the data from the printer // read the data from the printer
printer.on("data", async (data) => { printer.on("data", async (data) => {
const res = data.toString(); const res = data.toString();
// turn the data into an array to make it more easy to deal with // turn the data into an array to make it more easy to deal with
const tmp = res.split(","); const tmp = res.split(",");
//--------------- time stuff----------------------------------------------------------------- //--------------- time stuff-----------------------------------------------------------------
// get last time printed // get last time printed
const lastTime = new Date(p.lastTimePrinted).toISOString(); const lastTime = new Date(p.lastTimePrinted).toISOString();
// console.log(lastTime); // console.log(lastTime);
// current time? // current time?
/** /**
* *
* add the time zone to the settings db * add the time zone to the settings db
*/ */
// const currentTime = addHours( // const currentTime = addHours(
// new Date(Date.now()), // new Date(Date.now()),
// -6 // -6
// ).toISOString(); // ).toISOString();
const currentTime = timeZoneFix(); const currentTime = timeZoneFix();
let timeBetween = 0; 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 this is our first time printing pause the printer to start the timer, else we just update the time between timer
if (lastTime === undefined) { if (lastTime === undefined) {
printer.end(); printer.end();
printerUpdate(p, 8); printerUpdate(p, 8);
pausePrinter(p); pausePrinter(p);
resolve({ success: true, message: "First Time printing" }); resolve({ success: true, message: "First Time printing" });
} else { } else {
timeBetween = differenceInSeconds(currentTime, lastTime); timeBetween = differenceInSeconds(currentTime, lastTime);
} }
// --- end time --- // --- end time ---
// --- printer logic --- // --- printer logic ---
createLog( createLog(
"debug", "debug",
"ocp", "ocp",
"ocp", "ocp",
`${p.name}: timeBetween: ${timeBetween}, delay ${parseInt( `${p.name}: timeBetween: ${timeBetween}, delay ${parseInt(
p.printDelay p.printDelay,
)}, ${currentTime}... ${lastTime}` )}, ${currentTime}... ${lastTime}`,
); );
if (tmp[2] === "0" && tmp[4] !== "000") { if (tmp[2] === "0" && tmp[4] !== "000") {
// unpaused and printing labels - reset timer // unpaused and printing labels - reset timer
createLog( createLog(
"debug", "info",
"ocp", "ocp",
"ocp", "ocp",
`Unpaused and printing labels, time remaing ${differenceInSeconds( `Unpaused and printing labels, time remaing ${differenceInSeconds(
parseInt(p.printDelay), parseInt(p.printDelay),
timeBetween timeBetween,
)}` )}`,
); );
// update last time printed in the array // update last time printed in the array
printerUpdate(p, 1); printerUpdate(p, 1);
} else if (tmp[2] === "1" && tmp[4] !== "000") { } else if (tmp[2] === "1" && tmp[4] !== "000") {
// was paused or label sent from somewhere else // was paused or label sent from somewhere else
createLog( createLog(
"info", "info",
"ocp", "ocp",
"ocp", "ocp",
`${ `${
p.name p.name
} paused to soon, unpausing, remaining time: ${differenceInSeconds( } paused to soon, unpausing, remaining time: ${differenceInSeconds(
parseInt(p.printDelay), parseInt(p.printDelay),
timeBetween timeBetween,
)}` )}`,
); );
// reset the timer for this printer as well other labels shouldnt be sent but if we send them ok // reset the timer for this printer as well other labels shouldnt be sent but if we send them ok
printerUpdate(p, 2); printerUpdate(p, 2);
unPausePrinter(p); unPausePrinter(p);
} else if (tmp[2] === "0" && timeBetween < parseInt(p.printDelay)) { } else if (tmp[2] === "0" && timeBetween < parseInt(p.printDelay)) {
// was unpaused to soon so repause it // was unpaused to soon so repause it
createLog( createLog(
"debug", "info",
"ocp", "ocp",
"ocp", "ocp",
`${p.name} Unpaused before the time allowed, time left ${ `${p.name} Unpaused before the time allowed, time left ${
differenceInSeconds(parseInt(p.printDelay), timeBetween) //seconds differenceInSeconds(parseInt(p.printDelay), timeBetween) //seconds
}` }`,
); );
printerUpdate(p, 3); printerUpdate(p, 3);
pausePrinter(p); pausePrinter(p);
} else if (tmp[2] === "0" && timeBetween > parseInt(p.printDelay)) { } else if (tmp[2] === "0" && timeBetween > parseInt(p.printDelay)) {
// its been long enough we can print a label // its been long enough we can print a label
createLog( createLog(
"debug", "info",
"ocp", "ocp",
"ocp", "ocp",
`${p.name} Allowed time passed and printing new label` `${p.name} Allowed time passed and printing new label`,
); );
// update last time printed in the array // update last time printed in the array
printerUpdate(p, 4); printerUpdate(p, 4);
// sending over for labeling. // sending over for labeling.
labelingProcess({ printer: p }); labelingProcess({ printer: p });
autoLabelCreated(); autoLabelCreated();
} else if (tmp[2] === "0") { } else if (tmp[2] === "0") {
// printer was unpaused for the first time or made it here // printer was unpaused for the first time or made it here
createLog( createLog("info", "ocp", "ocp", `${p.name} Frist time printing`);
"debug",
"ocp",
"ocp",
`${p.name} Frist time printing`
);
// add the time and printer // add the time and printer
printerUpdate(p, 4); printerUpdate(p, 4);
// sending over for labeling. // sending over for labeling.
labelingProcess({ printer: p }); labelingProcess({ printer: p });
autoLabelCreated(); autoLabelCreated();
} else if (tmp[2] === "1") { } else if (tmp[2] === "1") {
// printer is paused and waiting // printer is paused and waiting
createLog( createLog("debug", "ocp", "ocp", `${p.name} paused and waiting`);
"debug",
"ocp",
"ocp",
`${p.name} paused and waiting`
);
printerUpdate(p, 6); printerUpdate(p, 6);
} }
printer.end(); printer.end();
resolve({ success: true, message: "Print cycle completed." }); resolve({ success: true, message: "Print cycle completed." });
}); });
// as a safety destory it if its still there // as a safety destory it if its still there
printer.on("end", () => { printer.on("end", () => {
setTimeout(() => { setTimeout(() => {
if (!printer.destroyed) { if (!printer.destroyed) {
createLog( createLog(
"info", "info",
"printerState", "printerState",
"ocp", "ocp",
`${p.name}: was force closed, during normal cycle counting` `${p.name}: was force closed, during normal cycle counting`,
); );
printer.destroy(); printer.destroy();
} }
}, 1000); }, 1000);
}); });
printer.on("error", async (error: any) => { printer.on("error", async (error: any) => {
// just going to say theres an error with the printer // just going to say theres an error with the printer
//console.log(error.code); //console.log(error.code);
if (error.code.includes("ETIMEDOUT") && !errorCheck) { if (error.code.includes("ETIMEDOUT") && !errorCheck) {
createLog("error", "ocp", "ocp", `${p.name} is offline`); createLog("error", "ocp", "ocp", `${p.name} is offline`);
await printerUpdate(p, 9); await printerUpdate(p, 9);
errorCheck = true; errorCheck = true;
printer.end(); printer.end();
resolve({ resolve({
success: false, success: false,
message: "The printer is offline.", message: "The printer is offline.",
}); });
} }
if (!error.code.includes("ETIMEDOUT") && !errorCheck) { if (!error.code.includes("ETIMEDOUT") && !errorCheck) {
createLog( createLog(
"error", "error",
"ocp", "ocp",
"ocp", "ocp",
`${p.name} encountered an error: ${error}` `${p.name} encountered an error: ${error}`,
); );
await printerUpdate(p, 7); await printerUpdate(p, 7);
errorCheck = true; errorCheck = true;
// send log data // send log data
// fake line // fake line
printer.end(); printer.end();
resolve({ resolve({
success: false, success: false,
message: "There was an error with the printer.", message: "There was an error with the printer.",
}); });
} }
}); });
}); });
}; };
export const autoLabelingStats = async (p: any) => { export const autoLabelingStats = async (p: any) => {
/** /**
* Checks autolabeling printers just to see what they are doing. * Checks autolabeling printers just to see what they are doing.
*/ */
createLog("debug", "ocp", "ocp", `Printer cycling`); createLog("debug", "ocp", "ocp", `Printer cycling`);
const printer = new net.Socket(); const printer = new net.Socket();
return new Promise((resolve) => { return new Promise((resolve) => {
// connect to the printer, and check its status // connect to the printer, and check its status
printer.connect(p.port, p.ipAddress, async () => { printer.connect(p.port, p.ipAddress, async () => {
// write the message to the printer below gives us a feedback of the printer // write the message to the printer below gives us a feedback of the printer
printer.write("~HS"); printer.write("~HS");
}); });
// read the data from the printer // read the data from the printer
printer.on("data", async (data) => { printer.on("data", async (data) => {
const res = data.toString(); const res = data.toString();
// turn the data into an array to make it more easy to deal with // turn the data into an array to make it more easy to deal with
const tmp = res.split(","); const tmp = res.split(",");
if (tmp[4] !== "000") { if (tmp[4] !== "000") {
// unpaused and printing labels - reset timer // unpaused and printing labels - reset timer
createLog("debug", "ocp", "ocp", `Printing Labels`); createLog("debug", "ocp", "ocp", `Printing Labels`);
// update last time printed in the array // update last time printed in the array
printerUpdate(p, 1); printerUpdate(p, 1);
} }
if (tmp[4] === "000") { if (tmp[4] === "000") {
// unpaused and printing labels - reset timer // unpaused and printing labels - reset timer
createLog("debug", "ocp", "ocp", `Printing Labels`); createLog("debug", "ocp", "ocp", `Printing Labels`);
// update last time printed in the array // update last time printed in the array
printerUpdate(p, 5); printerUpdate(p, 5);
} }
}); });
printer.on("error", async (error) => { printer.on("error", async (error) => {
// just going to say theres an error with the printer // just going to say theres an error with the printer
console.log(error); console.log(error);
if (!errorCheck) { if (!errorCheck) {
createLog( createLog(
"error", "error",
"ocp", "ocp",
"ocp", "ocp",
`${p.name}, encountered an error: ${error}` `${p.name}, encountered an error: ${error}`,
); );
} }
await printerUpdate(p, 7); await printerUpdate(p, 7);
errorCheck = true; errorCheck = true;
// send log data // send log data
// fake line // fake line
printer.end(); printer.end();
resolve({ resolve({
success: false, success: false,
message: "There was an error with the printer.", message: "There was an error with the printer.",
}); });
}); });
}); });
}; };