feat(ui): rich AlarmTriggerEditor in instance override modal
Replaces the per-row JSON textbox with an Edit button that opens a modal hosting the full AlarmTriggerEditor. The editor pre-populates with the merged inherited + override config so the operator sees the effective state, not the override delta. On Save: - HiLo: diff against inherited, store only changed keys - Binary trigger types: whole-replace if the edited config differs Value comparison in the diff is type-aware (decoded strings, numeric GetDouble) so JSON-escape differences (e.g., literal em-dash vs —) don't produce false-positive diffs that pollute the override JSON. FlatteningService.MergeHiLoConfig is now public so the UI can pre-merge the editor seed; new public DiffHiLoConfig handles the symmetric direction. +2 encoding tests cover the new equivalence behavior. The override row's summary column shows the diff'd keys + priority chip so operators see what's overridden at a glance.
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
@using ScadaLink.Commons.Entities.Templates
|
@using ScadaLink.Commons.Entities.Templates
|
||||||
@using ScadaLink.Commons.Interfaces.Repositories
|
@using ScadaLink.Commons.Interfaces.Repositories
|
||||||
@using ScadaLink.Commons.Types.Enums
|
@using ScadaLink.Commons.Types.Enums
|
||||||
|
@using ScadaLink.TemplateEngine.Flattening
|
||||||
@using ScadaLink.TemplateEngine.Services
|
@using ScadaLink.TemplateEngine.Services
|
||||||
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
|
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
|
||||||
@inject ITemplateEngineRepository TemplateEngineRepository
|
@inject ITemplateEngineRepository TemplateEngineRepository
|
||||||
@@ -170,8 +171,8 @@
|
|||||||
<div class="card-header py-2">
|
<div class="card-header py-2">
|
||||||
<strong>Alarm Overrides</strong>
|
<strong>Alarm Overrides</strong>
|
||||||
<small class="text-muted ms-2">
|
<small class="text-muted ms-2">
|
||||||
For HiLo alarms, override JSON merges into the inherited config setpoint-by-setpoint.
|
Click <em>Edit</em> to override an alarm's trigger configuration or priority.
|
||||||
For binary trigger types, override JSON replaces the whole config. Leave both fields empty to inherit.
|
HiLo overrides merge into the inherited setpoints; other trigger types replace the whole config.
|
||||||
</small>
|
</small>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body p-0">
|
<div class="card-body p-0">
|
||||||
@@ -187,9 +188,8 @@
|
|||||||
<th>Alarm</th>
|
<th>Alarm</th>
|
||||||
<th style="width: 110px;">Trigger</th>
|
<th style="width: 110px;">Trigger</th>
|
||||||
<th>Inherited Config</th>
|
<th>Inherited Config</th>
|
||||||
<th style="width: 320px;">Override JSON</th>
|
<th style="width: 280px;">Override</th>
|
||||||
<th style="width: 120px;">Priority</th>
|
<th style="width: 140px;">Actions</th>
|
||||||
<th style="width: 80px;"></th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -204,19 +204,21 @@
|
|||||||
title="@alarm.TriggerConfiguration">
|
title="@alarm.TriggerConfiguration">
|
||||||
@(alarm.TriggerConfiguration ?? "—")
|
@(alarm.TriggerConfiguration ?? "—")
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td class="small">
|
||||||
<input type="text" class="form-control form-control-sm font-monospace"
|
@if (HasOverride(alarm.Name))
|
||||||
placeholder='@GetAlarmJsonPlaceholder(alarm.TriggerType)'
|
{
|
||||||
value="@GetAlarmOverrideJson(alarm.Name)"
|
<span class="badge bg-warning text-dark me-1" title="Override is set">●</span>
|
||||||
@onchange="(e) => OnAlarmOverrideJsonChanged(alarm.Name, e)" />
|
<span class="text-muted">@OverrideSummary(alarm.Name)</span>
|
||||||
</td>
|
}
|
||||||
<td>
|
else
|
||||||
<input type="number" min="0" max="1000" class="form-control form-control-sm"
|
{
|
||||||
placeholder="@alarm.PriorityLevel"
|
<span class="text-muted fst-italic">inherited</span>
|
||||||
value="@GetAlarmOverridePriority(alarm.Name)"
|
}
|
||||||
@onchange="(e) => OnAlarmOverridePriorityChanged(alarm.Name, e)" />
|
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
|
<button class="btn btn-outline-primary btn-sm me-1"
|
||||||
|
@onclick="() => BeginEditOverride(alarm)"
|
||||||
|
disabled="@_saving">Edit</button>
|
||||||
@if (HasOverride(alarm.Name))
|
@if (HasOverride(alarm.Name))
|
||||||
{
|
{
|
||||||
<button class="btn btn-outline-danger btn-sm"
|
<button class="btn btn-outline-danger btn-sm"
|
||||||
@@ -228,13 +230,73 @@
|
|||||||
}
|
}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<div class="p-2">
|
|
||||||
<button class="btn btn-success btn-sm" @onclick="SaveAlarmOverrides" disabled="@_saving">Save Alarm Overrides</button>
|
|
||||||
</div>
|
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@* Override edit modal *@
|
||||||
|
@if (_editingAlarm != null)
|
||||||
|
{
|
||||||
|
<div class="modal show d-block" tabindex="-1" style="background: rgba(0,0,0,0.4);">
|
||||||
|
<div class="modal-dialog modal-dialog-scrollable modal-lg">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h6 class="modal-title">
|
||||||
|
Edit override: @_editingAlarm.Name
|
||||||
|
<span class="badge bg-light text-dark border ms-1">@_editingAlarm.TriggerType</span>
|
||||||
|
</h6>
|
||||||
|
<button type="button" class="btn-close" aria-label="Close" @onclick="CancelEditOverride"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="mb-3 small">
|
||||||
|
<div class="text-muted text-uppercase fw-semibold mb-1">Inherited from template</div>
|
||||||
|
<code class="d-block bg-light p-2 rounded text-break">@(_editingAlarm.TriggerConfiguration ?? "(none)")</code>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<div class="text-muted text-uppercase small fw-semibold mb-1">Configuration</div>
|
||||||
|
<AlarmTriggerEditor TriggerType="@_editingAlarm.TriggerType"
|
||||||
|
Value="@_editingOverrideValue"
|
||||||
|
ValueChanged="@(v => _editingOverrideValue = v)"
|
||||||
|
AvailableAttributes="@_editingAvailableAttributes"
|
||||||
|
FallbackPriority="@_editingAlarm.PriorityLevel" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row g-2">
|
||||||
|
<div class="col-md-6">
|
||||||
|
<label class="form-label small text-uppercase text-muted fw-semibold mb-1">
|
||||||
|
Priority override
|
||||||
|
</label>
|
||||||
|
<input type="number" min="0" max="1000" class="form-control form-control-sm"
|
||||||
|
placeholder="@_editingAlarm.PriorityLevel"
|
||||||
|
@bind="_editingPriorityText" @bind:event="oninput" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (_editingError != null)
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger small mt-2 mb-0">@_editingError</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer justify-content-between">
|
||||||
|
<div>
|
||||||
|
@if (HasOverride(_editingAlarm.Name))
|
||||||
|
{
|
||||||
|
<button class="btn btn-outline-danger btn-sm"
|
||||||
|
@onclick="() => ClearFromModal()"
|
||||||
|
disabled="@_saving">Clear Override</button>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button class="btn btn-outline-secondary btn-sm" @onclick="CancelEditOverride">Cancel</button>
|
||||||
|
<button class="btn btn-success btn-sm" @onclick="SaveOverrideFromModal" disabled="@_saving">Save Override</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
@* Area Assignment *@
|
@* Area Assignment *@
|
||||||
<div class="card mb-3">
|
<div class="card mb-3">
|
||||||
<div class="card-header py-2">
|
<div class="card-header py-2">
|
||||||
@@ -278,14 +340,18 @@
|
|||||||
private List<TemplateAttribute> _overrideAttrs = new();
|
private List<TemplateAttribute> _overrideAttrs = new();
|
||||||
private Dictionary<string, string?> _overrideValues = new();
|
private Dictionary<string, string?> _overrideValues = new();
|
||||||
|
|
||||||
// Alarm overrides — keyed by alarm name. Dirty rows live in
|
// Alarm overrides — read-only state pulled from the repo. The edit modal
|
||||||
// _alarmOverrideJson / _alarmOverridePriority; existing overrides are
|
// is the only mutation path (one alarm at a time).
|
||||||
// loaded via _existingAlarmOverrides so the UI can show inherited
|
|
||||||
// placeholders correctly.
|
|
||||||
private List<TemplateAlarm> _overridableAlarms = new();
|
private List<TemplateAlarm> _overridableAlarms = new();
|
||||||
private Dictionary<string, InstanceAlarmOverride> _existingAlarmOverrides = new();
|
private Dictionary<string, InstanceAlarmOverride> _existingAlarmOverrides = new();
|
||||||
private Dictionary<string, string?> _alarmOverrideJson = new();
|
|
||||||
private Dictionary<string, int?> _alarmOverridePriority = new();
|
// Override edit modal state — non-null while the modal is open.
|
||||||
|
private TemplateAlarm? _editingAlarm;
|
||||||
|
private string? _editingOverrideValue; // current Value parameter for AlarmTriggerEditor
|
||||||
|
private string? _editingInheritedValue; // the inherited config snapshot we diff against on save
|
||||||
|
private string? _editingPriorityText;
|
||||||
|
private string? _editingError;
|
||||||
|
private IReadOnlyList<AlarmAttributeChoice> _editingAvailableAttributes = Array.Empty<AlarmAttributeChoice>();
|
||||||
|
|
||||||
// Area
|
// Area
|
||||||
private List<Area> _siteAreas = new();
|
private List<Area> _siteAreas = new();
|
||||||
@@ -338,8 +404,6 @@
|
|||||||
foreach (var o in alarmOverrides)
|
foreach (var o in alarmOverrides)
|
||||||
{
|
{
|
||||||
_existingAlarmOverrides[o.AlarmCanonicalName] = o;
|
_existingAlarmOverrides[o.AlarmCanonicalName] = o;
|
||||||
_alarmOverrideJson[o.AlarmCanonicalName] = o.TriggerConfigurationOverride;
|
|
||||||
_alarmOverridePriority[o.AlarmCanonicalName] = o.PriorityLevelOverride;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -430,42 +494,158 @@
|
|||||||
private bool HasOverride(string alarmName) =>
|
private bool HasOverride(string alarmName) =>
|
||||||
_existingAlarmOverrides.ContainsKey(alarmName);
|
_existingAlarmOverrides.ContainsKey(alarmName);
|
||||||
|
|
||||||
private string? GetAlarmOverrideJson(string alarmName) =>
|
|
||||||
_alarmOverrideJson.GetValueOrDefault(alarmName);
|
|
||||||
|
|
||||||
private string? GetAlarmOverridePriority(string alarmName) =>
|
|
||||||
_alarmOverridePriority.GetValueOrDefault(alarmName)?.ToString();
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Placeholder hint shown in the JSON override input. Encourages the
|
/// Human-readable summary of the currently-saved override. Lists the
|
||||||
/// HiLo "partial JSON" idiom (e.g. {"hi":90}) vs. the binary-trigger
|
/// HiLo keys that differ from the inherited config plus a priority chip.
|
||||||
/// "whole-config" idiom.
|
/// Used by the row's "Override" column.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static string GetAlarmJsonPlaceholder(AlarmTriggerType type) => type switch
|
private string OverrideSummary(string alarmName)
|
||||||
{
|
{
|
||||||
AlarmTriggerType.HiLo => "{\"hi\":90} — partial keys merge",
|
if (!_existingAlarmOverrides.TryGetValue(alarmName, out var ovr))
|
||||||
_ => "{\"attributeName\":\"...\"} — whole-replace"
|
return "";
|
||||||
};
|
|
||||||
|
|
||||||
private void OnAlarmOverrideJsonChanged(string alarmName, ChangeEventArgs e)
|
var parts = new List<string>();
|
||||||
{
|
if (!string.IsNullOrWhiteSpace(ovr.TriggerConfigurationOverride))
|
||||||
var val = e.Value?.ToString();
|
{
|
||||||
if (string.IsNullOrWhiteSpace(val))
|
try
|
||||||
_alarmOverrideJson.Remove(alarmName);
|
{
|
||||||
else
|
using var doc = System.Text.Json.JsonDocument.Parse(ovr.TriggerConfigurationOverride);
|
||||||
_alarmOverrideJson[alarmName] = val;
|
if (doc.RootElement.ValueKind == System.Text.Json.JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
parts.AddRange(doc.RootElement.EnumerateObject().Select(p => p.Name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (System.Text.Json.JsonException)
|
||||||
|
{
|
||||||
|
parts.Add("(invalid JSON)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ovr.PriorityLevelOverride.HasValue)
|
||||||
|
parts.Add($"priority={ovr.PriorityLevelOverride.Value}");
|
||||||
|
|
||||||
|
return parts.Count == 0 ? "(empty)" : string.Join(", ", parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnAlarmOverridePriorityChanged(string alarmName, ChangeEventArgs e)
|
/// <summary>
|
||||||
|
/// Opens the override editor modal pre-populated with the merged
|
||||||
|
/// (inherited + existing override) config so the user sees the effective
|
||||||
|
/// state — not just the override delta.
|
||||||
|
/// </summary>
|
||||||
|
private void BeginEditOverride(TemplateAlarm alarm)
|
||||||
{
|
{
|
||||||
var raw = e.Value?.ToString();
|
_editingAlarm = alarm;
|
||||||
if (string.IsNullOrWhiteSpace(raw))
|
_editingError = null;
|
||||||
|
_editingInheritedValue = alarm.TriggerConfiguration;
|
||||||
|
|
||||||
|
var existing = _existingAlarmOverrides.GetValueOrDefault(alarm.Name);
|
||||||
|
|
||||||
|
// HiLo: merge inherited + override so the editor shows the effective
|
||||||
|
// setpoints. Binary: pre-fill with the override if present, else the
|
||||||
|
// inherited config — same idea.
|
||||||
|
_editingOverrideValue = alarm.TriggerType == AlarmTriggerType.HiLo
|
||||||
|
? FlatteningService.MergeHiLoConfig(alarm.TriggerConfiguration, existing?.TriggerConfigurationOverride)
|
||||||
|
: (existing?.TriggerConfigurationOverride ?? alarm.TriggerConfiguration);
|
||||||
|
|
||||||
|
_editingPriorityText = existing?.PriorityLevelOverride?.ToString();
|
||||||
|
_editingAvailableAttributes = _overrideAttrs
|
||||||
|
.Select(a => new AlarmAttributeChoice(a.Name, MapDataType(a.DataType), "Direct"))
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CancelEditOverride()
|
||||||
|
{
|
||||||
|
_editingAlarm = null;
|
||||||
|
_editingError = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SaveOverrideFromModal()
|
||||||
|
{
|
||||||
|
if (_editingAlarm == null) return;
|
||||||
|
|
||||||
|
_saving = true;
|
||||||
|
try
|
||||||
{
|
{
|
||||||
_alarmOverridePriority.Remove(alarmName);
|
int? priority = null;
|
||||||
return;
|
if (!string.IsNullOrWhiteSpace(_editingPriorityText))
|
||||||
|
{
|
||||||
|
if (!int.TryParse(_editingPriorityText, out var p))
|
||||||
|
{
|
||||||
|
_editingError = "Priority must be an integer.";
|
||||||
|
_saving = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
priority = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the override JSON. For HiLo, diff against inherited so we
|
||||||
|
// store only the changed keys (matches the merge-on-flatten flow).
|
||||||
|
// For binary, whole-replace if the edited config differs from
|
||||||
|
// inherited.
|
||||||
|
string? overrideJson;
|
||||||
|
if (_editingAlarm.TriggerType == AlarmTriggerType.HiLo)
|
||||||
|
{
|
||||||
|
overrideJson = FlatteningService.DiffHiLoConfig(_editingInheritedValue, _editingOverrideValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
overrideJson = _editingOverrideValue == _editingInheritedValue
|
||||||
|
? null
|
||||||
|
: _editingOverrideValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var user = await GetCurrentUserAsync();
|
||||||
|
var alarmName = _editingAlarm.Name;
|
||||||
|
|
||||||
|
// No diff + no priority → clear any existing override and close.
|
||||||
|
if (string.IsNullOrWhiteSpace(overrideJson) && !priority.HasValue)
|
||||||
|
{
|
||||||
|
if (_existingAlarmOverrides.ContainsKey(alarmName))
|
||||||
|
{
|
||||||
|
var del = await InstanceService.DeleteAlarmOverrideAsync(Id, alarmName, user);
|
||||||
|
if (!del.IsSuccess)
|
||||||
|
{
|
||||||
|
_editingError = del.Error;
|
||||||
|
_saving = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_existingAlarmOverrides.Remove(alarmName);
|
||||||
|
_toast.ShowSuccess($"Cleared override on '{alarmName}'.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_toast.ShowSuccess("No change.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var result = await InstanceService.SetAlarmOverrideAsync(
|
||||||
|
Id, alarmName, overrideJson, priority, user);
|
||||||
|
if (!result.IsSuccess)
|
||||||
|
{
|
||||||
|
_editingError = result.Error;
|
||||||
|
_saving = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_existingAlarmOverrides[alarmName] = result.Value!;
|
||||||
|
_toast.ShowSuccess($"Saved override on '{alarmName}'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
_editingAlarm = null;
|
||||||
|
_editingError = null;
|
||||||
}
|
}
|
||||||
if (int.TryParse(raw, out var p))
|
catch (Exception ex)
|
||||||
_alarmOverridePriority[alarmName] = p;
|
{
|
||||||
|
_editingError = ex.Message;
|
||||||
|
}
|
||||||
|
_saving = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ClearFromModal()
|
||||||
|
{
|
||||||
|
if (_editingAlarm == null) return;
|
||||||
|
var name = _editingAlarm.Name;
|
||||||
|
await ClearAlarmOverride(name);
|
||||||
|
_editingAlarm = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ClearAlarmOverride(string alarmName)
|
private async Task ClearAlarmOverride(string alarmName)
|
||||||
@@ -478,8 +658,6 @@
|
|||||||
if (result.IsSuccess)
|
if (result.IsSuccess)
|
||||||
{
|
{
|
||||||
_existingAlarmOverrides.Remove(alarmName);
|
_existingAlarmOverrides.Remove(alarmName);
|
||||||
_alarmOverrideJson.Remove(alarmName);
|
|
||||||
_alarmOverridePriority.Remove(alarmName);
|
|
||||||
_toast.ShowSuccess($"Cleared override on '{alarmName}'.");
|
_toast.ShowSuccess($"Cleared override on '{alarmName}'.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -494,54 +672,22 @@
|
|||||||
_saving = false;
|
_saving = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SaveAlarmOverrides()
|
/// <summary>
|
||||||
|
/// Mirrors TemplateEdit.MapDataType — converts the persisted DataType enum
|
||||||
|
/// to the canonical SCADA type string the AlarmTriggerEditor compares
|
||||||
|
/// against (Boolean / Integer / Float / String / Object).
|
||||||
|
/// </summary>
|
||||||
|
private static string MapDataType(DataType dt) => dt switch
|
||||||
{
|
{
|
||||||
_saving = true;
|
DataType.Boolean => "Boolean",
|
||||||
try
|
DataType.Int32 => "Integer",
|
||||||
{
|
DataType.Float => "Float",
|
||||||
var user = await GetCurrentUserAsync();
|
DataType.Double => "Float",
|
||||||
var touched = _alarmOverrideJson.Keys
|
DataType.String => "String",
|
||||||
.Union(_alarmOverridePriority.Keys, StringComparer.Ordinal)
|
DataType.DateTime => "String",
|
||||||
.Distinct()
|
DataType.Binary => "Object",
|
||||||
.ToList();
|
_ => "Object"
|
||||||
|
};
|
||||||
var saved = 0;
|
|
||||||
foreach (var alarmName in touched)
|
|
||||||
{
|
|
||||||
var json = _alarmOverrideJson.GetValueOrDefault(alarmName);
|
|
||||||
var priority = _alarmOverridePriority.GetValueOrDefault(alarmName);
|
|
||||||
|
|
||||||
// If both fields are empty AND there's an existing override,
|
|
||||||
// clear it. Otherwise upsert (allows priority-only or json-only).
|
|
||||||
if (string.IsNullOrWhiteSpace(json) && !priority.HasValue)
|
|
||||||
{
|
|
||||||
if (_existingAlarmOverrides.ContainsKey(alarmName))
|
|
||||||
{
|
|
||||||
await InstanceService.DeleteAlarmOverrideAsync(Id, alarmName, user);
|
|
||||||
_existingAlarmOverrides.Remove(alarmName);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = await InstanceService.SetAlarmOverrideAsync(
|
|
||||||
Id, alarmName, json, priority, user);
|
|
||||||
if (!result.IsSuccess)
|
|
||||||
{
|
|
||||||
_toast.ShowError($"Save failed for '{alarmName}': {result.Error}");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_existingAlarmOverrides[alarmName] = result.Value!;
|
|
||||||
saved++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (saved > 0) _toast.ShowSuccess($"Saved {saved} alarm override(s).");
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
_toast.ShowError($"Save alarm overrides failed: {ex.Message}");
|
|
||||||
}
|
|
||||||
_saving = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Area ────────────────────────────────────────────────
|
// ── Area ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -407,7 +407,7 @@ public class FlatteningService
|
|||||||
/// Returns the derived config verbatim on parse failure of either input —
|
/// Returns the derived config verbatim on parse failure of either input —
|
||||||
/// the existing whole-replace behavior is the safe fallback.
|
/// the existing whole-replace behavior is the safe fallback.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal static string? MergeHiLoConfig(string? inheritedJson, string? derivedJson)
|
public static string? MergeHiLoConfig(string? inheritedJson, string? derivedJson)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(inheritedJson)) return derivedJson;
|
if (string.IsNullOrWhiteSpace(inheritedJson)) return derivedJson;
|
||||||
if (string.IsNullOrWhiteSpace(derivedJson)) return inheritedJson;
|
if (string.IsNullOrWhiteSpace(derivedJson)) return inheritedJson;
|
||||||
@@ -455,6 +455,83 @@ public class FlatteningService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Computes the minimal HiLo override JSON given the inherited config and
|
||||||
|
/// an edited config — returns only the top-level keys whose values differ
|
||||||
|
/// from the inherited config. Returns <c>null</c> when no keys differ (the
|
||||||
|
/// caller should treat that as "no override").
|
||||||
|
///
|
||||||
|
/// Value comparison is type-aware so that JSON-escape differences (e.g.,
|
||||||
|
/// a literal em-dash in the inherited config vs. <c>—</c> in the
|
||||||
|
/// editor's serialized output) don't produce false-positive diffs. On
|
||||||
|
/// parse failure of either input, returns <paramref name="editedJson"/>
|
||||||
|
/// verbatim — safe fallback that matches the existing whole-replace
|
||||||
|
/// semantics.
|
||||||
|
/// </summary>
|
||||||
|
public static string? DiffHiLoConfig(string? inheritedJson, string? editedJson)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(editedJson)) return null;
|
||||||
|
if (string.IsNullOrWhiteSpace(inheritedJson)) return editedJson;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var inheritedDoc = JsonDocument.Parse(inheritedJson);
|
||||||
|
using var editedDoc = JsonDocument.Parse(editedJson);
|
||||||
|
|
||||||
|
if (inheritedDoc.RootElement.ValueKind != JsonValueKind.Object
|
||||||
|
|| editedDoc.RootElement.ValueKind != JsonValueKind.Object)
|
||||||
|
{
|
||||||
|
return editedJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
var changed = new List<JsonProperty>();
|
||||||
|
foreach (var prop in editedDoc.RootElement.EnumerateObject())
|
||||||
|
{
|
||||||
|
if (!inheritedDoc.RootElement.TryGetProperty(prop.Name, out var inhProp))
|
||||||
|
{
|
||||||
|
changed.Add(prop);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!ValuesEquivalent(prop.Value, inhProp))
|
||||||
|
changed.Add(prop);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (changed.Count == 0) return null;
|
||||||
|
|
||||||
|
using var stream = new MemoryStream();
|
||||||
|
using (var writer = new Utf8JsonWriter(stream))
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
foreach (var p in changed) p.WriteTo(writer);
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
return System.Text.Encoding.UTF8.GetString(stream.ToArray());
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return editedJson;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compares two JSON values by their decoded meaning rather than their
|
||||||
|
/// raw text. Strings are unescaped before comparison so equivalent values
|
||||||
|
/// in different escape forms (e.g., a literal "—" vs. "—") match.
|
||||||
|
/// Numbers compare by their double value so trailing-zero differences
|
||||||
|
/// don't produce false diffs.
|
||||||
|
/// </summary>
|
||||||
|
private static bool ValuesEquivalent(JsonElement a, JsonElement b)
|
||||||
|
{
|
||||||
|
if (a.ValueKind != b.ValueKind) return false;
|
||||||
|
return a.ValueKind switch
|
||||||
|
{
|
||||||
|
JsonValueKind.String => a.GetString() == b.GetString(),
|
||||||
|
JsonValueKind.Number => a.GetDouble() == b.GetDouble(),
|
||||||
|
JsonValueKind.True or JsonValueKind.False or JsonValueKind.Null => true,
|
||||||
|
_ => a.GetRawText() == b.GetRawText()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
private static void ResolveComposedAlarms(
|
private static void ResolveComposedAlarms(
|
||||||
IReadOnlyList<Template> templateChain,
|
IReadOnlyList<Template> templateChain,
|
||||||
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
IReadOnlyDictionary<int, IReadOnlyList<TemplateComposition>> compositionMap,
|
||||||
|
|||||||
@@ -94,6 +94,100 @@ public class FlatteningServiceMergeTests
|
|||||||
Assert.Equal(3, doc.RootElement.GetProperty("hiDeadband").GetDouble());
|
Assert.Equal(3, doc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── DiffHiLoConfig ─────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_NoChanges_ReturnsNull()
|
||||||
|
{
|
||||||
|
const string both = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||||
|
Assert.Null(FlatteningService.DiffHiLoConfig(both, both));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_ChangedKey_ReturnsOnlyChangedKey()
|
||||||
|
{
|
||||||
|
const string inherited = @"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":80,""hiHi"":100}";
|
||||||
|
const string edited = @"{""attributeName"":""Temp"",""loLo"":0,""lo"":10,""hi"":90,""hiHi"":100}";
|
||||||
|
|
||||||
|
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||||
|
|
||||||
|
Assert.NotNull(diff);
|
||||||
|
using var doc = JsonDocument.Parse(diff!);
|
||||||
|
var prop = Assert.Single(doc.RootElement.EnumerateObject());
|
||||||
|
Assert.Equal("hi", prop.Name);
|
||||||
|
Assert.Equal(90, prop.Value.GetDouble());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_NewKey_AddedToDiff()
|
||||||
|
{
|
||||||
|
const string inherited = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||||
|
const string edited = @"{""attributeName"":""Temp"",""hi"":80,""hiDeadband"":3}";
|
||||||
|
|
||||||
|
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||||
|
|
||||||
|
Assert.NotNull(diff);
|
||||||
|
using var doc = JsonDocument.Parse(diff!);
|
||||||
|
Assert.Equal(3, doc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||||
|
Assert.False(doc.RootElement.TryGetProperty("hi", out _));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_NullInherited_ReturnsEditedVerbatim()
|
||||||
|
{
|
||||||
|
const string edited = @"{""attributeName"":""Temp"",""hi"":80}";
|
||||||
|
Assert.Equal(edited, FlatteningService.DiffHiLoConfig(null, edited));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_NullEdited_ReturnsNull()
|
||||||
|
{
|
||||||
|
Assert.Null(FlatteningService.DiffHiLoConfig(@"{""hi"":80}", null));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_IgnoresStringEscapeDifferences()
|
||||||
|
{
|
||||||
|
// Inherited has literal em-dash; edited has the unicode-escaped form.
|
||||||
|
// Decoded values are identical, so the key should NOT be in the diff.
|
||||||
|
const string inherited = @"{""attributeName"":""Temp"",""hi"":80,""hiMessage"":""High — investigate""}";
|
||||||
|
const string edited = @"{""attributeName"":""Temp"",""hi"":80,""hiMessage"":""High — investigate""}";
|
||||||
|
|
||||||
|
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||||
|
|
||||||
|
Assert.Null(diff); // no real change once values are decoded
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_IgnoresNumericFormatDifferences()
|
||||||
|
{
|
||||||
|
// 85 vs 85.0 are the same number — should not produce a diff.
|
||||||
|
const string inherited = @"{""hi"":85}";
|
||||||
|
const string edited = @"{""hi"":85.0}";
|
||||||
|
Assert.Null(FlatteningService.DiffHiLoConfig(inherited, edited));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void DiffHiLoConfig_RoundTripsThroughMerge()
|
||||||
|
{
|
||||||
|
// Merge(inherited, Diff(inherited, edited)) ≡ edited — when the
|
||||||
|
// edited config is itself a superset/equivalent of inherited.
|
||||||
|
const string inherited = @"{""attributeName"":""Temp"",""hi"":80,""hiHi"":100}";
|
||||||
|
const string edited = @"{""attributeName"":""Temp"",""hi"":90,""hiHi"":100,""hiDeadband"":5}";
|
||||||
|
|
||||||
|
var diff = FlatteningService.DiffHiLoConfig(inherited, edited);
|
||||||
|
var merged = FlatteningService.MergeHiLoConfig(inherited, diff);
|
||||||
|
|
||||||
|
using var origDoc = JsonDocument.Parse(edited);
|
||||||
|
using var mergedDoc = JsonDocument.Parse(merged!);
|
||||||
|
Assert.Equal(origDoc.RootElement.GetProperty("hi").GetDouble(),
|
||||||
|
mergedDoc.RootElement.GetProperty("hi").GetDouble());
|
||||||
|
Assert.Equal(origDoc.RootElement.GetProperty("hiHi").GetDouble(),
|
||||||
|
mergedDoc.RootElement.GetProperty("hiHi").GetDouble());
|
||||||
|
Assert.Equal(origDoc.RootElement.GetProperty("hiDeadband").GetDouble(),
|
||||||
|
mergedDoc.RootElement.GetProperty("hiDeadband").GetDouble());
|
||||||
|
}
|
||||||
|
|
||||||
// ── Instance-level alarm override (end-to-end Flatten) ─────────────────
|
// ── Instance-level alarm override (end-to-end Flatten) ─────────────────
|
||||||
|
|
||||||
private static (Template, Instance) BuildHiLoFixture(string inheritedJson, InstanceAlarmOverride? ovr = null, bool locked = false)
|
private static (Template, Instance) BuildHiLoFixture(string inheritedJson, InstanceAlarmOverride? ovr = null, bool locked = false)
|
||||||
|
|||||||
Reference in New Issue
Block a user