feat(lstv2 move): moved lstv2 into this app to keep them combined and easier to maintain
This commit is contained in:
97
lstV2/server/services/ocp/utils/checkAssignments.ts
Normal file
97
lstV2/server/services/ocp/utils/checkAssignments.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
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("debug", "ocp", "ocp", "Lot assignment check");
|
||||
const { data: lot, error: lotError } = await tryCatch(getLots());
|
||||
const l: any = lot;
|
||||
if (lotError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting lots",
|
||||
data: lotError,
|
||||
};
|
||||
}
|
||||
|
||||
if (l.data.length === 0 && l.success) {
|
||||
//createLog("info", "lst", "ocp", `There are no lots assigned currently`);
|
||||
return {
|
||||
success: true,
|
||||
message: "There are no lots assigned currenly.",
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
|
||||
if (l.data.length === 0 && !l.data.sucess) {
|
||||
createLog(
|
||||
"error",
|
||||
"lst",
|
||||
"ocp",
|
||||
`There was an error getting the lots: ${l?.message}`
|
||||
);
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting lots",
|
||||
data: [],
|
||||
};
|
||||
}
|
||||
const { data: print, error: printerError } = await tryCatch(getPrinters());
|
||||
|
||||
if (printerError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Error getting lots",
|
||||
data: printerError,
|
||||
};
|
||||
}
|
||||
|
||||
const printers: any = print.data ?? [];
|
||||
const lots: any = l?.data.length === 0 ? [] : l?.data;
|
||||
|
||||
//return;
|
||||
for (let i = 0; i < printers.length; i++) {
|
||||
// is the printer assinged in alplalabel online?
|
||||
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;
|
||||
}
|
||||
|
||||
// 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)
|
||||
)
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
26
lstV2/server/services/ocp/utils/getMachineId.ts
Normal file
26
lstV2/server/services/ocp/utils/getMachineId.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
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 {
|
||||
const getMac: any = await query(updateQuery, "Machine id check");
|
||||
|
||||
mac = getMac.data;
|
||||
// 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;
|
||||
};
|
||||
66
lstV2/server/services/ocp/utils/pausePrinter.ts
Normal file
66
lstV2/server/services/ocp/utils/pausePrinter.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import net from "net";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
|
||||
export const pausePrinter = async (printerData: any) => {
|
||||
const pause = new net.Socket();
|
||||
|
||||
if (printerData.name) {
|
||||
createLog(
|
||||
"debug",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`${printerData.name}: paused printed`
|
||||
);
|
||||
} else {
|
||||
createLog(
|
||||
"error",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`Unknown name on printer was just paused, Body sent over: ${printerData.name}`
|
||||
);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
pause.connect(printerData.port, printerData.ipAddress, async () => {
|
||||
// console.log("Connected to printer");
|
||||
pause.write("~PP");
|
||||
pause.end();
|
||||
});
|
||||
|
||||
pause.on("error", (error) => {
|
||||
createLog(
|
||||
"error",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`There was an error pausing the printer: ${JSON.stringify(
|
||||
error
|
||||
)}`
|
||||
);
|
||||
reject({
|
||||
success: true,
|
||||
message: "There was an error pausing the printer",
|
||||
data: error,
|
||||
});
|
||||
});
|
||||
|
||||
pause.on("end", () => {
|
||||
setTimeout(() => {
|
||||
if (!pause.destroyed) {
|
||||
createLog(
|
||||
"info",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`${printerData.name}: was force closed, in pausing trigger`
|
||||
);
|
||||
pause.destroy();
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
message: `${
|
||||
printerData?.name || printerData.ipAddress
|
||||
} Printer was paused`,
|
||||
});
|
||||
});
|
||||
};
|
||||
69
lstV2/server/services/ocp/utils/unpausePrinter.ts
Normal file
69
lstV2/server/services/ocp/utils/unpausePrinter.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import net from "net";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
|
||||
export const unPausePrinter = async (printerData: any) => {
|
||||
const pause = new net.Socket();
|
||||
|
||||
if (printerData.name) {
|
||||
createLog(
|
||||
"debug",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`${printerData.name}: unpaused printed`
|
||||
);
|
||||
} else {
|
||||
createLog(
|
||||
"error",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`Unknown name on printer was just unpaused, Body sent over: ${JSON.stringify(
|
||||
printerData
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
pause.connect(printerData.port, printerData.ipAddress, async () => {
|
||||
// console.log("Connected to printer");
|
||||
pause.write("~PS");
|
||||
pause.end();
|
||||
});
|
||||
|
||||
pause.on("error", (error) => {
|
||||
createLog(
|
||||
"error",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`There was an error unpausing the printer: ${JSON.stringify(
|
||||
error
|
||||
)}`
|
||||
);
|
||||
reject({
|
||||
success: true,
|
||||
message: "There was an error unpausing the printer",
|
||||
data: error,
|
||||
});
|
||||
});
|
||||
|
||||
pause.on("end", () => {
|
||||
setTimeout(() => {
|
||||
if (!pause.destroyed) {
|
||||
createLog(
|
||||
"info",
|
||||
"printerState",
|
||||
"ocp",
|
||||
`${printerData.name}: was force closed, in unpause menu`
|
||||
);
|
||||
pause.destroy();
|
||||
}
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
message: `${
|
||||
printerData?.name || printerData.ipAddress
|
||||
} Printer was unpaused`,
|
||||
});
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user