Skip to content

Commit

Permalink
Simplify boolean logic in Monitor and improve code comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aidnem committed Nov 25, 2024
1 parent 80d2a3d commit a2df49b
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions monitors/src/main/java/coppercore/monitors/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ public class Monitor {
BooleanSupplier isStateValid; // Supplier with which to check whether the value is acceptable
Runnable faultCallback; // Function to call when the fault happens

double triggeredTime = 0.0; // Timestamp when monitor was first triggered
// If this value is zero, the monitor has been triggered for less than 1 tick
double triggeredTime = -1.0; // Timestamp when monitor was first triggered
// If this value is less than or equal to zero, the monitor has been triggered for less than 1
// tick

boolean triggered = false; // Is the value currently unnacceptable?
boolean faulted = false; // Has the monitor detected a fault?
Expand Down Expand Up @@ -59,19 +60,30 @@ public void periodic(double currentTimeSeconds) {

triggered = !isStateValid.getAsBoolean();
if (triggered) {
if (triggeredTime == 0.0) {
// If triggered time is less than zero, this means it hasn't been set yet.
// Therefore, this is the first loop that the monitor is triggered and we should
// store the current timestamp to reference how long it's been triggered for later.
if (triggeredTime <= 0.0) {
triggeredTime = currentTimeSeconds;
}

if (currentTimeSeconds - triggeredTime >= timeToFault) {
faulted = true;
}
// When triggered, the monitor will fault if either:
// - It is already faulted (it can't transition from faulted to non-faulted while
// triggered)
// or
// - It has been triggered for the timeToFault.
faulted = faulted || ((currentTimeSeconds - triggeredTime) >= timeToFault);
} else {
if (!sticky) {
faulted = false;
}
// If the monitor isn't triggered, it will only be faulted if it is sticky and already
// faulted.
faulted = faulted && sticky;

triggeredTime = 0.0;
// Use -1 as a sentinel value to indicate that the triggered time hasn't been stored
// yet.
triggeredTime = -1.0;
}
if (faulted) {
faultCallback.run();
Expand Down

0 comments on commit a2df49b

Please sign in to comment.