feat(ocp): create and book in plus dyco controller implemented
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
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";
|
||||
|
||||
const st = 5;
|
||||
const sm = 55;
|
||||
const et = 6;
|
||||
const em = 5;
|
||||
|
||||
export const billingCheck = async () => {
|
||||
const now = new Date();
|
||||
const startTime = new Date();
|
||||
const endTime = new Date();
|
||||
|
||||
let isOutsideBilling = false;
|
||||
const { data, error } = await tryCatch(db.select().from(settings));
|
||||
if (error) {
|
||||
return { success: false, message: "Error in getting settings." };
|
||||
}
|
||||
const plant = data.filter((n) => n.name === "plantToken");
|
||||
// set everything up
|
||||
startTime.setHours(st, sm, 0, 0);
|
||||
endTime.setHours(et, em, 0, 0);
|
||||
|
||||
if (plant[0].value === "usflo1") {
|
||||
if (now >= startTime && now <= endTime) {
|
||||
createLog(
|
||||
"warn",
|
||||
"specialProcess",
|
||||
"ocp",
|
||||
`You are inside the billing window no labels will print! please wait`
|
||||
);
|
||||
isOutsideBilling = true;
|
||||
} else {
|
||||
createLog(
|
||||
"info",
|
||||
"specialProcess",
|
||||
"ocp",
|
||||
"Out side billing window ok to print"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
createLog(
|
||||
"debug",
|
||||
"specialProcess",
|
||||
"ocp",
|
||||
"This plant dose not check for billing"
|
||||
);
|
||||
}
|
||||
|
||||
return isOutsideBilling;
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
import { Controller, Tag } from "st-ethernet-ip";
|
||||
import { createLog } from "../../../../logger/logger.js";
|
||||
import { labelerTagRead } from "./plcTags/labelerTag.js";
|
||||
import { palletSendTag } from "./plcTags/palletSendTag.js";
|
||||
|
||||
let PLC = new Controller();
|
||||
let isDycoRunning = false;
|
||||
|
||||
// PLC address
|
||||
let plcAddress = "10.44.5.4";
|
||||
|
||||
// Initialize the interval variable outside the function
|
||||
let plcCycle: any;
|
||||
let plcInterval = 500;
|
||||
|
||||
// Create Tag Instances
|
||||
const labelerTag: any = new Tag("labeler.line_info");
|
||||
const palletSend = new Tag("Zone_6.Ready_to_Send");
|
||||
const strapperError = new Tag("Zone_3.Strapper_Faulted");
|
||||
|
||||
export const dycoConnect = async () => {
|
||||
// if we crash or start over reset the timers so we dont get duplicates
|
||||
clearInterval(plcCycle);
|
||||
if (isDycoRunning)
|
||||
return { success: false, message: "Dyco is already connected." };
|
||||
|
||||
// Remove all listeners before adding a new one to prevent memory leaks
|
||||
PLC.removeAllListeners("error");
|
||||
|
||||
try {
|
||||
await PLC.connect(plcAddress, 0).then(async () => {
|
||||
createLog("info", "dyco", "ocp", `We are connected to the dyco.`);
|
||||
isDycoRunning = true;
|
||||
let buffer = "";
|
||||
plcCycle = setInterval(async () => {
|
||||
await PLC.readTag(labelerTag);
|
||||
await PLC.readTag(palletSend);
|
||||
|
||||
// send the labeler tag data over
|
||||
labelerTagRead(labelerTag);
|
||||
|
||||
// send the end of line check over.
|
||||
palletSendTag(palletSend);
|
||||
}, 500);
|
||||
});
|
||||
} catch (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`There was an error in the dyco: ${error}`
|
||||
);
|
||||
isDycoRunning = false;
|
||||
}
|
||||
};
|
||||
|
||||
export const closeDyco = async () => {
|
||||
if (!isDycoRunning)
|
||||
return { success: false, message: "Dyco is not connected." };
|
||||
|
||||
console.log(`Closing the connection`);
|
||||
try {
|
||||
await PLC.disconnect();
|
||||
isDycoRunning = false;
|
||||
return {
|
||||
success: true,
|
||||
message: "Dyco Connection is now closed.",
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error closing the dyco connection.",
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
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 { readTags } from "../../../../../rfid/controller/readTags.js";
|
||||
import { labelingProcess } from "../../../labeling/labelProcess.js";
|
||||
|
||||
let lastProcessedTimestamp = 0;
|
||||
|
||||
export const labelerTagRead = async (tagData: any) => {
|
||||
/**
|
||||
* Reads the tag data from the Dyco PLC and processes it based on the feedback.
|
||||
*/
|
||||
// Convert tag data buffer to a string and extract numeric values
|
||||
const buffer = Buffer.from(tagData.value);
|
||||
const numericString = buffer.toString("utf8").replace(/[^0-9#]/g, "");
|
||||
|
||||
// Ignore empty or invalid tag data
|
||||
if (numericString === "#") {
|
||||
return;
|
||||
}
|
||||
|
||||
const tagTime = new Date(tagData.state.timestamp).getTime();
|
||||
// get the settings
|
||||
const { data: settingData, error: settingError } = await tryCatch(
|
||||
db.select().from(settings)
|
||||
);
|
||||
|
||||
if (settingError) {
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
"There was an error getting the settings"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// check the dyco settings
|
||||
const dycoPrint = settingData.filter((n) => n.name === "dycoPrint");
|
||||
// Only process if this is a new timestamp within the last 5 seconds
|
||||
if (tagTime !== lastProcessedTimestamp && Date.now() - tagTime <= 5000) {
|
||||
lastProcessedTimestamp = tagTime;
|
||||
//console.log(numericString, tagData.state.timestamp);
|
||||
if (dycoPrint[0].value === "1") {
|
||||
createLog("info", "dyco", "ocp", "Dyco will be printing the label");
|
||||
// send over to print.
|
||||
labelingProcess({ line: numericString });
|
||||
}
|
||||
|
||||
if (dycoPrint[0].value === "0") {
|
||||
createLog(
|
||||
"info",
|
||||
"dyco",
|
||||
"ocp",
|
||||
"Rfid system is contorlling the printing"
|
||||
);
|
||||
// trigger the reader so we can get the label from the tag readers.
|
||||
await readTags("wrapper1");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* setting to switch between rfid and dyco labeling
|
||||
*/
|
||||
@@ -0,0 +1,54 @@
|
||||
import { createLog } from "../../../../../logger/logger.js";
|
||||
import { pickedup } from "../../../../../ocme/controller/pickedup.js";
|
||||
import { triggerScanner } from "../../../../../ocme/controller/triggerCamera.js";
|
||||
|
||||
let lastProcessedTimestamp = 0;
|
||||
|
||||
export const palletSendTag = async (tagData: any) => {
|
||||
/**
|
||||
* Reads the tag data from the Dyco PLC and processes it based on the feedback.
|
||||
* We will only trigger the camera and removal of pending tags
|
||||
*/
|
||||
|
||||
const tagTime = new Date(tagData.state.timestamp).getTime();
|
||||
|
||||
// Only process if this is a new timestamp within the last 5 seconds
|
||||
if (
|
||||
tagTime !== lastProcessedTimestamp &&
|
||||
Date.now() - tagTime <= 5000 &&
|
||||
tagData.value
|
||||
) {
|
||||
lastProcessedTimestamp = tagTime;
|
||||
//console.log(tagData.state.timestamp);
|
||||
createLog(
|
||||
"info",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Station 6 is ${tagData.value ? "full" : "empty"}`
|
||||
);
|
||||
|
||||
// take the picture
|
||||
setTimeout(async () => {
|
||||
const scan: any = await triggerScanner();
|
||||
if (!scan.success) {
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Scanner failed to take a picture trying one more time in 10 seconds`
|
||||
);
|
||||
setTimeout(async () => {
|
||||
await triggerScanner();
|
||||
}, 10 * 1000);
|
||||
}
|
||||
}, 15 * 1000);
|
||||
}
|
||||
|
||||
if (
|
||||
tagTime !== lastProcessedTimestamp &&
|
||||
Date.now() - tagTime <= 5000 &&
|
||||
!tagData.value
|
||||
) {
|
||||
await pickedup({ runningNr: 1234, all: true });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,91 @@
|
||||
import axios from "axios";
|
||||
import { prodEndpointCreation } from "../../../../../globalUtils/createUrl.js";
|
||||
import { lstAuth } from "../../../../../index.js";
|
||||
import { createLog } from "../../../../logger/logger.js";
|
||||
import { db } from "../../../../../../database/dbclient.js";
|
||||
import { prodlabels } from "../../../../../../database/schema/prodLabels.js";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
|
||||
export const delieryInhouse = async (data: any) => {
|
||||
// update sscc so we can book in
|
||||
const SSCC = data.SSCC.slice(2);
|
||||
// api url
|
||||
const url = await prodEndpointCreation(
|
||||
"/public/v1.0/Warehousing/InhouseDelivery"
|
||||
);
|
||||
|
||||
// create bookin
|
||||
const newDelivery = {
|
||||
scannerId: 99,
|
||||
sscc: SSCC,
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await axios.post(url, newDelivery, {
|
||||
headers: {
|
||||
Authorization: `Basic ${lstAuth}`,
|
||||
accept: "text/plain",
|
||||
},
|
||||
});
|
||||
|
||||
if (res.data.Result !== 0) {
|
||||
createLog(
|
||||
"error",
|
||||
"labeling",
|
||||
"ocp",
|
||||
`${data.printer.name}, Error:${res.data.Message}`
|
||||
);
|
||||
//printerUpdate(data.printer, 7, "Error while deliverying inhouse.");
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `${data.printer.name} had an error while trying to deliver.`,
|
||||
data: res.data,
|
||||
};
|
||||
} // label was just delivered
|
||||
createLog(
|
||||
"info",
|
||||
"labeling",
|
||||
"ocp",
|
||||
`${parseInt(data.SSCC.slice(10, -1))}, was just delivered`
|
||||
);
|
||||
|
||||
try {
|
||||
await db
|
||||
.update(prodlabels)
|
||||
.set({
|
||||
status: "Delivered",
|
||||
upd_date: sql`NOW()`,
|
||||
})
|
||||
.where(
|
||||
eq(prodlabels.runningNr, parseInt(data.SSCC.slice(10, -1)))
|
||||
);
|
||||
} catch (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"labeling",
|
||||
"ocp",
|
||||
`Error updating ${parseInt(data.SSCC.slice(10, -1))} in the DB.`
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: `Error updating ${parseInt(
|
||||
data.SSCC.slice(10, -1)
|
||||
)} in the DB.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
createLog(
|
||||
"error",
|
||||
"labeling",
|
||||
"ocp",
|
||||
`${data.printer.name}, "Error: ${error}`
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: `Error deliverying ${parseInt(data.SSCC.slice(10, -1))}.`,
|
||||
data: error,
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
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 { query } from "../../../../sqlServer/prodSqlServer.js";
|
||||
|
||||
export const firstLotLabel = async (lot: any) => {
|
||||
/*
|
||||
currently this is only requested by slc
|
||||
|
||||
when a lot is finished and not set to over run we will trigger this function to insert data into the custom T_firstLotLabel table.
|
||||
|
||||
in nice label we look for unique id to run the process and then if the add date is older than 7 days we delete it for tracablility purpose.
|
||||
|
||||
we will also only do this for specific customers currently henkel is the requested.
|
||||
*/
|
||||
|
||||
/* work on the server side
|
||||
new table created in the _cus db
|
||||
CREATE TABLE T_firstLotLabel
|
||||
(
|
||||
ID INT IDENTITY(1,1) PRIMARY KEY,
|
||||
printerName varchar(max),
|
||||
add_date datetime
|
||||
-- Add other columns here as needed
|
||||
);
|
||||
the nicelabel trigger to be activated.
|
||||
*/
|
||||
const { data: settingData, error: settingError } = await tryCatch(
|
||||
db.select().from(settings)
|
||||
);
|
||||
|
||||
if (settingError) {
|
||||
return {
|
||||
success: false,
|
||||
message: "There was an error getting the settings.",
|
||||
settingError,
|
||||
};
|
||||
}
|
||||
// get the plantToken
|
||||
const plantToken = settingData.filter((n) => n.name === "plantToken");
|
||||
|
||||
createLog(
|
||||
"info",
|
||||
"ocp",
|
||||
"ocp",
|
||||
`Checking if we can create the first label lot.`
|
||||
);
|
||||
|
||||
if (plantToken[0].value === "usslc1") {
|
||||
const newLabel = `insert into T_firstLotLabel (printerName, add_date) VALUES('${lot.PrinterName}', getdate())`;
|
||||
|
||||
// if (lot.CustomerId === 40) {
|
||||
createLog(
|
||||
"info",
|
||||
"ocp",
|
||||
"ocp",
|
||||
`Creating first lot label for ${lot.MachineDescription}`
|
||||
);
|
||||
await query(newLabel, "newLotLabel");
|
||||
|
||||
createLog(
|
||||
"info",
|
||||
"ocp",
|
||||
"ocp",
|
||||
`Was created for ${lot.MachineDescription}`
|
||||
);
|
||||
//}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
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 { query } from "../../../../sqlServer/prodSqlServer.js";
|
||||
import { machineCheck } from "../../../../sqlServer/querys/ocp/machineId.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 {
|
||||
mac = await query(updateQuery, "Machine id check");
|
||||
// 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;
|
||||
};
|
||||
Reference in New Issue
Block a user