Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Deployment/InstanceConfigure.razor
T
2026-09-09 16:13:58 -04:00

1958 lines
97 KiB
Plaintext

@using Microsoft.Extensions.DependencyInjection
@using ZB.MOM.WW.ScadaBridge.CentralUI.Services
@using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management
@inject IServiceProvider MutationServices
@page "/deployment/instances/{Id:int}/configure"
@using ZB.MOM.WW.ScadaBridge.Security
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites
@using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Protocol
@using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories
@using ZB.MOM.WW.ScadaBridge.Commons.Types
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums
@using ZB.MOM.WW.ScadaBridge.TemplateEngine.Flattening
@using ZB.MOM.WW.ScadaBridge.TemplateEngine.Services
@using ZB.MOM.WW.ScadaBridge.DeploymentManager
@using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Dialogs
@using ZB.MOM.WW.ScadaBridge.Commons.Types.Flattening
@attribute [Authorize(Policy = AuthorizationPolicies.RequireDeployment)]
@inject ITemplateEngineRepository TemplateEngineRepository
@inject ISiteRepository SiteRepository
@inject ZB.MOM.WW.ScadaBridge.CentralUI.Auth.SiteScopeService SiteScope
@inject InstanceService InstanceService
@inject IFlatteningPipeline FlatteningPipeline
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager
<div class="container-fluid mt-3">
<div class="d-flex align-items-center mb-3">
<button class="btn btn-outline-secondary btn-sm me-3" @onclick="GoBack">&larr; Back to Topology</button>
<h4 class="mb-0">Configure Instance</h4>
@* Drill-in: deep-link into the central Audit Log
pre-filtered to this instance. Instance is UI-only on the filter bar
(AuditEvent has no Instance column), so we use the ?instance= UI-text
seam — the filter bar's Instance free-text input is pre-populated. *@
@if (_instance != null)
{
<a class="btn btn-outline-secondary btn-sm ms-auto"
href="/audit/log?instance=@Uri.EscapeDataString(_instance.UniqueName)"
data-test="audit-link">
Recent audit activity
</a>
}
</div>
<ToastNotification @ref="_toast" />
@if (_loading)
{
<LoadingSpinner IsLoading="true" />
}
else if (_errorMessage != null)
{
<div class="alert alert-danger" data-test="instance-error-alert">@_errorMessage</div>
}
else if (_instance != null)
{
@* Instance Identity *@
<div class="card mb-3">
<div class="card-body py-2">
<div class="row">
<div class="col-md-4">
<small class="text-muted">Instance</small>
<div><strong>@_instance.UniqueName</strong></div>
</div>
<div class="col-md-2">
<small class="text-muted">Template</small>
<div>@_templateName</div>
</div>
<div class="col-md-2">
<small class="text-muted">Site</small>
<div>@_siteName</div>
</div>
<div class="col-md-2">
<small class="text-muted">Status</small>
<div><span class="badge @GetStateBadge(_instance.State)">@_instance.State</span></div>
</div>
<div class="col-md-2">
<small class="text-muted">Area</small>
<div>@(_instance.AreaId.HasValue ? _areaName : "—")</div>
</div>
</div>
</div>
</div>
@* One-shot notice for an instance that just arrived from the Create page
(design 2026-08-21). Query-string only — nothing is persisted, so it
disappears on any later visit. *@
@if (Created == 1)
{
<div class="alert @(BindError is null ? "alert-success" : "alert-warning") py-2 small" data-test="configure-created-notice">
Instance created.
@if (BindError is not null)
{
<text>Bindings were NOT saved: @BindError — bind the groups below.</text>
}
else if (Bound is > 0)
{
<text>@Bound binding(s) saved. Test Bindings, then Deploy.</text>
}
else
{
<text>Bind the groups below, then Deploy.</text>
}
</div>
}
@* Connection Bindings — grouped by module (design 2026-08-20). Rows come
from the flattened configuration so composed module members appear;
each group header carries a connection + Object apply that rewrites
every member to <object>.<memberTail>. *@
<div class="card mb-3 binding-panel">
<div class="card-header py-2 d-flex align-items-center gap-3 flex-wrap">
<strong>Connection Bindings</strong>
<span class="text-muted small">@_bindingRows.Count data-sourced attribute(s) in @_bindingGroups.Count group(s)</span>
<span class="ms-auto"></span>
@if (TotalUnbound > 0)
{
<span class="badge text-bg-warning" data-test="binding-total-unbound">@TotalUnbound unbound</span>
}
@if (MixedGroups > 0)
{
<span class="badge text-bg-secondary">@MixedGroups group(s) mixed</span>
}
</div>
@if (_bindingSourceWarning is not null)
{
<div class="alert alert-warning rounded-0 mb-0 py-2 small" data-test="binding-source-warning">@_bindingSourceWarning</div>
}
@if (_bindingRows.Count == 0)
{
<div class="card-body text-muted small">No data-sourced attributes in this template.</div>
}
else
{
<div class="card-body border-bottom py-2 d-flex align-items-center gap-3 flex-wrap small">
@if (_siteConnections.Count > 0)
{
<label class="d-flex align-items-center gap-2 mb-0">All groups
<select class="form-select form-select-sm" style="width: auto;" data-test="binding-bulk-select" @bind="_bulkConnectionId">
<option value="0">Assign all to...</option>
@foreach (var c in _siteConnections)
{
<option value="@c.Id">@c.Name (@c.Protocol)</option>
}
</select>
<button class="btn btn-outline-primary btn-sm" data-test="binding-bulk-apply"
@onclick="ApplyBulkBinding"
disabled="@(_bulkConnectionId == 0)">Apply connection</button>
</label>
}
<span class="ms-auto"></span>
<label class="form-check form-check-inline mb-0">
<input class="form-check-input" type="checkbox" @bind="_showOnlyUnbound" /> Show only unbound
</label>
<button class="btn btn-outline-secondary btn-sm" data-test="binding-expand-all" @onclick="ExpandAll">Expand all</button>
</div>
@foreach (var g in VisibleGroups)
{
var open = _expandedGroups.Contains(g.Key);
<div class="binding-group border-bottom">
<div class="binding-group-header d-flex align-items-center gap-2 px-3 py-2 @(open ? "bg-light" : "")" data-test="binding-group-header">
<button class="btn btn-link btn-sm p-0 text-decoration-none" @onclick="() => ToggleGroup(g.Key)"
aria-expanded="@(open ? "true" : "false")" title="@(open ? "Collapse" : "Expand")">@(open ? "▼" : "▶")</button>
<span class="fw-semibold binding-group-name">@g.Key
@if (g.IsInstanceGroup)
{
<small class="text-muted fw-normal ms-1">direct attributes</small>
}
else if (g.NestedModules.Count > 0)
{
<small class="text-muted fw-normal ms-1">incl. @string.Join(", ", g.NestedModules)</small>
}
</span>
<span class="text-muted small">@g.Members.Count</span>
<select class="form-select form-select-sm" style="width: 160px;" data-test="binding-group-connection"
value="@GroupConnectionInput(g)"
@onchange="e => _groupConnectionInput[g.Key] = int.TryParse(e.Value?.ToString(), out var v) ? v : 0">
<option value="0">@(g.ConnectionState == BindingConnectionState.Mixed ? "— mixed —" : "— none —")</option>
@foreach (var c in _siteConnections)
{
<option value="@c.Id">@c.Name</option>
}
</select>
<input class="form-control form-control-sm font-monospace" style="max-width: 260px;" data-test="binding-group-object"
value="@GroupObjectInput(g)"
placeholder="@(g.IsObjectMixed ? "mixed — enter object to unify" : "object, e.g. MESReceiver_022")"
title="Object path the module's members hang off; Apply rewrites every member to <object>.<member>. Blank clears the overrides."
@onchange="e => _groupObjectInput[g.Key] = e.Value?.ToString()" />
<button class="btn btn-outline-primary btn-sm text-nowrap" data-test="binding-group-apply"
@onclick="() => ApplyGroupObject(g)" disabled="@_saving">Apply to @g.Members.Count</button>
@if (g.UnboundCount > 0)
{
<span class="badge text-bg-danger" data-test="binding-group-unbound">@g.UnboundCount unbound</span>
}
else
{
<span class="badge text-bg-success">bound</span>
}
@if (g.IsObjectMixed || g.ConnectionState == BindingConnectionState.Mixed)
{
<span class="badge text-bg-secondary" data-test="binding-group-mixed">mixed</span>
}
@if (GroupChangedCount(g) is var changed && changed > 0)
{
<span class="badge border text-muted">@changed changed · unsaved</span>
}
</div>
@if (open)
{
<div class="table-responsive">
<table class="table table-sm table-bordered mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Member</th>
<th>Template default</th>
<th style="width: 200px;">Connection</th>
<th>Reference override</th>
<th style="width: 110px;"></th>
</tr>
</thead>
<tbody>
@foreach (var m in VisibleMembers(g))
{
var name = m.Row.CanonicalName;
var connId = GetBindingConnectionId(name);
var canBrowse = connId > 0;
var isBrowsable = IsBrowsable(connId);
var bare = !g.IsInstanceGroup && string.Equals(m.Row.DefaultReference, m.MemberTail, StringComparison.Ordinal);
<tr class="@(m.IsUnbound ? "table-danger" : "")">
<td class="small font-monospace ps-4">@m.MemberTail</td>
@* Defaults can be OPC UA node ids (nsu=…;s=…) — unbounded
text that would otherwise set the table's width. *@
<td class="small text-muted font-monospace">
<span class="cell-clip" title="@m.Row.DefaultReference">@m.Row.DefaultReference</span>
@if (bare)
{
<span class="ms-1 text-warning" data-test="binding-bare-default"
title="Composed members share a bare default name; set an override (or apply an Object on the group) or the deploy gate rejects the instance.">⚠</span>
}
</td>
<td>
<select class="form-select form-select-sm"
value="@connId"
@onchange="(e) => OnBindingChanged(name, e)">
<option value="0">— none —</option>
@foreach (var c in _siteConnections)
{
<option value="@c.Id">@c.Name</option>
}
</select>
</td>
<td>
<input class="form-control form-control-sm font-monospace"
value="@GetOverrideForAttr(name)"
@onchange="(e) => OnOverrideForAttrChanged(name, e)"
placeholder="@(m.Row.DefaultReference ?? "(no default)")" />
</td>
<td>
@if (isBrowsable)
{
<button class="btn btn-sm btn-outline-primary"
disabled="@(!canBrowse)"
title="@(canBrowse ? "Browse address space" : "Pick a connection first")"
@onclick="() => OpenBrowser(name)">
Browse…
</button>
}
</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
}
<div class="card-footer d-flex align-items-center gap-2 flex-wrap">
<div class="btn-group btn-group-sm" role="group" aria-label="Binding actions">
<button class="btn btn-success" @onclick="SaveBindings" disabled="@_saving">Save Bindings</button>
@* Test Bindings: one-shot live read of every bound attribute
whose row has a connection picked AND an effective tag
path — composed rows included. Protocol-agnostic — any
connection whose adapter implements ReadBatchAsync
(OPC UA and MxGateway today) round-trips through
ReadTagValuesCommand. *@
<button class="btn btn-outline-primary"
@onclick="OpenTestBindings"
disabled="@(!HasTestableBindings())">
Test Bindings (@BuildTestableRows().Count)
</button>
</div>
<span class="text-muted small ms-2">Saved bindings reach the site on the next Deploy.@(TotalUnbound > 0 ? $" {TotalUnbound} row(s) are unbound — Deploy will be rejected until they are bound." : "")</span>
</div>
}
</div>
@* Attribute Overrides *@
<div class="card mb-3">
<div class="card-header py-2 d-flex justify-content-between align-items-center">
<strong>Attribute Overrides</strong>
@* Bulk import of attribute overrides from a CSV
(AttributeName,Value[,ElementType]). Selecting a file parses +
validates it against this instance's overridable attributes and —
all-or-nothing — applies every row through the SAME
SetAttributeOverrideAsync path the manual editor uses, or shows the
per-line error list and applies nothing. *@
@if (_overrideAttrs.Count > 0)
{
<label class="btn btn-outline-secondary btn-sm mb-0">
Import overrides (CSV)
<InputFile OnChange="OnCsvImportSelectedAsync"
accept=".csv"
data-test="csv-import-input"
class="d-none"
disabled="@_saving" />
</label>
}
</div>
<div class="card-body p-0">
@if (_csvImportResult is not null)
{
<div class="p-2 pb-0">
<div class="alert @(_csvImportSucceeded ? "alert-success" : "alert-danger") small mb-0"
data-test="csv-import-result">
@if (_csvImportSucceeded)
{
<span>@_csvImportResult</span>
}
else
{
<div class="fw-semibold mb-1">@_csvImportResult</div>
<ul class="mb-0 ps-3">
@foreach (var err in _csvImportErrors)
{
<li>@err</li>
}
</ul>
}
</div>
</div>
}
@if (_overrideAttrs.Count == 0)
{
<p class="text-muted small p-3 mb-0">No overridable (non-locked) attributes in this template.</p>
}
else
{
<table class="table table-sm table-bordered mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Attribute</th>
<th>Type</th>
<th>Template Value</th>
<th style="width: 320px;">Override Value</th>
</tr>
</thead>
<tbody>
@foreach (var attr in _overrideAttrs)
{
<tr data-test="override-row-@attr.Name">
<td class="small">@attr.Name</td>
<td>
<span class="badge bg-secondary-subtle text-secondary-emphasis">@attr.DataType</span>
@if (attr.DataType == DataType.List)
{
@* Element type is fixed by the base attribute — shown
read-only here (the List editor renders it hidden via
ShowElementType="false"). *@
<span class="badge bg-secondary-subtle text-secondary-emphasis border ms-1"
data-test="override-element-type">
of @(attr.ElementDataType ?? DataType.String)
</span>
}
</td>
@* Template values are unbounded — canonical JSON for a
List attribute. Clip, full value on the title. *@
<td class="small text-muted">
<span class="cell-clip" title="@attr.Value">@(attr.Value ?? "—")</span>
</td>
<td>
@if (attr.DataType == DataType.List)
{
@* Whole-list replacement: the shared editor renders the
element-type select hidden (fixed by the base) plus the
repeatable rows. Clearing removes the override row. *@
<AttributeListEditor ElementDataType="@(attr.ElementDataType ?? DataType.String)"
Rows="@GetListRows(attr.Name)"
RowsChanged="@(r => OnListRowsChanged(attr.Name, r))"
ShowElementType="false" />
@if (_overrideErrors.TryGetValue(attr.Name, out var listErr))
{
@* Bounded: this alert lives inside a table cell,
so an unbounded message would widen the column. *@
<div class="alert alert-danger small mt-2 mb-0 text-break"
style="max-width: 20rem;"
data-test="override-list-error">@listErr</div>
}
@if (HasOverrideRow(attr.Name))
{
<button class="btn btn-outline-danger btn-sm mt-2"
data-test="override-clear-btn"
@onclick="() => ClearListOverride(attr.Name)"
disabled="@_saving">Clear Override</button>
}
}
else
{
<input type="text" class="form-control form-control-sm"
value="@GetOverrideValue(attr.Name)"
@onchange="(e) => OnOverrideChanged(attr.Name, e)" />
}
</td>
</tr>
}
</tbody>
</table>
<div class="p-2">
<button class="btn btn-success btn-sm" @onclick="SaveOverrides" disabled="@_saving">Save Overrides</button>
</div>
}
</div>
</div>
@* Alarm Overrides *@
<div class="card mb-3">
<div class="card-header py-2">
<strong>Alarm Overrides</strong>
</div>
<div class="card-body p-0">
@if (_overridableAlarms.Count == 0)
{
<p class="text-muted small p-3 mb-0">No overridable (non-locked) alarms on this template.</p>
}
else
{
<table class="table table-sm table-bordered mb-0">
<thead class="table-light">
<tr>
<th>Alarm</th>
<th style="width: 110px;">Trigger</th>
<th>Inherited Config</th>
<th style="width: 280px;">Override</th>
<th style="width: 140px;">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var alarm in _overridableAlarms)
{
// Comma-joined list of overridden JSON keys — unbounded key
// count, so it is clipped with the full text on the title.
var overrideSummary = HasOverride(alarm.Name) ? OverrideSummary(alarm.Name) : null;
<tr data-test="alarm-override-row-@alarm.Name">
<td class="small">@alarm.Name</td>
<td>
<span class="badge bg-secondary-subtle text-secondary-emphasis border">@alarm.TriggerType</span>
</td>
<td class="small text-muted text-truncate font-monospace" style="max-width: 280px;"
title="@alarm.TriggerConfiguration">
@(alarm.TriggerConfiguration ?? "—")
</td>
<td class="small">
@if (HasOverride(alarm.Name))
{
@* .cell-clip is display:block, so the badge and the
clipped summary share a flex line. *@
<span class="d-flex align-items-center">
<span class="badge bg-warning text-dark me-1" data-test="alarm-override-badge" title="Override is set">●</span>
<span class="text-muted cell-clip cell-clip-sm" title="@overrideSummary">@overrideSummary</span>
</span>
}
else
{
<span class="text-muted fst-italic">inherited</span>
}
</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="@($"Override actions for {alarm.Name}")">
<button class="btn btn-outline-primary"
data-test="alarm-edit-btn"
@onclick="() => BeginEditOverride(alarm)"
disabled="@_saving">Edit</button>
@if (HasOverride(alarm.Name))
{
<button class="btn btn-outline-danger"
data-test="alarm-clear-btn"
@onclick="() => ClearAlarmOverride(alarm.Name)"
disabled="@_saving">Clear</button>
}
</div>
</td>
</tr>
}
</tbody>
</table>
}
</div>
</div>
@* Override edit modal *@
@if (_editingAlarm != null)
{
<div class="modal show d-block sb-modal-backdrop" tabindex="-1">
<div class="modal-dialog modal-dialog-scrollable modal-lg">
<div class="modal-content">
<div class="modal-header">
@* Name and trigger-type badge separated so the heading is a
single identifier rather than an identifier run. *@
<div>
<h6 class="modal-title mb-1">Edit override: @_editingAlarm.Name</h6>
<span class="badge bg-secondary-subtle text-secondary-emphasis border">@_editingAlarm.TriggerType</span>
</div>
<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-body-secondary 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"
data-test="alarm-priority-input"
placeholder="@_editingAlarm.PriorityLevel"
@bind="_editingPriorityText" @bind:event="oninput" />
</div>
</div>
@if (_editingError != null)
{
<div class="alert alert-danger small mt-2 mb-0" data-test="alarm-override-error">@_editingError</div>
}
</div>
<div class="modal-footer justify-content-between">
<div>
@if (HasOverride(_editingAlarm.Name))
{
<button class="btn btn-outline-danger btn-sm"
data-test="alarm-clear-from-modal"
@onclick="() => ClearFromModal()"
disabled="@_saving">Clear Override</button>
}
</div>
<div class="btn-group btn-group-sm" role="group" aria-label="Override edit actions">
<button class="btn btn-outline-secondary" data-test="alarm-cancel-override" @onclick="CancelEditOverride">Cancel</button>
<button class="btn btn-success" data-test="alarm-save-override" @onclick="SaveOverrideFromModal" disabled="@_saving">Save Override</button>
</div>
</div>
</div>
</div>
</div>
}
@* Native Alarm Source Overrides *@
<div class="card mb-3">
<div class="card-header py-2 d-flex justify-content-between align-items-center">
<div>
<strong>Native Alarm Source Overrides</strong>
@* Kept: the blank-means-inherited rule is the only in-context
decoder for the three override inputs below. *@
<small class="text-muted ms-2">
Leave a field blank to keep the inherited value.
</small>
</div>
@* Bulk import of native alarm source retargets from a CSV
(SourceName,Connection,SourceReference,Filter) — the SAME file
format and batch rules as the CLI
`instance native-alarm-source import --file`. Selecting a file
parses it with the shared NativeAlarmSourceOverrideCsvParser and
validates every row against this template's sources; all-or-nothing,
it either upserts each row through the SAME repository path the
inline Save uses, or shows the per-line error list and applies
nothing. Merge semantics — sources absent from the file keep their
existing override. *@
@if (_nativeSources.Count > 0)
{
<label class="btn btn-outline-secondary btn-sm mb-0 flex-shrink-0 ms-2">
Import overrides (CSV)
<InputFile OnChange="OnNativeAlarmSourceCsvImportSelectedAsync"
accept=".csv"
data-test="nas-csv-import-input"
class="d-none"
disabled="@_saving" />
</label>
}
</div>
<div class="card-body p-0">
@if (_nasCsvImportResult is not null)
{
<div class="p-2 pb-0">
<div class="alert @(_nasCsvImportSucceeded ? "alert-success" : "alert-danger") small mb-0"
data-test="nas-csv-import-result">
@if (_nasCsvImportSucceeded)
{
<span>@_nasCsvImportResult</span>
}
else
{
<div class="fw-semibold mb-1">@_nasCsvImportResult</div>
<ul class="mb-0 ps-3">
@foreach (var err in _nasCsvImportErrors)
{
<li>@err</li>
}
</ul>
}
</div>
</div>
}
@if (_nativeSources.Count == 0)
{
<p class="text-muted small p-3 mb-0">No native alarm sources on this template.</p>
}
else
{
<table class="table table-sm table-bordered mb-0 align-middle">
<thead class="table-light">
<tr>
<th>Source</th>
<th>Inherited</th>
<th style="width: 220px;">Connection override</th>
<th style="width: 220px;">Source reference override</th>
<th style="width: 170px;">Filter override</th>
<th style="width: 140px;">Actions</th>
</tr>
</thead>
<tbody>
@foreach (var src in _nativeSources)
{
<tr>
<td class="small">
@src.Name
@if (HasNativeOverride(src.Name))
{
<span class="badge bg-warning text-dark ms-1" title="Override is set">●</span>
}
</td>
@* Two identifiers — connection then source reference —
stacked rather than slammed together with a slash. *@
<td class="small" style="max-width: 200px;"
title="@($"{src.ConnectionName} / {src.SourceReference}")">
<div class="text-truncate">@src.ConnectionName</div>
<div class="text-muted font-monospace text-truncate">@src.SourceReference</div>
</td>
<td>
<select class="form-select form-select-sm"
value="@(_nasConnEdit.GetValueOrDefault(src.Name) ?? "")"
@onchange="e => _nasConnEdit[src.Name] = string.IsNullOrEmpty((string?)e.Value) ? null : (string?)e.Value">
<option value="">(inherited)</option>
@foreach (var c in AlarmCapableConnections())
{
<option value="@c.Name" selected="@(_nasConnEdit.GetValueOrDefault(src.Name) == c.Name)">@c.Name (@c.Protocol)</option>
}
</select>
</td>
<td>
<input class="form-control form-control-sm font-monospace"
placeholder="@src.SourceReference"
value="@(_nasRefEdit.GetValueOrDefault(src.Name) ?? "")"
@onchange="e => _nasRefEdit[src.Name] = string.IsNullOrWhiteSpace((string?)e.Value) ? null : ((string?)e.Value)!.Trim()" />
</td>
<td>
<input class="form-control form-control-sm"
placeholder="@(string.IsNullOrEmpty(src.ConditionFilter) ? "(all)" : src.ConditionFilter)"
value="@(_nasFilterEdit.GetValueOrDefault(src.Name) ?? "")"
@onchange="e => _nasFilterEdit[src.Name] = string.IsNullOrWhiteSpace((string?)e.Value) ? null : ((string?)e.Value)!.Trim()" />
</td>
<td>
<div class="btn-group btn-group-sm" role="group" aria-label="@($"Override actions for {src.Name}")">
<button class="btn btn-success"
@onclick="() => SaveNativeOverride(src.Name)" disabled="@_saving">Save</button>
@if (HasNativeOverride(src.Name))
{
<button class="btn btn-outline-danger"
@onclick="() => ClearNativeOverride(src.Name)" disabled="@_saving">Clear</button>
}
</div>
</td>
</tr>
}
</tbody>
</table>
}
</div>
</div>
@* Area Assignment *@
<div class="card mb-3">
<div class="card-header py-2">
<strong>Area Assignment</strong>
</div>
<div class="card-body py-2">
<div class="d-flex align-items-center gap-2">
<select class="form-select form-select-sm" style="width: auto;" data-test="area-select" @bind="_reassignAreaId">
<option value="0">No area</option>
@foreach (var a in _siteAreas)
{
<option value="@a.Id">@a.Name</option>
}
</select>
<button class="btn btn-outline-primary btn-sm" @onclick="ReassignArea" disabled="@_saving">Set Area</button>
</div>
</div>
</div>
@* OPC UA Tag Browser dialog (Task 18) — rendered once; OpenBrowser
tracks which binding row's override input receives the picked node id. *@
<NodeBrowserDialog @ref="_browserRef"
SiteId="@_browserSiteIdentifier"
ConnectionName="@_browserConnectionName"
InitialNodeId="@_browserInitial"
OnSelected="OnBrowserSelected" />
@* Test Bindings dialog — one-shot live read of every bound attribute.
Method-arg ShowAsync(siteId, rows) — no Razor parameter propagation
race (same pattern as NodeBrowserDialog). *@
<TestBindingsDialog @ref="_testBindingsRef" />
}
</div>
@code {
[Parameter] public int Id { get; set; }
// Set by the Create Instance page's post-create redirect (design 2026-08-21):
// Created=1 always, then either the count of bindings saved or the error that
// stopped them being saved. Read-only display — never persisted.
[SupplyParameterFromQuery] public int? Created { get; set; }
[SupplyParameterFromQuery] public int? Bound { get; set; }
[SupplyParameterFromQuery] public string? BindError { get; set; }
private Instance? _instance;
private string _templateName = "";
private string _siteName = "";
private string _areaName = "";
private bool _loading = true;
private bool _saving;
private string? _errorMessage;
private ToastNotification _toast = default!;
// ── Bindings (design 2026-08-20: rows = flattened attributes, grouped by module) ──
private List<BindingRow> _bindingRows = new();
private IReadOnlyList<BindingGroup> _bindingGroups = Array.Empty<BindingGroup>();
private string? _bindingSourceWarning;
/// <summary>Template default per canonical name (placeholder text).</summary>
private readonly Dictionary<string, string?> _bindingDefaults = new(StringComparer.Ordinal);
/// <summary>
/// The flattened configuration the binding rows were built from; reused by the
/// alarm attribute picker so the page flattens once, not twice.
/// </summary>
private FlattenedConfiguration? _flattenedForBindings;
private List<DataConnection> _siteConnections = new();
private Dictionary<string, int> _bindingSelections = new();
/// <summary>
/// Per-attribute <c>DataSourceReferenceOverride</c> values (Task 18). Mirrors
/// <see cref="_bindingSelections"/> by attribute name. Loaded from the
/// existing <see cref="InstanceConnectionBinding"/> rows on init; round-tripped
/// through <see cref="ConnectionBinding"/> on <c>SaveBindings</c>.
/// </summary>
private Dictionary<string, string?> _bindingOverrides = new();
private int _bulkConnectionId;
// OPC UA tag browser (Task 18) — single dialog rendered at page bottom;
// _browserAttrInEdit tracks which row gets the picked node id on Select.
private NodeBrowserDialog? _browserRef;
private string? _browserAttrInEdit;
private string _browserSiteIdentifier = "";
private string _browserConnectionName = "";
private string? _browserInitial;
private string _siteIdentifier = "";
// Test Bindings dialog — single instance, args passed via ShowAsync (no
// Razor parameter propagation race; same pattern as the OPC UA browser).
private TestBindingsDialog? _testBindingsRef;
// Overrides
private List<TemplateAttribute> _overrideAttrs = new();
private Dictionary<string, string?> _overrideValues = new();
// MV-14: existing override rows keyed by attribute name — tracks which
// attributes already have a persisted override (so List rows know whether a
// Clear is available) and carries the row Id for repository-direct delete.
private Dictionary<string, InstanceAttributeOverride> _existingOverrides = new();
// MV-14: per-List working rows (whole-list replacement), keyed by attribute
// name. Seeded on load from the effective value; encoded to canonical JSON on
// save. Element type is fixed by the base attribute.
private Dictionary<string, List<string>> _listRows = new();
// MV-14: per-attribute validation errors surfaced inline (e.g. an
// un-parseable List element caught on the pre-submit round-trip).
private Dictionary<string, string> _overrideErrors = new();
// CSV bulk-import result summary. _csvImportResult is the headline
// ("Imported N overrides." or "Import rejected — N error(s)."); on failure the
// per-line messages are listed from _csvImportErrors. Null until an import runs.
private string? _csvImportResult;
private bool _csvImportSucceeded;
private IReadOnlyList<string> _csvImportErrors = Array.Empty<string>();
// Reject pathologically large uploads before buffering — a few hundred KB of
// override CSV is already extreme (thousands of attributes).
private const long MaxCsvImportBytes = 512 * 1024;
// Alarm overrides — read-only state pulled from the repo. The edit modal
// is the only mutation path (one alarm at a time).
private List<TemplateAlarm> _overridableAlarms = new();
private Dictionary<string, InstanceAlarmOverride> _existingAlarmOverrides = new();
// Native alarm source overrides — the template's source bindings plus any
// per-instance override rows. Editing is inline (connection / source-ref /
// filter; blank = inherited).
private List<TemplateNativeAlarmSource> _nativeSources = new();
private Dictionary<string, InstanceNativeAlarmSourceOverride> _existingNativeOverrides = new();
private Dictionary<string, string?> _nasConnEdit = new();
private Dictionary<string, string?> _nasRefEdit = new();
private Dictionary<string, string?> _nasFilterEdit = new();
// Native-alarm-source CSV bulk-import result summary — same shape as the
// attribute importer's (_csvImport*) trio. Null until an import runs.
private string? _nasCsvImportResult;
private bool _nasCsvImportSucceeded;
private IReadOnlyList<string> _nasCsvImportErrors = Array.Empty<string>();
// 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>();
// Cached flattened attribute list (direct + inherited + composed members,
// path-qualified canonical names). Populated once after the instance loads
// and fed to the alarm trigger editor so composed-member paths like
// "AlarmSensor.SensorReading" resolve in the picker.
private IReadOnlyList<AlarmAttributeChoice> _flattenedAttributes = Array.Empty<AlarmAttributeChoice>();
// Area
private List<Area> _siteAreas = new();
private int _reassignAreaId;
protected override async Task OnInitializedAsync()
{
try
{
_instance = await TemplateEngineRepository.GetInstanceByIdAsync(Id);
if (_instance == null)
{
_errorMessage = $"Instance #{Id} not found.";
_loading = false;
return;
}
// Site scoping (CentralUI-002): a scoped Deployment user must not be
// able to configure or deploy an instance on a site outside their
// grant by navigating straight to its URL.
if (!await SiteScope.IsSiteAllowedAsync(_instance.SiteId))
{
_instance = null;
_errorMessage = "You are not permitted to manage instances on this site.";
_loading = false;
return;
}
// Identity
var template = await TemplateEngineRepository.GetTemplateByIdAsync(_instance.TemplateId);
_templateName = template?.Name ?? $"#{_instance.TemplateId}";
var sites = await SiteRepository.GetAllSitesAsync();
var site = sites.FirstOrDefault(s => s.Id == _instance.SiteId);
_siteName = site?.Name ?? $"#{_instance.SiteId}";
// Task 18: cache the site's machine identifier — the OPC UA browse
// dialog routes by SiteIdentifier (string), not the numeric site id.
_siteIdentifier = site?.SiteIdentifier ?? "";
// Areas
_siteAreas = (await TemplateEngineRepository.GetAreasBySiteIdAsync(_instance.SiteId)).ToList();
_reassignAreaId = _instance.AreaId ?? 0;
_areaName = _siteAreas.FirstOrDefault(a => a.Id == _reassignAreaId)?.Name ?? "";
// Bindings — existing rows first, then the flattened row source
// (composed module members included) projected over them.
var attrs = await TemplateEngineRepository.GetAttributesByTemplateIdAsync(_instance.TemplateId);
_siteConnections = (await SiteRepository.GetDataConnectionsBySiteIdAsync(_instance.SiteId)).ToList();
var existingBindings = await TemplateEngineRepository.GetBindingsByInstanceIdAsync(Id);
foreach (var b in existingBindings)
{
_bindingSelections[b.AttributeName] = b.DataConnectionId;
if (!string.IsNullOrEmpty(b.DataSourceReferenceOverride))
_bindingOverrides[b.AttributeName] = b.DataSourceReferenceOverride;
}
await LoadBindingRowsAsync(attrs);
// Overrides
_overrideAttrs = attrs.Where(a => !a.IsLocked).ToList();
var existingOverrides = await TemplateEngineRepository.GetOverridesByInstanceIdAsync(Id);
foreach (var o in existingOverrides)
{
_overrideValues[o.AttributeName] = o.OverrideValue;
_existingOverrides[o.AttributeName] = o;
}
// MV-14: seed the per-List working rows. A List attribute's editor is
// initialized from the effective value — the existing override JSON if
// present, otherwise the template default — decoded into string rows
// using the element type fixed by the base attribute. A malformed
// stored value falls back to empty rows (the editor still opens).
foreach (var attr in _overrideAttrs.Where(a => a.DataType == DataType.List))
{
var effective = _existingOverrides.TryGetValue(attr.Name, out var ovr)
? ovr.OverrideValue
: attr.Value;
_listRows[attr.Name] = DecodeListRows(effective, attr.ElementDataType);
}
// Alarm overrides — load all non-locked template alarms and
// existing override rows. Pre-seed the dirty maps from existing
// values so the inputs render with what's currently saved.
var alarms = await TemplateEngineRepository.GetAlarmsByTemplateIdAsync(_instance.TemplateId);
_overridableAlarms = alarms.Where(a => !a.IsLocked).ToList();
var alarmOverrides = await TemplateEngineRepository.GetAlarmOverridesByInstanceIdAsync(Id);
foreach (var o in alarmOverrides)
{
_existingAlarmOverrides[o.AlarmCanonicalName] = o;
}
// Native alarm source bindings + per-instance overrides. Seed the
// inline edit maps from existing override rows (blank = inherited).
_nativeSources = (await TemplateEngineRepository.GetNativeAlarmSourcesByTemplateIdAsync(_instance.TemplateId)).ToList();
_existingNativeOverrides = new();
var nativeOverrides = await TemplateEngineRepository.GetNativeAlarmSourceOverridesByInstanceIdAsync(Id);
foreach (var o in nativeOverrides)
{
_existingNativeOverrides[o.SourceCanonicalName] = o;
}
foreach (var s in _nativeSources)
{
var ovr = _existingNativeOverrides.GetValueOrDefault(s.Name);
_nasConnEdit[s.Name] = ovr?.ConnectionNameOverride;
_nasRefEdit[s.Name] = ovr?.SourceReferenceOverride;
_nasFilterEdit[s.Name] = ovr?.ConditionFilterOverride;
}
_flattenedAttributes = await BuildFlattenedAttributesAsync();
}
catch (Exception ex)
{
_errorMessage = $"Failed to load instance: {ex.Message}";
}
_loading = false;
}
private void GoBack() => NavigationManager.NavigateTo("/deployment/topology");
// CentralUI-024: delegates to the shared helper so the claim type stays
// resolved through JwtTokenService rather than a duplicated magic string.
private async Task<string> GetCurrentUserAsync()
{
var user = await AuthStateProvider.RequireRoleAsync(ZB.MOM.WW.ScadaBridge.Security.Roles.Deployer);
var current = await TemplateEngineRepository.GetInstanceByIdAsync(Id);
if (current is null || !await SiteScope.IsSiteAllowedAsync(current.SiteId))
throw new UnauthorizedAccessException("The instance is outside your current site scope.");
return user;
}
// ── Bindings ────────────────────────────────────────────
private int GetBindingConnectionId(string attrName)
=> _bindingSelections.GetValueOrDefault(attrName, 0);
private void OnBindingChanged(string attrName, ChangeEventArgs e)
{
var val = int.TryParse(e.Value?.ToString(), out var id) ? id : 0;
if (val == 0) _bindingSelections.Remove(attrName);
else _bindingSelections[attrName] = val;
RebuildBindingGroups();
}
/// <summary>All-groups connection select: binds every row to one connection (overrides untouched).</summary>
private void ApplyBulkBinding()
{
if (_bulkConnectionId == 0) return;
foreach (var row in _bindingRows)
_bindingSelections[row.CanonicalName] = _bulkConnectionId;
RebuildBindingGroups();
}
/// <summary>
/// Builds the binding rows from the flattened configuration so composed module
/// members appear. Falls back to the template's direct attributes — with a visible
/// warning — only when the flatten itself fails; a validation failure (e.g. unbound
/// attributes, which is exactly what this panel exists to fix) is NOT a reason to
/// fall back (the pipeline reports validation inside a Success result).
/// </summary>
private async Task LoadBindingRowsAsync(IReadOnlyList<TemplateAttribute> directAttrs)
{
_bindingSourceWarning = null;
IEnumerable<(string Name, string? Default)> source;
try
{
var flat = await FlatteningPipeline.FlattenAndValidateAsync(Id, validateScripts: false);
if (flat.IsFailure)
{
_bindingSourceWarning = $"Could not flatten this instance ({flat.Error}); showing the template's direct attributes only.";
source = DirectAttributeRows(directAttrs);
}
else
{
_flattenedForBindings = flat.Value.Configuration;
source = flat.Value.Configuration.Attributes
// Same predicate as the deploy gate (ValidateConnectionBindingCompleteness:
// DataSourceReference != null), so every row the gate will check has a row here.
.Where(a => a.DefaultDataSourceReference is not null || a.DataSourceReference is not null)
.Select(a => (a.CanonicalName, a.DefaultDataSourceReference ?? a.DataSourceReference));
}
}
catch (Exception ex)
{
_bindingSourceWarning = $"Could not flatten this instance ({ex.Message}); showing the template's direct attributes only.";
source = DirectAttributeRows(directAttrs);
}
_bindingDefaults.Clear();
_bindingRows = source.Select(s =>
{
_bindingDefaults[s.Name] = s.Default;
return new BindingRow(s.Name, s.Default, null, null);
}).ToList();
RebuildBindingGroups();
SnapshotSavedBindingState();
}
private static IEnumerable<(string Name, string? Default)> DirectAttributeRows(IReadOnlyList<TemplateAttribute> directAttrs)
=> directAttrs.Where(a => a.DataSourceReference is not null).Select(a => (a.Name, a.DataSourceReference));
/// <summary>Re-projects the current selections/overrides into the rows, then regroups.</summary>
private void RebuildBindingGroups()
{
_bindingRows = _bindingRows
.Select(r => r with
{
ConnectionId = _bindingSelections.TryGetValue(r.CanonicalName, out var c) ? c : null,
Override = _bindingOverrides.GetValueOrDefault(r.CanonicalName)
})
.ToList();
// A binding to a connection that no longer exists at the site renders as
// "— none —" and the deploy gate rejects it — count it as unbound.
_bindingGroups = BindingGroupBuilder.Build(_bindingRows, _siteConnections.Select(c => c.Id).ToHashSet());
}
/// <summary>Per-row (connection, override) as last loaded/saved — the baseline for the "n changed · unsaved" pill.</summary>
private Dictionary<string, (int? Conn, string? Override)> _savedBindingState = new(StringComparer.Ordinal);
private void SnapshotSavedBindingState()
=> _savedBindingState = _bindingRows.ToDictionary(
r => r.CanonicalName,
r => (_bindingSelections.TryGetValue(r.CanonicalName, out var c) ? c : (int?)null, _bindingOverrides.GetValueOrDefault(r.CanonicalName)),
StringComparer.Ordinal);
/// <summary>Members whose current state differs from the last loaded/saved state (per-row edits and group Applies alike).</summary>
private int GroupChangedCount(BindingGroup g)
=> g.Members.Count(m =>
{
var name = m.Row.CanonicalName;
var current = (_bindingSelections.TryGetValue(name, out var c) ? c : (int?)null, _bindingOverrides.GetValueOrDefault(name));
return !_savedBindingState.TryGetValue(name, out var saved) || saved != current;
});
private int TotalUnbound => _bindingGroups.Sum(g => g.UnboundCount);
private int MixedGroups => _bindingGroups.Count(g => g.IsObjectMixed || g.ConnectionState == BindingConnectionState.Mixed);
// ── Group UI state (expand/collapse, per-group header inputs, unsaved counters) ──
private readonly HashSet<string> _expandedGroups = new(StringComparer.Ordinal);
private readonly Dictionary<string, string?> _groupObjectInput = new(StringComparer.Ordinal);
private readonly Dictionary<string, int?> _groupConnectionInput = new(StringComparer.Ordinal);
private bool _showOnlyUnbound;
private void ToggleGroup(string key) { if (!_expandedGroups.Remove(key)) _expandedGroups.Add(key); }
private void ExpandAll() { foreach (var g in _bindingGroups) _expandedGroups.Add(g.Key); }
/// <summary>Header Object input: the operator's pending edit, else the inferred shared prefix.</summary>
private string? GroupObjectInput(BindingGroup g)
=> _groupObjectInput.TryGetValue(g.Key, out var v) ? v : g.InferredObject;
/// <summary>Header connection select: the operator's pending edit, else the group's common connection.</summary>
private int GroupConnectionInput(BindingGroup g)
=> _groupConnectionInput.TryGetValue(g.Key, out var v) ? v ?? 0 : g.CommonConnectionId ?? 0;
/// <summary>
/// Per-module bulk apply: rewrites every member of the group to
/// <c>{object}.{memberTail}</c> on the header's connection (blank object clears
/// the overrides; a header left at "— none —"/"— mixed —" keeps each member's
/// current connection — Apply never unbinds a group). Edits only the in-memory
/// selection/override maps; Save Bindings persists them.
/// </summary>
private void ApplyGroupObject(BindingGroup g)
{
var connId = GroupConnectionInput(g);
var changes = BindingGroupBuilder.ApplyObject(g, connId > 0 ? connId : null, GroupObjectInput(g));
foreach (var c in changes)
{
if (c.ConnectionId is > 0) _bindingSelections[c.CanonicalName] = c.ConnectionId.Value;
else _bindingSelections.Remove(c.CanonicalName);
if (!string.IsNullOrEmpty(c.Override)) _bindingOverrides[c.CanonicalName] = c.Override;
else _bindingOverrides.Remove(c.CanonicalName);
}
_groupObjectInput.Remove(g.Key);
_groupConnectionInput.Remove(g.Key);
_expandedGroups.Add(g.Key);
RebuildBindingGroups();
}
private IEnumerable<BindingGroupMember> VisibleMembers(BindingGroup g)
=> _showOnlyUnbound ? g.Members.Where(m => m.IsUnbound) : g.Members;
private IEnumerable<BindingGroup> VisibleGroups
=> _showOnlyUnbound ? _bindingGroups.Where(g => g.UnboundCount > 0) : _bindingGroups;
// ── Task 18: per-attribute override input + OPC UA tag browser ──────────
private string? GetOverrideForAttr(string attrName)
=> _bindingOverrides.GetValueOrDefault(attrName);
private void OnOverrideForAttrChanged(string attrName, ChangeEventArgs e)
{
var val = e.Value?.ToString();
if (string.IsNullOrWhiteSpace(val))
_bindingOverrides.Remove(attrName);
else
_bindingOverrides[attrName] = val;
RebuildBindingGroups();
}
/// <summary>Looks up the template default <c>DataSourceReference</c> for an attribute.</summary>
private string? GetTemplateDefault(string attrName)
=> _bindingDefaults.GetValueOrDefault(attrName);
/// <summary>
/// True when the row's selected data connection supports address-space browsing
/// (the tag picker). OPC UA and MxGateway both implement
/// <c>IBrowsableDataConnection</c> site-side; other protocols return a
/// NotBrowsable failure, so the button is hidden for them.
/// </summary>
private bool IsBrowsable(int connectionId)
{
if (connectionId <= 0) return false;
var protocol = _siteConnections.FirstOrDefault(c => c.Id == connectionId)?.Protocol;
return string.Equals(protocol, "OpcUa", StringComparison.OrdinalIgnoreCase)
|| string.Equals(protocol, "MxGateway", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Opens the OPC UA tag browser dialog for the given attribute row. Remembers
/// which attribute is being edited so <see cref="OnBrowserSelected"/> can
/// write the picked node id back to the right override input.
/// </summary>
private async Task OpenBrowser(string attrName)
{
var connId = GetBindingConnectionId(attrName);
var conn = _siteConnections.FirstOrDefault(c => c.Id == connId);
if (conn is null) return;
_browserAttrInEdit = attrName;
_browserConnectionName = conn.Name;
_browserSiteIdentifier = _siteIdentifier;
_browserInitial = _bindingOverrides.GetValueOrDefault(attrName)
?? GetTemplateDefault(attrName);
if (_browserRef is not null)
await _browserRef.ShowAsync(_siteIdentifier, conn.Name, _browserInitial);
}
private void OnBrowserSelected(string nodeId)
{
if (_browserAttrInEdit is null) return;
_bindingOverrides[_browserAttrInEdit] = nodeId;
_browserAttrInEdit = null;
RebuildBindingGroups();
}
// ── Test Bindings (one-shot live read of bound tags) ────────────────────
/// <summary>
/// Builds the list of testable rows: attributes that have a connection
/// picked AND a non-empty effective tag path. Protocol-agnostic — every
/// data-connection adapter implements <c>ReadBatchAsync</c>, so the read
/// routes through <c>ReadTagValuesCommand</c> regardless of protocol
/// (OPC UA and MxGateway today).
/// </summary>
private List<TestBindingsDialog.BindingRowToTest> BuildTestableRows()
{
var rows = new List<TestBindingsDialog.BindingRowToTest>();
foreach (var g in _bindingGroups)
foreach (var m in g.Members)
{
var name = m.Row.CanonicalName;
var connId = GetBindingConnectionId(name);
if (connId <= 0) continue;
var conn = _siteConnections.FirstOrDefault(c => c.Id == connId);
if (conn is null) continue;
// Protocol-agnostic: ReadTagValuesCommand routes through the
// site-side IDataConnection.ReadBatchAsync contract, which every
// adapter implements (OPC UA and MxGateway today). A not-connected
// or unsupported connection short-circuits to a typed banner in the
// dialog rather than being filtered out here — mirrors IsBrowsable.
var effectivePath = _bindingOverrides.GetValueOrDefault(name)
?? GetTemplateDefault(name);
if (string.IsNullOrWhiteSpace(effectivePath)) continue;
rows.Add(new TestBindingsDialog.BindingRowToTest(name, conn.Name, effectivePath));
}
return rows;
}
private bool HasTestableBindings() => BuildTestableRows().Count > 0;
private async Task OpenTestBindings()
{
if (_testBindingsRef is null) return;
var rows = BuildTestableRows();
if (rows.Count == 0) return;
await _testBindingsRef.ShowAsync(_siteIdentifier, rows, _instance?.UniqueName ?? "");
}
private async Task SaveBindings()
{
_saving = true;
try
{
// Task 18: include the per-attribute DataSourceReferenceOverride on
// the wire record so it round-trips through SetConnectionBindingsAsync
// into the InstanceConnectionBinding entity.
var bindings = _bindingSelections
.Select(kv => new ConnectionBinding(
kv.Key,
kv.Value,
_bindingOverrides.GetValueOrDefault(kv.Key)))
.ToList();
var user = await GetCurrentUserAsync();
var result = await MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.SetConnectionBindingsAsync(Id, bindings, currentUser));
if (result.IsSuccess)
{
SnapshotSavedBindingState();
_toast.ShowSuccess($"Saved {bindings.Count} connection binding(s).");
}
else
_toast.ShowError($"Save failed: {result.Error}");
}
catch (Exception ex)
{
_toast.ShowError($"Save failed: {ex.Message}");
}
_saving = false;
}
// ── Overrides ───────────────────────────────────────────
private string? GetOverrideValue(string attrName)
=> _overrideValues.GetValueOrDefault(attrName);
private void OnOverrideChanged(string attrName, ChangeEventArgs e)
{
var val = e.Value?.ToString();
if (string.IsNullOrEmpty(val)) _overrideValues.Remove(attrName);
else _overrideValues[attrName] = val;
}
// ── MV-14: structured List (multi-value) overrides ──────────
/// <summary>Working rows for a List attribute's override (whole-list replacement).</summary>
private List<string> GetListRows(string attrName)
=> _listRows.TryGetValue(attrName, out var rows) ? rows : (_listRows[attrName] = new());
private void OnListRowsChanged(string attrName, List<string> rows)
{
_listRows[attrName] = rows;
// A fresh edit clears any stale validation error for this attribute.
_overrideErrors.Remove(attrName);
}
/// <summary>True if a persisted override row exists for the attribute (so Clear is offered).</summary>
private bool HasOverrideRow(string attrName) => _existingOverrides.ContainsKey(attrName);
/// <summary>
/// Decodes a stored List JSON value into editable string rows using the
/// element type fixed by the base attribute. A malformed stored value (e.g.
/// hand-edited or an element-type mismatch) falls back to empty rows rather
/// than crashing the editor — mirrors TemplateEdit.DecodeListRows.
/// </summary>
private static List<string> DecodeListRows(string? value, DataType? elementType)
{
if (string.IsNullOrEmpty(value)) return new();
try
{
var decoded = AttributeValueCodec.Decode(value, DataType.List, elementType ?? DataType.String);
if (decoded is System.Collections.IEnumerable items)
return items.Cast<object?>()
.Select(x => AttributeValueCodec.Encode(x) ?? string.Empty)
.ToList();
}
catch (FormatException)
{
// Malformed stored value — start from empty so the editor still opens.
}
return new();
}
/// <summary>
/// Removes a List attribute's override row entirely (repository-direct, the
/// same pattern as native-alarm-source overrides) and resets the editor to
/// the inherited template value.
/// </summary>
private async Task ClearListOverride(string attrName)
{
_saving = true;
try
{
if (_existingOverrides.TryGetValue(attrName, out var ovr))
{
await MutationServices.ExecuteConfigurationAsync(new DeleteInstanceAttributeOverrideCommand(Id, attrName));
_existingOverrides.Remove(attrName);
}
_overrideValues.Remove(attrName);
_overrideErrors.Remove(attrName);
// Reset the editor to the inherited template default.
var attr = _overrideAttrs.FirstOrDefault(a => a.Name == attrName);
_listRows[attrName] = DecodeListRows(attr?.Value, attr?.ElementDataType);
_toast.ShowSuccess($"Cleared override on '{attrName}'.");
}
catch (Exception ex)
{
_toast.ShowError($"Clear failed: {ex.Message}");
}
_saving = false;
}
private async Task SaveOverrides()
{
_saving = true;
try
{
_overrideErrors.Clear();
var user = await GetCurrentUserAsync();
// Build the set of override values to persist. Scalars come straight
// from the single-input map (unchanged). List attributes encode their
// working rows to canonical JSON; each is round-tripped through Decode
// first to surface any un-parseable element (mirrors TemplateEdit) —
// an invalid element aborts the whole save and is shown inline.
var toSave = new Dictionary<string, string?>(_overrideValues);
var listAttrs = _overrideAttrs.Where(a => a.DataType == DataType.List).ToList();
var hasError = false;
foreach (var attr in listAttrs)
{
var elementType = attr.ElementDataType ?? DataType.String;
var json = AttributeValueCodec.Encode(GetListRows(attr.Name));
try { AttributeValueCodec.Decode(json, DataType.List, elementType); }
catch (FormatException ex) { _overrideErrors[attr.Name] = ex.Message; hasError = true; continue; }
toSave[attr.Name] = json;
}
if (hasError)
{
_toast.ShowError("Some List overrides have invalid elements — see the highlighted rows.");
_saving = false;
return;
}
var failures = new List<string>();
foreach (var (attrName, value) in toSave)
{
var result = await MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.SetAttributeOverrideAsync(Id, attrName, value, currentUser));
if (result.IsSuccess)
_existingOverrides[attrName] = result.Value!;
else
failures.Add($"{attrName}: {result.Error}");
}
if (failures.Count > 0)
_toast.ShowError($"Failed to save {failures.Count} override(s): {string.Join("; ", failures)}");
var savedCount = toSave.Count - failures.Count;
if (savedCount > 0)
_toast.ShowSuccess($"Saved {savedCount} override(s).");
}
catch (Exception ex)
{
_toast.ShowError($"Save overrides failed: {ex.Message}");
}
_saving = false;
}
// ── CSV bulk override import ────────────────────
/// <summary>
/// Handles a selected override CSV. Reads the file text (size-capped), parses it
/// with the shared <see cref="OverrideCsvParser"/>, validates every row against
/// the instance's overridable attributes via <see cref="BuildCsvOverrideImport"/>,
/// and — all-or-nothing — either applies the parsed overrides through the SAME
/// <c>InstanceService.SetAttributeOverrideAsync</c> path the manual editor uses, or
/// shows the per-line error list and applies nothing. The override editor is
/// refreshed in-place so the applied values are immediately visible.
/// </summary>
private async Task OnCsvImportSelectedAsync(InputFileChangeEventArgs e)
{
_saving = true;
_csvImportResult = null;
_csvImportSucceeded = false;
_csvImportErrors = Array.Empty<string>();
try
{
var file = e.File;
if (file.Size > MaxCsvImportBytes)
{
ShowCsvImportFailure(
$"File too large ({file.Size:N0} bytes). The maximum is {MaxCsvImportBytes:N0} bytes.",
Array.Empty<string>());
return;
}
string text;
using (var reader = new StreamReader(file.OpenReadStream(MaxCsvImportBytes)))
{
text = await reader.ReadToEndAsync();
}
var parsed = OverrideCsvParser.Parse(text);
var outcome = BuildCsvOverrideImport(parsed, _overrideAttrs);
if (outcome.HasErrors)
{
ShowCsvImportFailure(
$"Import rejected — {outcome.Errors.Count} error(s); no overrides applied.",
outcome.Errors);
return;
}
if (outcome.Overrides.Count == 0)
{
ShowCsvImportFailure("No override rows found in the file.", Array.Empty<string>());
return;
}
// Apply through the EXISTING per-attribute submit path (no new server
// method) — identical to SaveOverrides. Update the editor's in-memory
// state so the applied values render immediately.
var user = await GetCurrentUserAsync();
var failures = new List<string>();
foreach (var (attrName, value) in outcome.Overrides)
{
var result = await MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.SetAttributeOverrideAsync(Id, attrName, value, currentUser));
if (result.IsSuccess)
{
_existingOverrides[attrName] = result.Value!;
if (value is null) _overrideValues.Remove(attrName);
else _overrideValues[attrName] = value;
RefreshEditorRowForImport(attrName, value);
}
else
{
failures.Add($"{attrName}: {result.Error}");
}
}
if (failures.Count > 0)
{
ShowCsvImportFailure(
$"Applied {outcome.Overrides.Count - failures.Count} of {outcome.Overrides.Count}; "
+ $"{failures.Count} failed.",
failures);
}
else
{
_csvImportSucceeded = true;
_csvImportResult = $"Imported {outcome.Overrides.Count} override(s).";
_toast.ShowSuccess(_csvImportResult);
}
}
catch (Exception ex)
{
ShowCsvImportFailure($"Import failed: {ex.Message}", Array.Empty<string>());
}
finally
{
_saving = false;
}
}
private void ShowCsvImportFailure(string headline, IReadOnlyList<string> errors)
{
_csvImportSucceeded = false;
_csvImportResult = headline;
_csvImportErrors = errors;
_toast.ShowError(headline);
}
/// <summary>
/// Re-seeds the List editor's working rows for an imported List attribute so the
/// applied value renders immediately. Scalar inputs read <c>_overrideValues</c>
/// directly, so nothing extra is needed for them.
/// </summary>
private void RefreshEditorRowForImport(string attrName, string? value)
{
var attr = _overrideAttrs.FirstOrDefault(a => a.Name == attrName);
if (attr?.DataType == DataType.List)
_listRows[attrName] = DecodeListRows(value, attr.ElementDataType);
}
// ── Alarm overrides ─────────────────────────────────────
private bool HasOverride(string alarmName) =>
_existingAlarmOverrides.ContainsKey(alarmName);
/// <summary>
/// Human-readable summary of the currently-saved override. Lists the
/// HiLo keys that differ from the inherited config plus a priority chip.
/// Used by the row's "Override" column.
/// </summary>
private string OverrideSummary(string alarmName)
{
if (!_existingAlarmOverrides.TryGetValue(alarmName, out var ovr))
return "";
var parts = new List<string>();
if (!string.IsNullOrWhiteSpace(ovr.TriggerConfigurationOverride))
{
try
{
using var doc = System.Text.Json.JsonDocument.Parse(ovr.TriggerConfigurationOverride);
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);
}
/// <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)
{
_editingAlarm = alarm;
_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 = _flattenedAttributes;
}
private void CancelEditOverride()
{
_editingAlarm = null;
_editingError = null;
}
private async Task SaveOverrideFromModal()
{
if (_editingAlarm == null) return;
_saving = true;
try
{
int? priority = null;
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 MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.DeleteAlarmOverrideAsync(Id, alarmName, currentUser));
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 MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.SetAlarmOverrideAsync(
Id, alarmName, overrideJson, priority, currentUser));
if (!result.IsSuccess)
{
_editingError = result.Error;
_saving = false;
return;
}
_existingAlarmOverrides[alarmName] = result.Value!;
_toast.ShowSuccess($"Saved override on '{alarmName}'.");
}
_editingAlarm = null;
_editingError = null;
}
catch (Exception ex)
{
_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)
{
_saving = true;
try
{
var user = await GetCurrentUserAsync();
var result = await MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.DeleteAlarmOverrideAsync(Id, alarmName, currentUser));
if (result.IsSuccess)
{
_existingAlarmOverrides.Remove(alarmName);
_toast.ShowSuccess($"Cleared override on '{alarmName}'.");
}
else
{
_toast.ShowError($"Clear failed: {result.Error}");
}
}
catch (Exception ex)
{
_toast.ShowError($"Clear failed: {ex.Message}");
}
_saving = false;
}
// ── Native alarm source overrides (repository-direct; blank field = inherited) ──
private bool HasNativeOverride(string sourceName) => _existingNativeOverrides.ContainsKey(sourceName);
private IEnumerable<DataConnection> AlarmCapableConnections() =>
_siteConnections.Where(c => AlarmCapableProtocols.IsAlarmCapable(c.Protocol));
private async Task SaveNativeOverride(string sourceName)
{
_saving = true;
try
{
var conn = Blank(_nasConnEdit.GetValueOrDefault(sourceName));
var sref = Blank(_nasRefEdit.GetValueOrDefault(sourceName));
var filt = Blank(_nasFilterEdit.GetValueOrDefault(sourceName));
// All blank → no override; clear any existing row.
if (conn == null && sref == null && filt == null)
{
await ClearNativeOverrideCore(sourceName);
_toast.ShowSuccess($"No override on '{sourceName}' (inherited).");
return;
}
await UpsertNativeOverrideCore(sourceName, conn, sref, filt);
_toast.ShowSuccess($"Saved native alarm source override on '{sourceName}'.");
}
catch (Exception ex)
{
_toast.ShowError($"Save failed: {ex.Message}");
}
_saving = false;
}
private async Task ClearNativeOverride(string sourceName)
{
_saving = true;
try
{
await ClearNativeOverrideCore(sourceName);
_toast.ShowSuccess($"Cleared override on '{sourceName}'.");
}
catch (Exception ex)
{
_toast.ShowError($"Clear failed: {ex.Message}");
}
_saving = false;
}
/// <summary>
/// Upserts one native alarm source override row and re-seeds the inline editor's
/// state for that source. Does NOT call <c>SaveChangesAsync</c> — the caller does,
/// so a bulk import commits the whole batch in one go. Shared by the inline Save
/// and the CSV import so both take the identical persistence path.
/// </summary>
private async Task UpsertNativeOverrideCore(string sourceName, string? conn, string? sref, string? filt)
{
var result = await MutationServices.ExecuteConfigurationAsync(new SetInstanceNativeAlarmSourceOverrideCommand(Id, sourceName, conn, sref, filt));
if (result is InstanceNativeAlarmSourceOverride saved) _existingNativeOverrides[sourceName] = saved;
// Keep the inline editor showing what was just persisted.
_nasConnEdit[sourceName] = conn;
_nasRefEdit[sourceName] = sref;
_nasFilterEdit[sourceName] = filt;
}
private async Task ClearNativeOverrideCore(string sourceName)
{
await MutationServices.ExecuteConfigurationAsync(new DeleteInstanceNativeAlarmSourceOverrideCommand(Id, sourceName));
_existingNativeOverrides.Remove(sourceName);
_nasConnEdit[sourceName] = null;
_nasRefEdit[sourceName] = null;
_nasFilterEdit[sourceName] = null;
}
private static string? Blank(string? v) => string.IsNullOrWhiteSpace(v) ? null : v.Trim();
// ── Native alarm source CSV bulk import ─────────────────
/// <summary>
/// Handles a selected native-alarm-source-override CSV. Reads the file text
/// (size-capped the same way as the attribute importer), parses it with the shared
/// <see cref="NativeAlarmSourceOverrideCsvParser"/> — the exact parser the CLI
/// <c>instance native-alarm-source import --file</c> command uses — validates every
/// row via <see cref="BuildNativeAlarmSourceCsvImport"/>, and — all-or-nothing —
/// either upserts each row through the SAME repository path the inline Save uses,
/// or shows the per-line error list and applies nothing. Merge semantics, matching
/// the CLI: sources absent from the file keep their existing override, a blank
/// field keeps the inherited value, and a row with all three fields blank clears
/// that source's override.
/// </summary>
private async Task OnNativeAlarmSourceCsvImportSelectedAsync(InputFileChangeEventArgs e)
{
_saving = true;
_nasCsvImportResult = null;
_nasCsvImportSucceeded = false;
_nasCsvImportErrors = Array.Empty<string>();
try
{
var file = e.File;
if (file.Size > MaxCsvImportBytes)
{
ShowNativeCsvImportFailure(
$"File too large ({file.Size:N0} bytes). The maximum is {MaxCsvImportBytes:N0} bytes.",
Array.Empty<string>());
return;
}
string text;
using (var reader = new StreamReader(file.OpenReadStream(MaxCsvImportBytes)))
{
text = await reader.ReadToEndAsync();
}
var parsed = NativeAlarmSourceOverrideCsvParser.Parse(text);
var outcome = BuildNativeAlarmSourceCsvImport(parsed, _nativeSources);
if (outcome.HasErrors)
{
ShowNativeCsvImportFailure(
$"Import rejected — {outcome.Errors.Count} error(s); no overrides applied.",
outcome.Errors);
return;
}
if (outcome.Rows.Count == 0)
{
ShowNativeCsvImportFailure("No override rows found in the file.", Array.Empty<string>());
return;
}
await MutationServices.ExecuteConfigurationAsync(new SetInstanceNativeAlarmSourceOverridesCommand(Id,
outcome.Rows.Select(row => new NativeAlarmSourceOverrideEntry(row.SourceName, row.Connection, row.SourceReference, row.Filter)).ToList()));
await OnInitializedAsync();
_nasCsvImportSucceeded = true;
_nasCsvImportResult = $"Imported {outcome.Rows.Count} native alarm source override(s).";
_toast.ShowSuccess(_nasCsvImportResult);
}
catch (Exception ex)
{
ShowNativeCsvImportFailure($"Import failed: {ex.Message}", Array.Empty<string>());
}
finally
{
_saving = false;
}
}
private void ShowNativeCsvImportFailure(string headline, IReadOnlyList<string> errors)
{
_nasCsvImportSucceeded = false;
_nasCsvImportResult = headline;
_nasCsvImportErrors = errors;
_toast.ShowError(headline);
}
/// <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
{
DataType.Boolean => "Boolean",
DataType.Int32 => "Integer",
DataType.Float => "Float",
DataType.Double => "Float",
DataType.String => "String",
DataType.DateTime => "String",
DataType.Binary => "Object",
_ => "Object"
};
/// <summary>
/// Same mapping for the string form emitted by <see cref="Commons.Types.Flattening.ResolvedAttribute.DataType"/>.
/// </summary>
private static string MapDataType(string dt) =>
Enum.TryParse<DataType>(dt, out var parsed) ? MapDataType(parsed) : dt;
/// <summary>
/// Builds the alarm picker choice list from the flattened configuration so
/// composed-member paths (e.g. <c>AlarmSensor.SensorReading</c>) and
/// inherited attributes appear alongside direct ones. Falls back to the
/// direct-only list if flattening fails for any reason.
/// </summary>
private async Task<IReadOnlyList<AlarmAttributeChoice>> BuildFlattenedAttributesAsync()
{
var fallback = (IReadOnlyList<AlarmAttributeChoice>)_overrideAttrs
.Select(a => new AlarmAttributeChoice(a.Name, MapDataType(a.DataType), "Direct"))
.ToList();
try
{
// Reuse the flatten the bindings panel already performed (design
// 2026-08-20) so the page flattens once; flatten afresh only when
// that path fell back.
var configuration = _flattenedForBindings;
if (configuration is null)
{
var flat = await FlatteningPipeline.FlattenAndValidateAsync(Id, validateScripts: false);
if (flat.IsFailure) return fallback;
configuration = flat.Value.Configuration;
}
return configuration.Attributes
.Select(a => new AlarmAttributeChoice(
a.CanonicalName,
MapDataType(a.DataType),
a.Source switch
{
"Composed" => "Composed",
"Inherited" => "Inherited",
_ => "Direct" // Template / Override
}))
.ToList();
}
catch
{
return fallback;
}
}
// ── Area ────────────────────────────────────────────────
private async Task ReassignArea()
{
_saving = true;
try
{
var user = await GetCurrentUserAsync();
var result = await MutationServices.GetRequiredService<ConfigurationCommandService>().ExecuteInstanceAsync(Id, null, (instanceService, currentUser) => instanceService.AssignToAreaAsync(Id, _reassignAreaId == 0 ? null : _reassignAreaId, currentUser));
if (result.IsSuccess)
{
_areaName = _siteAreas.FirstOrDefault(a => a.Id == _reassignAreaId)?.Name ?? "";
_toast.ShowSuccess("Area reassigned.");
}
else
_toast.ShowError($"Reassign failed: {result.Error}");
}
catch (Exception ex)
{
_toast.ShowError($"Reassign failed: {ex.Message}");
}
_saving = false;
}
private static string GetStateBadge(InstanceState state) => state switch
{
InstanceState.Enabled => "bg-success",
InstanceState.Disabled => "bg-secondary",
InstanceState.NotDeployed => "bg-light text-dark",
_ => "bg-secondary"
};
}