feat(alarms): HiLo trigger type with per-band level, hysteresis, messages, overrides
Adds a new HiLo alarm trigger type with four configurable setpoints
(LoLo / Lo / Hi / HiHi). Each setpoint carries an optional priority,
deadband (for hysteresis), and operator message. The site runtime emits
AlarmStateChanged with an AlarmLevel field so consumers can differentiate
warning vs critical bands.
Plumbing:
- new AlarmLevel enum + AlarmStateChanged.Level/Message init properties
- AlarmTriggerEditor (Blazor) gets a HiLo render with severity tinting
- AlarmTriggerConfigCodec extracted from the editor for testability
- sitestream.proto carries level + message over gRPC
- SemanticValidator enforces numeric attribute, setpoint ordering,
non-negative deadband
- on-trigger scripts get an Alarm global (Name/Level/Priority/Message)
so notification routing can branch by severity
- per-instance InstanceAlarmOverride entity + EF migration + flattening
step + CLI commands; HiLo overrides merge setpoint-by-setpoint, binary
types whole-replace
- DebugView shows a Level badge + per-band message tooltip
- App.razor auto-reloads on permanent Blazor circuit failure
- docker/regen-proto.sh automates the proto regen workflow (the linux/arm64
protoc segfault means generated files are checked in for now)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using ScadaLink.Commons.Entities.Instances;
|
||||
using ScadaLink.Commons.Entities.Sites;
|
||||
using ScadaLink.Commons.Entities.Templates;
|
||||
@@ -80,6 +81,7 @@ public class FlatteningService
|
||||
// Step 5: Resolve alarms from inheritance chain
|
||||
var alarms = ResolveInheritedAlarms(templateChain);
|
||||
ResolveComposedAlarms(templateChain, compositionMap, composedTemplateChains, alarms);
|
||||
ApplyInstanceAlarmOverrides(instance.AlarmOverrides, alarms);
|
||||
|
||||
// Step 6: Resolve scripts from inheritance chain
|
||||
var scripts = ResolveInheritedScripts(templateChain);
|
||||
@@ -292,6 +294,43 @@ public class FlatteningService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies per-instance alarm overrides on top of the
|
||||
/// inheritance-and-composition resolved alarms. Skips overrides for
|
||||
/// alarms that are locked at the template level. For HiLo triggers the
|
||||
/// override JSON is merged setpoint-by-setpoint (preserving inherited
|
||||
/// keys not present in the override); for other trigger types the
|
||||
/// override replaces the whole TriggerConfiguration.
|
||||
/// </summary>
|
||||
private static void ApplyInstanceAlarmOverrides(
|
||||
ICollection<InstanceAlarmOverride> overrides,
|
||||
Dictionary<string, ResolvedAlarm> alarms)
|
||||
{
|
||||
foreach (var ovr in overrides)
|
||||
{
|
||||
if (!alarms.TryGetValue(ovr.AlarmCanonicalName, out var existing))
|
||||
continue; // Cannot add new alarms via overrides
|
||||
|
||||
if (existing.IsLocked)
|
||||
continue; // Locked alarms cannot be overridden
|
||||
|
||||
var newConfig = existing.TriggerConfiguration;
|
||||
if (!string.IsNullOrWhiteSpace(ovr.TriggerConfigurationOverride))
|
||||
{
|
||||
newConfig = existing.TriggerType == nameof(AlarmTriggerType.HiLo)
|
||||
? MergeHiLoConfig(existing.TriggerConfiguration, ovr.TriggerConfigurationOverride)
|
||||
: ovr.TriggerConfigurationOverride;
|
||||
}
|
||||
|
||||
alarms[ovr.AlarmCanonicalName] = existing with
|
||||
{
|
||||
TriggerConfiguration = newConfig,
|
||||
PriorityLevel = ovr.PriorityLevelOverride ?? existing.PriorityLevel,
|
||||
Source = "Override"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyConnectionBindings(
|
||||
ICollection<InstanceConnectionBinding> bindings,
|
||||
Dictionary<string, ResolvedAttribute> attributes,
|
||||
@@ -332,6 +371,18 @@ public class FlatteningService
|
||||
if (result.TryGetValue(alarm.Name, out var existing) && existing.IsLocked)
|
||||
continue;
|
||||
|
||||
// HiLo per-setpoint override: derived templates can supply a
|
||||
// partial TriggerConfiguration (e.g., just `hi`) and have the
|
||||
// remaining setpoints inherited. Other trigger types replace
|
||||
// the whole config on override (current behavior).
|
||||
var triggerConfig = alarm.TriggerConfiguration;
|
||||
if (existing != null
|
||||
&& alarm.TriggerType == AlarmTriggerType.HiLo
|
||||
&& existing.TriggerType == nameof(AlarmTriggerType.HiLo))
|
||||
{
|
||||
triggerConfig = MergeHiLoConfig(existing.TriggerConfiguration, triggerConfig);
|
||||
}
|
||||
|
||||
result[alarm.Name] = new ResolvedAlarm
|
||||
{
|
||||
CanonicalName = alarm.Name,
|
||||
@@ -339,7 +390,7 @@ public class FlatteningService
|
||||
PriorityLevel = alarm.PriorityLevel,
|
||||
IsLocked = alarm.IsLocked,
|
||||
TriggerType = alarm.TriggerType.ToString(),
|
||||
TriggerConfiguration = alarm.TriggerConfiguration,
|
||||
TriggerConfiguration = triggerConfig,
|
||||
OnTriggerScriptCanonicalName = null, // Resolved later
|
||||
Source = source
|
||||
};
|
||||
@@ -349,6 +400,61 @@ public class FlatteningService
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merges a derived HiLo trigger configuration onto an inherited one.
|
||||
/// Top-level keys present in <paramref name="derivedJson"/> override the
|
||||
/// inherited values; keys absent in the derived config are inherited.
|
||||
/// Returns the derived config verbatim on parse failure of either input —
|
||||
/// the existing whole-replace behavior is the safe fallback.
|
||||
/// </summary>
|
||||
internal static string? MergeHiLoConfig(string? inheritedJson, string? derivedJson)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(inheritedJson)) return derivedJson;
|
||||
if (string.IsNullOrWhiteSpace(derivedJson)) return inheritedJson;
|
||||
|
||||
try
|
||||
{
|
||||
using var inheritedDoc = JsonDocument.Parse(inheritedJson);
|
||||
using var derivedDoc = JsonDocument.Parse(derivedJson);
|
||||
|
||||
if (inheritedDoc.RootElement.ValueKind != JsonValueKind.Object
|
||||
|| derivedDoc.RootElement.ValueKind != JsonValueKind.Object)
|
||||
{
|
||||
return derivedJson;
|
||||
}
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
using (var writer = new Utf8JsonWriter(stream))
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
|
||||
var derivedKeys = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (var prop in derivedDoc.RootElement.EnumerateObject())
|
||||
derivedKeys.Add(prop.Name);
|
||||
|
||||
// Inherited keys not present in derived survive.
|
||||
foreach (var prop in inheritedDoc.RootElement.EnumerateObject())
|
||||
{
|
||||
if (derivedKeys.Contains(prop.Name)) continue;
|
||||
prop.WriteTo(writer);
|
||||
}
|
||||
|
||||
// Derived keys win.
|
||||
foreach (var prop in derivedDoc.RootElement.EnumerateObject())
|
||||
{
|
||||
prop.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
return System.Text.Encoding.UTF8.GetString(stream.ToArray());
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return derivedJson;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ResolveComposedAlarms(
|
||||
IReadOnlyList<Template> templateChain,
|
||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||
|
||||
Reference in New Issue
Block a user