Files
ScadaBridge/src/ZB.MOM.WW.ScadaBridge.CentralUI/Components/Pages/Deployment/InstanceConfigure.razor
T

1524 lines
70 KiB
Plaintext

@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.Messages.Management
@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
@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>
@* Bundle D (#23 M7-T12) 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>
@* Connection Bindings *@
<div class="card mb-3">
<div class="card-header py-2 d-flex justify-content-between align-items-center">
<strong>Connection Bindings</strong>
@if (_bindingDataSourceAttrs.Count > 0 && _siteConnections.Count > 0)
{
<div class="d-flex align-items-center gap-1">
<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" @onclick="ApplyBulkBinding"
disabled="@(_bulkConnectionId == 0)">Apply</button>
</div>
}
</div>
<div class="card-body p-0">
@if (_bindingDataSourceAttrs.Count == 0)
{
<p class="text-muted small p-3 mb-0">No data-sourced attributes in this template.</p>
}
else
{
<table class="table table-sm table-bordered mb-0">
<thead class="table-light">
<tr>
<th>Attribute</th>
<th>Tag Path</th>
<th style="width: 280px;">Connection</th>
<th>Override</th>
<th style="width: 110px;"></th>
</tr>
</thead>
<tbody>
@foreach (var attr in _bindingDataSourceAttrs)
{
var connId = GetBindingConnectionId(attr.Name);
var canBrowse = connId > 0;
var isBrowsable = IsBrowsable(connId);
<tr>
<td class="small">@attr.Name</td>
<td class="small text-muted font-monospace">@attr.DataSourceReference</td>
<td>
<select class="form-select form-select-sm"
value="@connId"
@onchange="(e) => OnBindingChanged(attr.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"
value="@GetOverrideForAttr(attr.Name)"
@onchange="(e) => OnOverrideForAttrChanged(attr.Name, e)"
placeholder="@(attr.DataSourceReference ?? "(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(attr.Name)">
Browse…
</button>
}
</td>
</tr>
}
</tbody>
</table>
<div class="p-2 d-flex gap-2">
<button class="btn btn-success btn-sm" @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. Disabled when no testable rows. Protocol-agnostic —
any connection whose adapter implements ReadBatchAsync
(OPC UA and MxGateway today) round-trips through
ReadTagValuesCommand. *@
<button class="btn btn-outline-primary btn-sm"
@onclick="OpenTestBindings"
disabled="@(!HasTestableBindings())">
Test Bindings
</button>
</div>
}
</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>
@* M7-T16: 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-light text-dark">@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-light text-dark border ms-1"
data-test="override-element-type">
of @(attr.ElementDataType ?? DataType.String)
</span>
}
</td>
<td class="small text-muted">@(attr.Value ?? "—")</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))
{
<div class="alert alert-danger small mt-2 mb-0"
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>
<small class="text-muted ms-2">
Click <em>Edit</em> to override an alarm's trigger configuration or priority.
HiLo overrides merge into the inherited setpoints; other trigger types replace the whole config.
</small>
</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)
{
<tr data-test="alarm-override-row-@alarm.Name">
<td class="small">@alarm.Name</td>
<td>
<span class="badge bg-light text-dark 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))
{
<span class="badge bg-warning text-dark me-1" data-test="alarm-override-badge" title="Override is set">●</span>
<span class="text-muted">@OverrideSummary(alarm.Name)</span>
}
else
{
<span class="text-muted fst-italic">inherited</span>
}
</td>
<td>
<button class="btn btn-outline-primary btn-sm me-1"
data-test="alarm-edit-btn"
@onclick="() => BeginEditOverride(alarm)"
disabled="@_saving">Edit</button>
@if (HasOverride(alarm.Name))
{
<button class="btn btn-outline-danger btn-sm"
data-test="alarm-clear-btn"
@onclick="() => ClearAlarmOverride(alarm.Name)"
disabled="@_saving">Clear</button>
}
</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">
<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"
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>
<button class="btn btn-outline-secondary btn-sm" data-test="alarm-cancel-override" @onclick="CancelEditOverride">Cancel</button>
<button class="btn btn-success btn-sm" 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">
<strong>Native Alarm Source Overrides</strong>
<small class="text-muted ms-2">
Retarget an inherited native alarm source binding for this instance.
Leave a field blank to keep the inherited value.
</small>
</div>
<div class="card-body p-0">
@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>
<td class="small text-muted font-monospace text-truncate" style="max-width: 200px;"
title="@($"{src.ConnectionName} / {src.SourceReference}")">
@src.ConnectionName / @src.SourceReference
</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>
<button class="btn btn-success btn-sm me-1"
@onclick="() => SaveNativeOverride(src.Name)" disabled="@_saving">Save</button>
@if (HasNativeOverride(src.Name))
{
<button class="btn btn-outline-danger btn-sm"
@onclick="() => ClearNativeOverride(src.Name)" disabled="@_saving">Clear</button>
}
</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; }
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
private List<TemplateAttribute> _bindingDataSourceAttrs = new();
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();
// M7-T16: 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();
// 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
var attrs = await TemplateEngineRepository.GetAttributesByTemplateIdAsync(_instance.TemplateId);
_bindingDataSourceAttrs = attrs.Where(a => !string.IsNullOrEmpty(a.DataSourceReference)).ToList();
_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;
}
// 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 Task<string> GetCurrentUserAsync()
=> AuthStateProvider.GetCurrentUsernameAsync();
// ── 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;
}
private void ApplyBulkBinding()
{
if (_bulkConnectionId == 0) return;
foreach (var attr in _bindingDataSourceAttrs)
_bindingSelections[attr.Name] = _bulkConnectionId;
}
// ── 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;
}
/// <summary>Looks up the template default <c>DataSourceReference</c> for an attribute.</summary>
private string? GetTemplateDefault(string attrName)
=> _bindingDataSourceAttrs.FirstOrDefault(a => a.Name == attrName)?.DataSourceReference;
/// <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;
}
// ── 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 attr in _bindingDataSourceAttrs)
{
var connId = GetBindingConnectionId(attr.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(attr.Name)
?? GetTemplateDefault(attr.Name);
if (string.IsNullOrWhiteSpace(effectivePath)) continue;
rows.Add(new TestBindingsDialog.BindingRowToTest(attr.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 InstanceService.SetConnectionBindingsAsync(Id, bindings, user);
if (result.IsSuccess)
_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 TemplateEngineRepository.DeleteInstanceAttributeOverrideAsync(ovr.Id);
await TemplateEngineRepository.SaveChangesAsync();
_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 InstanceService.SetAttributeOverrideAsync(Id, attrName, value, user);
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;
}
// ── M7-T16: 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 InstanceService.SetAttributeOverrideAsync(Id, attrName, value, user);
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 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;
}
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 InstanceService.DeleteAlarmOverrideAsync(Id, alarmName, user);
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;
}
var existing = await TemplateEngineRepository.GetNativeAlarmSourceOverrideAsync(Id, sourceName);
if (existing == null)
{
var ovr = new InstanceNativeAlarmSourceOverride(sourceName)
{
InstanceId = Id,
ConnectionNameOverride = conn,
SourceReferenceOverride = sref,
ConditionFilterOverride = filt
};
await TemplateEngineRepository.AddInstanceNativeAlarmSourceOverrideAsync(ovr);
await TemplateEngineRepository.SaveChangesAsync();
_existingNativeOverrides[sourceName] = ovr;
}
else
{
existing.ConnectionNameOverride = conn;
existing.SourceReferenceOverride = sref;
existing.ConditionFilterOverride = filt;
await TemplateEngineRepository.UpdateInstanceNativeAlarmSourceOverrideAsync(existing);
await TemplateEngineRepository.SaveChangesAsync();
_existingNativeOverrides[sourceName] = existing;
}
_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;
}
private async Task ClearNativeOverrideCore(string sourceName)
{
var existing = await TemplateEngineRepository.GetNativeAlarmSourceOverrideAsync(Id, sourceName);
if (existing != null)
{
await TemplateEngineRepository.DeleteInstanceNativeAlarmSourceOverrideAsync(existing.Id);
await TemplateEngineRepository.SaveChangesAsync();
}
_existingNativeOverrides.Remove(sourceName);
_nasConnEdit[sourceName] = null;
_nasRefEdit[sourceName] = null;
_nasFilterEdit[sourceName] = null;
}
private static string? Blank(string? v) => string.IsNullOrWhiteSpace(v) ? null : v.Trim();
/// <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
{
var flat = await FlatteningPipeline.FlattenAndValidateAsync(Id);
if (flat.IsFailure) return fallback;
return flat.Value.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 InstanceService.AssignToAreaAsync(Id, _reassignAreaId == 0 ? null : _reassignAreaId, user);
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"
};
}