From 2e2699ab3c3786f36206629f069cfbd293c2dc29 Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Wed, 9 Apr 2025 17:49:03 -0500 Subject: [PATCH] refactor(ocp): change the way logs are brought in and other changes to clean the code up --- frontend/src/components/ocme/WrapperCard.tsx | 154 +++++++++++------- .../src/components/ocme/ocmeCycleCount.tsx | 2 +- .../components/production/ocp/LabelLog.tsx | 23 +-- .../ocp/ManualPrinting/ManualPrint.tsx | 34 ++-- .../src/components/production/ocp/OcpLogs.tsx | 8 +- .../src/components/production/ocp/OcpPage.tsx | 21 ++- .../production/ocp/PrinterStatus.tsx | 2 +- .../utils/querys/production/getOcmeInfo.tsx | 19 +++ .../src/utils/querys/production/labels.tsx | 2 +- .../src/utils/querys/production/ocpLogs.tsx | 2 +- server/services/logger/controller/clearLog.ts | 28 ++-- server/services/logger/controller/getLogs.ts | 5 +- server/services/ocme/controller/getInfo.ts | 3 +- .../ocp/controller/labeling/labelProcess.ts | 24 +-- .../dyco/plcTags/labelerTag.ts | 10 +- server/services/ocp/ocpService.ts | 6 + .../ocp/routes/printers/autoLabelerStart.ts | 41 +++++ .../ocp/routes/printers/autoLabelerStop.ts | 41 +++++ .../specialProcesses/dyco/connection.ts | 2 +- server/services/ocp/utils/checkAssignments.ts | 4 +- 20 files changed, 307 insertions(+), 124 deletions(-) create mode 100644 frontend/src/utils/querys/production/getOcmeInfo.tsx create mode 100644 server/services/ocp/routes/printers/autoLabelerStart.ts create mode 100644 server/services/ocp/routes/printers/autoLabelerStop.ts diff --git a/frontend/src/components/ocme/WrapperCard.tsx b/frontend/src/components/ocme/WrapperCard.tsx index 2bcd9c5..6b4cb0e 100644 --- a/frontend/src/components/ocme/WrapperCard.tsx +++ b/frontend/src/components/ocme/WrapperCard.tsx @@ -12,23 +12,28 @@ import { TableRow, } from "../ui/table"; import { toast } from "sonner"; +import { getOcmeInfo } from "@/utils/querys/production/getOcmeInfo"; +import { useQuery } from "@tanstack/react-query"; +import { format } from "date-fns"; +import { Trash } from "lucide-react"; const currentPallets = [ { key: "line", label: "Line" }, - { key: "runningNr", label: "Running #" }, + { key: "runningNumber", label: "Running #" }, { key: "upd_date", label: "Date Scanned" }, { key: "waitingfor", label: "Waiting For" }, { key: "clear", label: "Clear" }, ]; -const currentTags = [ - { key: "line", label: "Line" }, - { key: "printerName", label: "Printer" }, - { key: "runningNr", label: "Running #" }, - { key: "upd_date", label: "Label date" }, - { key: "status", label: "Label Status" }, -]; +// const currentTags = [ +// { key: "line", label: "Line" }, +// { key: "printerName", label: "Printer" }, +// { key: "runningNr", label: "Running #" }, +// { key: "upd_date", label: "Label date" }, +// { key: "status", label: "Label Status" }, +// ]; export default function WrapperManualTrigger() { + const { data, isError, isLoading } = useQuery(getOcmeInfo()); const cameraTrigger = async () => { try { const res = await axios.get("/ocme/api/v1/manualCameraTrigger"); @@ -46,6 +51,42 @@ export default function WrapperManualTrigger() { //stoast.success(error.data.message); } }; + + const clearLabel = async (d: any) => { + const data = { + runningNr: d.runningNr, + }; + + try { + const res = await axios.patch("/ocme/api/v1/pickedUp", data); + + if (res.data.success) { + toast.success( + `${d.runningNr} was just removed from being picked up.` + ); + return; + } + + if (!res.data.success) { + toast.error(res.data.message); + } + } catch (error) { + console.log(error); + //stoast.success(error.data.message); + } + }; + + if (isError) { + return ( +
+

