Hi,
I am trying to create an event box from PLC events. At a time, only one event can occur. I have four events: CrustBreaker, ANodeRemoval, CavityScoop, and AnodeInstallation. When any event is active, the event with the necessary parameters will be captured, including the trigger date and time. However, I only want to send it to the next node when the same trigger is off. This is because at that time, I will also capture the end time and date. Unfortunately, when I try the sequence, all of the data is not being captured. It was captured when the trigger was ON, but when the trigger is OFF, all of the data is erased.
Can you help me figure out why the data is not being captured when the trigger is OFF?
// Initialize the flag and eventBox
let flagged = false;
let eventBox = msg.eventBox || null; // Retain previous eventBox data
// Extract the Activities value
let Activities = msg.payload["LEVEL2TAGS[3]"];
// Interpret individual bits from Activities
const CrustBreaker = (Activities & (1 << 0)) !== 0;
const AnodeRemoval = (Activities & (1 << 1)) !== 0;
const CavityScoop = (Activities & (1 << 2)) !== 0;
const NewAnodeInstall = (Activities & (1 << 3)) !== 0;
// Get current timestamp
const currentTimestamp = new Date().toISOString();
// Create a function to generate event IDs
function generateEventID(eventNum, potNum, anodeNum) {
return `${eventNum}_${potNum}_${anodeNum}`;
}
// Check if the event flag is set and the event has turned inactive
if (flagged && !CrustBreaker && !AnodeRemoval && !CavityScoop && !NewAnodeInstall) {
if (eventBox && !eventBox.eventInactivatedTime) {
eventBox.eventInactivatedTime = currentTimestamp.slice(11, 19);
eventBox.eventInactivatedDate = currentTimestamp.slice(0, 10);
// eventBox.totalEventSeconds = Math.floor((new Date(currentTimestamp) - new Date(eventBox.eventActivatedTime)) / 1000);
flagged = false;
}
}
// Handle the active event (using switch instead of if-else)
switch (true) {
case CrustBreaker:
eventBox = {
event: "CrustBreaker",
eventActivatedTime: currentTimestamp.slice(11, 19),
eventActivatedDate: currentTimestamp.slice(0, 10),
eventInactivatedTime: null,
eventInactivatedDate: null,
totalEventSeconds: 0
};
flagged = true;
break;
case AnodeRemoval:
eventBox = {
event: "AnodeRemoval",
eventActivatedTime: currentTimestamp.slice(11, 19),
eventActivatedDate: currentTimestamp.slice(0, 10),
eventInactivatedTime: null,
eventInactivatedDate: null,
totalEventSeconds: 0
};
flagged = true;
break;
case CavityScoop:
eventBox = {
event: "CavityScoop",
eventActivatedTime: currentTimestamp.slice(11, 19),
eventActivatedDate: currentTimestamp.slice(0, 10),
eventInactivatedTime: null,
eventInactivatedDate: null,
totalEventSeconds: 0
};
flagged = true;
break;
case NewAnodeInstall:
eventBox = {
event: "NewAnodeInstall",
eventActivatedTime: currentTimestamp.slice(11, 19),
eventActivatedDate: currentTimestamp.slice(0, 10),
eventInactivatedTime: null,
eventInactivatedDate: null,
totalEventSeconds: 0
};
flagged = true;
break;
default:
// Keep existing eventBox data
flagged = false;
}
// Set the event box and flag as part of the message payload
msg.eventBox = eventBox;
msg.flagged = flagged;
// If all events are inactive and flagged, send the message to the next node
//if (!flagged) {
return msg;
//} else {
// return null; // Don't pass the message to the next node yet
//}