12: Extend parsing of persistent alarms with graceperiods
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package com.r35157.jupiterperpsalarm.impl.ref;
|
package com.r35157.jupiterperpsalarm.impl.ref;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
public final class PriceAlarm {
|
public final class PriceAlarm {
|
||||||
|
|
||||||
public PriceAlarm(PriceAlarmDefinition definition, AlarmAction action) {
|
public PriceAlarm(PriceAlarmDefinition definition, AlarmAction action) {
|
||||||
@@ -25,16 +27,27 @@ public final class PriceAlarm {
|
|||||||
|
|
||||||
previousReached = reached;
|
previousReached = reached;
|
||||||
|
|
||||||
if (!enteredTriggeredSide) {
|
if (!reached) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (definition.trigger() == AlarmTrigger.ONETIME && triggerCount > 0) {
|
if (definition.trigger() == AlarmTrigger.ONETIME) {
|
||||||
|
if (!enteredTriggeredSide || triggerCount > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
triggerCount++;
|
trigger(price);
|
||||||
action.trigger(price, definition);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (definition.trigger() == AlarmTrigger.PERSISTENT) {
|
||||||
|
if (enteredTriggeredSide || persistentGracePeriodHasPassed()) {
|
||||||
|
trigger(price);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IllegalStateException("Unsupported alarm trigger: " + definition.trigger());
|
||||||
}
|
}
|
||||||
|
|
||||||
public PriceAlarmDefinition definition() {
|
public PriceAlarmDefinition definition() {
|
||||||
@@ -45,9 +58,32 @@ public final class PriceAlarm {
|
|||||||
return triggerCount;
|
return triggerCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean persistentGracePeriodHasPassed() {
|
||||||
|
if (lastTriggeredAt == null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ΩsecondsΩ gracePeriod = definition.triggerGracePeriod();
|
||||||
|
|
||||||
|
if (gracePeriod == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !Instant.now().isBefore(
|
||||||
|
lastTriggeredAt.plusSeconds(gracePeriod)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void trigger(OraclePrice price) {
|
||||||
|
triggerCount++;
|
||||||
|
lastTriggeredAt = Instant.now();
|
||||||
|
action.trigger(price, definition);
|
||||||
|
}
|
||||||
|
|
||||||
private final PriceAlarmDefinition definition;
|
private final PriceAlarmDefinition definition;
|
||||||
private final AlarmAction action;
|
private final AlarmAction action;
|
||||||
|
|
||||||
|
private Instant lastTriggeredAt;
|
||||||
private Boolean previousReached;
|
private Boolean previousReached;
|
||||||
private long triggerCount;
|
private long triggerCount;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public record PriceAlarmDefinition(
|
|||||||
PriceDirection direction,
|
PriceDirection direction,
|
||||||
BigDecimal target,
|
BigDecimal target,
|
||||||
AlarmTrigger trigger,
|
AlarmTrigger trigger,
|
||||||
ΩsecondsΩ triggerGracePeriodSeconds,
|
ΩsecondsΩ triggerGracePeriod,
|
||||||
AlarmSeverity severity,
|
AlarmSeverity severity,
|
||||||
String note
|
String note
|
||||||
) {
|
) {
|
||||||
@@ -26,9 +26,9 @@ public record PriceAlarmDefinition(
|
|||||||
throw new IllegalArgumentException("Target price cannot be negative (was: " + target.signum() + ")!");
|
throw new IllegalArgumentException("Target price cannot be negative (was: " + target.signum() + ")!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (triggerGracePeriodSeconds < 0) {
|
if (triggerGracePeriod < 0) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Trigger grace period cannot be negative: " + triggerGracePeriodSeconds
|
"Trigger grace period cannot be negative: " + triggerGracePeriod
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user