refactor(ocp): lots of work for rfid and dyco contorl
This commit is contained in:
@@ -2,13 +2,14 @@ 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";
|
||||
import { strapperFaults } from "./plcTags/strapperFault.js";
|
||||
|
||||
let PLC = new Controller();
|
||||
let isDycoRunning = false;
|
||||
|
||||
// PLC address
|
||||
let plcAddress = "10.44.5.4";
|
||||
|
||||
let isReading = false;
|
||||
// Initialize the interval variable outside the function
|
||||
let plcCycle: any;
|
||||
let plcInterval = 500;
|
||||
@@ -31,17 +32,42 @@ export const dycoConnect = async () => {
|
||||
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);
|
||||
if (isReading) {
|
||||
createLog(
|
||||
"warn",
|
||||
"dyco",
|
||||
"ocp",
|
||||
"Skipping cycle: previous read still in progress."
|
||||
);
|
||||
return;
|
||||
}
|
||||
isReading = true; // Set flag
|
||||
try {
|
||||
await PLC.readTag(labelerTag);
|
||||
await PLC.readTag(palletSend);
|
||||
await PLC.readTag(strapperError);
|
||||
|
||||
// send the labeler tag data over
|
||||
labelerTagRead(labelerTag);
|
||||
// strapper check
|
||||
strapperFaults(strapperError);
|
||||
|
||||
// send the end of line check over.
|
||||
palletSendTag(palletSend);
|
||||
}, 500);
|
||||
// send the labeler tag data over
|
||||
labelerTagRead(labelerTag);
|
||||
|
||||
// send the end of line check over.
|
||||
palletSendTag(palletSend);
|
||||
} catch (error: any) {
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Error reading PLC tag: ${error.message}`
|
||||
);
|
||||
} finally {
|
||||
isReading = false; // Reset flag
|
||||
}
|
||||
}, plcInterval);
|
||||
});
|
||||
} catch (error) {
|
||||
createLog(
|
||||
@@ -50,6 +76,7 @@ export const dycoConnect = async () => {
|
||||
"ocp",
|
||||
`There was an error in the dyco: ${error}`
|
||||
);
|
||||
await PLC.disconnect();
|
||||
isDycoRunning = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,7 +4,10 @@ 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";
|
||||
import { stapperFaulted, strapperFaults } from "./strapperFault.js";
|
||||
|
||||
export let cameraPalletCheck = 20;
|
||||
export let currentPalletCheck = 0;
|
||||
let lastProcessedTimestamp = 0;
|
||||
|
||||
export const labelerTagRead = async (tagData: any) => {
|
||||
@@ -41,11 +44,48 @@ export const labelerTagRead = async (tagData: any) => {
|
||||
// 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);
|
||||
/**
|
||||
* add logic in to see if this is the first time we run this so we return an error to validate we are in sync.
|
||||
*/
|
||||
createLog(
|
||||
"info",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Current pallet at the wrapper is: ${numericString}`
|
||||
);
|
||||
if (dycoPrint[0].value === "1") {
|
||||
createLog("info", "dyco", "ocp", "Dyco will be printing the label");
|
||||
// send over to print.
|
||||
labelingProcess({ line: numericString });
|
||||
// if (!dycoControlCheck) {
|
||||
// createLog(
|
||||
// "error",
|
||||
// "dyco",
|
||||
// "ocp",
|
||||
// `Dyco was switch to be the printer guys. please validate the line is in sync, Current line is ${numericString}`
|
||||
// );
|
||||
// dycoControlCheck = true;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// check the stapper error logic.
|
||||
|
||||
if (stapperFaulted) {
|
||||
createLog("error", "dyco", "ocp", `Strapper is faulted.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// check if we need to manual check due to 20 pallets.
|
||||
if (currentPalletCheck <= cameraPalletCheck) {
|
||||
currentPalletCheck = currentPalletCheck + 1;
|
||||
labelingProcess({ line: numericString });
|
||||
} else {
|
||||
currentPalletCheck = 0;
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`You have reached 20 pallets since the last check please validate the labeler is still in sync.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (dycoPrint[0].value === "0") {
|
||||
@@ -56,7 +96,9 @@ export const labelerTagRead = async (tagData: any) => {
|
||||
"Rfid system is contorlling the printing"
|
||||
);
|
||||
// trigger the reader so we can get the label from the tag readers.
|
||||
await readTags("wrapper1");
|
||||
setTimeout(async () => {
|
||||
await readTags("wrapper1");
|
||||
}, 2 * 1000);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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";
|
||||
|
||||
// strapper related issues
|
||||
export let strapperActive = true;
|
||||
export let stapperFaulted = false;
|
||||
export let strapperFaultCount = 3; // move to db so we can control it outside the app
|
||||
|
||||
export const strapperFaults = async (tagData: any) => {
|
||||
const { data, error } = await tryCatch(db.select().from(settings));
|
||||
|
||||
if (error) {
|
||||
return { success: false, message: "Failed to get settings." };
|
||||
}
|
||||
|
||||
if (strapperActive) {
|
||||
// monitor strapper
|
||||
//console.log(`Strapper fault is ${strapperError.value}`);
|
||||
if (tagData.value && strapperFaultCount > 0) {
|
||||
// strapper faulted we want to start the trigger to force the check
|
||||
if (!stapperFaulted) {
|
||||
createLog(
|
||||
"error",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Strapper errored triggering manual checks will be required for the next ${strapperFaultCount}`
|
||||
);
|
||||
}
|
||||
|
||||
stapperFaulted = true;
|
||||
|
||||
// change move fault count to db....
|
||||
strapperFaultCount = 3;
|
||||
}
|
||||
}
|
||||
|
||||
const strapperCheckSetting = data.filter((n) => n.name === "strapperCheck");
|
||||
|
||||
if (strapperCheckSetting[0]?.value === "1") {
|
||||
if (stapperFaulted && strapperFaultCount > 0) {
|
||||
createLog(
|
||||
"warn",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`There was a strapper error, remaining pallets to check ${strapperFaultCount}.`
|
||||
);
|
||||
strapperFaultCount = strapperFaultCount - 1;
|
||||
return {
|
||||
success: false,
|
||||
message: `There was a strapper error, remaining pallets to check ${strapperFaultCount}.`,
|
||||
};
|
||||
} else {
|
||||
createLog(
|
||||
"debug",
|
||||
"dyco",
|
||||
"ocp",
|
||||
`Strapper check is active but not faulted, remaining pallets to check ${strapperFaultCount}.`
|
||||
);
|
||||
|
||||
// reset everything
|
||||
stapperFaulted = false;
|
||||
strapperFaultCount = 3; // move to db as well
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: `Strapper check is active but not faulted, remaining pallets to check ${strapperFaultCount}.`,
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user