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"));
|
BigDecimal target = parseTarget(cursor.nextToken("target"));
|
||||||
|
|
||||||
AlarmTrigger trigger = AlarmTrigger.valueOf(
|
TriggerConfiguration triggerConfiguration = parseTrigger(
|
||||||
cursor.nextToken("trigger").toUpperCase(Locale.ROOT)
|
cursor.nextToken("trigger")
|
||||||
);
|
);
|
||||||
|
|
||||||
AlarmSeverity severity = AlarmSeverity.valueOf(
|
AlarmSeverity severity = AlarmSeverity.valueOf(
|
||||||
@@ -81,7 +81,8 @@ public final class AlarmConfigurationParser {
|
|||||||
asset,
|
asset,
|
||||||
direction,
|
direction,
|
||||||
target,
|
target,
|
||||||
trigger,
|
triggerConfiguration.trigger(),
|
||||||
|
triggerConfiguration.gracePeriod(),
|
||||||
severity,
|
severity,
|
||||||
note
|
note
|
||||||
);
|
);
|
||||||
@@ -109,6 +110,50 @@ public final class AlarmConfigurationParser {
|
|||||||
return target;
|
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) {
|
private static void validateTarget(BigDecimal target, String originalTargetStr) {
|
||||||
if (target.compareTo(BigDecimal.ZERO) < 0) {
|
if (target.compareTo(BigDecimal.ZERO) < 0) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
@@ -274,6 +319,12 @@ public final class AlarmConfigurationParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private record TriggerConfiguration(
|
||||||
|
AlarmTrigger trigger,
|
||||||
|
ΩsecondsΩ gracePeriod
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
private AlarmConfigurationParser() {
|
private AlarmConfigurationParser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public record PriceAlarmDefinition(
|
|||||||
PriceDirection direction,
|
PriceDirection direction,
|
||||||
BigDecimal target,
|
BigDecimal target,
|
||||||
AlarmTrigger trigger,
|
AlarmTrigger trigger,
|
||||||
long triggerGracePeriodSeconds,
|
ΩsecondsΩ triggerGracePeriodSeconds,
|
||||||
AlarmSeverity severity,
|
AlarmSeverity severity,
|
||||||
String note
|
String note
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
package com.r35157.jupiterperpsalarm.impl.ref;
|
||||||
|
|
||||||
Reference in New Issue
Block a user