+ There was an error getting wrapper scans +

+
+ ); + } + + const info = data?.filter((r: any) => r.areaFrom === "wrapper_1"); return ( @@ -59,71 +100,66 @@ export default function WrapperManualTrigger() { ))} - - {Array(3) - .fill(0) - .map((_, i) => ( - + {isLoading ? ( + + {Array(3) + .fill(0) + .map((_, i) => ( + + + + + + + + + + + + + + + + + + ))} + + ) : ( + + {info.map((i: any) => ( + - + {i.lineNum} + {i.runningNr} - + {format( + i?.upd_date.replace("Z", ""), + "M/d/yyyy hh:mm" + )} + {i.waitingFor} - - - - - - - + ))} - - - - - - - - {currentTags.map((l) => ( - {l.label} - ))} - - - - {Array(3) - .fill(0) - .map((_, i) => ( - - - - - - - - - - - - - - - - - - ))} - + + )}
+

-

Manual Triggers

+

Manual Trigger

-
diff --git a/frontend/src/components/ocme/ocmeCycleCount.tsx b/frontend/src/components/ocme/ocmeCycleCount.tsx index c1c7ce2..f2cf7db 100644 --- a/frontend/src/components/ocme/ocmeCycleCount.tsx +++ b/frontend/src/components/ocme/ocmeCycleCount.tsx @@ -184,7 +184,7 @@ export default function OcmeCycleCount() { {data.map((i: any) => { let classname = ``; if ( - i.info === "Quality Check Required" + i.info === "Validate pallet is ok." ) { classname = `bg-red-500`; } diff --git a/frontend/src/components/production/ocp/LabelLog.tsx b/frontend/src/components/production/ocp/LabelLog.tsx index d38e3e2..ed972e3 100644 --- a/frontend/src/components/production/ocp/LabelLog.tsx +++ b/frontend/src/components/production/ocp/LabelLog.tsx @@ -5,7 +5,6 @@ import { Table, TableBody, TableCell, - TableFooter, TableHead, TableHeader, TableRow, @@ -26,17 +25,13 @@ const labelLogs = [ ]; export default function LabelLog() { const { data, isError, isLoading } = useQuery(getlabels("4")); - //const {user} = useSessionStore(); - //const {settings} = useSettingStore(); - //const server = settings.filter((n) => n.name === "server")[0]?.value || ""; - - //const roles = ["admin", "manager", "operator"]; if (isError) { return (

Labels for the last 2 hours

+ @@ -77,7 +72,13 @@ export default function LabelLog() { const labelData = data ? data : []; return ( -

Labels for the last 2 hours

+

+ {labelData.length === 0 ? ( + No labels have been printed in the last 2 hours + ) : ( + Labels for the last 2 hours + )} +

@@ -115,7 +116,7 @@ export default function LabelLog() { ) : ( {labelData.map((label: any) => ( - + {label.line} @@ -139,15 +140,15 @@ export default function LabelLog() { )} - + {/* {labelData.length === 0 && ( -
+

No labels have been printed in the last 2 hours

)} - + */}
); diff --git a/frontend/src/components/production/ocp/ManualPrinting/ManualPrint.tsx b/frontend/src/components/production/ocp/ManualPrinting/ManualPrint.tsx index 59daf69..8cff539 100644 --- a/frontend/src/components/production/ocp/ManualPrinting/ManualPrint.tsx +++ b/frontend/src/components/production/ocp/ManualPrinting/ManualPrint.tsx @@ -1,31 +1,43 @@ -import {Button} from "@/components/ui/button"; -import {useSessionStore} from "@/lib/store/sessionStore"; +import { Button } from "@/components/ui/button"; +import { useSessionStore } from "@/lib/store/sessionStore"; //import {useSettingStore} from "@/lib/store/useSettings"; -import {LotType} from "@/types/lots"; -import {Tag} from "lucide-react"; -import {toast} from "sonner"; -import {manualPrintLabels} from "./ManualPrintLabel"; +import { LotType } from "@/types/lots"; +import { Tag } from "lucide-react"; +import { toast } from "sonner"; +import { manualPrintLabels } from "./ManualPrintLabel"; +import { useState } from "react"; -export default function ManualPrint({lot}: {lot: LotType}) { - const {user} = useSessionStore(); +export default function ManualPrint({ lot }: { lot: LotType }) { + const { user } = useSessionStore(); + const [printing, setPrinting] = useState(false); //const {settings} = useSettingStore(); //const server = settings.filter((n) => n.name === "server")[0]?.value; //const serverPort = settings.filter((n) => n.name === "serverPort")[0]?.value; //const serverUrl = `http://${server}:${serverPort}`; const handlePrintLabel = async (lot: LotType) => { - //console.log(lot); - const labels: any = await manualPrintLabels(lot, user); if (labels.success) { toast.success(labels.message); + setTimeout(() => { + setPrinting(false); + }, 5 * 1000); + setPrinting(true); } else { toast.error(labels.message); + setTimeout(() => { + setPrinting(false); + }, 5 * 1000); } }; return ( - ); diff --git a/frontend/src/components/production/ocp/OcpLogs.tsx b/frontend/src/components/production/ocp/OcpLogs.tsx index 7f9b0d9..0b9d3c1 100644 --- a/frontend/src/components/production/ocp/OcpLogs.tsx +++ b/frontend/src/components/production/ocp/OcpLogs.tsx @@ -86,7 +86,13 @@ export default function OcpLogs() { return ( -

Labels for the last 2 hours

+

+ {data?.length === 0 ? ( + No errors in the last 4 hours + ) : ( + Logs for the last 4 hours + )} +

diff --git a/frontend/src/components/production/ocp/OcpPage.tsx b/frontend/src/components/production/ocp/OcpPage.tsx index 5e2aee7..48ce501 100644 --- a/frontend/src/components/production/ocp/OcpPage.tsx +++ b/frontend/src/components/production/ocp/OcpPage.tsx @@ -1,19 +1,19 @@ import WrapperManualTrigger from "@/components/ocme/WrapperCard"; -import LabelLog from "./LabelLog"; + import Lots from "./Lots"; import OcpLogs from "./OcpLogs"; import PrinterStatus from "./PrinterStatus"; import { useSettingStore } from "@/lib/store/useSettings"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; +import LabelLog from "./LabelLog"; export default function OCPPage() { const { settings } = useSettingStore(); - const server = settings.filter((n) => n.plantToken === "usday1"); - console.log(server); + let server = settings.filter((n) => n.name === "server"); return (
-
+
@@ -33,15 +33,18 @@ export default function OCPPage() {
-
- -
+
-
- {server.length >= 1 && ( +
+ {server[0].value === "usday1vms006" && ( +
+ +
+ )} + {server[0].value === "localhost" && (
diff --git a/frontend/src/components/production/ocp/PrinterStatus.tsx b/frontend/src/components/production/ocp/PrinterStatus.tsx index f55bdaf..4c0171e 100644 --- a/frontend/src/components/production/ocp/PrinterStatus.tsx +++ b/frontend/src/components/production/ocp/PrinterStatus.tsx @@ -81,7 +81,7 @@ export default function PrinterStatus() { {l.label} ))} - {" "} + {isLoading ? ( {Array(5) diff --git a/frontend/src/utils/querys/production/getOcmeInfo.tsx b/frontend/src/utils/querys/production/getOcmeInfo.tsx new file mode 100644 index 0000000..7810772 --- /dev/null +++ b/frontend/src/utils/querys/production/getOcmeInfo.tsx @@ -0,0 +1,19 @@ +import { queryOptions } from "@tanstack/react-query"; +import axios from "axios"; + +export function getOcmeInfo() { + return queryOptions({ + queryKey: ["ocmeInfo"], + queryFn: () => fetchSettings(), + staleTime: 1000, + refetchInterval: 2 * 2000, + refetchOnWindowFocus: true, + }); +} + +const fetchSettings = async () => { + const { data } = await axios.get(`/ocme/api/v1/getInfo`); + // if we are not localhost ignore the devDir setting. + //const url: string = window.location.host.split(":")[0]; + return data.data ?? []; +}; diff --git a/frontend/src/utils/querys/production/labels.tsx b/frontend/src/utils/querys/production/labels.tsx index 8e4b72a..c2fb79c 100644 --- a/frontend/src/utils/querys/production/labels.tsx +++ b/frontend/src/utils/querys/production/labels.tsx @@ -6,7 +6,7 @@ export function getlabels(hours: string) { queryKey: ["labels"], queryFn: () => fetchSettings(hours), - staleTime: 1000, + //staleTime: 1000, refetchInterval: 2 * 2000, refetchOnWindowFocus: true, }); diff --git a/frontend/src/utils/querys/production/ocpLogs.tsx b/frontend/src/utils/querys/production/ocpLogs.tsx index e6e5d17..a273ca8 100644 --- a/frontend/src/utils/querys/production/ocpLogs.tsx +++ b/frontend/src/utils/querys/production/ocpLogs.tsx @@ -14,7 +14,7 @@ export function getOcpLogs(hours: string) { const fetchSettings = async (hours: string) => { const { data } = await axios.get( - `/api/logger/logs?service=ocp&service=rfid&level=error&level=warn&hours=${hours}` + `/api/logger/logs?service=ocp&service=rfid&service=dyco&level=error&level=warn&hours=${hours}` ); // if we are not localhost ignore the devDir setting. //const url: string = window.location.host.split(":")[0]; diff --git a/server/services/logger/controller/clearLog.ts b/server/services/logger/controller/clearLog.ts index 05ae5ef..47fbf7a 100644 --- a/server/services/logger/controller/clearLog.ts +++ b/server/services/logger/controller/clearLog.ts @@ -1,7 +1,7 @@ -import {eq, sql} from "drizzle-orm"; -import {db} from "../../../../database/dbclient.js"; -import {logs} from "../../../../database/schema/logs.js"; -import {createLog} from "../logger.js"; +import { eq, sql } from "drizzle-orm"; +import { db } from "../../../../database/dbclient.js"; +import { logs } from "../../../../database/schema/logs.js"; +import { createLog } from "../logger.js"; export const clearLog = async (id: string) => { /** @@ -10,13 +10,21 @@ export const clearLog = async (id: string) => { try { const clear = await db - .update(logs) - .set({checked: true, checkedAt: sql`NOW()`}) - .where(eq(logs.log_id, id)); + .update(logs) + .set({ checked: true, created_at: sql`NOW()` }) + .where(eq(logs.log_id, id)); createLog("info", "lst", "logger", "Log just cleared."); - return {success: true, message: "Log was just cleared."}; + return { success: true, message: "Log was just cleared." }; } catch (error) { - createLog("error", "lst", "logger", "There was an error clearing the log."); - return {success: false, message: "There was an error clearing the log."}; + createLog( + "error", + "lst", + "logger", + "There was an error clearing the log." + ); + return { + success: false, + message: "There was an error clearing the log.", + }; } }; diff --git a/server/services/logger/controller/getLogs.ts b/server/services/logger/controller/getLogs.ts index 78bcd52..2262e5b 100644 --- a/server/services/logger/controller/getLogs.ts +++ b/server/services/logger/controller/getLogs.ts @@ -1,4 +1,4 @@ -import { and, eq, gte, inArray, lte, sql } from "drizzle-orm"; +import { and, desc, eq, gte, inArray, lte, sql } from "drizzle-orm"; import { db } from "../../../../database/dbclient.js"; import { logs } from "../../../../database/schema/logs.js"; import { createLog } from "../logger.js"; @@ -21,7 +21,8 @@ export const getLogs = async (data: any) => { inArray(logs.level, data.level), eq(logs.checked, checked) ) - ); + ) + .orderBy(desc(logs.created_at)); return { success: true, message: "logs returned", data: logData }; } catch (error) { diff --git a/server/services/ocme/controller/getInfo.ts b/server/services/ocme/controller/getInfo.ts index 5298643..c4a9956 100644 --- a/server/services/ocme/controller/getInfo.ts +++ b/server/services/ocme/controller/getInfo.ts @@ -3,6 +3,7 @@ import { ocmeData } from "../../../../database/schema/ocme.js"; import { differenceInMinutes } from "date-fns"; import { createLog } from "../../logger/logger.js"; import { eq } from "drizzle-orm"; +import { timeZoneFix } from "../../../globalUtils/timeZoneFix.js"; export const getInfo = async () => { let ocmeInfo: any = []; @@ -16,7 +17,7 @@ export const getInfo = async () => { ocmeInfo = ocmeInfo.map((o: any) => { const now = new Date(Date.now()); //const strippedDate = o.add_Date.replace("Z", ""); - const diff = differenceInMinutes(now, o.add_Date); + const diff = differenceInMinutes(timeZoneFix(), o.add_Date); return { ...o, waitingFor: diff }; }); createLog( diff --git a/server/services/ocp/controller/labeling/labelProcess.ts b/server/services/ocp/controller/labeling/labelProcess.ts index 8bc5ba3..17277de 100644 --- a/server/services/ocp/controller/labeling/labelProcess.ts +++ b/server/services/ocp/controller/labeling/labelProcess.ts @@ -60,11 +60,11 @@ export const labelingProcess = async ({ "error", "labeling", "ocp", - `There is not a lot assigned to ${macId[0]?.Name}.` + `There is not a lot assigned to ${line}.` ); return { success: false, - message: `There is not a lot assigned to ${macId[0]?.Name}.`, + message: `There is not a lot assigned to ${line}.`, }; } } @@ -128,7 +128,7 @@ export const labelingProcess = async ({ } // if there are more than 2 lots it might be an auto labeler, autolabeler will be defined by its id for now only dayton - if (filteredLot.length > 2 && plantToken[0].value !== "usday1") { + if (filteredLot?.length > 2 && plantToken[0].value !== "usday1") { createLog( "error", "labeling", @@ -214,15 +214,15 @@ export const labelingProcess = async ({ // create the label const label = await createLabel(filteredLot[0], userPrinted); - if (!label.success) { - createLog( - "error", - "labeling", - "ocp", - `There was an error creating the label: ${label.message}` - ); - return { sucess: false, message: label.message, data: label.data }; - } + // if (!label.success) { + // createLog( + // "error", + // "labeling", + // "ocp", + // `There was an error creating the label: ${label.message}` + // ); + // return { sucess: false, message: label.message, data: label.data }; + // } // send over to be booked in if we can do it. const bookin = settingData.filter((s) => s.name === "bookin"); diff --git a/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts b/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts index b67e66c..f1def70 100644 --- a/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts +++ b/server/services/ocp/controller/specialProcesses/dyco/plcTags/labelerTag.ts @@ -75,8 +75,16 @@ export const labelerTagRead = async (tagData: any) => { // check if we need to manual check due to 20 pallets. if (currentPalletCheck <= cameraPalletCheck) { - currentPalletCheck = currentPalletCheck + 1; + currentPalletCheck++; labelingProcess({ line: numericString }); + createLog( + "info", + "dyco", + "ocp", + `You have printed ${currentPalletCheck} pallets, remaining until ${ + cameraPalletCheck - currentPalletCheck + }.` + ); } else { currentPalletCheck = 0; createLog( diff --git a/server/services/ocp/ocpService.ts b/server/services/ocp/ocpService.ts index 791d91a..db7cc3b 100644 --- a/server/services/ocp/ocpService.ts +++ b/server/services/ocp/ocpService.ts @@ -17,6 +17,9 @@ import { assignedPrinters } from "./utils/checkAssignments.js"; import { printerCycle } from "./controller/printers/printerCycle.js"; import stopPrinterCycle from "./routes/printers/stopCycle.js"; import startPrinterCycle from "./routes/printers/startCycle.js"; +import { printerCycleAutoLabelers } from "./controller/printers/printerCycleAutoLabelers.js"; +import AutostartPrinterCycle from "./routes/printers/autoLabelerStart.js"; +import AutostopPrinterCycle from "./routes/printers/autoLabelerStop.js"; const app = new OpenAPIHono(); @@ -27,6 +30,8 @@ const routes = [ updateprinters, startPrinterCycle, stopPrinterCycle, + AutostartPrinterCycle, + AutostopPrinterCycle, // lots getLots, // labeling @@ -76,6 +81,7 @@ setTimeout(async () => { await updatePrinters(); await assignedPrinters(); printerCycle(); + printerCycleAutoLabelers(); } }, 10 * 1000); diff --git a/server/services/ocp/routes/printers/autoLabelerStart.ts b/server/services/ocp/routes/printers/autoLabelerStart.ts new file mode 100644 index 0000000..9967c6e --- /dev/null +++ b/server/services/ocp/routes/printers/autoLabelerStart.ts @@ -0,0 +1,41 @@ +// an external way to creating logs +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { authMiddleware } from "../../../auth/middleware/authMiddleware.js"; +import { responses } from "../../../../globalUtils/routeDefs/responses.js"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import { printerCycle } from "../../controller/printers/printerCycle.js"; + +const app = new OpenAPIHono({ strict: false }); + +app.openapi( + createRoute({ + tags: ["ocp:printers"], + summary: "starts the printers cycling.", + method: "get", + path: "/startsprintercycle", + middleware: authMiddleware, + //description: "This might be a temp soltuin during the transtion between versions", + // request: { + // body: {content: {"application/json": {schema: CreateLog}}}, + // }, + responses: responses(), + }), + async (c) => { + const { data, error } = await tryCatch(printerCycle()); + const dataError: any = error; + if (error) { + return c.json({ + success: false, + message: "Error in stopping the printer cycle", + data: dataError?.data, + }); + } + const getData: any = data; + return c.json({ + success: getData?.success, + message: getData?.message, + data: getData.data ?? [], + }); + } +); +export default app; diff --git a/server/services/ocp/routes/printers/autoLabelerStop.ts b/server/services/ocp/routes/printers/autoLabelerStop.ts new file mode 100644 index 0000000..f651d0e --- /dev/null +++ b/server/services/ocp/routes/printers/autoLabelerStop.ts @@ -0,0 +1,41 @@ +// an external way to creating logs +import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; +import { authMiddleware } from "../../../auth/middleware/authMiddleware.js"; +import { responses } from "../../../../globalUtils/routeDefs/responses.js"; +import { tryCatch } from "../../../../globalUtils/tryCatch.js"; +import { stopPrinterCycle } from "../../controller/printers/printerCycle.js"; + +const app = new OpenAPIHono({ strict: false }); + +app.openapi( + createRoute({ + tags: ["ocp:printers"], + summary: "Stops the printers cycling.", + method: "get", + path: "/stopprintercycle", + middleware: authMiddleware, + //description: "This might be a temp soltuin during the transtion between versions", + // request: { + // body: {content: {"application/json": {schema: CreateLog}}}, + // }, + responses: responses(), + }), + async (c) => { + const { data, error } = await tryCatch(stopPrinterCycle()); + const dataError: any = error; + if (error) { + return c.json({ + success: false, + message: "Error in stopping the printer cycle", + data: dataError?.data, + }); + } + const getData: any = data; + return c.json({ + success: getData?.success, + message: getData?.message, + data: getData.data ?? [], + }); + } +); +export default app; diff --git a/server/services/ocp/routes/specialProcesses/dyco/connection.ts b/server/services/ocp/routes/specialProcesses/dyco/connection.ts index ff7aabd..f08b0fb 100644 --- a/server/services/ocp/routes/specialProcesses/dyco/connection.ts +++ b/server/services/ocp/routes/specialProcesses/dyco/connection.ts @@ -49,7 +49,7 @@ app.openapi( return c.json({ success: getData?.success, message: getData?.message, - data: getData.data ?? [], + data: getData?.data ?? [], }); } ); diff --git a/server/services/ocp/utils/checkAssignments.ts b/server/services/ocp/utils/checkAssignments.ts index cc51f3c..e84f5de 100644 --- a/server/services/ocp/utils/checkAssignments.ts +++ b/server/services/ocp/utils/checkAssignments.ts @@ -28,8 +28,8 @@ export const assignedPrinters = async () => { }; } - const printers: any = print.data; - const lots: any = l.data; + const printers: any = print.data ?? []; + const lots: any = l.data ?? []; for (let i = 0; i < printers.length; i++) { // is the printer assinged in alplalabel online?