refactor(ocp): plc reading changes to disconnect and reconnect

it was found that there were some errors that spammed the log and caused the server to actually stop
responding and crash weirdly so added a disconnect and reconnect back. so we can figure out whats
going on.
This commit is contained in:
2025-12-30 10:55:28 -06:00
parent 6cbffa4ac5
commit e8a2ef8b85

View File

@@ -20,84 +20,90 @@ const palletSend = new Tag("Zone_6.Ready_to_Send");
const strapperError = new Tag("Zone_3.Strapper_Faulted"); const strapperError = new Tag("Zone_3.Strapper_Faulted");
export const dycoConnect = async () => { export const dycoConnect = async () => {
// if we crash or start over reset the timers so we dont get duplicates // if we crash or start over reset the timers so we dont get duplicates
clearInterval(plcCycle); clearInterval(plcCycle);
if (isDycoRunning) if (isDycoRunning)
return { success: false, message: "Dyco is already connected." }; return { success: false, message: "Dyco is already connected." };
// Remove all listeners before adding a new one to prevent memory leaks // Remove all listeners before adding a new one to prevent memory leaks
PLC.removeAllListeners("error"); PLC.removeAllListeners("error");
try { try {
await PLC.connect(plcAddress, 0).then(async () => { await PLC.connect(plcAddress, 0).then(async () => {
createLog("info", "dyco", "ocp", `We are connected to the dyco.`); createLog("info", "dyco", "ocp", `We are connected to the dyco.`);
isDycoRunning = true; isDycoRunning = true;
plcCycle = setInterval(async () => { plcCycle = setInterval(async () => {
if (isReading) { if (isReading) {
createLog( createLog(
"debug", "debug",
"dyco", "dyco",
"ocp", "ocp",
"Skipping cycle: previous read still in progress." "Skipping cycle: previous read still in progress.",
); );
return; return;
} }
isReading = true; // Set flag isReading = true; // Set flag
try { try {
await PLC.readTag(labelerTag); await PLC.readTag(labelerTag);
await PLC.readTag(palletSend); await PLC.readTag(palletSend);
await PLC.readTag(strapperError); await PLC.readTag(strapperError);
// strapper check // strapper check
strapperFaults(strapperError); strapperFaults(strapperError);
// send the labeler tag data over // send the labeler tag data over
labelerTagRead(labelerTag); labelerTagRead(labelerTag);
// send the end of line check over. // send the end of line check over.
palletSendTag(palletSend); palletSendTag(palletSend);
} catch (error: any) { } catch (error: any) {
createLog( createLog(
"error", "error",
"dyco", "dyco",
"ocp", "ocp",
`Error reading PLC tag: ${error.message}` `Error reading PLC tag: ${error.message}`,
); );
} finally { // if we error out we want to disconnect and reconnect
isReading = false; // Reset flag closeDyco();
} setTimeout(() => {
}, plcInterval); createLog("info", "dyco", "ocp", `Reconnecting to the dyco`);
}); dycoConnect();
} catch (error) { }, 2 * 1000);
createLog( } finally {
"error", isReading = false; // Reset flag
"dyco", }
"ocp", }, plcInterval);
`There was an error in the dyco: ${error}` });
); } catch (error) {
await PLC.disconnect(); createLog(
isDycoRunning = false; "error",
} "dyco",
"ocp",
`There was an error in the dyco: ${error}`,
);
await PLC.disconnect();
isDycoRunning = false;
}
}; };
export const closeDyco = async () => { export const closeDyco = async () => {
if (!isDycoRunning) if (!isDycoRunning)
return { success: false, message: "Dyco is not connected." }; return { success: false, message: "Dyco is not connected." };
console.log(`Closing the connection`); console.log(`Closing the connection`);
try { try {
await PLC.disconnect(); await PLC.disconnect();
isDycoRunning = false; isDycoRunning = false;
return { return {
success: true, success: true,
message: "Dyco Connection is now closed.", message: "Dyco Connection is now closed.",
}; };
} catch (error) { } catch (error) {
console.log(error); console.log(error);
return { return {
success: false, success: false,
message: "There was an error closing the dyco connection.", message: "There was an error closing the dyco connection.",
}; };
} }
}; };