12: Extend parsing of persistent alarms with graceperiods
This commit is contained in:
+54
-3
@@ -59,8 +59,8 @@ public final class AlarmConfigurationParser {
|
||||
|
||||
BigDecimal target = parseTarget(cursor.nextToken("target"));
|
||||
|
||||
AlarmTrigger trigger = AlarmTrigger.valueOf(
|
||||
cursor.nextToken("trigger").toUpperCase(Locale.ROOT)
|
||||
TriggerConfiguration triggerConfiguration = parseTrigger(
|
||||
cursor.nextToken("trigger")
|
||||
);
|
||||
|
||||
AlarmSeverity severity = AlarmSeverity.valueOf(
|
||||
@@ -81,7 +81,8 @@ public final class AlarmConfigurationParser {
|
||||
asset,
|
||||
direction,
|
||||
target,
|
||||
trigger,
|
||||
triggerConfiguration.trigger(),
|
||||
triggerConfiguration.gracePeriod(),
|
||||
severity,
|
||||
note
|
||||
);
|
||||
@@ -109,6 +110,50 @@ public final class AlarmConfigurationParser {
|
||||
return target;
|
||||
}
|
||||
|
||||
private static TriggerConfiguration parseTrigger(String triggerText) {
|
||||
String normalized = triggerText.toUpperCase(Locale.ROOT);
|
||||
|
||||
if (normalized.equals("ONETIME")) {
|
||||
return new TriggerConfiguration(AlarmTrigger.ONETIME, 0);
|
||||
}
|
||||
|
||||
if (normalized.equals("PERSISTENT")) {
|
||||
return new TriggerConfiguration(AlarmTrigger.PERSISTENT, 0);
|
||||
}
|
||||
|
||||
if (normalized.startsWith("PERSISTENT:")) {
|
||||
String graceText = normalized.substring("PERSISTENT:".length());
|
||||
|
||||
if (graceText.isEmpty()) {
|
||||
throw new IllegalArgumentException("Missing persistent grace period: " + triggerText);
|
||||
}
|
||||
|
||||
ΩsecondsΩ gracePeriodSeconds;
|
||||
try {
|
||||
gracePeriodSeconds = Integer.parseInt(graceText);
|
||||
} catch (NumberFormatException exception) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid persistent grace period: " + triggerText,
|
||||
exception
|
||||
);
|
||||
}
|
||||
|
||||
if (gracePeriodSeconds < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
"Persistent grace period cannot be negative: " + triggerText
|
||||
);
|
||||
}
|
||||
|
||||
return new TriggerConfiguration(AlarmTrigger.PERSISTENT, gracePeriodSeconds);
|
||||
}
|
||||
|
||||
if (normalized.startsWith("ONETIME:")) {
|
||||
throw new IllegalArgumentException("ONETIME cannot have a grace period: " + triggerText);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unknown trigger: " + triggerText);
|
||||
}
|
||||
|
||||
private static void validateTarget(BigDecimal target, String originalTargetStr) {
|
||||
if (target.compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new IllegalArgumentException(
|
||||
@@ -274,6 +319,12 @@ public final class AlarmConfigurationParser {
|
||||
}
|
||||
}
|
||||
|
||||
private record TriggerConfiguration(
|
||||
AlarmTrigger trigger,
|
||||
ΩsecondsΩ gracePeriod
|
||||
) {
|
||||
}
|
||||
|
||||
private AlarmConfigurationParser() {
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ public record PriceAlarmDefinition(
|
||||
PriceDirection direction,
|
||||
BigDecimal target,
|
||||
AlarmTrigger trigger,
|
||||
long triggerGracePeriodSeconds,
|
||||
ΩsecondsΩ triggerGracePeriodSeconds,
|
||||
AlarmSeverity severity,
|
||||
String note
|
||||
) {
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
package com.r35157.jupiterperpsalarm.impl.ref;
|
||||
|
||||
Reference in New Issue
Block a user