test(ocp): working on more migrations

This commit is contained in:
2025-04-04 17:13:15 -05:00
parent a7818b4ca3
commit 9f26f2334f
8 changed files with 224 additions and 16 deletions

View File

@@ -1,6 +1,3 @@
import { db } from "../../../../database/dbclient.js";
import { settings } from "../../../../database/schema/settings.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
import type { User } from "../../../types/users.js"; import type { User } from "../../../types/users.js";
import { alplaStockInv } from "./cycleCount/alplaStockInventory.js"; import { alplaStockInv } from "./cycleCount/alplaStockInventory.js";
import { emptyCount } from "./cycleCount/emptyCycleCount.js"; import { emptyCount } from "./cycleCount/emptyCycleCount.js";

View File

@@ -1,6 +1,6 @@
import { eq, gte, sql } from "drizzle-orm"; import { eq, gte, sql } from "drizzle-orm";
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { printers } from "../../../../../database/schema/printers.js"; import { printerData } from "../../../../../database/schema/printers.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 { prodEndpointCreation } from "../../../../globalUtils/createUrl.js"; import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
@@ -15,8 +15,8 @@ export const createLabel = async (data: any, userPrinted: any) => {
const { data: printer, error: printerError } = await tryCatch( const { data: printer, error: printerError } = await tryCatch(
db db
.select() .select()
.from(printers) .from(printerData)
.where(eq(printers.humanReadableId, data.printerID)) .where(eq(printerData.humanReadableId, data.printerID))
); );
const { data: settingsData, error: settingsError } = await tryCatch( const { data: settingsData, error: settingsError } = await tryCatch(
db.select().from(settings) db.select().from(settings)

View File

@@ -1,12 +1,10 @@
import { db } from "../../../../../database/dbclient.js"; import { db } from "../../../../../database/dbclient.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js";
import { printers } from "../../../../../database/schema/printers.js"; import { printerData } from "../../../../../database/schema/printers.js";
export const getPrinters = async () => { export const getPrinters = async () => {
const currentTime = new Date(Date.now()); const { data: printers, error: printerError } = await tryCatch(
db.select().from(printerData)
const { data: printerData, error: printerError } = await tryCatch(
db.select().from(printers)
); );
if (printerError) { if (printerError) {
@@ -17,5 +15,5 @@ export const getPrinters = async () => {
}; };
} }
return { success: true, message: "Printers", data: printerData }; return { success: true, message: "Printers", data: printers };
}; };

View File

@@ -0,0 +1,20 @@
import { getPrinters } from "./getPrinters.js";
export const printerCycle = async () => {
/**
* We will cycle through the printers to check there states.
*/
let printers = await getPrinters();
/**
* 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.
*/
};

View File

@@ -1,6 +1,5 @@
import { printers } from "../../../../../database/schema/printers.js";
import axios from "axios"; import axios from "axios";
import { printerData } from "../../../../../database/schema/printers.js";
import { sql } from "drizzle-orm"; import { sql } from "drizzle-orm";
import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js"; import { prodEndpointCreation } from "../../../../globalUtils/createUrl.js";
import { tryCatch } from "../../../../globalUtils/tryCatch.js"; import { tryCatch } from "../../../../globalUtils/tryCatch.js";
@@ -47,10 +46,10 @@ export const updatePrinters = async () => {
}; };
const { data, error } = await tryCatch( const { data, error } = await tryCatch(
db db
.insert(printers) .insert(printerData)
.values(printerStuff) .values(printerStuff)
.onConflictDoUpdate({ .onConflictDoUpdate({
target: printers.humanReadableId, target: printerData.humanReadableId,
set: { set: {
//humanReadableId: prodPrinterInfo[i].humanReadableId, //humanReadableId: prodPrinterInfo[i].humanReadableId,
name: prodPrinterInfo[i].name, name: prodPrinterInfo[i].name,

View File

@@ -0,0 +1,87 @@
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";
export const assignedPrinters = async () => {
const { data: l, error: lotError } = await tryCatch(getLots());
if (lotError) {
return {
success: false,
message: "Error getting lots",
data: lotError,
};
}
const { data: print, error: printerError } = await tryCatch(getPrinters());
if (printerError) {
return {
success: false,
message: "Error getting lots",
data: printerError,
};
}
const printers: any = print;
const lots: any = l;
for (let i = 0; i < printers.data.length; i++) {
// is the printer assinged in alplalabel online?
const assigned = lots.data.filter(
(p: any) => p.printerID === 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;
// // 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(),
// },
// });
//delaying 250ms
await delay(250);
}
};

View File

@@ -0,0 +1,52 @@
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,
});
});
resolve({
success: true,
message: `${
printerData?.name || printerData.ipAddress
} Printer was paused`,
});
});
};

View File

@@ -0,0 +1,55 @@
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,
});
});
resolve({
success: true,
message: `${
printerData?.name || printerData.ipAddress
} Printer was unpaused`,
});
});
};