chore(cleanup): delete OtOpcUa.Server, OtOpcUa.Admin, and obsolete v1 tests
Task 56: removes the legacy in-process Server + Admin Web project + their test projects (Server.Tests, Admin.Tests, Admin.E2ETests). The fused OtOpcUa.Host binary built across Phases 1-9 is now the sole production entry point. What happened to the 47 legacy Admin Blazor pages: per follow-up F15, the v1 architecture's draft/publish UX is replaced by v2's live-edit + snapshot- deploy model, so a 1:1 migration is not meaningful. The mechanical move via git mv preserves the history; service classes + page bodies that referenced removed v1 types (ConfigGeneration, RedundancyRole, GenerationId) were deleted. AdminUI now ships a minimal Home page + the v2 Deployments page. Per-page rebuild against the v2 surface is tracked as F15. The v2 Deployments page (Task 52) is the only first-party UI shipping in this PR. Task 57: solution build green; 84+ tests green across active v2 + legacy driver test projects.
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
@* Root Blazor component. *@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>OtOpcUa Admin</title>
|
||||
<base href="/"/>
|
||||
@* Admin-010: Bootstrap 5 is vendored under wwwroot/lib/bootstrap/ per admin-ui.md
|
||||
"Tech Stack" — no public-CDN dependency so air-gapped fleet deployments work. *@
|
||||
<link rel="stylesheet" href="lib/bootstrap/css/bootstrap.min.css"/>
|
||||
<link rel="stylesheet" href="css/theme.css"/>
|
||||
<link rel="stylesheet" href="css/site.css"/>
|
||||
<HeadOutlet/>
|
||||
</head>
|
||||
<body>
|
||||
<Routes/>
|
||||
<script src="lib/bootstrap/js/bootstrap.bundle.min.js"></script>
|
||||
<script src="_framework/blazor.web.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,44 +0,0 @@
|
||||
@* Cluster-scoped counterpart of <AuthorizeView>. Renders Authorized/ChildContent only when the
|
||||
signed-in user's effective role for ClusterId meets MinRole; otherwise renders NotAuthorized.
|
||||
Effective role combines fleet-wide and cluster-scoped grants — see ClaimsPrincipalClusterExtensions. *@
|
||||
@using System.Security.Claims
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Security
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
|
||||
@if (_authorized)
|
||||
{
|
||||
@(Authorized ?? ChildContent)
|
||||
}
|
||||
else
|
||||
{
|
||||
@NotAuthorized
|
||||
}
|
||||
|
||||
@code {
|
||||
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
|
||||
|
||||
/// <summary>Cluster the grant is evaluated against.</summary>
|
||||
[Parameter, EditorRequired] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Minimum effective role required to render the authorized content.</summary>
|
||||
[Parameter] public AdminRole MinRole { get; set; } = AdminRole.ConfigViewer;
|
||||
|
||||
/// <summary>Content shown when authorized (alias-friendly: use this or <see cref="ChildContent"/>).</summary>
|
||||
[Parameter] public RenderFragment? Authorized { get; set; }
|
||||
|
||||
/// <summary>Default content slot — shown when authorized if <see cref="Authorized"/> is unset.</summary>
|
||||
[Parameter] public RenderFragment? ChildContent { get; set; }
|
||||
|
||||
/// <summary>Content shown when the user lacks the required role; renders nothing when unset.</summary>
|
||||
[Parameter] public RenderFragment? NotAuthorized { get; set; }
|
||||
|
||||
private bool _authorized;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_authorized = false;
|
||||
if (AuthState is null) return;
|
||||
var user = (await AuthState).User;
|
||||
_authorized = user.HasClusterRole(ClusterId, MinRole);
|
||||
}
|
||||
}
|
||||
@@ -1,152 +0,0 @@
|
||||
@page "/account"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using System.Security.Claims
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Security
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">My account</h4>
|
||||
</div>
|
||||
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
@{
|
||||
var username = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "—";
|
||||
var displayName = context.User.Identity?.Name ?? "—";
|
||||
var roles = context.User.Claims
|
||||
.Where(c => c.Type == ClaimTypes.Role).Select(c => c.Value).ToList();
|
||||
var ldapGroups = context.User.Claims
|
||||
.Where(c => c.Type == "ldap_group").Select(c => c.Value).ToList();
|
||||
var clusterGrants = context.User.Claims
|
||||
.Where(c => c.Type == ClusterRoleClaims.ClaimType)
|
||||
.Select(c => ClusterRoleClaims.Decode(c.Value))
|
||||
.Where(d => d is not null)
|
||||
.Select(d => d!.Value)
|
||||
.OrderBy(d => d.ClusterId, StringComparer.OrdinalIgnoreCase)
|
||||
.ThenBy(d => d.Role)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
<section class="card-grid rise" style="animation-delay:.02s">
|
||||
<div class="metric-card">
|
||||
<div class="panel-head">Identity</div>
|
||||
<div class="kv"><span class="k">Username</span><span class="v mono">@username</span></div>
|
||||
<div class="kv"><span class="k">Display name</span><span class="v">@displayName</span></div>
|
||||
</div>
|
||||
|
||||
<div class="metric-card">
|
||||
<div class="panel-head">Admin roles</div>
|
||||
@if (roles.Count == 0 && clusterGrants.Count == 0)
|
||||
{
|
||||
<div class="kv"><span class="k">Roles</span><span class="v text-muted">No Admin roles mapped — sign-in would have been blocked, so if you're seeing this, the session claim is likely stale.</span></div>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="kv">
|
||||
<span class="k">Fleet-wide roles</span>
|
||||
<span class="v">
|
||||
@if (roles.Count == 0)
|
||||
{
|
||||
<span class="text-muted">none</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
@foreach (var r in roles)
|
||||
{
|
||||
<span class="chip chip-idle me-1">@r</span>
|
||||
}
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
@if (clusterGrants.Count > 0)
|
||||
{
|
||||
<div class="kv">
|
||||
<span class="k">Cluster-scoped roles</span>
|
||||
<span class="v">
|
||||
@foreach (var g in clusterGrants)
|
||||
{
|
||||
<span class="chip chip-idle me-1"><span class="mono">@g.ClusterId</span>: @g.Role</span>
|
||||
}
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
<div class="kv"><span class="k">LDAP groups</span><span class="v">@(ldapGroups.Count == 0 ? "(none surfaced)" : string.Join(", ", ldapGroups))</span></div>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Capabilities</div>
|
||||
<p class="px-3 pt-2 text-muted small">
|
||||
Each Admin role grants a fixed capability set per <span class="mono">admin-ui.md</span> §Admin Roles.
|
||||
Pages below reflect what this session can access; the route's <span class="mono">[Authorize]</span> guard
|
||||
is the ground truth — this table mirrors it for readability. This table covers
|
||||
<em>fleet-wide</em> capabilities only — a cluster-scoped grant unlocks the same actions inside its
|
||||
named cluster without satisfying these fleet-wide policies.
|
||||
</p>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Capability</th>
|
||||
<th>Required role(s)</th>
|
||||
<th class="text-end">You have it?</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var cap in Capabilities)
|
||||
{
|
||||
var has = cap.RequiredRoles.Any(r => roles.Contains(r, StringComparer.OrdinalIgnoreCase));
|
||||
<tr>
|
||||
<td>@cap.Name<br /><small class="text-muted">@cap.Description</small></td>
|
||||
<td>@string.Join(" or ", cap.RequiredRoles)</td>
|
||||
<td class="text-end">
|
||||
@if (has)
|
||||
{
|
||||
<span class="chip chip-ok">Yes</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="chip chip-idle">No</span>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="mt-4">
|
||||
<form method="post" action="/auth/logout">
|
||||
<button class="btn btn-outline-danger" type="submit">Sign out</button>
|
||||
</form>
|
||||
</div>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
@code {
|
||||
private sealed record Capability(string Name, string Description, string[] RequiredRoles);
|
||||
|
||||
// Kept in sync with Program.cs authorization policies + each page's [Authorize] attribute.
|
||||
// When a new page or policy is added, extend this list so operators can self-service check
|
||||
// whether their session has access without trial-and-error navigation.
|
||||
private static readonly IReadOnlyList<Capability> Capabilities =
|
||||
[
|
||||
new("View clusters + fleet status",
|
||||
"Read-only access to the cluster list, fleet dashboard, and generation history.",
|
||||
[AdminRoles.ConfigViewer, AdminRoles.ConfigEditor, AdminRoles.FleetAdmin]),
|
||||
new("Edit configuration drafts",
|
||||
"Create and edit draft generations, manage namespace bindings and node ACLs. CanEdit policy.",
|
||||
[AdminRoles.ConfigEditor, AdminRoles.FleetAdmin]),
|
||||
new("Publish generations",
|
||||
"Promote a draft to Published — triggers node roll-out. CanPublish policy.",
|
||||
[AdminRoles.FleetAdmin]),
|
||||
new("Manage certificate trust",
|
||||
"Trust rejected client certs + revoke trust. FleetAdmin-only because the trust decision gates OPC UA client access.",
|
||||
[AdminRoles.FleetAdmin]),
|
||||
new("Manage external-ID reservations",
|
||||
"Reserve / release external IDs that map into Galaxy contained names.",
|
||||
[AdminRoles.ConfigEditor, AdminRoles.FleetAdmin]),
|
||||
];
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
@page "/alarms/historian"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject HistorianDiagnosticsService Diag
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Alarm historian</h4>
|
||||
</div>
|
||||
<p class="text-muted">Local store-and-forward queue that ships alarm events to Aveva Historian via Galaxy.Host.</p>
|
||||
|
||||
<section class="agg-grid rise" style="animation-delay:.02s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Drain state</div>
|
||||
<div class="agg-value"><span class="chip @BadgeFor(_status.DrainState)">@_status.DrainState</span></div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Queue depth</div>
|
||||
<div class="agg-value numeric">@_status.QueueDepth.ToString("N0")</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Dead-letter depth</div>
|
||||
<div class="agg-value numeric">@_status.DeadLetterDepth.ToString("N0")</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Last success</div>
|
||||
<div class="agg-value">@(_status.LastSuccessUtc?.ToString("u") ?? "—")</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (!string.IsNullOrEmpty(_status.LastError))
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.08s">
|
||||
<strong>Last error:</strong> @_status.LastError
|
||||
</section>
|
||||
}
|
||||
|
||||
<div class="d-flex gap-2 mt-3">
|
||||
<button class="btn btn-outline-secondary" @onclick="RefreshAsync">Refresh</button>
|
||||
<button class="btn btn-warning" disabled="@(_status.DeadLetterDepth == 0)" @onclick="RetryDeadLetteredAsync">
|
||||
Retry dead-lettered (@_status.DeadLetterDepth)
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@if (_retryResult is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.08s">Requeued @_retryResult row(s) for retry.</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private HistorianSinkStatus _status = new(0, 0, null, null, null, HistorianDrainState.Disabled);
|
||||
private int? _retryResult;
|
||||
|
||||
protected override void OnInitialized() => _status = Diag.GetStatus();
|
||||
|
||||
private Task RefreshAsync()
|
||||
{
|
||||
_status = Diag.GetStatus();
|
||||
_retryResult = null;
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task RetryDeadLetteredAsync()
|
||||
{
|
||||
_retryResult = Diag.TryRetryDeadLettered();
|
||||
_status = Diag.GetStatus();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static string BadgeFor(HistorianDrainState s) => s switch
|
||||
{
|
||||
HistorianDrainState.Idle => "chip-ok",
|
||||
HistorianDrainState.Draining => "chip-idle",
|
||||
HistorianDrainState.BackingOff => "chip-warn",
|
||||
HistorianDrainState.Disabled => "chip-idle",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
@page "/certificates"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize(Roles = AdminRoles.FleetAdmin)]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject CertTrustService Certs
|
||||
@inject AuthenticationStateProvider AuthState
|
||||
@inject ILogger<Certificates> Log
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Certificate trust</h4>
|
||||
</div>
|
||||
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
PKI store root <span class="mono">@Certs.PkiStoreRoot</span>. Trusting a rejected cert moves the file into the trusted store — the OPC UA server picks up the change on the next client handshake.
|
||||
</section>
|
||||
|
||||
@if (_status is not null)
|
||||
{
|
||||
<div class="alert alert-@_statusKind alert-dismissible">
|
||||
@_status
|
||||
<button type="button" class="btn-close" @onclick="ClearStatus"></button>
|
||||
</div>
|
||||
}
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Rejected (@_rejected.Count)</div>
|
||||
@if (_rejected.Count == 0)
|
||||
{
|
||||
<p class="px-3 py-2 text-muted">No rejected certificates. Clients that fail to handshake with an untrusted cert land here.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>Subject</th><th>Issuer</th><th>Thumbprint</th><th>Valid</th><th class="text-end">Actions</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var c in _rejected)
|
||||
{
|
||||
<tr>
|
||||
<td>@c.Subject</td>
|
||||
<td>@c.Issuer</td>
|
||||
<td><span class="mono small">@c.Thumbprint</span></td>
|
||||
<td class="small">@c.NotBefore.ToString("yyyy-MM-dd") → @c.NotAfter.ToString("yyyy-MM-dd")</td>
|
||||
<td class="text-end">
|
||||
<button class="btn btn-sm btn-success me-1" @onclick="() => TrustAsync(c)">Trust</button>
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteRejectedAsync(c)">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">Trusted (@_trusted.Count)</div>
|
||||
@if (_trusted.Count == 0)
|
||||
{
|
||||
<p class="px-3 py-2 text-muted">No client certs have been explicitly trusted. The server's own application cert lives in <span class="mono">own/</span> and is not listed here.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>Subject</th><th>Issuer</th><th>Thumbprint</th><th>Valid</th><th class="text-end">Actions</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var c in _trusted)
|
||||
{
|
||||
<tr>
|
||||
<td>@c.Subject</td>
|
||||
<td>@c.Issuer</td>
|
||||
<td><span class="mono small">@c.Thumbprint</span></td>
|
||||
<td class="small">@c.NotBefore.ToString("yyyy-MM-dd") → @c.NotAfter.ToString("yyyy-MM-dd")</td>
|
||||
<td class="text-end">
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => UntrustAsync(c)">Revoke</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
@code {
|
||||
private IReadOnlyList<CertInfo> _rejected = [];
|
||||
private IReadOnlyList<CertInfo> _trusted = [];
|
||||
private string? _status;
|
||||
private string _statusKind = "success";
|
||||
|
||||
protected override void OnInitialized() => Reload();
|
||||
|
||||
private void Reload()
|
||||
{
|
||||
_rejected = Certs.ListRejected();
|
||||
_trusted = Certs.ListTrusted();
|
||||
}
|
||||
|
||||
private async Task TrustAsync(CertInfo c)
|
||||
{
|
||||
if (Certs.TrustRejected(c.Thumbprint))
|
||||
{
|
||||
await LogActionAsync("cert.trust", c);
|
||||
Set($"Trusted cert {c.Subject} ({Short(c.Thumbprint)}).", "success");
|
||||
}
|
||||
else
|
||||
{
|
||||
Set($"Could not trust {Short(c.Thumbprint)} — file missing; another admin may have already handled it.", "warning");
|
||||
}
|
||||
Reload();
|
||||
}
|
||||
|
||||
private async Task DeleteRejectedAsync(CertInfo c)
|
||||
{
|
||||
if (Certs.DeleteRejected(c.Thumbprint))
|
||||
{
|
||||
await LogActionAsync("cert.delete.rejected", c);
|
||||
Set($"Deleted rejected cert {c.Subject} ({Short(c.Thumbprint)}).", "success");
|
||||
}
|
||||
else
|
||||
{
|
||||
Set($"Could not delete {Short(c.Thumbprint)} — file missing.", "warning");
|
||||
}
|
||||
Reload();
|
||||
}
|
||||
|
||||
private async Task UntrustAsync(CertInfo c)
|
||||
{
|
||||
if (Certs.UntrustCert(c.Thumbprint))
|
||||
{
|
||||
await LogActionAsync("cert.untrust", c);
|
||||
Set($"Revoked trust for {c.Subject} ({Short(c.Thumbprint)}).", "success");
|
||||
}
|
||||
else
|
||||
{
|
||||
Set($"Could not revoke {Short(c.Thumbprint)} — file missing.", "warning");
|
||||
}
|
||||
Reload();
|
||||
}
|
||||
|
||||
private async Task LogActionAsync(string action, CertInfo c)
|
||||
{
|
||||
// Cert trust changes are operator-initiated and security-sensitive — Serilog captures the
|
||||
// user + thumbprint trail. CertTrustService also logs at Information on each filesystem
|
||||
// move/delete; this line ties the action to the authenticated admin user so the two logs
|
||||
// correlate. DB-level ConfigAuditLog persistence is deferred — its schema is
|
||||
// cluster-scoped and cert actions are cluster-agnostic.
|
||||
var state = await AuthState.GetAuthenticationStateAsync();
|
||||
var user = state.User.Identity?.Name ?? "(anonymous)";
|
||||
Log.LogInformation("Admin cert action: user={User} action={Action} thumbprint={Thumbprint} subject={Subject}",
|
||||
user, action, c.Thumbprint, c.Subject);
|
||||
}
|
||||
|
||||
private void Set(string message, string kind)
|
||||
{
|
||||
_status = message;
|
||||
_statusKind = kind;
|
||||
}
|
||||
|
||||
private void ClearStatus() => _status = null;
|
||||
|
||||
private static string Short(string thumbprint) =>
|
||||
thumbprint.Length > 12 ? thumbprint[..12] + "…" : thumbprint;
|
||||
}
|
||||
@@ -1,295 +0,0 @@
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.Authorization
|
||||
@inject NodeAclService AclSvc
|
||||
@inject PermissionProbeService ProbeSvc
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<div class="d-flex justify-content-between mb-3">
|
||||
<h4 class="panel-head">Access-control grants</h4>
|
||||
<button class="btn btn-sm btn-primary" @onclick="() => _showForm = true">Add grant</button>
|
||||
</div>
|
||||
|
||||
@if (_acls is null) { <p>Loading…</p> }
|
||||
else if (_acls.Count == 0) { <p class="text-muted">No ACL grants in this draft. Publish will result in a cluster with no external access.</p> }
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Grants</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>LDAP group</th><th>Scope</th><th>Scope ID</th><th>Permissions</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var a in _acls)
|
||||
{
|
||||
<tr>
|
||||
<td>@a.LdapGroup</td>
|
||||
<td>@a.ScopeKind</td>
|
||||
<td><span class="mono">@(a.ScopeId ?? "-")</span></td>
|
||||
<td><span class="mono">@a.PermissionFlags</span></td>
|
||||
<td><button class="btn btn-sm btn-outline-danger" @onclick="() => RevokeAsync(a.NodeAclRowId)">Revoke</button></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@* Probe-this-permission — task #196 slice 1 *@
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">
|
||||
Probe this permission
|
||||
<span class="small text-muted ms-2">
|
||||
Ask the trie "if LDAP group X asks for permission Y on node Z, would it be granted?" —
|
||||
answers the same way the live server does at request time.
|
||||
</span>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="row g-2 align-items-end">
|
||||
<div class="col-md-3">
|
||||
<label class="form-label small">LDAP group</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeGroup" placeholder="cn=fleet-admin,…"/>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<label class="form-label small">Namespace</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeNamespaceId" placeholder="ns-1"/>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<label class="form-label small">UnsArea</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeUnsAreaId"/>
|
||||
</div>
|
||||
<div class="col-md-2">
|
||||
<label class="form-label small">UnsLine</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeUnsLineId"/>
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<label class="form-label small">Equipment</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeEquipmentId"/>
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<label class="form-label small">Tag</label>
|
||||
<input class="form-control form-control-sm" @bind="_probeTagId"/>
|
||||
</div>
|
||||
<div class="col-md-1">
|
||||
<label class="form-label small">Permission</label>
|
||||
<select class="form-select form-select-sm" @bind="_probePermission">
|
||||
@foreach (var p in Enum.GetValues<NodePermissions>())
|
||||
{
|
||||
if (p == NodePermissions.None) continue;
|
||||
<option value="@p">@p</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-outline-primary" @onclick="RunProbeAsync" disabled="@_probing">Probe</button>
|
||||
@if (_probeResult is not null)
|
||||
{
|
||||
<span class="ms-3">
|
||||
@if (_probeResult.Granted)
|
||||
{
|
||||
<span class="chip chip-ok">Granted</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="chip chip-bad">Denied</span>
|
||||
}
|
||||
<span class="small ms-2">
|
||||
Required <span class="mono">@_probeResult.Required</span>,
|
||||
Effective <span class="mono">@_probeResult.Effective</span>
|
||||
</span>
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
@if (_probeResult is not null && _probeResult.Matches.Count > 0)
|
||||
{
|
||||
<div class="table-wrap mt-3">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>LDAP group matched</th><th>Level</th><th>Flags contributed</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var m in _probeResult.Matches)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@m.LdapGroup</span></td>
|
||||
<td>@m.Scope</td>
|
||||
<td><span class="mono">@m.PermissionFlags</span></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
else if (_probeResult is not null)
|
||||
{
|
||||
<div class="mt-2 small text-muted">No matching grants for this (group, scope) — effective permission is <span class="mono">None</span>.</div>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">Add grant</div>
|
||||
<div class="card-body">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">LDAP group</label>
|
||||
<input class="form-control form-control-sm" @bind="_group"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Scope kind</label>
|
||||
<select class="form-select form-select-sm" @bind="_scopeKind">
|
||||
@foreach (var k in Enum.GetValues<NodeAclScopeKind>()) { <option value="@k">@k</option> }
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Scope ID (empty for Cluster-wide)</label>
|
||||
<input class="form-control form-control-sm" @bind="_scopeId"/>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label class="form-label">Permissions (bundled presets — per-flag editor in v2.1)</label>
|
||||
<select class="form-select form-select-sm" @bind="_preset">
|
||||
<option value="Read">Read (Browse + Read)</option>
|
||||
<option value="WriteOperate">Read + Write Operate</option>
|
||||
<option value="Engineer">Read + Write Tune + Write Configure</option>
|
||||
<option value="AlarmAck">Read + Alarm Ack</option>
|
||||
<option value="Full">Full (every flag)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@if (_error is not null) { <section class="panel notice mt-3">@_error</section> }
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private List<NodeAcl>? _acls;
|
||||
private bool _showForm;
|
||||
private string _group = string.Empty;
|
||||
private NodeAclScopeKind _scopeKind = NodeAclScopeKind.Cluster;
|
||||
private string _scopeId = string.Empty;
|
||||
private string _preset = "Read";
|
||||
private string? _error;
|
||||
|
||||
// Probe-this-permission state
|
||||
private string _probeGroup = string.Empty;
|
||||
private string _probeNamespaceId = string.Empty;
|
||||
private string _probeUnsAreaId = string.Empty;
|
||||
private string _probeUnsLineId = string.Empty;
|
||||
private string _probeEquipmentId = string.Empty;
|
||||
private string _probeTagId = string.Empty;
|
||||
private NodePermissions _probePermission = NodePermissions.Read;
|
||||
private PermissionProbeResult? _probeResult;
|
||||
private bool _probing;
|
||||
|
||||
private async Task RunProbeAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_probeGroup)) { _probeResult = null; return; }
|
||||
_probing = true;
|
||||
try
|
||||
{
|
||||
var scope = new NodeScope
|
||||
{
|
||||
ClusterId = ClusterId,
|
||||
NamespaceId = NullIfBlank(_probeNamespaceId),
|
||||
UnsAreaId = NullIfBlank(_probeUnsAreaId),
|
||||
UnsLineId = NullIfBlank(_probeUnsLineId),
|
||||
EquipmentId = NullIfBlank(_probeEquipmentId),
|
||||
TagId = NullIfBlank(_probeTagId),
|
||||
Kind = NodeHierarchyKind.Equipment,
|
||||
};
|
||||
_probeResult = await ProbeSvc.ProbeAsync(GenerationId, _probeGroup.Trim(), scope, _probePermission, CancellationToken.None);
|
||||
}
|
||||
finally { _probing = false; }
|
||||
}
|
||||
|
||||
private static string? NullIfBlank(string s) => string.IsNullOrWhiteSpace(s) ? null : s;
|
||||
|
||||
private HubConnection? _hub;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (!firstRender || _hub is not null) return;
|
||||
_hub = HubFactory.Create("/hubs/fleet");
|
||||
_hub.On<NodeAclChangedMessage>("NodeAclChanged", async msg =>
|
||||
{
|
||||
if (msg.ClusterId != ClusterId || msg.GenerationId != GenerationId) return;
|
||||
_acls = await AclSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
await InvokeAsync(StateHasChanged);
|
||||
});
|
||||
// Best-effort: FleetStatusHub requires an authenticated caller, and the server-side
|
||||
// HubConnection cannot forward the browser auth cookie — swallow connect failures so
|
||||
// the tab still renders. Live ACL-change updates degrade.
|
||||
try
|
||||
{
|
||||
await _hub.StartAsync();
|
||||
await _hub.SendAsync("SubscribeCluster", ClusterId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// best-effort live updates — see comment above
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hub is not null) { await _hub.DisposeAsync(); _hub = null; }
|
||||
}
|
||||
|
||||
protected override async Task OnParametersSetAsync() =>
|
||||
_acls = await AclSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
|
||||
private NodePermissions ResolvePreset() => _preset switch
|
||||
{
|
||||
"Read" => NodePermissions.Browse | NodePermissions.Read,
|
||||
"WriteOperate" => NodePermissions.Browse | NodePermissions.Read | NodePermissions.WriteOperate,
|
||||
"Engineer" => NodePermissions.Browse | NodePermissions.Read | NodePermissions.WriteTune | NodePermissions.WriteConfigure,
|
||||
"AlarmAck" => NodePermissions.Browse | NodePermissions.Read | NodePermissions.AlarmRead | NodePermissions.AlarmAcknowledge,
|
||||
"Full" => unchecked((NodePermissions)(-1)),
|
||||
_ => NodePermissions.Browse | NodePermissions.Read,
|
||||
};
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
_error = null;
|
||||
if (string.IsNullOrWhiteSpace(_group)) { _error = "LDAP group is required"; return; }
|
||||
|
||||
var scopeId = _scopeKind == NodeAclScopeKind.Cluster ? null
|
||||
: string.IsNullOrWhiteSpace(_scopeId) ? null : _scopeId;
|
||||
|
||||
if (_scopeKind != NodeAclScopeKind.Cluster && scopeId is null)
|
||||
{
|
||||
_error = $"ScopeId required for {_scopeKind}";
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await AclSvc.GrantAsync(GenerationId, ClusterId, _group, _scopeKind, scopeId,
|
||||
ResolvePreset(), notes: null, CancellationToken.None);
|
||||
_group = string.Empty; _scopeId = string.Empty;
|
||||
_showForm = false;
|
||||
_acls = await AclSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private async Task RevokeAsync(Guid rowId)
|
||||
{
|
||||
await AclSvc.RevokeAsync(rowId, CancellationToken.None);
|
||||
_acls = await AclSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject AuditLogService AuditSvc
|
||||
|
||||
<h4 class="panel-head">Recent audit log</h4>
|
||||
|
||||
@if (_entries is null) { <p>Loading…</p> }
|
||||
else if (_entries.Count == 0) { <p class="text-muted">No audit entries for this cluster yet.</p> }
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Entries</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>When</th><th>Principal</th><th>Event</th><th>Node</th><th class="num">Generation</th><th>Details</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var a in _entries)
|
||||
{
|
||||
<tr>
|
||||
<td>@a.Timestamp.ToString("u")</td>
|
||||
<td>@a.Principal</td>
|
||||
<td><span class="mono">@a.EventType</span></td>
|
||||
<td>@a.NodeId</td>
|
||||
<td class="num">@a.GenerationId</td>
|
||||
<td><small class="text-muted">@a.DetailsJson</small></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
private List<ConfigAuditLog>? _entries;
|
||||
|
||||
protected override async Task OnParametersSetAsync() =>
|
||||
_entries = await AuditSvc.ListRecentAsync(ClusterId, limit: 100, CancellationToken.None);
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
@page "/clusters/{ClusterId}"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using System.Security.Claims
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Security
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@implements IAsyncDisposable
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
|
||||
@if (!_loaded)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (!_canView)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
You don't have access to cluster <span class="mono">@ClusterId</span>. A fleet-wide or
|
||||
cluster-scoped Admin role grant is required — ask a fleet admin to add one on the
|
||||
<a href="/role-grants">role grants</a> page.
|
||||
</section>
|
||||
}
|
||||
else if (_cluster is null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Cluster <span class="mono">@ClusterId</span> was not found.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (_liveBanner is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
<strong>Live update:</strong> @_liveBanner
|
||||
<button type="button" class="btn-close float-end" @onclick="() => _liveBanner = null"></button>
|
||||
</section>
|
||||
}
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="mb-0">@_cluster.Name</h4>
|
||||
<span class="mono text-muted">@_cluster.ClusterId</span>
|
||||
@if (!_cluster.Enabled) { <span class="chip chip-idle ms-2">Disabled</span> }
|
||||
</div>
|
||||
<div>
|
||||
@if (!_canEdit)
|
||||
{
|
||||
<span class="chip chip-idle">Read-only access</span>
|
||||
}
|
||||
else if (_currentDraft is not null)
|
||||
{
|
||||
<a href="/clusters/@ClusterId/draft/@_currentDraft.GenerationId" class="btn btn-outline-primary">
|
||||
Edit current draft (gen @_currentDraft.GenerationId)
|
||||
</a>
|
||||
}
|
||||
else
|
||||
{
|
||||
<button class="btn btn-primary" @onclick="CreateDraftAsync" disabled="@_busy">New draft</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ul class="nav nav-tabs mb-3">
|
||||
<li class="nav-item"><button class="nav-link @Tab("overview")" @onclick='() => _tab = "overview"'>Overview</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("generations")" @onclick='() => _tab = "generations"'>Generations</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("equipment")" @onclick='() => _tab = "equipment"'>Equipment</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("uns")" @onclick='() => _tab = "uns"'>UNS Structure</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("namespaces")" @onclick='() => _tab = "namespaces"'>Namespaces</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("drivers")" @onclick='() => _tab = "drivers"'>Drivers</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("tags")" @onclick='() => _tab = "tags"'>Tags</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("acls")" @onclick='() => _tab = "acls"'>ACLs</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("redundancy")" @onclick='() => _tab = "redundancy"'>Redundancy</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Tab("audit")" @onclick='() => _tab = "audit"'>Audit</button></li>
|
||||
</ul>
|
||||
|
||||
@if (_tab == "overview")
|
||||
{
|
||||
<section class="card-grid rise" style="animation-delay:.08s">
|
||||
<div class="metric-card">
|
||||
<div class="panel-head">Cluster details</div>
|
||||
<div class="kv"><span class="k">Enterprise / Site</span><span class="v">@_cluster.Enterprise / @_cluster.Site</span></div>
|
||||
<div class="kv"><span class="k">Redundancy</span><span class="v">@_cluster.RedundancyMode (@_cluster.NodeCount node@(_cluster.NodeCount == 1 ? "" : "s"))</span></div>
|
||||
<div class="kv">
|
||||
<span class="k">Current published</span>
|
||||
<span class="v">
|
||||
@if (_currentPublished is not null) { <span>@_currentPublished.GenerationId (@_currentPublished.PublishedAt?.ToString("u"))</span> }
|
||||
else { <span class="text-muted">none published yet</span> }
|
||||
</span>
|
||||
</div>
|
||||
<div class="kv"><span class="k">Created</span><span class="v">@_cluster.CreatedAt.ToString("u") by @_cluster.CreatedBy</span></div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
else if (_tab == "generations")
|
||||
{
|
||||
<Generations ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "equipment" && _currentDraft is not null)
|
||||
{
|
||||
<EquipmentTab GenerationId="@_currentDraft.GenerationId"/>
|
||||
}
|
||||
else if (_tab == "uns" && _currentDraft is not null)
|
||||
{
|
||||
<UnsTab GenerationId="@_currentDraft.GenerationId" ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "namespaces" && _currentDraft is not null)
|
||||
{
|
||||
<NamespacesTab GenerationId="@_currentDraft.GenerationId" ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "drivers" && _currentDraft is not null)
|
||||
{
|
||||
<DriversTab GenerationId="@_currentDraft.GenerationId" ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "tags" && _currentDraft is not null)
|
||||
{
|
||||
<TagsTab GenerationId="@_currentDraft.GenerationId" ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "acls" && _currentDraft is not null)
|
||||
{
|
||||
<AclsTab GenerationId="@_currentDraft.GenerationId" ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "redundancy")
|
||||
{
|
||||
<RedundancyTab ClusterId="@ClusterId"/>
|
||||
}
|
||||
else if (_tab == "audit")
|
||||
{
|
||||
<AuditTab ClusterId="@ClusterId"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">Open a draft to edit this cluster's content.</section>
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
|
||||
private ServerCluster? _cluster;
|
||||
private ConfigGeneration? _currentDraft;
|
||||
private ConfigGeneration? _currentPublished;
|
||||
private string _tab = "overview";
|
||||
private bool _busy;
|
||||
private bool _loaded;
|
||||
private bool _canView;
|
||||
private bool _canEdit;
|
||||
private HubConnection? _hub;
|
||||
private string? _liveBanner;
|
||||
|
||||
private string Tab(string key) => _tab == key ? "active" : string.Empty;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
if (AuthState is not null)
|
||||
{
|
||||
var user = (await AuthState).User;
|
||||
_canView = user.HasClusterRole(ClusterId, AdminRole.ConfigViewer);
|
||||
_canEdit = user.HasClusterRole(ClusterId, AdminRole.ConfigEditor);
|
||||
}
|
||||
_loaded = true;
|
||||
if (!_canView) return;
|
||||
|
||||
await LoadAsync();
|
||||
await ConnectHubAsync();
|
||||
}
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
_cluster = await ClusterSvc.FindAsync(ClusterId, CancellationToken.None);
|
||||
var gens = await GenerationSvc.ListRecentAsync(ClusterId, 50, CancellationToken.None);
|
||||
_currentDraft = gens.FirstOrDefault(g => g.Status == GenerationStatus.Draft);
|
||||
_currentPublished = gens.FirstOrDefault(g => g.Status == GenerationStatus.Published);
|
||||
}
|
||||
|
||||
private async Task ConnectHubAsync()
|
||||
{
|
||||
_hub = HubFactory.Create("/hubs/fleet");
|
||||
|
||||
_hub.On<NodeStateChangedMessage>("NodeStateChanged", async msg =>
|
||||
{
|
||||
if (msg.ClusterId != ClusterId) return;
|
||||
_liveBanner = $"Node {msg.NodeId}: {msg.LastAppliedStatus ?? "seen"} at {msg.LastAppliedAt?.ToString("u") ?? msg.LastSeenAt?.ToString("u") ?? "-"}";
|
||||
await LoadAsync();
|
||||
await InvokeAsync(StateHasChanged);
|
||||
});
|
||||
|
||||
// Best-effort: FleetStatusHub requires an authenticated caller, and the server-side
|
||||
// HubConnection cannot forward the browser auth cookie — a connect failure must not
|
||||
// crash the page. Live banner updates degrade; the page still renders.
|
||||
try
|
||||
{
|
||||
await _hub.StartAsync();
|
||||
await _hub.SendAsync("SubscribeCluster", ClusterId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// best-effort live updates — see comment above
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CreateDraftAsync()
|
||||
{
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
// Admin-007: record the authenticated operator's name, not a static literal.
|
||||
var user = AuthState is not null ? (await AuthState).User : null;
|
||||
var operatorName = user?.FindFirstValue(ClaimTypes.Name)
|
||||
?? user?.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? "unknown";
|
||||
var draft = await GenerationSvc.CreateDraftAsync(ClusterId, createdBy: operatorName, CancellationToken.None);
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/draft/{draft.GenerationId}");
|
||||
}
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hub is not null) await _hub.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
@page "/clusters"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject ClusterService ClusterSvc
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Clusters</h4>
|
||||
<a href="/clusters/new" class="btn btn-primary">New cluster</a>
|
||||
</div>
|
||||
|
||||
@if (_clusters is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_clusters.Count == 0)
|
||||
{
|
||||
<p class="text-muted">No clusters yet. Create the first one.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">All clusters</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ClusterId</th><th>Name</th><th>Enterprise</th><th>Site</th>
|
||||
<th>RedundancyMode</th><th class="num">NodeCount</th><th>Enabled</th><th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var c in _clusters)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@c.ClusterId</span></td>
|
||||
<td>@c.Name</td>
|
||||
<td>@c.Enterprise</td>
|
||||
<td>@c.Site</td>
|
||||
<td>@c.RedundancyMode</td>
|
||||
<td class="num">@c.NodeCount</td>
|
||||
<td>
|
||||
@if (c.Enabled) { <span class="chip chip-ok">Active</span> }
|
||||
else { <span class="chip chip-idle">Disabled</span> }
|
||||
</td>
|
||||
<td><a href="/clusters/@c.ClusterId" class="btn btn-sm btn-outline-primary">Open</a></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private List<ServerCluster>? _clusters;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_clusters = await ClusterSvc.ListAsync(CancellationToken.None);
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
|
||||
@* Per-section diff renderer — the base used by DiffViewer for every known TableName. Caps
|
||||
output at RowCap rows so a pathological draft (e.g. 20k tags churned) can't freeze the
|
||||
Blazor render; overflow banner tells operator how many rows were hidden. *@
|
||||
|
||||
<section class="panel rise mb-3" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<div>
|
||||
<strong>@Title</strong>
|
||||
<small class="text-muted ms-2">@Description</small>
|
||||
</div>
|
||||
<div>
|
||||
@if (_added > 0) { <span class="chip chip-ok me-1">+@_added</span> }
|
||||
@if (_removed > 0) { <span class="chip chip-bad me-1">−@_removed</span> }
|
||||
@if (_modified > 0) { <span class="chip chip-warn me-1">~@_modified</span> }
|
||||
@if (_total == 0) { <span class="chip chip-idle">no changes</span> }
|
||||
</div>
|
||||
</div>
|
||||
@if (_total == 0)
|
||||
{
|
||||
<p class="p-3 text-muted small mb-0">No changes in this section.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (_total > RowCap)
|
||||
{
|
||||
<div class="p-2 small text-muted border-bottom">
|
||||
<span class="s-warn">Showing the first @RowCap of @_total rows</span> — cap protects the browser from megabyte-class
|
||||
diffs. Inspect the remainder via the SQL <span class="mono">sp_ComputeGenerationDiff</span> directly.
|
||||
</div>
|
||||
}
|
||||
<div class="table-wrap" style="max-height: 400px; overflow-y: auto;">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr><th>LogicalId</th><th style="width: 120px;">Change</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _visibleRows)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.LogicalId</span></td>
|
||||
<td>
|
||||
@switch (r.ChangeKind)
|
||||
{
|
||||
case "Added": <span class="chip chip-ok">@r.ChangeKind</span> break;
|
||||
case "Removed": <span class="chip chip-bad">@r.ChangeKind</span> break;
|
||||
case "Modified": <span class="chip chip-warn">@r.ChangeKind</span> break;
|
||||
default: <span class="chip chip-idle">@r.ChangeKind</span> break;
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
@code {
|
||||
/// <summary>Default row-cap per section — matches task #156's acceptance criterion.</summary>
|
||||
public const int DefaultRowCap = 1000;
|
||||
|
||||
[Parameter, EditorRequired] public string Title { get; set; } = string.Empty;
|
||||
[Parameter] public string Description { get; set; } = string.Empty;
|
||||
[Parameter, EditorRequired] public IReadOnlyList<DiffRow> Rows { get; set; } = [];
|
||||
[Parameter] public int RowCap { get; set; } = DefaultRowCap;
|
||||
|
||||
private int _total;
|
||||
private int _added;
|
||||
private int _removed;
|
||||
private int _modified;
|
||||
private List<DiffRow> _visibleRows = [];
|
||||
|
||||
protected override void OnParametersSet()
|
||||
{
|
||||
_total = Rows.Count;
|
||||
_added = 0; _removed = 0; _modified = 0;
|
||||
foreach (var r in Rows)
|
||||
{
|
||||
switch (r.ChangeKind)
|
||||
{
|
||||
case "Added": _added++; break;
|
||||
case "Removed": _removed++; break;
|
||||
case "Modified": _modified++; break;
|
||||
}
|
||||
}
|
||||
_visibleRows = _total > RowCap ? Rows.Take(RowCap).ToList() : Rows.ToList();
|
||||
}
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
@page "/clusters/{ClusterId}/draft/{GenerationId:long}/diff"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject GenerationService GenerationSvc
|
||||
|
||||
<ClusterAuthorizeView ClusterId="@ClusterId" MinRole="AdminRole.ConfigViewer">
|
||||
<NotAuthorized>
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Viewing cluster <span class="mono">@ClusterId</span> requires a fleet-wide or
|
||||
cluster-scoped Admin role grant.
|
||||
</section>
|
||||
</NotAuthorized>
|
||||
<Authorized>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="mb-0">Draft diff</h4>
|
||||
<small class="text-muted">
|
||||
Cluster <span class="mono">@ClusterId</span> — from last published (@(_fromLabel)) → to draft @GenerationId
|
||||
</small>
|
||||
</div>
|
||||
<a class="btn btn-outline-secondary" href="/clusters/@ClusterId/draft/@GenerationId">Back to editor</a>
|
||||
</div>
|
||||
|
||||
@if (_rows is null)
|
||||
{
|
||||
<p>Computing diff…</p>
|
||||
}
|
||||
else if (_error is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s"><span class="s-bad">@_error</span></section>
|
||||
}
|
||||
else if (_rows.Count == 0)
|
||||
{
|
||||
<p class="text-muted">No differences — draft is structurally identical to the last published generation.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<p class="small text-muted mb-3">
|
||||
@_rows.Count row@(_rows.Count == 1 ? "" : "s") across @_sectionsWithChanges of @Sections.Count sections.
|
||||
Each section is capped at @DiffSection.DefaultRowCap rows to keep the browser responsive on pathological drafts.
|
||||
</p>
|
||||
|
||||
@foreach (var sec in Sections)
|
||||
{
|
||||
<DiffSection Title="@sec.Title"
|
||||
Description="@sec.Description"
|
||||
Rows="@RowsFor(sec.TableName)"/>
|
||||
}
|
||||
}
|
||||
|
||||
</Authorized>
|
||||
</ClusterAuthorizeView>
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Ordered section definitions — each maps a <c>TableName</c> emitted by
|
||||
/// <c>sp_ComputeGenerationDiff</c> to a human label + description. The proc currently
|
||||
/// emits Namespace/DriverInstance/Equipment/Tag; UnsLine + NodeAcl entries render as
|
||||
/// empty "no changes" cards until the proc is extended (tracked in tasks #196 + #156
|
||||
/// follow-up). Six sections total matches the task #156 target.
|
||||
/// </summary>
|
||||
private static readonly IReadOnlyList<SectionDef> Sections = new[]
|
||||
{
|
||||
new SectionDef("Namespace", "Namespaces", "OPC UA namespace URIs + enablement"),
|
||||
new SectionDef("DriverInstance", "Driver instances","Per-cluster driver configuration rows"),
|
||||
new SectionDef("Equipment", "Equipment", "UNS level-5 rows + identification fields"),
|
||||
new SectionDef("Tag", "Tags", "Per-device tag definitions + poll-group binding"),
|
||||
new SectionDef("UnsLine", "UNS structure", "Site / Area / Line hierarchy (proc-extension pending)"),
|
||||
new SectionDef("NodeAcl", "ACLs", "LDAP-group → node-scope permission grants (logical id = LdapGroup|ScopeKind|ScopeId)"),
|
||||
};
|
||||
|
||||
private List<DiffRow>? _rows;
|
||||
private string _fromLabel = "(empty)";
|
||||
private string? _error;
|
||||
private int _sectionsWithChanges;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var all = await GenerationSvc.ListRecentAsync(ClusterId, 50, CancellationToken.None);
|
||||
var from = all.FirstOrDefault(g => g.Status == GenerationStatus.Published);
|
||||
_fromLabel = from is null ? "(empty)" : $"gen {from.GenerationId}";
|
||||
_rows = await GenerationSvc.ComputeDiffAsync(from?.GenerationId ?? 0, GenerationId, CancellationToken.None);
|
||||
_sectionsWithChanges = Sections.Count(s => _rows.Any(r => r.TableName == s.TableName));
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private IReadOnlyList<DiffRow> RowsFor(string tableName) =>
|
||||
_rows?.Where(r => r.TableName == tableName).ToList() ?? [];
|
||||
|
||||
private sealed record SectionDef(string TableName, string Title, string Description);
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
@page "/clusters/{ClusterId}/draft/{GenerationId:long}"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Validation
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject DraftValidationService ValidationSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<ClusterAuthorizeView ClusterId="@ClusterId" MinRole="AdminRole.ConfigEditor">
|
||||
<NotAuthorized>
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Editing cluster <span class="mono">@ClusterId</span> requires the
|
||||
<span class="mono">ConfigEditor</span> role for this cluster.
|
||||
</section>
|
||||
</NotAuthorized>
|
||||
<Authorized>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="mb-0">Draft editor</h4>
|
||||
<small class="text-muted">Cluster <span class="mono">@ClusterId</span> · generation @GenerationId</small>
|
||||
</div>
|
||||
<div>
|
||||
<a class="btn btn-outline-secondary" href="/clusters/@ClusterId">Back to cluster</a>
|
||||
<a class="btn btn-outline-primary ms-2" href="/clusters/@ClusterId/draft/@GenerationId/diff">View diff</a>
|
||||
<ClusterAuthorizeView ClusterId="@ClusterId" MinRole="AdminRole.FleetAdmin">
|
||||
<button class="btn btn-primary ms-2" disabled="@(_errors.Count != 0 || _busy)" @onclick="PublishAsync">Publish</button>
|
||||
</ClusterAuthorizeView>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ul class="nav nav-tabs mb-3">
|
||||
<li class="nav-item"><button class="nav-link @Active("equipment")" @onclick='() => _tab = "equipment"'>Equipment</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("uns")" @onclick='() => _tab = "uns"'>UNS</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("namespaces")" @onclick='() => _tab = "namespaces"'>Namespaces</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("drivers")" @onclick='() => _tab = "drivers"'>Drivers</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("acls")" @onclick='() => _tab = "acls"'>ACLs</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("scripts")" @onclick='() => _tab = "scripts"'>Scripts</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("virtual-tags")" @onclick='() => _tab = "virtual-tags"'>Virtual Tags</button></li>
|
||||
<li class="nav-item"><button class="nav-link @Active("scripted-alarms")" @onclick='() => _tab = "scripted-alarms"'>Scripted Alarms</button></li>
|
||||
</ul>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
@if (_tab == "equipment") { <EquipmentTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "uns") { <UnsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "namespaces") { <NamespacesTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "drivers") { <DriversTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "acls") { <AclsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "scripts") { <ScriptsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "virtual-tags") { <VirtualTagsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
else if (_tab == "scripted-alarms") { <ScriptedAlarmsTab GenerationId="@GenerationId" ClusterId="@ClusterId"/> }
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<section class="panel rise sticky-top" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<strong>Validation</strong>
|
||||
<button class="btn btn-sm btn-outline-secondary" @onclick="RevalidateAsync">Re-run</button>
|
||||
</div>
|
||||
<div class="p-3">
|
||||
@if (_validating) { <p class="text-muted">Checking…</p> }
|
||||
else if (_errors.Count == 0) { <p class="s-ok mb-0">No validation errors — safe to publish.</p> }
|
||||
else
|
||||
{
|
||||
<p class="s-bad mb-2">@_errors.Count error@(_errors.Count == 1 ? "" : "s")</p>
|
||||
<ul class="list-unstyled">
|
||||
@foreach (var e in _errors)
|
||||
{
|
||||
<li class="mb-2">
|
||||
<span class="chip chip-bad me-1">@e.Code</span>
|
||||
<small>@e.Message</small>
|
||||
@if (!string.IsNullOrEmpty(e.Context)) { <div class="text-muted"><span class="mono">@e.Context</span></div> }
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_publishError is not null) { <section class="panel notice rise mt-2" style="animation-delay:.08s"><span class="s-bad">@_publishError</span></section> }
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</Authorized>
|
||||
</ClusterAuthorizeView>
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
|
||||
private string _tab = "equipment";
|
||||
private List<ValidationError> _errors = [];
|
||||
private bool _validating;
|
||||
private bool _busy;
|
||||
private string? _publishError;
|
||||
|
||||
private string Active(string k) => _tab == k ? "active" : string.Empty;
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await RevalidateAsync();
|
||||
|
||||
private async Task RevalidateAsync()
|
||||
{
|
||||
_validating = true;
|
||||
try
|
||||
{
|
||||
var errors = await ValidationSvc.ValidateAsync(GenerationId, CancellationToken.None);
|
||||
_errors = errors.ToList();
|
||||
}
|
||||
finally { _validating = false; }
|
||||
}
|
||||
|
||||
private async Task PublishAsync()
|
||||
{
|
||||
_busy = true;
|
||||
_publishError = null;
|
||||
try
|
||||
{
|
||||
await GenerationSvc.PublishAsync(ClusterId, GenerationId, notes: "Published via Admin UI", CancellationToken.None);
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}");
|
||||
}
|
||||
catch (Exception ex) { _publishError = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
@using System.Text.Json
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Pages.Modbus
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject DriverInstanceService DriverSvc
|
||||
@inject NamespaceService NsSvc
|
||||
|
||||
<div class="d-flex justify-content-between mb-3">
|
||||
<h4 class="panel-head">DriverInstances</h4>
|
||||
<button class="btn btn-sm btn-primary" @onclick="() => _showForm = true">Add driver</button>
|
||||
</div>
|
||||
|
||||
@if (_drivers is null) { <p>Loading…</p> }
|
||||
else if (_drivers.Count == 0) { <p class="text-muted">No drivers configured in this draft.</p> }
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Configured drivers</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>DriverInstanceId</th><th>Name</th><th>Type</th><th>Namespace</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var d in _drivers)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@d.DriverInstanceId</span></td>
|
||||
<td>@d.Name</td>
|
||||
<td>
|
||||
@if (string.Equals(d.DriverType, "Focas", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
<a href="/drivers/focas/@d.DriverInstanceId">@d.DriverType</a>
|
||||
}
|
||||
else
|
||||
{
|
||||
@d.DriverType
|
||||
}
|
||||
</td>
|
||||
<td><span class="mono">@d.NamespaceId</span></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_showForm && _namespaces is not null)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Add driver</div>
|
||||
<div class="card-body">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-3">
|
||||
<label class="form-label">Name</label>
|
||||
<input class="form-control form-control-sm" @bind="_name"/>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<label class="form-label">DriverType</label>
|
||||
<select class="form-select form-select-sm" @bind="_type">
|
||||
<option>Galaxy</option>
|
||||
<option>Modbus</option>
|
||||
<option>AbCip</option>
|
||||
<option>AbLegacy</option>
|
||||
<option>S7</option>
|
||||
<option>Focas</option>
|
||||
<option>OpcUaClient</option>
|
||||
</select>
|
||||
<div class="form-text">Type string must match the driver's registered factory name; this dropdown wraps the canonical names.</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Namespace</label>
|
||||
<select class="form-select form-select-sm" @bind="_nsId">
|
||||
@foreach (var n in _namespaces) { <option value="@n.NamespaceId">@n.Kind — @n.NamespaceUri</option> }
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
@if (string.Equals(_type, "Modbus", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
@* #147 — typed editor for Modbus drivers. The generic textarea is a fall-back
|
||||
for driver types that haven't yet shipped a typed editor. *@
|
||||
<label class="form-label">Modbus options (typed editor)</label>
|
||||
<ModbusOptionsEditor Model="_modbusOptions"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<label class="form-label">DriverConfig JSON (schemaless per driver type)</label>
|
||||
<textarea class="form-control form-control-sm font-monospace" rows="6" @bind="_config"></textarea>
|
||||
<div class="form-text">Phase 1: generic JSON editor — per-driver schema validation arrives in each driver's phase (decision #94).</div>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@if (_error is not null) { <section class="panel notice mt-3">@_error</section> }
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private List<DriverInstance>? _drivers;
|
||||
private List<Namespace>? _namespaces;
|
||||
private bool _showForm;
|
||||
private string _name = string.Empty;
|
||||
private string _type = "Modbus";
|
||||
private string _nsId = string.Empty;
|
||||
private string _config = "{}";
|
||||
private string? _error;
|
||||
|
||||
// #147 — typed editor model for Modbus drivers. Defaults match ModbusDriverOptions
|
||||
// defaults so an unedited form produces config equivalent to the historical
|
||||
// pre-typed-editor wire output. Serialised to _config on Save when type=Modbus.
|
||||
private ModbusOptionsEditor.ModbusOptionsViewModel _modbusOptions = new();
|
||||
private static readonly JsonSerializerOptions ModbusJsonOptions = new() { WriteIndented = true };
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_drivers = await DriverSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_namespaces = await NsSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_nsId = _namespaces.FirstOrDefault()?.NamespaceId ?? string.Empty;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
_error = null;
|
||||
if (string.IsNullOrWhiteSpace(_name) || string.IsNullOrWhiteSpace(_nsId))
|
||||
{
|
||||
_error = "Name and Namespace are required";
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
// #147 — for Modbus drivers serialize the typed editor model into the DriverConfig
|
||||
// JSON column. Other driver types still use the raw textarea contents until each
|
||||
// ships its own typed editor (decision #94 — per-driver schema validation arrives
|
||||
// per driver phase).
|
||||
var configJson = string.Equals(_type, "Modbus", StringComparison.OrdinalIgnoreCase)
|
||||
? SerializeModbusOptions(_modbusOptions)
|
||||
: _config;
|
||||
|
||||
await DriverSvc.AddAsync(GenerationId, ClusterId, _nsId, _name, _type, configJson, CancellationToken.None);
|
||||
_name = string.Empty; _config = "{}";
|
||||
_modbusOptions = new();
|
||||
_showForm = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maps the view-model field names onto the JSON shape <c>ModbusDriverFactoryExtensions</c>
|
||||
/// consumes. Hand-rolled because the DTO uses millisecond / byte field flavours that the
|
||||
/// view model exposes as TimeSpan-derived integers; a System.Text.Json round-trip would
|
||||
/// emit the .NET-native names instead.
|
||||
/// </summary>
|
||||
private static string SerializeModbusOptions(ModbusOptionsEditor.ModbusOptionsViewModel m) =>
|
||||
JsonSerializer.Serialize(new
|
||||
{
|
||||
host = m.Host,
|
||||
port = m.Port,
|
||||
unitId = m.UnitId,
|
||||
family = m.Family.ToString(),
|
||||
melsecSubFamily = m.MelsecSubFamily.ToString(),
|
||||
keepAlive = new
|
||||
{
|
||||
enabled = m.KeepAliveEnabled,
|
||||
timeMs = m.KeepAliveTimeSec * 1000,
|
||||
intervalMs = m.KeepAliveIntervalSec * 1000,
|
||||
retryCount = m.KeepAliveRetryCount,
|
||||
},
|
||||
reconnect = new
|
||||
{
|
||||
initialDelayMs = m.ReconnectInitialDelayMs,
|
||||
maxDelayMs = m.ReconnectMaxDelayMs,
|
||||
backoffMultiplier = m.ReconnectBackoffMultiplier,
|
||||
},
|
||||
maxRegistersPerRead = m.MaxRegistersPerRead,
|
||||
maxRegistersPerWrite = m.MaxRegistersPerWrite,
|
||||
maxCoilsPerRead = m.MaxCoilsPerRead,
|
||||
maxReadGap = m.MaxReadGap,
|
||||
useFC15ForSingleCoilWrites = m.UseFC15ForSingleCoilWrites,
|
||||
useFC16ForSingleRegisterWrites = m.UseFC16ForSingleRegisterWrites,
|
||||
writeOnChangeOnly = m.WriteOnChangeOnly,
|
||||
tags = Array.Empty<object>(),
|
||||
}, ModbusJsonOptions);
|
||||
}
|
||||
@@ -1,332 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Validation
|
||||
@inject EquipmentService EquipmentSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<div class="d-flex justify-content-between mb-3">
|
||||
<h4 class="panel-head">Equipment (draft gen @GenerationId)</h4>
|
||||
<div>
|
||||
<button class="btn btn-outline-primary btn-sm me-2" @onclick="GoImport">Import CSV…</button>
|
||||
<button class="btn btn-primary btn-sm" @onclick="StartAdd">Add equipment</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@* Five-identifier search — decision #117: ZTag / MachineCode / SAPID / EquipmentId / EquipmentUuid *@
|
||||
<section class="panel rise mb-3" style="animation-delay:.02s">
|
||||
<div class="panel-head">Search equipment</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-2 align-items-end">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label small mb-1">
|
||||
Search by ZTag, MachineCode, SAPID, EquipmentId, or EquipmentUuid
|
||||
</label>
|
||||
<input class="form-control form-control-sm"
|
||||
placeholder="e.g. z-001 or MC-42 or SAP-…"
|
||||
@bind="_searchQuery"
|
||||
@bind:event="oninput"
|
||||
@onkeydown="OnSearchKeyDown"/>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<div class="form-check form-check-inline mb-0">
|
||||
<input class="form-check-input" type="checkbox" id="fuzzyCheck" @bind="_searchFuzzy"/>
|
||||
<label class="form-check-label small" for="fuzzyCheck">Fuzzy (substring)</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-auto">
|
||||
<button class="btn btn-sm btn-outline-secondary" @onclick="RunSearchAsync" disabled="@_searchBusy">Search</button>
|
||||
@if (_searchHits is not null)
|
||||
{
|
||||
<button class="btn btn-sm btn-link ms-1" @onclick="ClearSearch">Clear</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
@if (_searchError is not null)
|
||||
{
|
||||
<p class="small text-danger mt-2 mb-0">@_searchError</p>
|
||||
}
|
||||
</div>
|
||||
|
||||
@if (_searchHits is not null)
|
||||
{
|
||||
@if (_searchHits.Count == 0)
|
||||
{
|
||||
<p class="p-3 text-muted small mb-0">No matches.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="table-wrap" style="max-height: 340px; overflow-y: auto;">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>EquipmentId</th><th>Name</th><th>MachineCode</th><th>ZTag</th><th>SAPID</th>
|
||||
<th style="width:110px">Matched</th><th style="width:80px">Gen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var hit in _searchHits)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@hit.Equipment.EquipmentId</span></td>
|
||||
<td>@hit.Equipment.Name</td>
|
||||
<td>@hit.Equipment.MachineCode</td>
|
||||
<td>@hit.Equipment.ZTag</td>
|
||||
<td>@hit.Equipment.SAPID</td>
|
||||
<td>
|
||||
@if (hit.MatchedField is not null)
|
||||
{
|
||||
var chipClass = hit.Score switch
|
||||
{
|
||||
100 => "chip chip-ok",
|
||||
50 => "chip chip-warn",
|
||||
_ => "chip chip-idle",
|
||||
};
|
||||
<span class="@chipClass">@hit.MatchedField</span>
|
||||
}
|
||||
</td>
|
||||
<td>
|
||||
@if (hit.IsPublished)
|
||||
{ <span class="chip chip-ok">pub</span> }
|
||||
else
|
||||
{ <span class="chip chip-idle">draft</span> }
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p class="p-2 text-muted small mb-0">
|
||||
@_searchHits.Count result@(_searchHits.Count == 1 ? "" : "s").
|
||||
Exact = green, prefix = amber, fuzzy = grey.
|
||||
Fuzzy matching requires the "Fuzzy" checkbox.
|
||||
</p>
|
||||
}
|
||||
}
|
||||
</section>
|
||||
|
||||
@if (_equipment is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_equipment.Count == 0 && !_showForm)
|
||||
{
|
||||
<p class="text-muted">No equipment in this draft yet.</p>
|
||||
}
|
||||
else if (_equipment.Count > 0)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Equipment list</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>EquipmentId</th><th>Name</th><th>MachineCode</th><th>ZTag</th><th>SAPID</th>
|
||||
<th>Manufacturer / Model</th><th>Serial</th><th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var e in _equipment)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@e.EquipmentId</span></td>
|
||||
<td>@e.Name</td>
|
||||
<td>@e.MachineCode</td>
|
||||
<td>@e.ZTag</td>
|
||||
<td>@e.SAPID</td>
|
||||
<td>@e.Manufacturer / @e.Model</td>
|
||||
<td>@e.SerialNumber</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-outline-secondary me-1" @onclick="() => StartEdit(e)">Edit</button>
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteAsync(e.EquipmentRowId)">Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||||
<div class="panel-head">@(_editMode ? "Edit equipment" : "New equipment")</div>
|
||||
<div class="card-body">
|
||||
<EditForm Model="_draft" OnValidSubmit="SaveAsync" FormName="equipment-form">
|
||||
<DataAnnotationsValidator/>
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Name (UNS segment)</label>
|
||||
<InputText @bind-Value="_draft.Name" class="form-control form-control-sm"/>
|
||||
<ValidationMessage For="() => _draft.Name"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">MachineCode</label>
|
||||
<InputText @bind-Value="_draft.MachineCode" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">DriverInstanceId</label>
|
||||
<InputText @bind-Value="_draft.DriverInstanceId" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">UnsLineId</label>
|
||||
<InputText @bind-Value="_draft.UnsLineId" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">ZTag</label>
|
||||
<InputText @bind-Value="_draft.ZTag" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">SAPID</label>
|
||||
<InputText @bind-Value="_draft.SAPID" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<IdentificationFields Equipment="_draft"/>
|
||||
|
||||
@if (_error is not null) { <section class="panel notice mt-3">@_error</section> }
|
||||
|
||||
<div class="mt-3">
|
||||
<button type="submit" class="btn btn-primary btn-sm">Save</button>
|
||||
<button type="button" class="btn btn-secondary btn-sm ms-2" @onclick="Cancel">Cancel</button>
|
||||
</div>
|
||||
</EditForm>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private void GoImport() => Nav.NavigateTo($"/clusters/{ClusterId}/draft/{GenerationId}/import-equipment");
|
||||
private List<Equipment>? _equipment;
|
||||
private bool _showForm;
|
||||
private bool _editMode;
|
||||
private Equipment _draft = NewBlankDraft();
|
||||
private string? _error;
|
||||
|
||||
// ── Five-identifier search ──────────────────────────────────────────
|
||||
private string _searchQuery = string.Empty;
|
||||
private bool _searchFuzzy;
|
||||
private IReadOnlyList<EquipmentSearchHit>? _searchHits;
|
||||
private bool _searchBusy;
|
||||
private string? _searchError;
|
||||
|
||||
private async Task RunSearchAsync()
|
||||
{
|
||||
_searchError = null;
|
||||
if (string.IsNullOrWhiteSpace(_searchQuery)) { _searchHits = null; return; }
|
||||
_searchBusy = true;
|
||||
try
|
||||
{
|
||||
_searchHits = await EquipmentSvc.SearchAsync(
|
||||
_searchQuery, ClusterId, CancellationToken.None,
|
||||
maxResults: 50, allowFuzzy: _searchFuzzy);
|
||||
}
|
||||
catch (Exception ex) { _searchError = ex.Message; }
|
||||
finally { _searchBusy = false; }
|
||||
}
|
||||
|
||||
private void ClearSearch()
|
||||
{
|
||||
_searchQuery = string.Empty;
|
||||
_searchHits = null;
|
||||
_searchError = null;
|
||||
}
|
||||
|
||||
private async Task OnSearchKeyDown(KeyboardEventArgs e)
|
||||
{
|
||||
if (e.Key == "Enter") await RunSearchAsync();
|
||||
}
|
||||
// ───────────────────────────────────────────────────────────────────
|
||||
|
||||
private static Equipment NewBlankDraft() => new()
|
||||
{
|
||||
EquipmentId = string.Empty, DriverInstanceId = string.Empty,
|
||||
UnsLineId = string.Empty, Name = string.Empty, MachineCode = string.Empty,
|
||||
};
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_equipment = await EquipmentSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private void StartAdd()
|
||||
{
|
||||
_draft = NewBlankDraft();
|
||||
_editMode = false;
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private void StartEdit(Equipment row)
|
||||
{
|
||||
// Shallow-clone so Cancel doesn't mutate the list-displayed row with in-flight form edits.
|
||||
_draft = new Equipment
|
||||
{
|
||||
EquipmentRowId = row.EquipmentRowId,
|
||||
GenerationId = row.GenerationId,
|
||||
EquipmentId = row.EquipmentId,
|
||||
EquipmentUuid = row.EquipmentUuid,
|
||||
DriverInstanceId = row.DriverInstanceId,
|
||||
DeviceId = row.DeviceId,
|
||||
UnsLineId = row.UnsLineId,
|
||||
Name = row.Name,
|
||||
MachineCode = row.MachineCode,
|
||||
ZTag = row.ZTag,
|
||||
SAPID = row.SAPID,
|
||||
Manufacturer = row.Manufacturer,
|
||||
Model = row.Model,
|
||||
SerialNumber = row.SerialNumber,
|
||||
HardwareRevision = row.HardwareRevision,
|
||||
SoftwareRevision = row.SoftwareRevision,
|
||||
YearOfConstruction = row.YearOfConstruction,
|
||||
AssetLocation = row.AssetLocation,
|
||||
ManufacturerUri = row.ManufacturerUri,
|
||||
DeviceManualUri = row.DeviceManualUri,
|
||||
EquipmentClassRef = row.EquipmentClassRef,
|
||||
Enabled = row.Enabled,
|
||||
};
|
||||
_editMode = true;
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
_showForm = false;
|
||||
_editMode = false;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
if (_editMode)
|
||||
{
|
||||
await EquipmentSvc.UpdateAsync(_draft, CancellationToken.None);
|
||||
}
|
||||
else
|
||||
{
|
||||
_draft.EquipmentUuid = Guid.NewGuid();
|
||||
_draft.EquipmentId = DraftValidator.DeriveEquipmentId(_draft.EquipmentUuid);
|
||||
_draft.GenerationId = GenerationId;
|
||||
await EquipmentSvc.CreateAsync(GenerationId, _draft, CancellationToken.None);
|
||||
}
|
||||
_showForm = false;
|
||||
_editMode = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await EquipmentSvc.DeleteAsync(id, CancellationToken.None);
|
||||
await ReloadAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
@if (_generations is null) { <p>Loading…</p> }
|
||||
else if (_generations.Count == 0) { <p class="text-muted">No generations in this cluster yet.</p> }
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Generations</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr><th class="num">ID</th><th>Status</th><th>Created</th><th>Published</th><th>PublishedBy</th><th>Notes</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var g in _generations)
|
||||
{
|
||||
<tr>
|
||||
<td class="num mono">@g.GenerationId</td>
|
||||
<td>@StatusBadge(g.Status)</td>
|
||||
<td><small>@g.CreatedAt.ToString("u") by @g.CreatedBy</small></td>
|
||||
<td><small>@(g.PublishedAt?.ToString("u") ?? "-")</small></td>
|
||||
<td><small>@g.PublishedBy</small></td>
|
||||
<td><small>@g.Notes</small></td>
|
||||
<td>
|
||||
@if (g.Status == GenerationStatus.Draft)
|
||||
{
|
||||
<a class="btn btn-sm btn-primary" href="/clusters/@ClusterId/draft/@g.GenerationId">Open</a>
|
||||
}
|
||||
else if (g.Status is GenerationStatus.Published or GenerationStatus.Superseded)
|
||||
{
|
||||
<button class="btn btn-sm btn-outline-warning" @onclick="() => RollbackAsync(g.GenerationId)">Roll back to this</button>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_error is not null) { <section class="panel notice rise" style="animation-delay:.02s">@_error</section> }
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
private List<ConfigGeneration>? _generations;
|
||||
private string? _error;
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync() =>
|
||||
_generations = await GenerationSvc.ListRecentAsync(ClusterId, 100, CancellationToken.None);
|
||||
|
||||
private async Task RollbackAsync(long targetId)
|
||||
{
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
await GenerationSvc.RollbackAsync(ClusterId, targetId, notes: $"Rollback via Admin UI", CancellationToken.None);
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private static MarkupString StatusBadge(GenerationStatus s) => s switch
|
||||
{
|
||||
GenerationStatus.Draft => new MarkupString("<span class='chip chip-idle'>Draft</span>"),
|
||||
GenerationStatus.Published => new MarkupString("<span class='chip chip-ok'>Published</span>"),
|
||||
GenerationStatus.Superseded => new MarkupString("<span class='chip chip-idle'>Superseded</span>"),
|
||||
_ => new MarkupString($"<span class='chip chip-idle'>{s}</span>"),
|
||||
};
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
|
||||
@* Reusable OPC 40010 Machinery Identification editor. Binds to an Equipment row and renders the
|
||||
nine decision #139 fields in a consistent 3-column Bootstrap grid. Used by EquipmentTab's
|
||||
create + edit forms so the same UI renders regardless of which flow opened it. *@
|
||||
|
||||
<div class="card mb-3">
|
||||
<div class="card-body">
|
||||
<h6 class="text-muted border-bottom pb-1">OPC 40010 Identification</h6>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Manufacturer</label>
|
||||
<InputText @bind-Value="Equipment!.Manufacturer" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Model</label>
|
||||
<InputText @bind-Value="Equipment!.Model" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Serial number</label>
|
||||
<InputText @bind-Value="Equipment!.SerialNumber" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Hardware rev</label>
|
||||
<InputText @bind-Value="Equipment!.HardwareRevision" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Software rev</label>
|
||||
<InputText @bind-Value="Equipment!.SoftwareRevision" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Year of construction</label>
|
||||
<InputNumber @bind-Value="Equipment!.YearOfConstruction" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Asset location</label>
|
||||
<InputText @bind-Value="Equipment!.AssetLocation" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Manufacturer URI</label>
|
||||
<InputText @bind-Value="Equipment!.ManufacturerUri" class="form-control form-control-sm" placeholder="https://…"/>
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Device manual URI</label>
|
||||
<InputText @bind-Value="Equipment!.DeviceManualUri" class="form-control form-control-sm" placeholder="https://…"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired] public Equipment? Equipment { get; set; }
|
||||
}
|
||||
@@ -1,228 +0,0 @@
|
||||
@page "/clusters/{ClusterId}/draft/{GenerationId:long}/import-equipment"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject DriverInstanceService DriverSvc
|
||||
@inject UnsService UnsSvc
|
||||
@inject EquipmentImportBatchService BatchSvc
|
||||
@inject NavigationManager Nav
|
||||
@inject AuthenticationStateProvider AuthProvider
|
||||
|
||||
<ClusterAuthorizeView ClusterId="@ClusterId" MinRole="AdminRole.ConfigEditor">
|
||||
<NotAuthorized>
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Importing equipment into cluster <span class="mono">@ClusterId</span> requires the
|
||||
<span class="mono">ConfigEditor</span> role for this cluster.
|
||||
</section>
|
||||
</NotAuthorized>
|
||||
<Authorized>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="mb-0">Equipment CSV import</h4>
|
||||
<small class="text-muted">Cluster <span class="mono">@ClusterId</span> · draft generation @GenerationId</small>
|
||||
</div>
|
||||
<a class="btn btn-outline-secondary" href="/clusters/@ClusterId/draft/@GenerationId">Back to draft</a>
|
||||
</div>
|
||||
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Accepts <span class="mono">@EquipmentCsvImporter.VersionMarker</span>-headered CSV per Stream B.3.
|
||||
Required columns: @string.Join(", ", EquipmentCsvImporter.RequiredColumns).
|
||||
Optional columns cover the OPC 40010 Identification fields. Paste the file contents
|
||||
or upload directly — the parser runs client-stream-side and shows a row-level preview
|
||||
before anything lands in the draft. ZTag + SAPID reservation conflicts (task #197) are
|
||||
checked at parse time: rows whose ZTag or SAPID is already reserved by a different
|
||||
EquipmentUuid appear in the Rejected list so you can resolve them before finalising.
|
||||
</section>
|
||||
|
||||
<section class="panel notice rise mt-2" style="animation-delay:.08s">
|
||||
<strong>Per-tag addressing for Modbus drivers</strong> isn't part of equipment import —
|
||||
tags are configured at the driver-instance level via the
|
||||
<a href="/clusters/@ClusterId/draft/@GenerationId">Drivers tab</a>. Use the
|
||||
<a href="/modbus/address-preview" target="_blank">address-preview tool</a> to sanity-check
|
||||
grammar strings (<span class="mono">40001:F:CDAB</span>, <span class="mono">HR1:I</span>, <span class="mono">V2000</span> for
|
||||
DL205 family, etc.) before pasting them into the driver config.
|
||||
</section>
|
||||
|
||||
<section class="panel rise mt-2" style="animation-delay:.14s">
|
||||
<div class="panel-head">Import configuration</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-5">
|
||||
<label class="form-label">Target driver instance (for every accepted row)</label>
|
||||
<select class="form-select form-select-sm" @bind="_driverInstanceId">
|
||||
<option value="">-- select driver --</option>
|
||||
@if (_drivers is not null)
|
||||
{
|
||||
@foreach (var d in _drivers) { <option value="@d.DriverInstanceId">@d.DriverInstanceId</option> }
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-5">
|
||||
<label class="form-label">Target UNS line (for every accepted row)</label>
|
||||
<select class="form-select form-select-sm" @bind="_unsLineId">
|
||||
<option value="">-- select line --</option>
|
||||
@if (_unsLines is not null)
|
||||
{
|
||||
@foreach (var l in _unsLines) { <option value="@l.UnsLineId">@l.UnsLineId — @l.Name</option> }
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2 pt-4">
|
||||
<InputFile OnChange="HandleFileAsync" class="form-control form-control-sm" accept=".csv,.txt"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<label class="form-label">CSV content (paste or uploaded)</label>
|
||||
<textarea class="form-control mono" rows="8" @bind="_csvText"
|
||||
placeholder="# OtOpcUaCsv v1 ZTag,MachineCode,SAPID,EquipmentUuid,Name,…"/>
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-outline-primary" @onclick="ParseAsync" disabled="@_busy">Parse</button>
|
||||
<button class="btn btn-sm btn-primary ms-2" @onclick="StageAndFinaliseAsync"
|
||||
disabled="@(_parseResult is null || _parseResult.AcceptedRows.Count == 0 || string.IsNullOrWhiteSpace(_driverInstanceId) || string.IsNullOrWhiteSpace(_unsLineId) || _busy)">
|
||||
Stage + Finalise
|
||||
</button>
|
||||
@if (_parseError is not null) { <span class="chip chip-bad ms-3">@_parseError</span> }
|
||||
@if (_result is not null) { <span class="chip chip-ok ms-3">@_result</span> }
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_parseResult is not null)
|
||||
{
|
||||
<div class="row g-3 mt-1">
|
||||
<div class="col-md-6">
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head"><span class="s-ok">Accepted (@_parseResult.AcceptedRows.Count)</span></div>
|
||||
<div class="table-wrap" style="max-height: 400px; overflow-y: auto;">
|
||||
@if (_parseResult.AcceptedRows.Count == 0)
|
||||
{
|
||||
<p class="text-muted p-3 mb-0">No accepted rows.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr><th>ZTag</th><th>Machine</th><th>Name</th><th>Line</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _parseResult.AcceptedRows)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.ZTag</span></td>
|
||||
<td>@r.MachineCode</td>
|
||||
<td>@r.Name</td>
|
||||
<td>@r.UnsLineName</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head"><span class="s-bad">Rejected (@_parseResult.RejectedRows.Count)</span></div>
|
||||
<div class="table-wrap" style="max-height: 400px; overflow-y: auto;">
|
||||
@if (_parseResult.RejectedRows.Count == 0)
|
||||
{
|
||||
<p class="text-muted p-3 mb-0">No rejections.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<table class="data-table">
|
||||
<thead><tr><th class="num">Line</th><th>Reason</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var e in _parseResult.RejectedRows)
|
||||
{
|
||||
<tr>
|
||||
<td class="num">@e.LineNumber</td>
|
||||
<td><span class="s-bad">@e.Reason</span></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
</Authorized>
|
||||
</ClusterAuthorizeView>
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
|
||||
private List<DriverInstance>? _drivers;
|
||||
private List<UnsLine>? _unsLines;
|
||||
private string _driverInstanceId = string.Empty;
|
||||
private string _unsLineId = string.Empty;
|
||||
private string _csvText = string.Empty;
|
||||
private EquipmentCsvParseResult? _parseResult;
|
||||
private string? _parseError;
|
||||
private string? _result;
|
||||
private bool _busy;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_drivers = await DriverSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_unsLines = await UnsSvc.ListLinesAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private async Task HandleFileAsync(InputFileChangeEventArgs e)
|
||||
{
|
||||
// 5 MiB cap — refuses pathological uploads that would OOM the server.
|
||||
using var stream = e.File.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
|
||||
using var reader = new StreamReader(stream);
|
||||
_csvText = await reader.ReadToEndAsync();
|
||||
}
|
||||
|
||||
private async Task ParseAsync()
|
||||
{
|
||||
_parseError = null;
|
||||
_parseResult = null;
|
||||
_result = null;
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
var raw = EquipmentCsvImporter.Parse(_csvText);
|
||||
_parseResult = await BatchSvc.ApplyReservationPreCheckAsync(raw, CancellationToken.None);
|
||||
}
|
||||
catch (InvalidCsvFormatException ex) { _parseError = ex.Message; }
|
||||
catch (Exception ex) { _parseError = $"Parse failed: {ex.Message}"; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private async Task StageAndFinaliseAsync()
|
||||
{
|
||||
if (_parseResult is null) return;
|
||||
_busy = true;
|
||||
_result = null;
|
||||
_parseError = null;
|
||||
try
|
||||
{
|
||||
var auth = await AuthProvider.GetAuthenticationStateAsync();
|
||||
var createdBy = auth.User.Identity?.Name ?? "unknown";
|
||||
|
||||
var batch = await BatchSvc.CreateBatchAsync(ClusterId, createdBy, CancellationToken.None);
|
||||
await BatchSvc.StageRowsAsync(batch.Id, _parseResult.AcceptedRows, _parseResult.RejectedRows, CancellationToken.None);
|
||||
await BatchSvc.FinaliseBatchAsync(batch.Id, GenerationId, _driverInstanceId, _unsLineId, CancellationToken.None);
|
||||
|
||||
_result = $"Finalised batch {batch.Id:N} — {_parseResult.AcceptedRows.Count} rows added.";
|
||||
// Pause 600 ms so the success banner is visible, then navigate back.
|
||||
await Task.Delay(600);
|
||||
Nav.NavigateTo($"/clusters/{ClusterId}/draft/{GenerationId}");
|
||||
}
|
||||
catch (Exception ex) { _parseError = $"Finalise failed: {ex.Message}"; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject NamespaceService NsSvc
|
||||
|
||||
<div class="d-flex justify-content-between mb-3">
|
||||
<h4 class="panel-head">Namespaces</h4>
|
||||
<button class="btn btn-sm btn-primary" @onclick="() => _showForm = true">Add namespace</button>
|
||||
</div>
|
||||
|
||||
@if (_namespaces is null) { <p>Loading…</p> }
|
||||
else if (_namespaces.Count == 0) { <p class="text-muted">No namespaces defined in this draft.</p> }
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Defined namespaces</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>NamespaceId</th><th>Kind</th><th>URI</th><th>Enabled</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var n in _namespaces)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@n.NamespaceId</span></td>
|
||||
<td>@n.Kind</td>
|
||||
<td>@n.NamespaceUri</td>
|
||||
<td>@(n.Enabled ? "yes" : "no")</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Add namespace</div>
|
||||
<div class="card-body">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6"><label class="form-label">NamespaceUri</label><input class="form-control form-control-sm" @bind="_uri"/></div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Kind</label>
|
||||
<select class="form-select form-select-sm" @bind="_kind">
|
||||
<option value="@NamespaceKind.Equipment">Equipment</option>
|
||||
<option value="@NamespaceKind.SystemPlatform">SystemPlatform (Galaxy)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
private List<Namespace>? _namespaces;
|
||||
private bool _showForm;
|
||||
private string _uri = string.Empty;
|
||||
private NamespaceKind _kind = NamespaceKind.Equipment;
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync() =>
|
||||
_namespaces = await NsSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_uri)) return;
|
||||
await NsSvc.AddAsync(GenerationId, ClusterId, _uri, _kind, CancellationToken.None);
|
||||
_uri = string.Empty;
|
||||
_showForm = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
@page "/clusters/new"
|
||||
@* Cluster creation is a FleetAdmin operation per admin-ui.md "Add a new cluster" —
|
||||
CanPublish gates it (Admin-002). Without this attribute the page was reachable
|
||||
and its CreateAsync write path exploitable by any caller. *@
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize(Policy = "CanPublish")]
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using System.Security.Claims
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">New cluster</h4>
|
||||
</div>
|
||||
|
||||
<EditForm Model="_input" OnValidSubmit="CreateAsync" FormName="new-cluster">
|
||||
<DataAnnotationsValidator/>
|
||||
|
||||
<div class="card mb-3">
|
||||
<div class="card-body">
|
||||
<h6 class="text-muted border-bottom pb-1">Identity</h6>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">ClusterId <span class="text-danger">*</span></label>
|
||||
<InputText @bind-Value="_input.ClusterId" class="form-control form-control-sm"/>
|
||||
<div class="form-text">Stable internal ID. Lowercase alphanumeric + hyphens; ≤ 64 chars.</div>
|
||||
<ValidationMessage For="() => _input.ClusterId"/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label small">Display name <span class="text-danger">*</span></label>
|
||||
<InputText @bind-Value="_input.Name" class="form-control form-control-sm"/>
|
||||
<ValidationMessage For="() => _input.Name"/>
|
||||
</div>
|
||||
|
||||
<h6 class="text-muted border-bottom pb-1">Placement</h6>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Enterprise</label>
|
||||
<InputText @bind-Value="_input.Enterprise" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label small">Site</label>
|
||||
<InputText @bind-Value="_input.Site" class="form-control form-control-sm"/>
|
||||
</div>
|
||||
|
||||
<h6 class="text-muted border-bottom pb-1">Topology</h6>
|
||||
<div class="mb-2">
|
||||
<label class="form-label small">Redundancy</label>
|
||||
<InputSelect @bind-Value="_input.RedundancyMode" class="form-select form-select-sm">
|
||||
<option value="@RedundancyMode.None">None (single node)</option>
|
||||
<option value="@RedundancyMode.Warm">Warm (2 nodes)</option>
|
||||
<option value="@RedundancyMode.Hot">Hot (2 nodes)</option>
|
||||
</InputSelect>
|
||||
</div>
|
||||
|
||||
@if (!string.IsNullOrEmpty(_error))
|
||||
{
|
||||
<div class="text-danger small mt-2">@_error</div>
|
||||
}
|
||||
|
||||
<div class="mt-3">
|
||||
<button type="submit" class="btn btn-success btn-sm me-1" disabled="@_submitting">Save</button>
|
||||
<a href="/clusters" class="btn btn-outline-secondary btn-sm">Cancel</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</EditForm>
|
||||
|
||||
@code {
|
||||
private sealed class Input
|
||||
{
|
||||
[Required, RegularExpression("^[a-z0-9-]{1,64}$", ErrorMessage = "Lowercase alphanumeric + hyphens only")]
|
||||
public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
[Required, StringLength(128)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[StringLength(32)] public string Enterprise { get; set; } = "zb";
|
||||
[StringLength(32)] public string Site { get; set; } = "dev";
|
||||
public RedundancyMode RedundancyMode { get; set; } = RedundancyMode.None;
|
||||
}
|
||||
|
||||
// Admin-007: record the authenticated operator's identity on every write path, not
|
||||
// the static literal "admin-ui" which produced an unattributable audit trail.
|
||||
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
|
||||
|
||||
private Input _input = new();
|
||||
private bool _submitting;
|
||||
private string? _error;
|
||||
|
||||
private async Task CreateAsync()
|
||||
{
|
||||
_submitting = true;
|
||||
_error = null;
|
||||
|
||||
try
|
||||
{
|
||||
// Resolve the signed-in principal name. The page is [Authorize(Policy="CanPublish")]
|
||||
// so AuthState will always be available with an authenticated user here; fall back to
|
||||
// "unknown" only as a defensive last resort (should never happen in practice).
|
||||
var user = AuthState is not null ? (await AuthState).User : null;
|
||||
var operatorName = user?.FindFirstValue(ClaimTypes.Name)
|
||||
?? user?.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? "unknown";
|
||||
|
||||
var cluster = new ServerCluster
|
||||
{
|
||||
ClusterId = _input.ClusterId,
|
||||
Name = _input.Name,
|
||||
Enterprise = _input.Enterprise,
|
||||
Site = _input.Site,
|
||||
RedundancyMode = _input.RedundancyMode,
|
||||
NodeCount = _input.RedundancyMode == RedundancyMode.None ? (byte)1 : (byte)2,
|
||||
Enabled = true,
|
||||
CreatedBy = operatorName,
|
||||
};
|
||||
|
||||
await ClusterSvc.CreateAsync(cluster, createdBy: operatorName, CancellationToken.None);
|
||||
await GenerationSvc.CreateDraftAsync(cluster.ClusterId, createdBy: operatorName, CancellationToken.None);
|
||||
|
||||
Nav.NavigateTo($"/clusters/{cluster.ClusterId}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_error = ex.Message;
|
||||
}
|
||||
finally { _submitting = false; }
|
||||
}
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject ClusterNodeService NodeSvc
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<h4 class="panel-head">Redundancy topology</h4>
|
||||
@if (_roleChangedBanner is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">@_roleChangedBanner</section>
|
||||
}
|
||||
<p class="text-muted small">
|
||||
One row per <span class="mono">ClusterNode</span> in this cluster. Role, <span class="mono">ApplicationUri</span>,
|
||||
and <span class="mono">ServiceLevelBase</span> are authored separately; the Admin UI shows them read-only
|
||||
here so operators can confirm the published topology without touching it. LastSeen older than
|
||||
@((int)ClusterNodeService.StaleThreshold.TotalSeconds)s is flagged Stale — the node has
|
||||
stopped heart-beating and is likely down. Role swap goes through the server-side
|
||||
<span class="mono">RedundancyCoordinator</span> apply-lease flow, not direct DB edits.
|
||||
</p>
|
||||
|
||||
@if (_nodes is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_nodes.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
No ClusterNode rows for this cluster. The server process needs at least one entry
|
||||
(with a non-blank <span class="mono">ApplicationUri</span>) before it can start up per OPC UA spec.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
var primaries = _nodes.Count(n => n.RedundancyRole == RedundancyRole.Primary);
|
||||
var secondaries = _nodes.Count(n => n.RedundancyRole == RedundancyRole.Secondary);
|
||||
var standalone = _nodes.Count(n => n.RedundancyRole == RedundancyRole.Standalone);
|
||||
var staleCount = _nodes.Count(ClusterNodeService.IsStale);
|
||||
|
||||
<section class="agg-grid rise" style="animation-delay:.02s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Nodes</div>
|
||||
<div class="agg-value numeric">@_nodes.Count</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Primary</div>
|
||||
<div class="agg-value numeric @(primaries > 0 ? "s-ok" : "")">@primaries</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Secondary</div>
|
||||
<div class="agg-value numeric">@secondaries</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Stale</div>
|
||||
<div class="agg-value numeric @(staleCount > 0 ? "s-warn" : "")">@staleCount</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (primaries == 0 && standalone == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.08s">
|
||||
<span class="s-bad">No Primary or Standalone node — the cluster has no authoritative write target. Secondaries
|
||||
stay read-only until one of them gets promoted via <span class="mono">RedundancyCoordinator</span>.</span>
|
||||
</section>
|
||||
}
|
||||
else if (primaries > 1)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.08s">
|
||||
<span class="s-bad"><strong>Split-brain:</strong> @primaries nodes claim the Primary role. Apply-lease
|
||||
enforcement should have made this impossible at the coordinator level. Investigate
|
||||
immediately — one of the rows was likely hand-edited.</span>
|
||||
</section>
|
||||
}
|
||||
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Node</th>
|
||||
<th>Role</th>
|
||||
<th>Host</th>
|
||||
<th class="num">OPC UA port</th>
|
||||
<th class="num">ServiceLevel base</th>
|
||||
<th>ApplicationUri</th>
|
||||
<th>Enabled</th>
|
||||
<th>Last seen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var n in _nodes)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@n.NodeId</span></td>
|
||||
<td><span class="chip @RoleBadge(n.RedundancyRole)">@n.RedundancyRole</span></td>
|
||||
<td>@n.Host</td>
|
||||
<td class="num mono">@n.OpcUaPort</td>
|
||||
<td class="num">@n.ServiceLevelBase</td>
|
||||
<td class="mono">@n.ApplicationUri</td>
|
||||
<td>
|
||||
@if (n.Enabled) { <span class="chip chip-ok">Enabled</span> }
|
||||
else { <span class="chip chip-idle">Disabled</span> }
|
||||
</td>
|
||||
<td class="@(ClusterNodeService.IsStale(n) ? "s-warn" : "")">
|
||||
@(n.LastSeenAt is null ? "never" : FormatAge(n.LastSeenAt.Value))
|
||||
@if (ClusterNodeService.IsStale(n)) { <span class="chip chip-warn ms-1">Stale</span> }
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private List<ClusterNode>? _nodes;
|
||||
private HubConnection? _hub;
|
||||
private string? _roleChangedBanner;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_nodes = await NodeSvc.ListByClusterAsync(ClusterId, CancellationToken.None);
|
||||
if (_hub is null) await ConnectHubAsync();
|
||||
}
|
||||
|
||||
private async Task ConnectHubAsync()
|
||||
{
|
||||
_hub = HubFactory.Create("/hubs/fleet");
|
||||
|
||||
_hub.On<RoleChangedMessage>("RoleChanged", async msg =>
|
||||
{
|
||||
if (msg.ClusterId != ClusterId) return;
|
||||
_roleChangedBanner = $"Role changed on {msg.NodeId}: {msg.FromRole} → {msg.ToRole} at {msg.ObservedAtUtc:HH:mm:ss 'UTC'}";
|
||||
_nodes = await NodeSvc.ListByClusterAsync(ClusterId, CancellationToken.None);
|
||||
await InvokeAsync(StateHasChanged);
|
||||
});
|
||||
|
||||
// Best-effort: FleetStatusHub requires an authenticated caller, and the server-side
|
||||
// HubConnection cannot forward the browser auth cookie — swallow connect failures so
|
||||
// the tab still renders. Live role-change updates degrade.
|
||||
try
|
||||
{
|
||||
await _hub.StartAsync();
|
||||
await _hub.SendAsync("SubscribeCluster", ClusterId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// best-effort live updates — see comment above
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hub is not null)
|
||||
{
|
||||
await _hub.DisposeAsync();
|
||||
_hub = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string RowClass(ClusterNode n) =>
|
||||
ClusterNodeService.IsStale(n) ? "table-warning" :
|
||||
!n.Enabled ? "table-secondary" : "";
|
||||
|
||||
private static string RoleBadge(RedundancyRole r) => r switch
|
||||
{
|
||||
RedundancyRole.Primary => "chip-ok",
|
||||
RedundancyRole.Secondary => "chip-idle",
|
||||
RedundancyRole.Standalone => "chip-idle",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
|
||||
private static string FormatAge(DateTime t)
|
||||
{
|
||||
var age = DateTime.UtcNow - t;
|
||||
if (age.TotalSeconds < 60) return $"{(int)age.TotalSeconds}s ago";
|
||||
if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m ago";
|
||||
if (age.TotalHours < 24) return $"{(int)age.TotalHours}h ago";
|
||||
return t.ToString("yyyy-MM-dd HH:mm 'UTC'");
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@inject IJSRuntime JS
|
||||
|
||||
@*
|
||||
Monaco-backed C# code editor (Phase 7 Stream F). Progressive enhancement:
|
||||
textarea renders immediately, Monaco mounts via JS interop after first render.
|
||||
Monaco script tags are loaded once from the parent layout (wwwroot/js/monaco-loader.js
|
||||
pulls the CDN bundle).
|
||||
|
||||
Stream F keeps the interop surface small — bind `Source` two-way, and the parent
|
||||
tab re-renders on change for the dependency preview. The test-harness button
|
||||
lives in the parent so one editor can drive multiple script types.
|
||||
*@
|
||||
|
||||
<div class="script-editor">
|
||||
<textarea class="form-control mono" rows="14" spellcheck="false"
|
||||
@bind="Source" @bind:event="oninput" id="@_editorId">@Source</textarea>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter] public string Source { get; set; } = string.Empty;
|
||||
[Parameter] public EventCallback<string> SourceChanged { get; set; }
|
||||
|
||||
private readonly string _editorId = $"script-editor-{Guid.NewGuid():N}";
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (firstRender)
|
||||
{
|
||||
try
|
||||
{
|
||||
await JS.InvokeVoidAsync("otOpcUaScriptEditor.attach", _editorId);
|
||||
}
|
||||
catch (JSException)
|
||||
{
|
||||
// Monaco bundle not yet loaded on this page — textarea fallback is
|
||||
// still functional.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,260 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject ScriptedAlarmService AlarmSvc
|
||||
@inject ScriptService ScriptSvc
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="panel-head mb-0">Scripted Alarms</h4>
|
||||
<small class="text-muted">OPC UA Part 9 alarms raised by C# predicate scripts. Additive to driver-native alarm streams.</small>
|
||||
</div>
|
||||
<button class="btn btn-primary" @onclick="StartNew">+ New alarm</button>
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p class="text-muted">Loading…</p>
|
||||
}
|
||||
else if (_alarms.Count == 0 && !_showForm)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">No scripted alarms yet in this draft.</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (_alarms.Count > 0)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>Scripted alarms in draft gen @GenerationId</span>
|
||||
<span class="tb-count text-muted">@_alarms.Count alarm@(_alarms.Count == 1 ? "" : "s")</span>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Equipment</th>
|
||||
<th>Type</th>
|
||||
<th class="num">Severity</th>
|
||||
<th>Predicate script</th>
|
||||
<th>Historize</th>
|
||||
<th>Retain</th>
|
||||
<th>Enabled</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var a in _alarms)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@a.Name</span></td>
|
||||
<td><span class="mono">@a.EquipmentId</span></td>
|
||||
<td><span class="chip chip-idle">@a.AlarmType</span></td>
|
||||
<td class="num">@a.Severity <small class="text-muted">@SeverityBand(a.Severity)</small></td>
|
||||
<td><span class="mono">@(ScriptName(a.PredicateScriptId))</span></td>
|
||||
<td>
|
||||
@if (a.HistorizeToAveva) { <span class="chip chip-ok">Aveva</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (a.Retain) { <span class="chip chip-ok">yes</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (a.Enabled) { <span class="chip chip-ok">enabled</span> }
|
||||
else { <span class="chip chip-idle">disabled</span> }
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteAsync(a.ScriptedAlarmId)">Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
}
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<strong>New scripted alarm</strong>
|
||||
<button class="btn btn-sm btn-outline-secondary" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Equipment ID</label>
|
||||
<input class="form-control form-control-sm" @bind="_draft.EquipmentId"
|
||||
placeholder="e.g. eq-abc123 — logical FK to Equipment"/>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Alarm name <small class="text-muted">(operator-facing display name)</small></label>
|
||||
<input class="form-control form-control-sm" @bind="_draft.Name"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Alarm type <small class="text-muted">(OPC UA Part 9 subtype)</small></label>
|
||||
<select class="form-select form-select-sm" @bind="_draft.AlarmType">
|
||||
@foreach (var t in AlarmTypes)
|
||||
{
|
||||
<option value="@t">@t</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">
|
||||
Severity <small class="text-muted">1–1000 (Low <250, Med <500, High <750, Critical 1000)</small>
|
||||
</label>
|
||||
<input type="number" min="1" max="1000" class="form-control form-control-sm" @bind="_draft.Severity"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Predicate script <small class="text-muted">(returns bool)</small></label>
|
||||
<select class="form-select form-select-sm" @bind="_draft.PredicateScriptId">
|
||||
<option value="">— select script —</option>
|
||||
@foreach (var s in _scripts)
|
||||
{
|
||||
<option value="@s.ScriptId">@s.Name (@s.ScriptId)</option>
|
||||
}
|
||||
</select>
|
||||
@if (_scripts.Count == 0)
|
||||
{
|
||||
<div class="form-text s-warn">No scripts in this draft — create one in the Scripts tab first.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label class="form-label">
|
||||
Message template
|
||||
<small class="text-muted">Use <code class="mono">{EquipmentPath/TagName}</code> tokens — resolved at alarm emission time</small>
|
||||
</label>
|
||||
<input class="form-control form-control-sm" @bind="_draft.MessageTemplate"
|
||||
placeholder='e.g. "Oven {Plant/Line1/Oven/Temp} exceeds limit {Plant/Line1/Oven/TempLimit}"'/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="form-check mt-2">
|
||||
<input type="checkbox" class="form-check-input" id="salHistorize" @bind="_draft.HistorizeToAveva"/>
|
||||
<label class="form-check-label" for="salHistorize">
|
||||
Historize to Aveva <small class="text-muted">(SQLite store-and-forward sink)</small>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="form-check mt-2">
|
||||
<input type="checkbox" class="form-check-input" id="salRetain" @bind="_draft.Retain"/>
|
||||
<label class="form-check-label" for="salRetain">
|
||||
Retain <small class="text-muted">(keep condition visible after clear while un-acked)</small>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_error is not null)
|
||||
{
|
||||
<section class="panel notice mt-3"><span class="s-bad">@_error</span></section>
|
||||
}
|
||||
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" disabled="@_busy" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private static readonly string[] AlarmTypes =
|
||||
["AlarmCondition", "LimitAlarm", "OffNormalAlarm", "DiscreteAlarm"];
|
||||
|
||||
private bool _loading = true;
|
||||
private bool _busy;
|
||||
private bool _showForm;
|
||||
private List<ScriptedAlarm> _alarms = [];
|
||||
private List<Script> _scripts = [];
|
||||
private string? _error;
|
||||
|
||||
private ScriptedAlarm _draft = NewDraft();
|
||||
|
||||
private static ScriptedAlarm NewDraft() => new()
|
||||
{
|
||||
ScriptedAlarmId = string.Empty,
|
||||
EquipmentId = string.Empty,
|
||||
Name = string.Empty,
|
||||
AlarmType = "AlarmCondition",
|
||||
Severity = 500,
|
||||
MessageTemplate = string.Empty,
|
||||
PredicateScriptId = string.Empty,
|
||||
HistorizeToAveva = true,
|
||||
Retain = true,
|
||||
};
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_alarms = await AlarmSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_scripts = await ScriptSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
private void StartNew()
|
||||
{
|
||||
_draft = NewDraft();
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_draft.EquipmentId) ||
|
||||
string.IsNullOrWhiteSpace(_draft.Name) ||
|
||||
string.IsNullOrWhiteSpace(_draft.PredicateScriptId))
|
||||
{
|
||||
_error = "Equipment ID, Name, and Predicate script are required.";
|
||||
return;
|
||||
}
|
||||
if (_draft.Severity is < 1 or > 1000)
|
||||
{
|
||||
_error = "Severity must be between 1 and 1000.";
|
||||
return;
|
||||
}
|
||||
|
||||
_busy = true;
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
await AlarmSvc.AddAsync(
|
||||
GenerationId,
|
||||
_draft.EquipmentId, _draft.Name, _draft.AlarmType,
|
||||
_draft.Severity, _draft.MessageTemplate, _draft.PredicateScriptId,
|
||||
_draft.HistorizeToAveva, _draft.Retain,
|
||||
CancellationToken.None);
|
||||
_showForm = false;
|
||||
_alarms = await AlarmSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(string id)
|
||||
{
|
||||
await AlarmSvc.DeleteAsync(GenerationId, id, CancellationToken.None);
|
||||
_alarms = await AlarmSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private string ScriptName(string scriptId)
|
||||
{
|
||||
var s = _scripts.FirstOrDefault(x => x.ScriptId == scriptId);
|
||||
return s is not null ? s.Name : scriptId;
|
||||
}
|
||||
|
||||
private static string SeverityBand(int s) => s switch
|
||||
{
|
||||
<= 250 => "(Low)",
|
||||
<= 500 => "(Medium)",
|
||||
<= 750 => "(High)",
|
||||
_ => "(Critical)",
|
||||
};
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.Abstractions
|
||||
@using ZB.MOM.WW.OtOpcUa.Core.Scripting
|
||||
@inject ScriptService ScriptSvc
|
||||
@inject ScriptTestHarnessService Harness
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="panel-head mb-0">Scripts</h4>
|
||||
<small class="text-muted">C# (Roslyn). Used by virtual tags + scripted alarms.</small>
|
||||
</div>
|
||||
<button class="btn btn-primary" @onclick="StartNew">+ New script</button>
|
||||
</div>
|
||||
|
||||
<script src="/js/monaco-loader.js"></script>
|
||||
|
||||
@if (_loading) { <p class="text-muted">Loading…</p> }
|
||||
else if (_scripts.Count == 0 && _editing is null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">No scripts yet in this draft.</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<div class="list-group">
|
||||
@foreach (var s in _scripts)
|
||||
{
|
||||
<button class="list-group-item list-group-item-action @(_editing?.ScriptId == s.ScriptId ? "active" : "")"
|
||||
@onclick="() => Open(s)">
|
||||
<strong>@s.Name</strong>
|
||||
<div class="small text-muted mono">@s.ScriptId</div>
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-8">
|
||||
@if (_editing is not null)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<strong>@(_isNew ? "New script" : _editing.Name)</strong>
|
||||
<div>
|
||||
@if (!_isNew)
|
||||
{
|
||||
<button class="btn btn-sm btn-outline-danger me-2" @onclick="DeleteAsync">Delete</button>
|
||||
}
|
||||
<button class="btn btn-sm btn-primary" disabled="@_busy" @onclick="SaveAsync">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="p-3">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Name</label>
|
||||
<input class="form-control form-control-sm" @bind="_editing.Name"/>
|
||||
</div>
|
||||
<label class="form-label">Source</label>
|
||||
<ScriptEditor @bind-Source="_editing.SourceCode"/>
|
||||
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-outline-secondary" @onclick="PreviewDependencies">Analyze dependencies</button>
|
||||
<button class="btn btn-sm btn-outline-info ms-2" @onclick="RunHarnessAsync" disabled="@_harnessBusy">Run test harness</button>
|
||||
</div>
|
||||
|
||||
@if (_dependencies is not null)
|
||||
{
|
||||
<div class="mt-3">
|
||||
<strong>Inferred reads</strong>
|
||||
@if (_dependencies.Reads.Count == 0) { <span class="text-muted ms-2">none</span> }
|
||||
else
|
||||
{
|
||||
<ul class="mb-1">
|
||||
@foreach (var r in _dependencies.Reads) { <li><span class="mono">@r</span></li> }
|
||||
</ul>
|
||||
}
|
||||
<strong>Inferred writes</strong>
|
||||
@if (_dependencies.Writes.Count == 0) { <span class="text-muted ms-2">none</span> }
|
||||
else
|
||||
{
|
||||
<ul class="mb-1">
|
||||
@foreach (var w in _dependencies.Writes) { <li><span class="mono">@w</span></li> }
|
||||
</ul>
|
||||
}
|
||||
@if (_dependencies.Rejections.Count > 0)
|
||||
{
|
||||
<section class="panel notice mt-2">
|
||||
<strong>Non-literal paths rejected:</strong>
|
||||
<ul class="mb-0">
|
||||
@foreach (var r in _dependencies.Rejections) { <li><span class="s-bad">@r.Message</span></li> }
|
||||
</ul>
|
||||
</section>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (_testResult is not null)
|
||||
{
|
||||
<div class="mt-3 border-top pt-3">
|
||||
<strong>Harness result:</strong> <span class="chip chip-idle">@_testResult.Outcome</span>
|
||||
@if (_testResult.Outcome == ScriptTestOutcome.Success)
|
||||
{
|
||||
<div>Output: <span class="mono">@(_testResult.Output?.ToString() ?? "null")</span></div>
|
||||
@if (_testResult.Writes.Count > 0)
|
||||
{
|
||||
<div class="mt-1"><strong>Writes:</strong>
|
||||
<ul class="mb-0">
|
||||
@foreach (var kv in _testResult.Writes) { <li><span class="mono">@kv.Key</span> = <span class="mono">@(kv.Value?.ToString() ?? "null")</span></li> }
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@if (_testResult.Errors.Count > 0)
|
||||
{
|
||||
<section class="panel notice mt-2 mb-0">
|
||||
@foreach (var e in _testResult.Errors) { <div><span class="s-warn">@e</span></div> }
|
||||
</section>
|
||||
}
|
||||
@if (_testResult.LogEvents.Count > 0)
|
||||
{
|
||||
<div class="mt-2"><strong>Script log output:</strong>
|
||||
<ul class="small mb-0">
|
||||
@foreach (var e in _testResult.LogEvents) { <li>[@e.Level] @e.RenderMessage()</li> }
|
||||
</ul>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private bool _loading = true;
|
||||
private bool _busy;
|
||||
private bool _harnessBusy;
|
||||
private bool _isNew;
|
||||
private List<Script> _scripts = [];
|
||||
private Script? _editing;
|
||||
private DependencyExtractionResult? _dependencies;
|
||||
private ScriptTestResult? _testResult;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_scripts = await ScriptSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
private void Open(Script s)
|
||||
{
|
||||
_editing = new Script
|
||||
{
|
||||
ScriptRowId = s.ScriptRowId, GenerationId = s.GenerationId,
|
||||
ScriptId = s.ScriptId, Name = s.Name, SourceCode = s.SourceCode,
|
||||
SourceHash = s.SourceHash, Language = s.Language,
|
||||
};
|
||||
_isNew = false;
|
||||
_dependencies = null;
|
||||
_testResult = null;
|
||||
}
|
||||
|
||||
private void StartNew()
|
||||
{
|
||||
_editing = new Script
|
||||
{
|
||||
GenerationId = GenerationId, ScriptId = "",
|
||||
Name = "new-script", SourceCode = "return 0;", SourceHash = "",
|
||||
};
|
||||
_isNew = true;
|
||||
_dependencies = null;
|
||||
_testResult = null;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (_editing is null) return;
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
if (_isNew)
|
||||
await ScriptSvc.AddAsync(GenerationId, _editing.Name, _editing.SourceCode, CancellationToken.None);
|
||||
else
|
||||
await ScriptSvc.UpdateAsync(GenerationId, _editing.ScriptId, _editing.Name, _editing.SourceCode, CancellationToken.None);
|
||||
_scripts = await ScriptSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_isNew = false;
|
||||
}
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync()
|
||||
{
|
||||
if (_editing is null || _isNew) return;
|
||||
await ScriptSvc.DeleteAsync(GenerationId, _editing.ScriptId, CancellationToken.None);
|
||||
_editing = null;
|
||||
_scripts = await ScriptSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private void PreviewDependencies()
|
||||
{
|
||||
if (_editing is null) return;
|
||||
_dependencies = DependencyExtractor.Extract(_editing.SourceCode);
|
||||
}
|
||||
|
||||
private async Task RunHarnessAsync()
|
||||
{
|
||||
if (_editing is null) return;
|
||||
_harnessBusy = true;
|
||||
try
|
||||
{
|
||||
_dependencies ??= DependencyExtractor.Extract(_editing.SourceCode);
|
||||
var inputs = new Dictionary<string, DataValueSnapshot>();
|
||||
foreach (var read in _dependencies.Reads)
|
||||
inputs[read] = new DataValueSnapshot(0.0, 0u, DateTime.UtcNow, DateTime.UtcNow);
|
||||
_testResult = await Harness.RunVirtualTagAsync(_editing.SourceCode, inputs, CancellationToken.None);
|
||||
}
|
||||
finally { _harnessBusy = false; }
|
||||
}
|
||||
}
|
||||
@@ -1,373 +0,0 @@
|
||||
@using System.Text.Json
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Pages.Modbus
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@using ZB.MOM.WW.OtOpcUa.Driver.Modbus
|
||||
@inject TagService TagSvc
|
||||
@inject DriverInstanceService DriverSvc
|
||||
@inject EquipmentService EquipmentSvc
|
||||
|
||||
@*
|
||||
#155 — interactive Tag CRUD scoped to a draft generation. Conditional editor: when the
|
||||
selected DriverInstance is Modbus, the address input switches to ModbusAddressEditor (#145)
|
||||
so users get the live-parse preview + grammar validation. Other driver types fall back to
|
||||
a generic JSON textarea, matching the DriversTab pattern from #147.
|
||||
*@
|
||||
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>Tags (draft gen @GenerationId)</span>
|
||||
<button class="btn btn-primary btn-sm" @onclick="StartAdd">Add tag</button>
|
||||
</div>
|
||||
<div class="toolbar">
|
||||
<select class="form-select form-select-sm tb-state" @bind="_filterDriverId" @bind:after="ReloadAsync">
|
||||
<option value="">— all drivers —</option>
|
||||
@if (_drivers is not null)
|
||||
{
|
||||
@foreach (var d in _drivers)
|
||||
{
|
||||
<option value="@d.DriverInstanceId">@d.Name (@d.DriverType)</option>
|
||||
}
|
||||
}
|
||||
</select>
|
||||
<span class="spacer"></span>
|
||||
@if (_tags is not null) { <span class="tb-count">@_tags.Count tags</span> }
|
||||
</div>
|
||||
@if (_tags is null) { <p class="p-3">Loading…</p> }
|
||||
else if (_tags.Count == 0 && !_showForm) { <p class="p-3 text-muted">No tags in this filter.</p> }
|
||||
else if (_tags.Count > 0)
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr><th>Name</th><th>Driver</th><th>Equipment</th><th>DataType</th><th>Access</th><th>TagConfig</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var t in _tags)
|
||||
{
|
||||
<tr>
|
||||
<td>@t.Name</td>
|
||||
<td><span class="mono">@t.DriverInstanceId</span></td>
|
||||
<td>@(t.EquipmentId ?? "—")</td>
|
||||
<td>@t.DataType</td>
|
||||
<td>@t.AccessLevel</td>
|
||||
<td class="mono" style="max-width:18rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">@t.TagConfig</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-outline-secondary me-1" @onclick="() => StartEdit(t)">Edit</button>
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteAsync(t.TagRowId)">Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||||
<div class="panel-head">@(_editMode ? "Edit tag" : "New tag")</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Name</label>
|
||||
<input class="form-control" @bind="_draft.Name"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">DriverInstance</label>
|
||||
<select class="form-select" @bind="_draft.DriverInstanceId" @bind:after="OnDriverChanged">
|
||||
<option value="">— select driver —</option>
|
||||
@if (_drivers is not null)
|
||||
{
|
||||
@foreach (var d in _drivers) { <option value="@d.DriverInstanceId">@d.Name (@d.DriverType)</option> }
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">Equipment (optional)</label>
|
||||
<select class="form-select" @bind="_draft.EquipmentId">
|
||||
<option value="">— none (folder-path mode) —</option>
|
||||
@if (_equipment is not null)
|
||||
{
|
||||
@foreach (var e in _equipment) { <option value="@e.EquipmentId">@e.Name</option> }
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">DataType</label>
|
||||
<input class="form-control" @bind="_draft.DataType" placeholder="Boolean / Int32 / Float / etc."/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">AccessLevel</label>
|
||||
<select class="form-select" @bind="_draft.AccessLevel">
|
||||
@foreach (var a in Enum.GetValues<TagAccessLevel>())
|
||||
{
|
||||
<option value="@a">@a</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="form-check mt-4">
|
||||
<input type="checkbox" class="form-check-input" @bind="_draft.WriteIdempotent"/>
|
||||
<label class="form-check-label">WriteIdempotent</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-3">
|
||||
@if (_isModbus)
|
||||
{
|
||||
<ModbusAddressEditor @bind-AddressString="_modbusAddress"
|
||||
@bind-AddressString:after="OnAddressChanged"/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<label class="form-label">TagConfig (driver-specific JSON or string)</label>
|
||||
<textarea class="form-control font-monospace" rows="3" @bind="_draft.TagConfig"
|
||||
placeholder='@("{\"address\": ...}")'></textarea>
|
||||
}
|
||||
</div>
|
||||
|
||||
@* #156 — advanced Modbus fields. Collapsed by default; the basic form covers the
|
||||
typical edit workflow. Expander surfaces Deadband (#141) / UnitId override (#142) /
|
||||
CoalesceProhibited (#143) for the multi-slave / noisy-analog / protected-hole
|
||||
deployments. Save-side flushes these into TagConfig as a structured JSON object
|
||||
that ModbusTagDto's BuildTag honours alongside the address string. *@
|
||||
@if (_isModbus)
|
||||
{
|
||||
<div class="mt-3">
|
||||
<button type="button" class="btn btn-sm btn-link p-0"
|
||||
@onclick="() => _showAdvanced = !_showAdvanced">
|
||||
@(_showAdvanced ? "▼ Advanced" : "▶ Advanced") (Deadband / UnitId override / CoalesceProhibited)
|
||||
</button>
|
||||
</div>
|
||||
@if (_showAdvanced)
|
||||
{
|
||||
<div class="row g-3 mt-1 ps-3 border-start">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label small">Deadband
|
||||
<span class="text-muted">(numeric scalar types only)</span>
|
||||
</label>
|
||||
<input type="number" step="any" class="form-control form-control-sm"
|
||||
@bind="_advancedDeadband" @bind:after="OnAdvancedChanged"
|
||||
placeholder="e.g. 0.5"/>
|
||||
<div class="form-text">Suppress publishes when |new - last| < threshold.</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label small">UnitId override
|
||||
<span class="text-muted">(0–255, blank = use driver default)</span>
|
||||
</label>
|
||||
<input type="number" min="0" max="255" class="form-control form-control-sm"
|
||||
@bind="_advancedUnitId" @bind:after="OnAdvancedChanged"
|
||||
placeholder="leave blank for driver-level"/>
|
||||
<div class="form-text">Per-tag MBAP unit ID. Required when fronting a multi-slave gateway.</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label small">CoalesceProhibited</label>
|
||||
<div class="form-check mt-1">
|
||||
<input type="checkbox" class="form-check-input"
|
||||
@bind="_advancedCoalesceProhibited" @bind:after="OnAdvancedChanged"/>
|
||||
<label class="form-check-label">Read in isolation (#143)</label>
|
||||
</div>
|
||||
<div class="form-text">Use when surrounding registers are write-only or fault on read.</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
|
||||
@if (_error is not null) { <section class="panel notice mt-3"><span class="s-bad">@_error</span></section> }
|
||||
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="Cancel">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private List<Tag>? _tags;
|
||||
private List<DriverInstance>? _drivers;
|
||||
private List<Equipment>? _equipment;
|
||||
private string _filterDriverId = string.Empty;
|
||||
|
||||
private bool _showForm;
|
||||
private bool _editMode;
|
||||
private Tag _draft = NewBlankDraft();
|
||||
private string? _error;
|
||||
private bool _isModbus;
|
||||
private string? _modbusAddress;
|
||||
|
||||
// #156 — advanced Modbus fields. Bound separately from _draft.TagConfig because they
|
||||
// round-trip through a structured JSON shape, not a single string. Synced into TagConfig
|
||||
// by OnAdvancedChanged / OnAddressChanged (whichever fires).
|
||||
private bool _showAdvanced;
|
||||
private double? _advancedDeadband;
|
||||
private byte? _advancedUnitId;
|
||||
private bool _advancedCoalesceProhibited;
|
||||
|
||||
private static Tag NewBlankDraft() => new()
|
||||
{
|
||||
TagId = string.Empty, DriverInstanceId = string.Empty, Name = string.Empty,
|
||||
DataType = "Int32", AccessLevel = TagAccessLevel.Read, TagConfig = string.Empty,
|
||||
};
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_drivers = await DriverSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_equipment = await EquipmentSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_tags = await TagSvc.ListAsync(GenerationId,
|
||||
string.IsNullOrWhiteSpace(_filterDriverId) ? null : _filterDriverId,
|
||||
equipmentId: null,
|
||||
CancellationToken.None);
|
||||
}
|
||||
|
||||
private void StartAdd()
|
||||
{
|
||||
_draft = NewBlankDraft();
|
||||
_editMode = false;
|
||||
_modbusAddress = null;
|
||||
_isModbus = false;
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
ResetAdvanced();
|
||||
}
|
||||
|
||||
private void ResetAdvanced()
|
||||
{
|
||||
_showAdvanced = false;
|
||||
_advancedDeadband = null;
|
||||
_advancedUnitId = null;
|
||||
_advancedCoalesceProhibited = false;
|
||||
}
|
||||
|
||||
private void StartEdit(Tag row)
|
||||
{
|
||||
_draft = new Tag
|
||||
{
|
||||
TagRowId = row.TagRowId,
|
||||
GenerationId = row.GenerationId,
|
||||
TagId = row.TagId,
|
||||
DriverInstanceId = row.DriverInstanceId,
|
||||
DeviceId = row.DeviceId,
|
||||
EquipmentId = row.EquipmentId,
|
||||
Name = row.Name,
|
||||
FolderPath = row.FolderPath,
|
||||
DataType = row.DataType,
|
||||
AccessLevel = row.AccessLevel,
|
||||
WriteIdempotent = row.WriteIdempotent,
|
||||
PollGroupId = row.PollGroupId,
|
||||
TagConfig = row.TagConfig,
|
||||
};
|
||||
_editMode = true;
|
||||
OnDriverChanged();
|
||||
// Try to extract addressString + advanced fields from existing JSON config so the
|
||||
// form pre-fills correctly when an operator hits Edit on an existing row.
|
||||
ResetAdvanced();
|
||||
if (_isModbus) HydrateModbusFromTagConfig(row.TagConfig);
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private void HydrateModbusFromTagConfig(string tagConfig)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(tagConfig);
|
||||
var root = doc.RootElement;
|
||||
if (root.TryGetProperty("addressString", out var addr) && addr.ValueKind == JsonValueKind.String)
|
||||
_modbusAddress = addr.GetString();
|
||||
if (root.TryGetProperty("deadband", out var db) && db.ValueKind is JsonValueKind.Number)
|
||||
_advancedDeadband = db.GetDouble();
|
||||
if (root.TryGetProperty("unitId", out var uid) && uid.ValueKind is JsonValueKind.Number)
|
||||
_advancedUnitId = uid.GetByte();
|
||||
if (root.TryGetProperty("coalesceProhibited", out var cp) && cp.ValueKind is JsonValueKind.True or JsonValueKind.False)
|
||||
_advancedCoalesceProhibited = cp.GetBoolean();
|
||||
|
||||
// Auto-expand the advanced panel when any of those fields was actually set so
|
||||
// operators see immediately what's been configured.
|
||||
if (_advancedDeadband.HasValue || _advancedUnitId.HasValue || _advancedCoalesceProhibited)
|
||||
_showAdvanced = true;
|
||||
}
|
||||
catch { /* Malformed JSON falls back to empty advanced state. */ }
|
||||
}
|
||||
|
||||
private void OnDriverChanged()
|
||||
{
|
||||
var driver = _drivers?.FirstOrDefault(d => d.DriverInstanceId == _draft.DriverInstanceId);
|
||||
_isModbus = driver is not null
|
||||
&& string.Equals(driver.DriverType, "Modbus", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private void OnAddressChanged() => RefreshTagConfigJson();
|
||||
private void OnAdvancedChanged() => RefreshTagConfigJson();
|
||||
|
||||
/// <summary>
|
||||
/// Re-serializes the current address + advanced fields into TagConfig as a structured
|
||||
/// JSON object. ModbusTagDto's BuildTag honours every field — addressString drives
|
||||
/// the parser, while the structured bits (deadband / unitId / coalesceProhibited)
|
||||
/// pass through directly. Fields with default / empty values are omitted from the
|
||||
/// JSON to keep diffs in the existing draft-diff viewer clean.
|
||||
/// </summary>
|
||||
private void RefreshTagConfigJson()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_modbusAddress)
|
||||
&& !_advancedDeadband.HasValue
|
||||
&& !_advancedUnitId.HasValue
|
||||
&& !_advancedCoalesceProhibited)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = new Dictionary<string, object?>();
|
||||
if (!string.IsNullOrWhiteSpace(_modbusAddress)) payload["addressString"] = _modbusAddress;
|
||||
if (_advancedDeadband.HasValue) payload["deadband"] = _advancedDeadband.Value;
|
||||
if (_advancedUnitId.HasValue) payload["unitId"] = _advancedUnitId.Value;
|
||||
if (_advancedCoalesceProhibited) payload["coalesceProhibited"] = true;
|
||||
|
||||
_draft.TagConfig = JsonSerializer.Serialize(payload);
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
_showForm = false;
|
||||
_editMode = false;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_draft.Name) || string.IsNullOrWhiteSpace(_draft.DriverInstanceId))
|
||||
{
|
||||
_error = "Name and DriverInstance are required.";
|
||||
return;
|
||||
}
|
||||
if (_editMode)
|
||||
await TagSvc.UpdateAsync(_draft, CancellationToken.None);
|
||||
else
|
||||
await TagSvc.CreateAsync(GenerationId, _draft, CancellationToken.None);
|
||||
_showForm = false;
|
||||
_editMode = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await TagSvc.DeleteAsync(id, CancellationToken.None);
|
||||
await ReloadAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,276 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject UnsService UnsSvc
|
||||
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Drag any line in the <strong>UNS Lines</strong> table onto an area row in <strong>UNS Areas</strong>
|
||||
to re-parent it. A preview modal shows the impact (equipment re-home count) + lets you confirm
|
||||
or cancel. If another operator modifies the draft while you're confirming, you'll see a 409
|
||||
refresh-required modal instead of clobbering their work.
|
||||
</section>
|
||||
|
||||
<div class="row mt-3">
|
||||
<div class="col-md-6">
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>UNS Areas</span>
|
||||
<button class="btn btn-sm btn-primary" @onclick="() => _showAreaForm = true">Add area</button>
|
||||
</div>
|
||||
|
||||
@if (_areas is null) { <p class="p-3">Loading…</p> }
|
||||
else if (_areas.Count == 0) { <p class="p-3 text-muted">No areas yet.</p> }
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>AreaId</th><th>Name</th><th class="text-muted">(drop target)</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var a in _areas)
|
||||
{
|
||||
<tr class="@(_hoverAreaId == a.UnsAreaId ? "table-primary" : "")"
|
||||
@ondragover="e => OnAreaDragOver(e, a.UnsAreaId)"
|
||||
@ondragover:preventDefault
|
||||
@ondragleave="() => _hoverAreaId = null"
|
||||
@ondrop="() => OnLineDroppedAsync(a.UnsAreaId)"
|
||||
@ondrop:preventDefault>
|
||||
<td><span class="mono">@a.UnsAreaId</span></td>
|
||||
<td>@a.Name</td>
|
||||
<td class="text-muted">drop here</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (_showAreaForm)
|
||||
{
|
||||
<div class="p-3 border-top">
|
||||
<div class="mb-2"><label class="form-label">Name (lowercase segment)</label><input class="form-control form-control-sm" @bind="_newAreaName"/></div>
|
||||
<button class="btn btn-sm btn-primary" @onclick="AddAreaAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showAreaForm = false">Cancel</button>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="col-md-6">
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>UNS Lines</span>
|
||||
<button class="btn btn-sm btn-primary" @onclick="() => _showLineForm = true" disabled="@(_areas is null || _areas.Count == 0)">Add line</button>
|
||||
</div>
|
||||
|
||||
@if (_lines is null) { <p class="p-3">Loading…</p> }
|
||||
else if (_lines.Count == 0) { <p class="p-3 text-muted">No lines yet.</p> }
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>LineId</th><th>Area</th><th>Name</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var l in _lines)
|
||||
{
|
||||
<tr draggable="true"
|
||||
@ondragstart="() => _dragLineId = l.UnsLineId"
|
||||
@ondragend="() => { _dragLineId = null; _hoverAreaId = null; }"
|
||||
style="cursor: grab;">
|
||||
<td><span class="mono">@l.UnsLineId</span></td>
|
||||
<td><span class="mono">@l.UnsAreaId</span></td>
|
||||
<td>@l.Name</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
|
||||
@if (_showLineForm && _areas is not null)
|
||||
{
|
||||
<div class="p-3 border-top">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Area</label>
|
||||
<select class="form-select form-select-sm" @bind="_newLineAreaId">
|
||||
@foreach (var a in _areas) { <option value="@a.UnsAreaId">@a.Name (@a.UnsAreaId)</option> }
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-2"><label class="form-label">Name</label><input class="form-control form-control-sm" @bind="_newLineName"/></div>
|
||||
<button class="btn btn-sm btn-primary" @onclick="AddLineAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showLineForm = false">Cancel</button>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@* Preview / confirm modal for a pending drag-drop move *@
|
||||
@if (_pendingPreview is not null)
|
||||
{
|
||||
<div class="modal show d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Confirm UNS move</h5>
|
||||
<button type="button" class="btn-close" @onclick="CancelMove"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>@_pendingPreview.HumanReadableSummary</p>
|
||||
<p class="text-muted small">
|
||||
Equipment re-homed: <strong>@_pendingPreview.AffectedEquipmentCount</strong>.
|
||||
Tags re-parented: <strong>@_pendingPreview.AffectedTagCount</strong>.
|
||||
</p>
|
||||
@if (_pendingPreview.CascadeWarnings.Count > 0)
|
||||
{
|
||||
<section class="panel notice small mb-0">
|
||||
<ul class="mb-0">
|
||||
@foreach (var w in _pendingPreview.CascadeWarnings) { <li>@w</li> }
|
||||
</ul>
|
||||
</section>
|
||||
}
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" @onclick="CancelMove">Cancel</button>
|
||||
<button class="btn btn-primary" @onclick="ConfirmMoveAsync" disabled="@_committing">Confirm move</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@* 409 concurrent-edit modal — another operator changed the draft between preview + commit *@
|
||||
@if (_conflictMessage is not null)
|
||||
{
|
||||
<div class="modal show d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content border-danger">
|
||||
<div class="modal-header bg-danger text-white">
|
||||
<h5 class="modal-title">Draft changed — refresh required</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>@_conflictMessage</p>
|
||||
<p class="small text-muted">
|
||||
Concurrency guard per DraftRevisionToken prevented overwriting the peer
|
||||
operator's edit. Reload the tab + redo the move on the current draft state.
|
||||
</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-primary" @onclick="ReloadAfterConflict">Reload draft</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private List<UnsArea>? _areas;
|
||||
private List<UnsLine>? _lines;
|
||||
private bool _showAreaForm;
|
||||
private bool _showLineForm;
|
||||
private string _newAreaName = string.Empty;
|
||||
private string _newLineName = string.Empty;
|
||||
private string _newLineAreaId = string.Empty;
|
||||
|
||||
private string? _dragLineId;
|
||||
private string? _hoverAreaId;
|
||||
private UnsImpactPreview? _pendingPreview;
|
||||
private UnsMoveOperation? _pendingMove;
|
||||
private bool _committing;
|
||||
private string? _conflictMessage;
|
||||
|
||||
protected override async Task OnParametersSetAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_areas = await UnsSvc.ListAreasAsync(GenerationId, CancellationToken.None);
|
||||
_lines = await UnsSvc.ListLinesAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private async Task AddAreaAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_newAreaName)) return;
|
||||
await UnsSvc.AddAreaAsync(GenerationId, ClusterId, _newAreaName, notes: null, CancellationToken.None);
|
||||
_newAreaName = string.Empty;
|
||||
_showAreaForm = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
private async Task AddLineAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_newLineName) || string.IsNullOrWhiteSpace(_newLineAreaId)) return;
|
||||
await UnsSvc.AddLineAsync(GenerationId, _newLineAreaId, _newLineName, notes: null, CancellationToken.None);
|
||||
_newLineName = string.Empty;
|
||||
_showLineForm = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
private void OnAreaDragOver(DragEventArgs _, string areaId) => _hoverAreaId = areaId;
|
||||
|
||||
private async Task OnLineDroppedAsync(string targetAreaId)
|
||||
{
|
||||
var lineId = _dragLineId;
|
||||
_hoverAreaId = null;
|
||||
_dragLineId = null;
|
||||
if (string.IsNullOrWhiteSpace(lineId)) return;
|
||||
|
||||
var line = _lines?.FirstOrDefault(l => l.UnsLineId == lineId);
|
||||
if (line is null || line.UnsAreaId == targetAreaId) return;
|
||||
|
||||
var snapshot = await UnsSvc.LoadSnapshotAsync(GenerationId, CancellationToken.None);
|
||||
var move = new UnsMoveOperation(
|
||||
Kind: UnsMoveKind.LineMove,
|
||||
SourceClusterId: ClusterId,
|
||||
TargetClusterId: ClusterId,
|
||||
SourceLineId: lineId,
|
||||
TargetAreaId: targetAreaId);
|
||||
try
|
||||
{
|
||||
_pendingPreview = UnsImpactAnalyzer.Analyze(snapshot, move);
|
||||
_pendingMove = move;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_conflictMessage = ex.Message; // CrossCluster or validation failure surfaces here
|
||||
}
|
||||
}
|
||||
|
||||
private void CancelMove()
|
||||
{
|
||||
_pendingPreview = null;
|
||||
_pendingMove = null;
|
||||
}
|
||||
|
||||
private async Task ConfirmMoveAsync()
|
||||
{
|
||||
if (_pendingPreview is null || _pendingMove is null) return;
|
||||
_committing = true;
|
||||
try
|
||||
{
|
||||
await UnsSvc.MoveLineAsync(
|
||||
GenerationId,
|
||||
_pendingPreview.RevisionToken,
|
||||
_pendingMove.SourceLineId!,
|
||||
_pendingMove.TargetAreaId!,
|
||||
CancellationToken.None);
|
||||
|
||||
_pendingPreview = null;
|
||||
_pendingMove = null;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (DraftRevisionConflictException ex)
|
||||
{
|
||||
_pendingPreview = null;
|
||||
_pendingMove = null;
|
||||
_conflictMessage = ex.Message;
|
||||
}
|
||||
finally { _committing = false; }
|
||||
}
|
||||
|
||||
private async Task ReloadAfterConflict()
|
||||
{
|
||||
_conflictMessage = null;
|
||||
await ReloadAsync();
|
||||
}
|
||||
}
|
||||
@@ -1,248 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject VirtualTagService VirtualTagSvc
|
||||
@inject ScriptService ScriptSvc
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<div>
|
||||
<h4 class="panel-head mb-0">Virtual Tags</h4>
|
||||
<small class="text-muted">Computed tags driven by C# scripts. Appear in the Equipment browse tree alongside driver tags.</small>
|
||||
</div>
|
||||
<button class="btn btn-primary" @onclick="StartNew">+ New virtual tag</button>
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p class="text-muted">Loading…</p>
|
||||
}
|
||||
else if (_tags.Count == 0 && !_showForm)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">No virtual tags yet in this draft.</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
@if (_tags.Count > 0)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>Virtual tags in draft gen @GenerationId</span>
|
||||
<span class="tb-count text-muted">@_tags.Count tag@(_tags.Count == 1 ? "" : "s")</span>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Equipment</th>
|
||||
<th>DataType</th>
|
||||
<th>Script</th>
|
||||
<th>Triggers</th>
|
||||
<th>Historize</th>
|
||||
<th>Enabled</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var t in _tags)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@t.Name</span></td>
|
||||
<td><span class="mono">@t.EquipmentId</span></td>
|
||||
<td>@t.DataType</td>
|
||||
<td><span class="mono">@(ScriptName(t.ScriptId))</span></td>
|
||||
<td>
|
||||
@if (t.ChangeTriggered) { <span class="chip chip-idle me-1">change</span> }
|
||||
@if (t.TimerIntervalMs.HasValue) { <span class="chip chip-idle">@t.TimerIntervalMs ms</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (t.Historize) { <span class="chip chip-ok">yes</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (t.Enabled) { <span class="chip chip-ok">enabled</span> }
|
||||
else { <span class="chip chip-idle">disabled</span> }
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-outline-secondary me-1" @onclick="() => ToggleEnabledAsync(t)">
|
||||
@(t.Enabled ? "Disable" : "Enable")
|
||||
</button>
|
||||
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteAsync(t.VirtualTagId)">Remove</button>
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
}
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<strong>New virtual tag</strong>
|
||||
<button class="btn btn-sm btn-outline-secondary" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Equipment ID</label>
|
||||
<input class="form-control form-control-sm" @bind="_draft.EquipmentId"
|
||||
placeholder="e.g. eq-abc123 — logical FK to Equipment"/>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Name <small class="text-muted">(browse name, unique in Equipment)</small></label>
|
||||
<input class="form-control form-control-sm" @bind="_draft.Name"/>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">DataType</label>
|
||||
<select class="form-select form-select-sm" @bind="_draft.DataType">
|
||||
@foreach (var dt in DataTypes)
|
||||
{
|
||||
<option value="@dt">@dt</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-8">
|
||||
<label class="form-label">Script</label>
|
||||
<select class="form-select form-select-sm" @bind="_draft.ScriptId">
|
||||
<option value="">— select script —</option>
|
||||
@foreach (var s in _scripts)
|
||||
{
|
||||
<option value="@s.ScriptId">@s.Name (@s.ScriptId)</option>
|
||||
}
|
||||
</select>
|
||||
@if (_scripts.Count == 0)
|
||||
{
|
||||
<div class="form-text s-warn">No scripts in this draft — create one in the Scripts tab first.</div>
|
||||
}
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-check mt-2">
|
||||
<input type="checkbox" class="form-check-input" id="vtChangeTriggered" @bind="_draft.ChangeTriggered"/>
|
||||
<label class="form-check-label" for="vtChangeTriggered">
|
||||
Change-triggered <small class="text-muted">(re-evaluate on any input change)</small>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">Timer interval (ms) <small class="text-muted">leave blank to disable timer</small></label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="_timerMs" placeholder="e.g. 5000"/>
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" id="vtHistorize" @bind="_draft.Historize"/>
|
||||
<label class="form-check-label" for="vtHistorize">
|
||||
Historize <small class="text-muted">(route evaluations to IHistoryWriter)</small>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@if (_error is not null)
|
||||
{
|
||||
<section class="panel notice mt-3"><span class="s-bad">@_error</span></section>
|
||||
}
|
||||
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" disabled="@_busy" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public long GenerationId { get; set; }
|
||||
[Parameter] public string ClusterId { get; set; } = string.Empty;
|
||||
|
||||
private static readonly string[] DataTypes =
|
||||
["Boolean", "Int32", "Int64", "Float32", "Float64", "String", "DateTime"];
|
||||
|
||||
private bool _loading = true;
|
||||
private bool _busy;
|
||||
private bool _showForm;
|
||||
private List<VirtualTag> _tags = [];
|
||||
private List<Script> _scripts = [];
|
||||
private string? _error;
|
||||
|
||||
// Draft form state (VirtualTag doesn't have update besides Enabled — add-only form)
|
||||
private VirtualTag _draft = NewDraft();
|
||||
private int? _timerMs;
|
||||
|
||||
private static VirtualTag NewDraft() => new()
|
||||
{
|
||||
VirtualTagId = string.Empty,
|
||||
EquipmentId = string.Empty,
|
||||
Name = string.Empty,
|
||||
DataType = "Float32",
|
||||
ScriptId = string.Empty,
|
||||
ChangeTriggered = true,
|
||||
};
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_tags = await VirtualTagSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_scripts = await ScriptSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
_loading = false;
|
||||
}
|
||||
|
||||
private void StartNew()
|
||||
{
|
||||
_draft = NewDraft();
|
||||
_timerMs = null;
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(_draft.EquipmentId) ||
|
||||
string.IsNullOrWhiteSpace(_draft.Name) ||
|
||||
string.IsNullOrWhiteSpace(_draft.ScriptId))
|
||||
{
|
||||
_error = "Equipment ID, Name, and Script are required.";
|
||||
return;
|
||||
}
|
||||
if (!_draft.ChangeTriggered && _timerMs is null)
|
||||
{
|
||||
_error = "At least one trigger must be set (change-triggered or timer).";
|
||||
return;
|
||||
}
|
||||
|
||||
_busy = true;
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
await VirtualTagSvc.AddAsync(
|
||||
GenerationId,
|
||||
_draft.EquipmentId, _draft.Name, _draft.DataType, _draft.ScriptId,
|
||||
_draft.ChangeTriggered, _timerMs, _draft.Historize,
|
||||
CancellationToken.None);
|
||||
_showForm = false;
|
||||
_tags = await VirtualTagSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(string id)
|
||||
{
|
||||
await VirtualTagSvc.DeleteAsync(GenerationId, id, CancellationToken.None);
|
||||
_tags = await VirtualTagSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private async Task ToggleEnabledAsync(VirtualTag t)
|
||||
{
|
||||
await VirtualTagSvc.UpdateEnabledAsync(GenerationId, t.VirtualTagId, !t.Enabled, CancellationToken.None);
|
||||
_tags = await VirtualTagSvc.ListAsync(GenerationId, CancellationToken.None);
|
||||
}
|
||||
|
||||
private string ScriptName(string scriptId)
|
||||
{
|
||||
var s = _scripts.FirstOrDefault(x => x.ScriptId == scriptId);
|
||||
return s is not null ? s.Name : scriptId;
|
||||
}
|
||||
}
|
||||
@@ -1,250 +0,0 @@
|
||||
@page "/drivers/focas/{InstanceId}"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@inject FocasDriverDetailService DetailSvc
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">FOCAS driver <span class="mono">@InstanceId</span></h4>
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_detail is null)
|
||||
{
|
||||
<section class="panel notice">
|
||||
No FOCAS driver instance with id <span class="mono">@InstanceId</span> was found.
|
||||
<div class="small text-muted mt-1">
|
||||
Either the id is wrong, or the instance's <span class="mono">DriverType</span> is not "Focas". The list of drivers per cluster draft is on the <a href="/clusters">Clusters</a> page.
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="agg-grid rise" style="animation-delay:.02s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Name</div>
|
||||
<div class="agg-value">@_detail.Instance.Name</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Cluster</div>
|
||||
<div class="agg-value mono">@_detail.Instance.ClusterId</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Namespace</div>
|
||||
<div class="agg-value mono">@_detail.Instance.NamespaceId</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Enabled</div>
|
||||
<div class="agg-value">@(_detail.Instance.Enabled ? "Yes" : "No")</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_detail.ParseError is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.08s">
|
||||
<strong>DriverConfig JSON failed to parse:</strong> @_detail.ParseError
|
||||
<div class="small text-muted mt-1">
|
||||
Falling back to raw-JSON view below; the per-section tables are hidden because the shape couldn't be projected.
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
else if (_detail.Config is not null)
|
||||
{
|
||||
@if (_detail.Config.Devices is null || _detail.Config.Devices.Count == 0)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Devices</div>
|
||||
<p class="text-muted" style="padding:.75rem 1rem 0">No devices configured.</p>
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Devices</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>HostAddress</th><th>DeviceName</th><th>Series</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var d in _detail.Config.Devices)
|
||||
{
|
||||
<tr>
|
||||
<td class="mono">@d.HostAddress</td>
|
||||
<td>@(d.DeviceName ?? "—")</td>
|
||||
<td>@(string.IsNullOrEmpty(d.Series) ? "Unknown" : d.Series)</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_detail.Config.Tags is null || _detail.Config.Tags.Count == 0)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">Tags</div>
|
||||
<p class="text-muted" style="padding:.75rem 1rem 0">No tags configured.</p>
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">Tags</div>
|
||||
<div class="toolbar">
|
||||
<span class="spacer"></span>
|
||||
<span class="tb-count">@_detail.Config.Tags.Count tag(s)</span>
|
||||
</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>Name</th><th>Device</th><th>Address</th><th>DataType</th><th>Writable</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var t in _detail.Config.Tags)
|
||||
{
|
||||
<tr>
|
||||
<td>@t.Name</td>
|
||||
<td class="mono">@t.DeviceHostAddress</td>
|
||||
<td class="mono">@t.Address</td>
|
||||
<td>@t.DataType</td>
|
||||
<td>@(t.Writable ? "Yes" : "No")</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
<section class="card-grid rise" style="animation-delay:.20s">
|
||||
<div class="metric-card">
|
||||
<div class="panel-head">Driver behaviour</div>
|
||||
<div class="kv">
|
||||
<span class="k">Probe</span>
|
||||
<span class="v">
|
||||
@if (_detail.Config.Probe is { } probe)
|
||||
{
|
||||
<span class="chip @(probe.Enabled ? "chip-ok" : "chip-idle")">@(probe.Enabled ? "Enabled" : "Disabled")</span>
|
||||
<span class="ms-2 small text-muted">Interval: @(probe.Interval ?? "default")</span>
|
||||
}
|
||||
else { <span class="text-muted">default (enabled)</span> }
|
||||
</span>
|
||||
</div>
|
||||
<div class="kv">
|
||||
<span class="k">Alarm projection</span>
|
||||
<span class="v">
|
||||
@if (_detail.Config.AlarmProjection is { } ap)
|
||||
{
|
||||
<span class="chip @(ap.Enabled ? "chip-ok" : "chip-idle")">@(ap.Enabled ? "Enabled" : "Disabled")</span>
|
||||
<span class="ms-2 small text-muted">PollInterval: @(ap.PollInterval ?? "default")</span>
|
||||
}
|
||||
else { <span class="text-muted">disabled (default)</span> }
|
||||
</span>
|
||||
</div>
|
||||
<div class="kv">
|
||||
<span class="k">Handle recycling</span>
|
||||
<span class="v">
|
||||
@if (_detail.Config.HandleRecycle is { } hr)
|
||||
{
|
||||
<span class="chip @(hr.Enabled ? "chip-warn" : "chip-idle")">@(hr.Enabled ? "Enabled" : "Disabled")</span>
|
||||
<span class="ms-2 small text-muted">Interval: @(hr.Interval ?? "default (01:00:00)")</span>
|
||||
}
|
||||
else { <span class="text-muted">disabled (default)</span> }
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_detail.HostStatuses.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.26s">
|
||||
No <span class="mono">DriverHostStatus</span> rows yet for this instance. The Server publishes its first
|
||||
tick ~2 s after the driver starts — if this stays empty after a minute, check that the Server is running and the instance is in a published generation.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.26s">
|
||||
<div class="panel-head">Host status</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Node</th>
|
||||
<th>Host</th>
|
||||
<th>State</th>
|
||||
<th class="num" title="Consecutive failures">Fail#</th>
|
||||
<th>Breaker last opened</th>
|
||||
<th>Last recycled</th>
|
||||
<th>Last seen</th>
|
||||
<th>Detail</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _detail.HostStatuses)
|
||||
{
|
||||
<tr>
|
||||
<td class="mono">@r.NodeId</td>
|
||||
<td>@r.HostName</td>
|
||||
<td><span class="chip @StateBadge(r.State)">@r.State</span></td>
|
||||
<td class="num small">@r.ConsecutiveFailures</td>
|
||||
<td class="small">@FormatUtc(r.LastCircuitBreakerOpenUtc)</td>
|
||||
<td class="small">@FormatUtc(r.LastRecycleUtc)</td>
|
||||
<td class="small @(IsStale(r) ? "s-warn" : "")">@FormatAge(r.LastSeenUtc)</td>
|
||||
<td class="text-truncate small" style="max-width: 240px;" title="@r.Detail">@r.Detail</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
<section class="panel rise" style="animation-delay:.32s">
|
||||
<div class="panel-head">Raw DriverConfig JSON</div>
|
||||
<pre class="small" style="padding:1rem;margin:0;overflow-x:auto"><code>@_detail.Instance.DriverConfig</code></pre>
|
||||
</section>
|
||||
|
||||
<div class="mt-4 small text-muted">
|
||||
Docs: <span class="mono">docs/drivers/FOCAS.md</span> (getting started) · <span class="mono">docs/v2/focas-deployment.md</span> (NSSM + pipe ACL) · <span class="mono">docs/drivers/FOCAS-Test-Fixture.md</span> (test coverage).
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public string InstanceId { get; set; } = string.Empty;
|
||||
|
||||
private FocasDriverDetail? _detail;
|
||||
private bool _loading = true;
|
||||
|
||||
protected override async Task OnParametersSetAsync()
|
||||
{
|
||||
_loading = true;
|
||||
try { _detail = await DetailSvc.GetAsync(InstanceId, CancellationToken.None); }
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private static bool IsStale(FocasHostStatusRow r) =>
|
||||
DateTime.UtcNow - r.LastSeenUtc > TimeSpan.FromSeconds(30);
|
||||
|
||||
private static string StateBadge(string state) => state switch
|
||||
{
|
||||
"Running" => "chip-ok",
|
||||
"Faulted" => "chip-bad",
|
||||
"Starting" => "chip-idle",
|
||||
"Stopped" => "chip-idle",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
|
||||
private static string FormatUtc(DateTime? utc) =>
|
||||
utc is null ? "—" : utc.Value.ToString("yyyy-MM-dd HH:mm 'UTC'");
|
||||
|
||||
private static string FormatAge(DateTime utc)
|
||||
{
|
||||
var age = DateTime.UtcNow - utc;
|
||||
if (age.TotalSeconds < 60) return $"{(int)age.TotalSeconds}s ago";
|
||||
if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m ago";
|
||||
if (age.TotalHours < 48) return $"{(int)age.TotalHours}h ago";
|
||||
return utc.ToString("yyyy-MM-dd HH:mm 'UTC'");
|
||||
}
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
@page "/fleet"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject IServiceScopeFactory ScopeFactory
|
||||
@implements IDisposable
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Fleet status</h4>
|
||||
</div>
|
||||
|
||||
<div class="d-flex align-items-center mb-3 gap-2">
|
||||
<button class="btn btn-sm btn-outline-primary" @onclick="RefreshAsync" disabled="@_refreshing">
|
||||
@if (_refreshing) { <span class="spinner-border spinner-border-sm me-1" /> }
|
||||
Refresh
|
||||
</button>
|
||||
<span class="text-muted small">
|
||||
Auto-refresh every @RefreshIntervalSeconds s. Last updated: @(_lastRefreshUtc?.ToString("HH:mm:ss 'UTC'") ?? "—")
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@if (_rows is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_rows.Count == 0)
|
||||
{
|
||||
<section class="panel notice" style="animation-delay:.02s">
|
||||
No node state recorded yet. Nodes publish their state to the central DB on each poll; if
|
||||
this list is empty, either no nodes have been registered or the poller hasn't run yet.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="agg-grid rise" style="animation-delay:.02s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Nodes</div>
|
||||
<div class="agg-value numeric">@_rows.Count</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Applied</div>
|
||||
<div class="agg-value numeric">@_rows.Count(r => r.Status == "Applied")</div>
|
||||
</div>
|
||||
<div class="agg-card caution">
|
||||
<div class="agg-label">Stale</div>
|
||||
<div class="agg-value numeric">@_rows.Count(r => IsStale(r))</div>
|
||||
</div>
|
||||
<div class="agg-card alert">
|
||||
<div class="agg-label">Failed</div>
|
||||
<div class="agg-value numeric">@_rows.Count(r => r.Status == "Failed")</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Nodes</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Node</th>
|
||||
<th>Cluster</th>
|
||||
<th class="num">Generation</th>
|
||||
<th>Status</th>
|
||||
<th>Last applied</th>
|
||||
<th>Last seen</th>
|
||||
<th>Error</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _rows)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.NodeId</span></td>
|
||||
<td>@r.ClusterId</td>
|
||||
<td class="num">@(r.GenerationId?.ToString() ?? "—")</td>
|
||||
<td>
|
||||
<span class="chip @StatusBadge(r.Status)">@(r.Status ?? "—")</span>
|
||||
</td>
|
||||
<td>@FormatAge(r.AppliedAt)</td>
|
||||
<td>@FormatAge(r.SeenAt)</td>
|
||||
<td class="text-truncate" style="max-width: 320px;" title="@r.Error">@r.Error</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
// Refresh cadence. 5s matches FleetStatusPoller's poll interval — the dashboard always sees
|
||||
// the most recent published state without polling ahead of the broadcaster.
|
||||
private const int RefreshIntervalSeconds = 5;
|
||||
|
||||
private List<FleetNodeRow>? _rows;
|
||||
private bool _refreshing;
|
||||
private DateTime? _lastRefreshUtc;
|
||||
private Timer? _timer;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await RefreshAsync();
|
||||
_timer = new Timer(async _ => await InvokeAsync(RefreshAsync),
|
||||
state: null,
|
||||
dueTime: TimeSpan.FromSeconds(RefreshIntervalSeconds),
|
||||
period: TimeSpan.FromSeconds(RefreshIntervalSeconds));
|
||||
}
|
||||
|
||||
private async Task RefreshAsync()
|
||||
{
|
||||
if (_refreshing) return;
|
||||
_refreshing = true;
|
||||
try
|
||||
{
|
||||
using var scope = ScopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<OtOpcUaConfigDbContext>();
|
||||
// Order on the join's plain columns before projecting into FleetNodeRow —
|
||||
// EF Core cannot translate OrderBy over a property of a constructed record.
|
||||
var rows = await db.ClusterNodeGenerationStates.AsNoTracking()
|
||||
.Join(db.ClusterNodes.AsNoTracking(), s => s.NodeId, n => n.NodeId, (s, n) => new { s, n })
|
||||
.OrderBy(x => x.n.ClusterId)
|
||||
.ThenBy(x => x.s.NodeId)
|
||||
.Select(x => new FleetNodeRow(
|
||||
x.s.NodeId, x.n.ClusterId, x.s.CurrentGenerationId,
|
||||
x.s.LastAppliedStatus != null ? x.s.LastAppliedStatus.ToString() : null,
|
||||
x.s.LastAppliedError, x.s.LastAppliedAt, x.s.LastSeenAt))
|
||||
.ToListAsync();
|
||||
_rows = rows;
|
||||
_lastRefreshUtc = DateTime.UtcNow;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_refreshing = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsStale(FleetNodeRow r)
|
||||
{
|
||||
if (r.SeenAt is null) return true;
|
||||
return (DateTime.UtcNow - r.SeenAt.Value) > TimeSpan.FromSeconds(30);
|
||||
}
|
||||
|
||||
private static string RowClass(FleetNodeRow r) => r.Status switch
|
||||
{
|
||||
"Failed" => "table-danger",
|
||||
_ when IsStale(r) => "table-warning",
|
||||
_ => "",
|
||||
};
|
||||
|
||||
private static string StatusBadge(string? status) => status switch
|
||||
{
|
||||
"Applied" => "chip-ok",
|
||||
"Failed" => "chip-bad",
|
||||
"Applying" => "chip-idle",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
|
||||
private static string FormatAge(DateTime? t)
|
||||
{
|
||||
if (t is null) return "—";
|
||||
var age = DateTime.UtcNow - t.Value;
|
||||
if (age.TotalSeconds < 60) return $"{(int)age.TotalSeconds}s ago";
|
||||
if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m ago";
|
||||
if (age.TotalHours < 24) return $"{(int)age.TotalHours}h ago";
|
||||
return t.Value.ToString("yyyy-MM-dd HH:mm 'UTC'");
|
||||
}
|
||||
|
||||
public void Dispose() => _timer?.Dispose();
|
||||
|
||||
internal sealed record FleetNodeRow(
|
||||
string NodeId, string ClusterId, long? GenerationId,
|
||||
string? Status, string? Error, DateTime? AppliedAt, DateTime? SeenAt);
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
@page "/"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Fleet overview</h4>
|
||||
</div>
|
||||
|
||||
@if (_clusters is null)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Loading</div>
|
||||
<div style="padding:1.4rem;color:var(--ink-faint);font-style:italic">Loading fleet…</div>
|
||||
</section>
|
||||
}
|
||||
else if (_clusters.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
No clusters configured yet. <a href="/clusters/new">Create the first cluster</a>.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="agg-grid rise" style="animation-delay:.02s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Clusters</div>
|
||||
<div class="agg-value numeric">@_clusters.Count</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Active drafts</div>
|
||||
<div class="agg-value numeric">@_activeDraftCount</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Published generations</div>
|
||||
<div class="agg-value numeric">@_publishedCount</div>
|
||||
</div>
|
||||
<div class="agg-card @(_disabledCount > 0 ? "caution" : "")">
|
||||
<div class="agg-label">Disabled clusters</div>
|
||||
<div class="agg-value numeric">@_disabledCount</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Clusters</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Cluster ID</th>
|
||||
<th>Name</th>
|
||||
<th>Enterprise / Site</th>
|
||||
<th>Redundancy</th>
|
||||
<th>State</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var c in _clusters)
|
||||
{
|
||||
<tr @onclick="@(() => Nav.NavigateTo($"/clusters/{c.ClusterId}"))">
|
||||
<td class="mono">@c.ClusterId</td>
|
||||
<td>@c.Name</td>
|
||||
<td>@c.Enterprise / @c.Site</td>
|
||||
<td class="mono">@c.RedundancyMode</td>
|
||||
<td>
|
||||
@if (c.Enabled)
|
||||
{
|
||||
<span class="chip chip-ok">enabled</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="chip chip-idle">disabled</span>
|
||||
}
|
||||
</td>
|
||||
<td><a href="/clusters/@c.ClusterId">Open</a></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private List<ServerCluster>? _clusters;
|
||||
private int _activeDraftCount;
|
||||
private int _publishedCount;
|
||||
private int _disabledCount;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_clusters = await ClusterSvc.ListAsync(CancellationToken.None);
|
||||
_disabledCount = _clusters.Count(c => !c.Enabled);
|
||||
|
||||
foreach (var c in _clusters)
|
||||
{
|
||||
var gens = await GenerationSvc.ListRecentAsync(c.ClusterId, 50, CancellationToken.None);
|
||||
_activeDraftCount += gens.Count(g => g.Status.ToString() == "Draft");
|
||||
_publishedCount += gens.Count(g => g.Status.ToString() == "Published");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,242 +0,0 @@
|
||||
@page "/hosts"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject IServiceScopeFactory ScopeFactory
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Driver host status</h4>
|
||||
</div>
|
||||
|
||||
<div class="d-flex align-items-center mb-3 gap-2">
|
||||
<button class="btn btn-sm btn-outline-primary" @onclick="RefreshAsync" disabled="@_refreshing">
|
||||
@if (_refreshing) { <span class="spinner-border spinner-border-sm me-1" /> }
|
||||
Refresh
|
||||
</button>
|
||||
<span class="text-muted small">
|
||||
Auto-refresh every @RefreshIntervalSeconds s. Last updated: @(_lastRefreshUtc?.ToString("HH:mm:ss 'UTC'") ?? "—")
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
Each row is one host reported by a driver instance on a server node. Galaxy drivers report
|
||||
per-Platform / per-AppEngine entries; Modbus drivers report the PLC endpoint. Rows age out
|
||||
of the Server's publisher on every 10-second heartbeat — rows whose LastSeen is older than
|
||||
30s are flagged Stale, which usually means the owning Server process has crashed or lost
|
||||
its DB connection.
|
||||
</section>
|
||||
|
||||
@if (_rows is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_rows.Count == 0)
|
||||
{
|
||||
<section class="panel notice" style="animation-delay:.08s">
|
||||
No host-status rows yet. The Server publishes its first tick 2s after startup; if this list stays empty, check that the Server is running and the driver implements <span class="mono">IHostConnectivityProbe</span>.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="agg-grid rise" style="animation-delay:.08s">
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Hosts</div>
|
||||
<div class="agg-value numeric">@_rows.Count</div>
|
||||
</div>
|
||||
<div class="agg-card">
|
||||
<div class="agg-label">Running</div>
|
||||
<div class="agg-value numeric">@_rows.Count(r => r.State == DriverHostState.Running && !HostStatusService.IsStale(r))</div>
|
||||
</div>
|
||||
<div class="agg-card caution">
|
||||
<div class="agg-label">Stale</div>
|
||||
<div class="agg-value numeric">@_rows.Count(HostStatusService.IsStale)</div>
|
||||
</div>
|
||||
<div class="agg-card alert">
|
||||
<div class="agg-label">Faulted</div>
|
||||
<div class="agg-value numeric">@_rows.Count(r => r.State == DriverHostState.Faulted)</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@if (_rows.Any(HostStatusService.IsFlagged))
|
||||
{
|
||||
var flaggedCount = _rows.Count(HostStatusService.IsFlagged);
|
||||
<section class="panel notice rise" style="animation-delay:.14s">
|
||||
<strong>@flaggedCount host@(flaggedCount == 1 ? "" : "s")</strong>
|
||||
reporting ≥ @HostStatusService.FailureFlagThreshold consecutive failures — circuit breaker
|
||||
may trip soon. Inspect the resilience columns below to locate.
|
||||
</section>
|
||||
}
|
||||
|
||||
@foreach (var cluster in _rows.GroupBy(r => r.ClusterId ?? "(unassigned)").OrderBy(g => g.Key))
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">Cluster: <span class="mono">@cluster.Key</span></div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Node</th>
|
||||
<th>Driver</th>
|
||||
<th>Host</th>
|
||||
<th>State</th>
|
||||
<th class="num" title="Consecutive failures — resets when a call succeeds or the breaker closes">Fail#</th>
|
||||
<th class="num" title="In-flight capability calls (bulkhead-depth proxy)">In-flight</th>
|
||||
<th>Breaker opened</th>
|
||||
<th>Last transition</th>
|
||||
<th>Last seen</th>
|
||||
<th>Detail</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in cluster)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.NodeId</span></td>
|
||||
<td><span class="mono">@r.DriverInstanceId</span></td>
|
||||
<td>@r.HostName</td>
|
||||
<td>
|
||||
<span class="chip @StateBadge(r.State)">@r.State</span>
|
||||
@if (HostStatusService.IsStale(r))
|
||||
{
|
||||
<span class="chip chip-warn ms-1">Stale</span>
|
||||
}
|
||||
@if (HostStatusService.IsFlagged(r))
|
||||
{
|
||||
<span class="chip chip-bad ms-1" title="≥ @HostStatusService.FailureFlagThreshold consecutive failures">Flagged</span>
|
||||
}
|
||||
</td>
|
||||
<td class="num small @(HostStatusService.IsFlagged(r) ? "s-bad fw-bold" : "")">
|
||||
@r.ConsecutiveFailures
|
||||
</td>
|
||||
<td class="num small">@r.CurrentBulkheadDepth</td>
|
||||
<td class="small">
|
||||
@(r.LastCircuitBreakerOpenUtc is null ? "—" : FormatAge(r.LastCircuitBreakerOpenUtc.Value))
|
||||
</td>
|
||||
<td class="small">@FormatAge(r.StateChangedUtc)</td>
|
||||
<td class="small @(HostStatusService.IsStale(r) ? "s-warn" : "")">@FormatAge(r.LastSeenUtc)</td>
|
||||
<td class="text-truncate small" style="max-width: 240px;" title="@r.Detail">@r.Detail</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
}
|
||||
|
||||
@code {
|
||||
// Mirrors HostStatusPublisher.HeartbeatInterval — polling ahead of the broadcaster
|
||||
// produces stale-looking rows mid-cycle.
|
||||
private const int RefreshIntervalSeconds = 10;
|
||||
|
||||
private List<HostStatusRow>? _rows;
|
||||
private bool _refreshing;
|
||||
private DateTime? _lastRefreshUtc;
|
||||
private Timer? _timer;
|
||||
private HubConnection? _hub;
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await RefreshAsync();
|
||||
_timer = new Timer(async _ => await InvokeAsync(RefreshAsync),
|
||||
state: null,
|
||||
dueTime: TimeSpan.FromSeconds(RefreshIntervalSeconds),
|
||||
period: TimeSpan.FromSeconds(RefreshIntervalSeconds));
|
||||
await ConnectHubAsync();
|
||||
}
|
||||
|
||||
// Phase 6.1 Stream E.2 — subscribe to FleetStatusHub so resilience deltas upsert the
|
||||
// matching row without waiting for the next RefreshIntervalSeconds tick. The 10 s
|
||||
// poll stays as a safety net in case the hub connection is down.
|
||||
private async Task ConnectHubAsync()
|
||||
{
|
||||
_hub = HubFactory.Create("/hubs/fleet");
|
||||
_hub.On<ResilienceStatusChangedMessage>("ResilienceStatusChanged", OnResilienceChanged);
|
||||
try
|
||||
{
|
||||
await _hub.StartAsync();
|
||||
await _hub.SendAsync("SubscribeFleet");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Hub is best-effort; polling refresh is the fallback. Swallow connect errors
|
||||
// so the page still renders against the initial RefreshAsync pass.
|
||||
}
|
||||
}
|
||||
|
||||
private async Task OnResilienceChanged(ResilienceStatusChangedMessage msg)
|
||||
{
|
||||
if (_rows is null) return;
|
||||
var idx = _rows.FindIndex(r =>
|
||||
r.DriverInstanceId == msg.DriverInstanceId && r.HostName == msg.HostName);
|
||||
if (idx < 0) return;
|
||||
var prior = _rows[idx];
|
||||
_rows[idx] = prior with
|
||||
{
|
||||
ConsecutiveFailures = msg.ConsecutiveFailures,
|
||||
LastCircuitBreakerOpenUtc = msg.LastCircuitBreakerOpenUtc,
|
||||
CurrentBulkheadDepth = msg.CurrentBulkheadDepth,
|
||||
LastRecycleUtc = msg.LastRecycleUtc,
|
||||
};
|
||||
await InvokeAsync(StateHasChanged);
|
||||
}
|
||||
|
||||
private async Task RefreshAsync()
|
||||
{
|
||||
if (_refreshing) return;
|
||||
_refreshing = true;
|
||||
try
|
||||
{
|
||||
using var scope = ScopeFactory.CreateScope();
|
||||
var svc = scope.ServiceProvider.GetRequiredService<HostStatusService>();
|
||||
_rows = (await svc.ListAsync()).ToList();
|
||||
_lastRefreshUtc = DateTime.UtcNow;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_refreshing = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private static string RowClass(HostStatusRow r) => r.State switch
|
||||
{
|
||||
DriverHostState.Faulted => "table-danger",
|
||||
_ when HostStatusService.IsStale(r) => "table-warning",
|
||||
_ => "",
|
||||
};
|
||||
|
||||
private static string StateBadge(DriverHostState s) => s switch
|
||||
{
|
||||
DriverHostState.Running => "chip-ok",
|
||||
DriverHostState.Stopped => "chip-idle",
|
||||
DriverHostState.Faulted => "chip-bad",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
|
||||
private static string FormatAge(DateTime t)
|
||||
{
|
||||
var age = DateTime.UtcNow - t;
|
||||
if (age.TotalSeconds < 60) return $"{(int)age.TotalSeconds}s ago";
|
||||
if (age.TotalMinutes < 60) return $"{(int)age.TotalMinutes}m ago";
|
||||
if (age.TotalHours < 24) return $"{(int)age.TotalHours}h ago";
|
||||
return t.ToString("yyyy-MM-dd HH:mm 'UTC'");
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
_timer?.Dispose();
|
||||
if (_hub is not null)
|
||||
{
|
||||
try { await _hub.DisposeAsync(); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
@page "/login"
|
||||
@* The login page must stay anonymously reachable — otherwise the fallback
|
||||
authorization policy (Admin-001) would lock operators out of the only way in. *@
|
||||
@attribute [Microsoft.AspNetCore.Authorization.AllowAnonymous]
|
||||
|
||||
@* Admin-005: this page is static server-rendered (no @rendermode). It is a plain HTML
|
||||
form that POSTs to the /auth/login minimal-API endpoint with data-enhance="false", so
|
||||
the LDAP bind, cookie SignInAsync and redirect all run while the endpoint still owns
|
||||
an unstarted HTTP response. SignInAsync must NOT be called from an interactive Blazor
|
||||
circuit — by then the original HTTP response has long completed. *@
|
||||
|
||||
<div class="login-wrap rise" style="animation-delay:.02s">
|
||||
<section class="panel">
|
||||
<div class="panel-head">OtOpcUa Admin — sign in</div>
|
||||
<div style="padding:1.1rem 1.1rem 1.25rem">
|
||||
<form method="post" action="/auth/login" data-enhance="false">
|
||||
@if (ReturnUrl is not null)
|
||||
{
|
||||
<input type="hidden" name="returnUrl" value="@ReturnUrl"/>
|
||||
}
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="username">Username</label>
|
||||
<input id="username" name="username" type="text"
|
||||
class="form-control form-control-sm" autocomplete="username"/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label" for="password">Password</label>
|
||||
<input id="password" name="password" type="password"
|
||||
class="form-control form-control-sm" autocomplete="current-password"/>
|
||||
</div>
|
||||
|
||||
@if (!string.IsNullOrWhiteSpace(Error))
|
||||
{
|
||||
<div class="panel notice" style="margin-bottom:.85rem">@Error</div>
|
||||
}
|
||||
|
||||
<button class="btn btn-primary w-100" type="submit">Sign in</button>
|
||||
</form>
|
||||
|
||||
<div style="margin-top:1rem;padding-top:.85rem;border-top:1px solid var(--rule);
|
||||
font-size:.78rem;color:var(--ink-faint)">
|
||||
LDAP bind against the configured directory. Dev defaults to GLAuth on
|
||||
<span class="mono">localhost:3893</span>.
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
/// <summary>Error message surfaced by the /auth/login endpoint after a failed bind.</summary>
|
||||
[SupplyParameterFromQuery]
|
||||
private string? Error { get; set; }
|
||||
|
||||
/// <summary>Original protected URL the operator was bounced from; round-tripped to the endpoint.</summary>
|
||||
[SupplyParameterFromQuery]
|
||||
private string? ReturnUrl { get; set; }
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Driver.Modbus
|
||||
|
||||
@*
|
||||
#145 — Live address-string parser preview for Modbus tag editing. Bound to a string
|
||||
AddressString; on every input keystroke the parser runs and surfaces the resolved
|
||||
breakdown (Region, PduOffset, DataType, Bit, ByteOrder, ArrayCount, StringLength) or
|
||||
the parse error. Family flag drives the parser's family-native branch (#144).
|
||||
|
||||
Re-uses the same ModbusAddressParser the wire driver uses, so grammar drift is
|
||||
impossible by construction. Internal-namespace component called from the larger
|
||||
DriverInstance editor.
|
||||
*@
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Address string</label>
|
||||
<input type="text" class="form-control form-control-sm @(IsValid ? "is-valid" : Diagnostic is null ? "" : "is-invalid")"
|
||||
value="@AddressString"
|
||||
@oninput="@OnInputChanged"
|
||||
placeholder="e.g. 40001:F:CDAB:5"/>
|
||||
@if (IsValid && _parsed is not null)
|
||||
{
|
||||
<div class="form-text text-success">
|
||||
<strong>Parsed:</strong>
|
||||
Region=<span class="mono">@_parsed.Region</span>
|
||||
Offset=<span class="mono">@_parsed.Offset</span>
|
||||
Type=<span class="mono">@_parsed.DataType</span>
|
||||
@if (_parsed.Bit.HasValue) { <text>Bit=<span class="mono">@_parsed.Bit</span></text> }
|
||||
@if (_parsed.ByteOrder != ModbusByteOrder.BigEndian) { <text>Order=<span class="mono">@_parsed.ByteOrder</span></text> }
|
||||
@if (_parsed.ArrayCount.HasValue) { <text>Array[<span class="mono">@_parsed.ArrayCount</span>]</text> }
|
||||
@if (_parsed.StringLength > 0) { <text>StrLen=<span class="mono">@_parsed.StringLength</span></text> }
|
||||
</div>
|
||||
}
|
||||
else if (Diagnostic is not null)
|
||||
{
|
||||
<div class="invalid-feedback">@Diagnostic</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter] public string? AddressString { get; set; }
|
||||
[Parameter] public EventCallback<string?> AddressStringChanged { get; set; }
|
||||
[Parameter] public ModbusFamily Family { get; set; } = ModbusFamily.Generic;
|
||||
[Parameter] public MelsecFamily MelsecSubFamily { get; set; } = MelsecFamily.Q_L_iQR;
|
||||
[Parameter] public EventCallback<ParsedModbusAddress?> ParsedChanged { get; set; }
|
||||
|
||||
private ParsedModbusAddress? _parsed;
|
||||
private string? Diagnostic;
|
||||
private bool IsValid => _parsed is not null && Diagnostic is null;
|
||||
|
||||
protected override void OnParametersSet() => Reparse();
|
||||
|
||||
private async Task OnInputChanged(ChangeEventArgs e)
|
||||
{
|
||||
AddressString = e.Value as string;
|
||||
await AddressStringChanged.InvokeAsync(AddressString);
|
||||
Reparse();
|
||||
await ParsedChanged.InvokeAsync(_parsed);
|
||||
}
|
||||
|
||||
private void Reparse()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(AddressString))
|
||||
{
|
||||
_parsed = null;
|
||||
Diagnostic = null;
|
||||
return;
|
||||
}
|
||||
if (ModbusAddressParser.TryParse(AddressString, Family, MelsecSubFamily, out var parsed, out var err))
|
||||
{
|
||||
_parsed = parsed;
|
||||
Diagnostic = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
_parsed = null;
|
||||
Diagnostic = err;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
@page "/modbus/address-preview"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Driver.Modbus
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
|
||||
@*
|
||||
#149 — standalone preview / sanity-check tool for Modbus address strings. The Admin UI
|
||||
doesn't yet have a per-tag CRUD surface (tags are seeded via SQL or arrive at runtime
|
||||
through ITagDiscovery), so the ModbusAddressEditor component shipped in #145 needs a
|
||||
page where operators can paste an address string and confirm it parses to what they
|
||||
expect before committing it to a config row.
|
||||
|
||||
Doubles as a "did the parser ship correctly" smoke target for QA + a copy-pasteable
|
||||
grammar reference for users skimming the docs.
|
||||
*@
|
||||
|
||||
<PageTitle>Modbus address preview</PageTitle>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Modbus address preview</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
Paste an address string and watch the parser break it down field by field. Useful for
|
||||
sanity-checking a tag spreadsheet row before adding it to a driver's <span class="mono">DriverConfig</span>.
|
||||
Full grammar: <a href="https://github.com/" target="_blank">docs/v2/modbus-addressing.md</a>.
|
||||
</p>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Family</div>
|
||||
<div style="padding:.75rem 1rem">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">PLC family hint (drives the family-native branch)</label>
|
||||
<select class="form-select form-select-sm" @bind="_family">
|
||||
@foreach (var f in Enum.GetValues<ModbusFamily>())
|
||||
{
|
||||
<option value="@f">@f</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
@if (_family == ModbusFamily.MELSEC)
|
||||
{
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">MELSEC sub-family</label>
|
||||
<select class="form-select form-select-sm" @bind="_melsecSubFamily">
|
||||
@foreach (var f in Enum.GetValues<MelsecFamily>())
|
||||
{
|
||||
<option value="@f">@f</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="mt-3">
|
||||
<ModbusAddressEditor @bind-AddressString="_address"
|
||||
Family="_family"
|
||||
MelsecSubFamily="_melsecSubFamily"/>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Quick-reference grammar</div>
|
||||
<pre class="small" style="padding:1rem;margin:0;overflow-x:auto">@_grammarReference</pre>
|
||||
</section>
|
||||
|
||||
@code {
|
||||
private string? _address;
|
||||
private ModbusFamily _family = ModbusFamily.Generic;
|
||||
private MelsecFamily _melsecSubFamily = MelsecFamily.Q_L_iQR;
|
||||
|
||||
// Held as a const string rather than inline markup so the Razor parser doesn't try to
|
||||
// interpret the angle-bracket grammar tokens as element open/close.
|
||||
private const string _grammarReference = @"<region><offset>[.<bit>][:<type>[<len>]][:<order>][:<count>]
|
||||
|
||||
Examples (post-#146 type codes):
|
||||
40001 HoldingRegisters[0], Int16
|
||||
400001 same, 6-digit form
|
||||
40001:F Float32 (HR[0..1])
|
||||
40001:F:CDAB Float32 word-swapped
|
||||
40001:STR20 20-char ASCII string
|
||||
40001:S:5 Int16[5] array (3-field shorthand)
|
||||
40001:I:CDAB:10 Int32[10] word-swapped (4-field strict)
|
||||
40001.5 bit 5 of HR[0]
|
||||
HR1:I Int32 via mnemonic region (matches Wonderware)
|
||||
C100 Coil 100 (mnemonic, 1-based)
|
||||
V2000:F:CDAB DL205 V-memory at PDU 1024 (Family=DL205)
|
||||
D100:I MELSEC D-register 100, Int32 (Family=MELSEC)
|
||||
|
||||
Type codes: BOOL, S (Int16), US (UInt16), I (Int32), UI (UInt32),
|
||||
I_64 (Int64), UI_64 (UInt64), F, D, BCD, BCD_32, STR<n>
|
||||
Byte order: ABCD (BE default), CDAB (word-swap), BADC (byte-swap), DCBA (full reverse)";
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
@page "/modbus/diagnostics/{DriverInstanceId}"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject DriverDiagnosticsClient Diagnostics
|
||||
|
||||
@*
|
||||
#154 — operator-facing view of the Server's auto-prohibition state for a Modbus driver.
|
||||
Fetches via DriverDiagnosticsClient (HttpClient against the Server's HealthEndpointsHost).
|
||||
Refreshes on demand; auto-refresh is a future task once a SignalR diag channel exists.
|
||||
*@
|
||||
|
||||
<PageTitle>Modbus diagnostics — @DriverInstanceId</PageTitle>
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Modbus auto-prohibitions</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
Driver instance <span class="mono">@DriverInstanceId</span>. Live snapshot of coalesced ranges
|
||||
the planner has learned to read individually (#148 / #150 / #151 / #152).
|
||||
</p>
|
||||
|
||||
<div class="toolbar" style="margin-bottom:.75rem">
|
||||
<button class="btn btn-sm btn-outline-primary" @onclick="LoadAsync" disabled="@_loading">
|
||||
@(_loading ? "Loading…" : "Refresh")
|
||||
</button>
|
||||
@if (_lastRefreshed is not null)
|
||||
{
|
||||
<span class="text-muted ms-3 small">Last refreshed @_lastRefreshed.Value.ToLocalTime().ToString("HH:mm:ss")</span>
|
||||
}
|
||||
<span class="spacer"></span>
|
||||
</div>
|
||||
|
||||
@if (_error is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">@_error</section>
|
||||
}
|
||||
else if (_response is null)
|
||||
{
|
||||
<p class="text-muted">Click <strong>Refresh</strong> to load.</p>
|
||||
}
|
||||
else if (_response.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">No auto-prohibitions. The planner is coalescing freely.</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Prohibited ranges</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Unit</th>
|
||||
<th>Region</th>
|
||||
<th class="num">Start</th>
|
||||
<th class="num">End</th>
|
||||
<th class="num">Span</th>
|
||||
<th>Status</th>
|
||||
<th>Last probed</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _response.Ranges.OrderBy(r => r.UnitId).ThenBy(r => r.Region).ThenBy(r => r.StartAddress))
|
||||
{
|
||||
<tr>
|
||||
<td class="mono">@r.UnitId</td>
|
||||
<td class="mono">@r.Region</td>
|
||||
<td class="num mono">@r.StartAddress</td>
|
||||
<td class="num mono">@r.EndAddress</td>
|
||||
<td class="num">@(r.EndAddress - r.StartAddress + 1)</td>
|
||||
<td>
|
||||
@if (r.BisectionPending)
|
||||
{
|
||||
<span class="chip chip-warn">BISECTING</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span class="chip chip-bad">ISOLATED</span>
|
||||
}
|
||||
</td>
|
||||
<td class="small text-muted">@FormatTimeSince(r.LastProbedUtc)</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
[Parameter] public string DriverInstanceId { get; set; } = string.Empty;
|
||||
|
||||
private ModbusAutoProhibitionsResponse? _response;
|
||||
private string? _error;
|
||||
private bool _loading;
|
||||
private DateTime? _lastRefreshed;
|
||||
|
||||
private async Task LoadAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
_response = await Diagnostics.GetModbusAutoProhibitedRangesAsync(DriverInstanceId);
|
||||
_lastRefreshed = DateTime.UtcNow;
|
||||
if (_response is null)
|
||||
_error = $"Server reports driver '{DriverInstanceId}' is not present or is not a Modbus driver.";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_error = $"Fetch failed: {ex.Message}";
|
||||
}
|
||||
finally
|
||||
{
|
||||
_loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static string FormatTimeSince(DateTime utc)
|
||||
{
|
||||
var span = DateTime.UtcNow - utc;
|
||||
if (span.TotalSeconds < 60) return $"{(int)span.TotalSeconds}s ago";
|
||||
if (span.TotalMinutes < 60) return $"{(int)span.TotalMinutes}m ago";
|
||||
if (span.TotalHours < 24) return $"{(int)span.TotalHours}h ago";
|
||||
return $"{(int)span.TotalDays}d ago";
|
||||
}
|
||||
}
|
||||
@@ -1,169 +0,0 @@
|
||||
@using ZB.MOM.WW.OtOpcUa.Driver.Modbus
|
||||
|
||||
@*
|
||||
#145 — Driver-instance options panel for the Modbus driver. Surfaces every option group
|
||||
added by #136-#144 so users can configure the driver via the UI rather than hand-editing
|
||||
DriverConfig JSON. Bound to a ModbusOptionsViewModel; the parent page round-trips that
|
||||
model to the DriverConfig.json column on save.
|
||||
*@
|
||||
|
||||
<div class="modbus-options-editor">
|
||||
|
||||
<div class="panel-head">Connection</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-6">
|
||||
<label class="form-label">Host</label>
|
||||
<input class="form-control form-control-sm" @bind="Model.Host"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Port</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.Port"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Default UnitId</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.UnitId"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-head">Family (#144)</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-6">
|
||||
<label class="form-label">PLC family</label>
|
||||
<select class="form-select form-select-sm" @bind="Model.Family">
|
||||
@foreach (var f in Enum.GetValues<ModbusFamily>())
|
||||
{
|
||||
<option value="@f">@f</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
@if (Model.Family == ModbusFamily.MELSEC)
|
||||
{
|
||||
<div class="col-sm-6">
|
||||
<label class="form-label">MELSEC sub-family</label>
|
||||
<select class="form-select form-select-sm" @bind="Model.MelsecSubFamily">
|
||||
@foreach (var f in Enum.GetValues<MelsecFamily>())
|
||||
{
|
||||
<option value="@f">@f</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="panel-head">Keep-alive (#139)</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-3">
|
||||
<div class="form-check mt-4">
|
||||
<input type="checkbox" class="form-check-input" @bind="Model.KeepAliveEnabled"/>
|
||||
<label class="form-check-label">Enabled</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Time (s)</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.KeepAliveTimeSec"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Interval (s)</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.KeepAliveIntervalSec"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Retry count</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.KeepAliveRetryCount"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-head">Reconnect (#139)</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-4">
|
||||
<label class="form-label">Initial delay (ms)</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.ReconnectInitialDelayMs"/>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<label class="form-label">Max delay (ms)</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.ReconnectMaxDelayMs"/>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<label class="form-label">Backoff multiplier</label>
|
||||
<input type="number" step="0.1" class="form-control form-control-sm" @bind="Model.ReconnectBackoffMultiplier"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-head">Protocol (#140)</div>
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Max regs / read</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.MaxRegistersPerRead"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Max regs / write</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.MaxRegistersPerWrite"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Max coils / read</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.MaxCoilsPerRead"/>
|
||||
</div>
|
||||
<div class="col-sm-3">
|
||||
<label class="form-label">Max read gap (#143)</label>
|
||||
<input type="number" class="form-control form-control-sm" @bind="Model.MaxReadGap"/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row mb-3">
|
||||
<div class="col-sm-4">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" @bind="Model.UseFC15ForSingleCoilWrites"/>
|
||||
<label class="form-check-label">Use FC15 for single coil</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" @bind="Model.UseFC16ForSingleRegisterWrites"/>
|
||||
<label class="form-check-label">Use FC16 for single reg</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-4">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" @bind="Model.WriteOnChangeOnly"/>
|
||||
<label class="form-check-label">Write-on-change only (#141)</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
@code {
|
||||
[Parameter, EditorRequired] public ModbusOptionsViewModel Model { get; set; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// UI binding model. Maps 1:1 onto the JSON DTO the driver factory accepts; serialised
|
||||
/// to DriverConfig.json by the calling save handler. Defaults match
|
||||
/// <c>ModbusDriverOptions</c> defaults so unedited rows produce the historical wire
|
||||
/// output verbatim.
|
||||
/// </summary>
|
||||
public sealed class ModbusOptionsViewModel
|
||||
{
|
||||
public string Host { get; set; } = "127.0.0.1";
|
||||
public int Port { get; set; } = 502;
|
||||
public byte UnitId { get; set; } = 1;
|
||||
public ModbusFamily Family { get; set; } = ModbusFamily.Generic;
|
||||
public MelsecFamily MelsecSubFamily { get; set; } = MelsecFamily.Q_L_iQR;
|
||||
|
||||
public bool KeepAliveEnabled { get; set; } = true;
|
||||
public int KeepAliveTimeSec { get; set; } = 30;
|
||||
public int KeepAliveIntervalSec { get; set; } = 10;
|
||||
public int KeepAliveRetryCount { get; set; } = 3;
|
||||
|
||||
public int ReconnectInitialDelayMs { get; set; } = 0;
|
||||
public int ReconnectMaxDelayMs { get; set; } = 30000;
|
||||
public double ReconnectBackoffMultiplier { get; set; } = 2.0;
|
||||
|
||||
public int MaxRegistersPerRead { get; set; } = 125;
|
||||
public int MaxRegistersPerWrite { get; set; } = 123;
|
||||
public int MaxCoilsPerRead { get; set; } = 2000;
|
||||
public int MaxReadGap { get; set; } = 0;
|
||||
|
||||
public bool UseFC15ForSingleCoilWrites { get; set; } = false;
|
||||
public bool UseFC16ForSingleRegisterWrites { get; set; } = false;
|
||||
public bool WriteOnChangeOnly { get; set; } = false;
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
@page "/reservations"
|
||||
@using System.Security.Claims
|
||||
@using Microsoft.AspNetCore.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@attribute [Authorize(Policy = "CanPublish")]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@inject ReservationService ReservationSvc
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">External-ID reservations</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
Fleet-wide ZTag + SAPID reservation state (decision #124). Releasing a reservation is a
|
||||
FleetAdmin-only audit-logged action — only release when the physical asset is permanently
|
||||
retired and its ID needs to be reused by a different equipment.
|
||||
</p>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.02s">
|
||||
<div class="panel-head">Active</div>
|
||||
@if (_active is null) { <p class="px-3 py-2">Loading…</p> }
|
||||
else if (_active.Count == 0) { <p class="px-3 py-2 text-muted">No active reservations.</p> }
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>Kind</th><th>Value</th><th>EquipmentUuid</th><th>Cluster</th><th>First published</th><th>Last published</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var r in _active)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.Kind</span></td>
|
||||
<td><span class="mono">@r.Value</span></td>
|
||||
<td><span class="mono">@r.EquipmentUuid</span></td>
|
||||
<td>@r.ClusterId</td>
|
||||
<td><small>@r.FirstPublishedAt.ToString("u") by @r.FirstPublishedBy</small></td>
|
||||
<td><small>@r.LastPublishedAt.ToString("u")</small></td>
|
||||
<td><button class="btn btn-sm btn-outline-danger" @onclick='() => OpenReleaseDialog(r)'>Release…</button></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Released (most recent 100)</div>
|
||||
@if (_released is null) { <p class="px-3 py-2">Loading…</p> }
|
||||
else if (_released.Count == 0) { <p class="px-3 py-2 text-muted">No released reservations yet.</p> }
|
||||
else
|
||||
{
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead><tr><th>Kind</th><th>Value</th><th>Released at</th><th>By</th><th>Reason</th></tr></thead>
|
||||
<tbody>
|
||||
@foreach (var r in _released)
|
||||
{
|
||||
<tr><td><span class="mono">@r.Kind</span></td><td><span class="mono">@r.Value</span></td><td>@r.ReleasedAt?.ToString("u")</td><td>@r.ReleasedBy</td><td>@r.ReleaseReason</td></tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
}
|
||||
</section>
|
||||
|
||||
@if (_releasing is not null)
|
||||
{
|
||||
<div class="modal show d-block" tabindex="-1" style="background-color: rgba(0,0,0,0.5);">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">Release reservation <span class="mono">@_releasing.Kind</span> = <span class="mono">@_releasing.Value</span></h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>This makes the (Kind, Value) pair available for a different EquipmentUuid in a future publish. Audit-logged.</p>
|
||||
<label class="form-label">Reason (required)</label>
|
||||
<textarea class="form-control form-control-sm" rows="3" @bind="_reason"></textarea>
|
||||
@if (_error is not null) { <section class="panel notice mt-2">@_error</section> }
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button class="btn btn-secondary" @onclick='() => _releasing = null'>Cancel</button>
|
||||
<button class="btn btn-danger" @onclick="ReleaseAsync" disabled="@_busy">Release</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
// Admin-008: capture the signed-in operator so the release is attributed correctly in the
|
||||
// ExternalIdReservation.ReleasedBy column and the ConfigAuditLog.Principal column.
|
||||
[CascadingParameter] private Task<AuthenticationState>? AuthState { get; set; }
|
||||
|
||||
private List<ExternalIdReservation>? _active;
|
||||
private List<ExternalIdReservation>? _released;
|
||||
private ExternalIdReservation? _releasing;
|
||||
private string _reason = string.Empty;
|
||||
private bool _busy;
|
||||
private string? _error;
|
||||
|
||||
protected override async Task OnInitializedAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_active = await ReservationSvc.ListActiveAsync(CancellationToken.None);
|
||||
_released = await ReservationSvc.ListReleasedAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
private void OpenReleaseDialog(ExternalIdReservation r)
|
||||
{
|
||||
_releasing = r;
|
||||
_reason = string.Empty;
|
||||
_error = null;
|
||||
}
|
||||
|
||||
private async Task ReleaseAsync()
|
||||
{
|
||||
if (_releasing is null || string.IsNullOrWhiteSpace(_reason)) { _error = "Reason is required"; return; }
|
||||
|
||||
// Resolve the operator principal. The page is [Authorize(Policy="CanPublish")] so
|
||||
// AuthState will be available with an authenticated user; fall back to "unknown" only
|
||||
// as a defensive last resort (should never happen in practice).
|
||||
var user = AuthState is not null ? (await AuthState).User : null;
|
||||
var operatorName = user?.FindFirstValue(ClaimTypes.Name)
|
||||
?? user?.FindFirstValue(ClaimTypes.NameIdentifier)
|
||||
?? "unknown";
|
||||
|
||||
_busy = true;
|
||||
try
|
||||
{
|
||||
await ReservationSvc.ReleaseAsync(
|
||||
_releasing.Kind.ToString(), _releasing.Value, _reason, operatorName, CancellationToken.None);
|
||||
_releasing = null;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
finally { _busy = false; }
|
||||
}
|
||||
}
|
||||
@@ -1,198 +0,0 @@
|
||||
@page "/role-grants"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize(Policy = "CanPublish")]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Services
|
||||
@inject ILdapGroupRoleMappingService RoleSvc
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject AclChangeNotifier Notifier
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">LDAP group → Admin role grants</h4>
|
||||
<button class="btn btn-primary btn-sm" @onclick="StartAdd">Add grant</button>
|
||||
</div>
|
||||
|
||||
@if (_rows is null)
|
||||
{
|
||||
<p>Loading…</p>
|
||||
}
|
||||
else if (_rows.Count == 0)
|
||||
{
|
||||
<p class="text-muted">No role grants defined yet. Without at least one FleetAdmin grant,
|
||||
only the bootstrap admin can publish drafts.</p>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Grants</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr><th>LDAP group</th><th>Role</th><th>Scope</th><th>Created</th><th>Notes</th><th></th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var r in _rows)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@r.LdapGroup</span></td>
|
||||
<td><span class="chip chip-idle">@r.Role</span></td>
|
||||
<td>@(r.IsSystemWide ? "Fleet-wide" : $"Cluster: {r.ClusterId}")</td>
|
||||
<td class="small">@r.CreatedAtUtc.ToString("yyyy-MM-dd")</td>
|
||||
<td class="small text-muted">@r.Notes</td>
|
||||
<td><button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteAsync(r.Id)">Revoke</button></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_showForm)
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.14s">
|
||||
<div class="panel-head">New role grant</div>
|
||||
<div class="p-3">
|
||||
<div class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label class="form-label">LDAP group (DN)</label>
|
||||
<input class="form-control form-control-sm" @bind="_group" placeholder="cn=fleet-admin,ou=groups,dc=…"/>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<label class="form-label">Role</label>
|
||||
<select class="form-select form-select-sm" @bind="_role">
|
||||
@foreach (var r in Enum.GetValues<AdminRole>())
|
||||
{
|
||||
<option value="@r">@r</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-2 pt-4">
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" id="systemWide" @bind="_isSystemWide"/>
|
||||
<label class="form-check-label" for="systemWide">Fleet-wide</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<label class="form-label">Cluster @(_isSystemWide ? "(disabled)" : "")</label>
|
||||
<select class="form-select form-select-sm" @bind="_clusterId" disabled="@_isSystemWide">
|
||||
<option value="">-- select --</option>
|
||||
@if (_clusters is not null)
|
||||
{
|
||||
@foreach (var c in _clusters)
|
||||
{
|
||||
<option value="@c.ClusterId">@c.ClusterId</option>
|
||||
}
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-12">
|
||||
<label class="form-label">Notes (optional)</label>
|
||||
<input class="form-control form-control-sm" @bind="_notes"/>
|
||||
</div>
|
||||
</div>
|
||||
@if (_error is not null) { <section class="panel notice mt-3">@_error</section> }
|
||||
<div class="mt-3">
|
||||
<button class="btn btn-sm btn-primary" @onclick="SaveAsync">Save</button>
|
||||
<button class="btn btn-sm btn-secondary ms-2" @onclick="() => _showForm = false">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private IReadOnlyList<LdapGroupRoleMapping>? _rows;
|
||||
private List<ServerCluster>? _clusters;
|
||||
private bool _showForm;
|
||||
private string _group = string.Empty;
|
||||
private AdminRole _role = AdminRole.ConfigViewer;
|
||||
private bool _isSystemWide;
|
||||
private string _clusterId = string.Empty;
|
||||
private string? _notes;
|
||||
private string? _error;
|
||||
|
||||
protected override async Task OnInitializedAsync() => await ReloadAsync();
|
||||
|
||||
private async Task ReloadAsync()
|
||||
{
|
||||
_rows = await RoleSvc.ListAllAsync(CancellationToken.None);
|
||||
_clusters = await ClusterSvc.ListAsync(CancellationToken.None);
|
||||
}
|
||||
|
||||
private void StartAdd()
|
||||
{
|
||||
_group = string.Empty;
|
||||
_role = AdminRole.ConfigViewer;
|
||||
_isSystemWide = false;
|
||||
_clusterId = string.Empty;
|
||||
_notes = null;
|
||||
_error = null;
|
||||
_showForm = true;
|
||||
}
|
||||
|
||||
private async Task SaveAsync()
|
||||
{
|
||||
_error = null;
|
||||
try
|
||||
{
|
||||
var row = new LdapGroupRoleMapping
|
||||
{
|
||||
LdapGroup = _group.Trim(),
|
||||
Role = _role,
|
||||
IsSystemWide = _isSystemWide,
|
||||
ClusterId = _isSystemWide ? null : (string.IsNullOrWhiteSpace(_clusterId) ? null : _clusterId),
|
||||
Notes = string.IsNullOrWhiteSpace(_notes) ? null : _notes,
|
||||
};
|
||||
await RoleSvc.CreateAsync(row, CancellationToken.None);
|
||||
await Notifier.NotifyRoleGrantsChangedAsync(CancellationToken.None);
|
||||
_showForm = false;
|
||||
await ReloadAsync();
|
||||
}
|
||||
catch (Exception ex) { _error = ex.Message; }
|
||||
}
|
||||
|
||||
private async Task DeleteAsync(Guid id)
|
||||
{
|
||||
await RoleSvc.DeleteAsync(id, CancellationToken.None);
|
||||
await Notifier.NotifyRoleGrantsChangedAsync(CancellationToken.None);
|
||||
await ReloadAsync();
|
||||
}
|
||||
|
||||
private HubConnection? _hub;
|
||||
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if (!firstRender || _hub is not null) return;
|
||||
_hub = HubFactory.Create("/hubs/fleet");
|
||||
_hub.On<RoleGrantsChangedMessage>("RoleGrantsChanged", async _ =>
|
||||
{
|
||||
await ReloadAsync();
|
||||
await InvokeAsync(StateHasChanged);
|
||||
});
|
||||
// Best-effort: FleetStatusHub requires an authenticated caller, and the server-side
|
||||
// HubConnection cannot forward the browser auth cookie — swallow connect failures so
|
||||
// the page still renders. Live role-grant updates degrade.
|
||||
try
|
||||
{
|
||||
await _hub.StartAsync();
|
||||
await _hub.SendAsync("SubscribeFleet");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// best-effort live updates — see comment above
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_hub is not null) { await _hub.DisposeAsync(); _hub = null; }
|
||||
}
|
||||
}
|
||||
@@ -1,238 +0,0 @@
|
||||
@page "/script-log"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.SignalR.Client
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Hubs
|
||||
@inject NavigationManager Nav
|
||||
@inject AdminHubConnectionFactory HubFactory
|
||||
@implements IAsyncDisposable
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Script log viewer</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
Live tail of the <code class="mono">scripts-*.log</code> file produced by the OPC UA Server's
|
||||
Roslyn script runtime. Useful for diagnosing virtual-tag and scripted-alarm script errors in production.
|
||||
Filter by script name to see only events from one script.
|
||||
</p>
|
||||
|
||||
<div class="toolbar mb-3">
|
||||
<input class="form-control form-control-sm"
|
||||
style="max-width:22rem"
|
||||
placeholder="Filter by script name (optional)"
|
||||
@bind="_scriptNameFilter"
|
||||
@bind:event="oninput"
|
||||
disabled="@_streaming"/>
|
||||
<select class="form-select form-select-sm ms-2" style="max-width:10rem" @bind="_minLevel" disabled="@_streaming">
|
||||
<option value="VRB">All (VRB+)</option>
|
||||
<option value="DBG">DBG+</option>
|
||||
<option value="INF">INF+</option>
|
||||
<option value="WRN">WRN+</option>
|
||||
<option value="ERR">ERR+</option>
|
||||
</select>
|
||||
<button class="btn btn-sm btn-primary ms-2" @onclick="StartAsync" disabled="@_streaming">Start</button>
|
||||
<button class="btn btn-sm btn-outline-secondary ms-1" @onclick="StopAsync" disabled="@(!_streaming)">Stop</button>
|
||||
<button class="btn btn-sm btn-outline-danger ms-1" @onclick="ClearLines">Clear</button>
|
||||
<span class="spacer"></span>
|
||||
@if (_streaming)
|
||||
{
|
||||
<span class="chip chip-ok">Streaming</span>
|
||||
}
|
||||
else if (_stopped)
|
||||
{
|
||||
<span class="chip chip-idle">Stopped</span>
|
||||
}
|
||||
@if (_lines.Count > 0) { <span class="tb-count ms-2">@_lines.Count line@(_lines.Count == 1 ? "" : "s")</span> }
|
||||
</div>
|
||||
|
||||
@if (_error is not null)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
<span class="s-bad">@_error</span>
|
||||
<button type="button" class="btn-close float-end" @onclick="() => _error = null"></button>
|
||||
</section>
|
||||
}
|
||||
|
||||
@if (_lines.Count == 0 && !_streaming && !_stopped)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.04s">
|
||||
Press <strong>Start</strong> to begin tailing the script log. The last @ScriptLogHub.TailSeedLines lines
|
||||
are replayed first, then new lines appear as they are written by the OPC UA Server script runtime.
|
||||
</section>
|
||||
}
|
||||
else if (_lines.Count == 0 && (_streaming || _stopped))
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.04s">
|
||||
No matching log lines found. Check that the OPC UA Server is running and has executed at least one script,
|
||||
and that the <code class="mono">ScriptLog:Directory</code> setting points to the correct log folder.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head d-flex justify-content-between align-items-center">
|
||||
<span>Script log</span>
|
||||
<small class="text-muted">Latest @_lines.Count entries — oldest first</small>
|
||||
</div>
|
||||
<div class="table-wrap" style="max-height:60vh;overflow-y:auto" @ref="_tableContainer">
|
||||
<table class="data-table" style="font-size:.85rem">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:7rem">Level</th>
|
||||
<th style="width:14rem">Script</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var line in _lines)
|
||||
{
|
||||
<tr class="@RowClass(line.Level)">
|
||||
<td><span class="chip @LevelBadge(line.Level)">@line.Level</span></td>
|
||||
<td><span class="mono small">@(line.ScriptName ?? "—")</span></td>
|
||||
<td><span class="mono small" style="white-space:pre-wrap;word-break:break-all">@line.Raw</span></td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
// Keep at most this many lines in-memory to avoid DOM growth.
|
||||
private const int MaxLines = 1000;
|
||||
|
||||
private HubConnection? _hub;
|
||||
private CancellationTokenSource? _streamCts;
|
||||
private List<ScriptLogLine> _lines = [];
|
||||
private string _scriptNameFilter = string.Empty;
|
||||
private string _minLevel = "INF";
|
||||
private bool _streaming;
|
||||
private bool _stopped;
|
||||
private string? _error;
|
||||
private ElementReference _tableContainer;
|
||||
|
||||
private static readonly string[] LevelOrder = ["VRB", "DBG", "INF", "WRN", "ERR", "FTL"];
|
||||
|
||||
private async Task StartAsync()
|
||||
{
|
||||
_error = null;
|
||||
_streaming = false;
|
||||
_stopped = false;
|
||||
|
||||
try
|
||||
{
|
||||
_hub ??= HubFactory.Create("/hubs/script-log");
|
||||
|
||||
if (_hub.State == HubConnectionState.Disconnected)
|
||||
await _hub.StartAsync();
|
||||
|
||||
_streamCts = new CancellationTokenSource();
|
||||
_streaming = true;
|
||||
|
||||
// Fire-and-forget into the background; updates come via StateHasChanged.
|
||||
_ = Task.Run(() => ConsumeStreamAsync(_streamCts.Token));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_error = $"Failed to connect to script log hub: {ex.Message}";
|
||||
_streaming = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ConsumeStreamAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var stream = _hub!.StreamAsync<ScriptLogLine>(
|
||||
"TailLogAsync", _scriptNameFilter, ct);
|
||||
|
||||
await foreach (var line in stream.WithCancellation(ct))
|
||||
{
|
||||
if (!PassesLevelFilter(line.Level)) continue;
|
||||
|
||||
await InvokeAsync(() =>
|
||||
{
|
||||
_lines.Add(line);
|
||||
if (_lines.Count > MaxLines)
|
||||
_lines.RemoveRange(0, _lines.Count - MaxLines);
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException) { /* normal stop */ }
|
||||
catch (Exception ex)
|
||||
{
|
||||
await InvokeAsync(() =>
|
||||
{
|
||||
_error = $"Stream error: {ex.Message}";
|
||||
_streaming = false;
|
||||
_stopped = true;
|
||||
StateHasChanged();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await InvokeAsync(() =>
|
||||
{
|
||||
_streaming = false;
|
||||
_stopped = true;
|
||||
StateHasChanged();
|
||||
});
|
||||
}
|
||||
|
||||
private async Task StopAsync()
|
||||
{
|
||||
if (_streamCts is not null)
|
||||
{
|
||||
await _streamCts.CancelAsync();
|
||||
_streamCts.Dispose();
|
||||
_streamCts = null;
|
||||
}
|
||||
_streaming = false;
|
||||
_stopped = true;
|
||||
}
|
||||
|
||||
private void ClearLines()
|
||||
{
|
||||
_lines.Clear();
|
||||
_stopped = false;
|
||||
}
|
||||
|
||||
private bool PassesLevelFilter(string level)
|
||||
{
|
||||
var minIdx = Array.IndexOf(LevelOrder, _minLevel);
|
||||
var lineIdx = Array.IndexOf(LevelOrder, level);
|
||||
return lineIdx >= minIdx;
|
||||
}
|
||||
|
||||
private static string LevelBadge(string level) => level switch
|
||||
{
|
||||
"ERR" or "FTL" => "chip-bad",
|
||||
"WRN" => "chip-warn",
|
||||
"INF" => "chip-ok",
|
||||
_ => "chip-idle",
|
||||
};
|
||||
|
||||
private static string RowClass(string level) => level switch
|
||||
{
|
||||
"ERR" or "FTL" => "table-danger",
|
||||
"WRN" => "table-warning",
|
||||
_ => string.Empty,
|
||||
};
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_streamCts is not null)
|
||||
{
|
||||
await _streamCts.CancelAsync();
|
||||
_streamCts.Dispose();
|
||||
}
|
||||
if (_hub is not null)
|
||||
{
|
||||
await _hub.DisposeAsync();
|
||||
_hub = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
@page "/scripted-alarms"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Enums
|
||||
@inject ScriptedAlarmService AlarmSvc
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Scripted Alarms</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
OPC UA Part 9 alarms raised by C# predicate scripts. To author scripted alarms, open a cluster
|
||||
draft and use the <strong>Scripted Alarms</strong> tab in the draft editor.
|
||||
This view lists all scripted alarms across clusters and generations for reference.
|
||||
</p>
|
||||
|
||||
<div class="toolbar mb-3">
|
||||
<select class="form-select form-select-sm tb-state" @bind="_filterClusterId" @bind:after="OnClusterChangedAsync">
|
||||
<option value="">— all clusters —</option>
|
||||
@foreach (var c in _clusters)
|
||||
{
|
||||
<option value="@c.ClusterId">@c.Name (@c.ClusterId)</option>
|
||||
}
|
||||
</select>
|
||||
<select class="form-select form-select-sm tb-state ms-2" @bind="_filterGenerationId" @bind:after="LoadAlarmsAsync"
|
||||
disabled="@(string.IsNullOrEmpty(_filterClusterId))">
|
||||
<option value="0">— all generations —</option>
|
||||
@foreach (var g in _generations)
|
||||
{
|
||||
<option value="@g.GenerationId">gen @g.GenerationId (@g.Status) @(g.PublishedAt?.ToString("yyyy-MM-dd") ?? "")</option>
|
||||
}
|
||||
</select>
|
||||
<span class="spacer"></span>
|
||||
@if (_alarms is not null) { <span class="tb-count">@_alarms.Count alarm@(_alarms.Count == 1 ? "" : "s")</span> }
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p class="text-muted">Loading…</p>
|
||||
}
|
||||
else if (_alarms is null || _alarms.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
No scripted alarms found for the selected filter.
|
||||
Open a cluster draft and use the <strong>Scripted Alarms</strong> tab to author scripted alarms.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Scripted alarms</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Equipment</th>
|
||||
<th>Type</th>
|
||||
<th class="num">Severity</th>
|
||||
<th>Message template</th>
|
||||
<th>Predicate script</th>
|
||||
<th>Historize</th>
|
||||
<th>Retain</th>
|
||||
<th>Enabled</th>
|
||||
<th class="num">Generation</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var a in _alarms)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@a.Name</span></td>
|
||||
<td><span class="mono">@a.EquipmentId</span></td>
|
||||
<td><span class="chip chip-idle">@a.AlarmType</span></td>
|
||||
<td class="num">@a.Severity <small class="text-muted">@SeverityBand(a.Severity)</small></td>
|
||||
<td class="text-truncate" style="max-width:18rem" title="@a.MessageTemplate">
|
||||
<span class="mono small">@a.MessageTemplate</span>
|
||||
</td>
|
||||
<td><span class="mono">@a.PredicateScriptId</span></td>
|
||||
<td>
|
||||
@if (a.HistorizeToAveva) { <span class="chip chip-ok">Aveva</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (a.Retain) { <span class="chip chip-ok">yes</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (a.Enabled) { <span class="chip chip-ok">enabled</span> }
|
||||
else { <span class="chip chip-idle">disabled</span> }
|
||||
</td>
|
||||
<td class="num">@a.GenerationId</td>
|
||||
<td>
|
||||
@if (DraftLink(a.GenerationId) is { } link)
|
||||
{
|
||||
<a class="btn btn-sm btn-outline-secondary" href="@link">Edit draft</a>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private List<ServerCluster> _clusters = [];
|
||||
private List<ConfigGeneration> _generations = [];
|
||||
private List<ScriptedAlarm>? _alarms;
|
||||
private string _filterClusterId = string.Empty;
|
||||
private long _filterGenerationId;
|
||||
private bool _loading;
|
||||
private Dictionary<long, (string clusterId, bool isDraft)> _genMap = [];
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_clusters = await ClusterSvc.ListAsync(CancellationToken.None);
|
||||
await LoadAlarmsAsync();
|
||||
}
|
||||
|
||||
private async Task OnClusterChangedAsync()
|
||||
{
|
||||
_filterGenerationId = 0;
|
||||
_generations = string.IsNullOrEmpty(_filterClusterId)
|
||||
? []
|
||||
: await GenerationSvc.ListRecentAsync(_filterClusterId, 20, CancellationToken.None);
|
||||
await LoadAlarmsAsync();
|
||||
}
|
||||
|
||||
private async Task LoadAlarmsAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_alarms = null;
|
||||
try
|
||||
{
|
||||
var all = new List<ScriptedAlarm>();
|
||||
_genMap = [];
|
||||
|
||||
IEnumerable<ConfigGeneration> gens;
|
||||
if (!string.IsNullOrEmpty(_filterClusterId) && _filterGenerationId != 0)
|
||||
{
|
||||
gens = _generations.Where(g => g.GenerationId == _filterGenerationId);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_filterClusterId))
|
||||
{
|
||||
gens = await GenerationSvc.ListRecentAsync(_filterClusterId, 20, CancellationToken.None);
|
||||
_generations = gens.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
var allGens = new List<ConfigGeneration>();
|
||||
foreach (var c in _clusters)
|
||||
{
|
||||
var cGens = await GenerationSvc.ListRecentAsync(c.ClusterId, 5, CancellationToken.None);
|
||||
allGens.AddRange(cGens);
|
||||
}
|
||||
gens = allGens;
|
||||
}
|
||||
|
||||
foreach (var g in gens)
|
||||
{
|
||||
_genMap[g.GenerationId] = (g.ClusterId, g.Status == GenerationStatus.Draft);
|
||||
var alarms = await AlarmSvc.ListAsync(g.GenerationId, CancellationToken.None);
|
||||
all.AddRange(alarms);
|
||||
}
|
||||
|
||||
_alarms = all;
|
||||
}
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private string? DraftLink(long generationId)
|
||||
{
|
||||
if (_genMap.TryGetValue(generationId, out var info) && info.isDraft)
|
||||
return $"/clusters/{info.clusterId}/draft/{generationId}";
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string SeverityBand(int s) => s switch
|
||||
{
|
||||
<= 250 => "(Low)",
|
||||
<= 500 => "(Medium)",
|
||||
<= 750 => "(High)",
|
||||
_ => "(Critical)",
|
||||
};
|
||||
}
|
||||
@@ -1,184 +0,0 @@
|
||||
@page "/virtual-tags"
|
||||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
@rendermode RenderMode.InteractiveServer
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||||
@inject VirtualTagService VirtualTagSvc
|
||||
@inject GenerationService GenerationSvc
|
||||
@inject ClusterService ClusterSvc
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||||
<h4 class="mb-0">Virtual Tags</h4>
|
||||
</div>
|
||||
<p class="text-muted">
|
||||
Computed tags driven by C# scripts. To author virtual tags, open a cluster draft and use the
|
||||
<strong>Virtual Tags</strong> tab in the draft editor.
|
||||
This view lists all virtual tags across clusters and generations for reference.
|
||||
</p>
|
||||
|
||||
<div class="toolbar mb-3">
|
||||
<select class="form-select form-select-sm tb-state" @bind="_filterClusterId" @bind:after="OnClusterChangedAsync">
|
||||
<option value="">— all clusters —</option>
|
||||
@foreach (var c in _clusters)
|
||||
{
|
||||
<option value="@c.ClusterId">@c.Name (@c.ClusterId)</option>
|
||||
}
|
||||
</select>
|
||||
<select class="form-select form-select-sm tb-state ms-2" @bind="_filterGenerationId" @bind:after="LoadTagsAsync"
|
||||
disabled="@(string.IsNullOrEmpty(_filterClusterId))">
|
||||
<option value="0">— all generations —</option>
|
||||
@foreach (var g in _generations)
|
||||
{
|
||||
<option value="@g.GenerationId">gen @g.GenerationId (@g.Status) @(g.PublishedAt?.ToString("yyyy-MM-dd") ?? "")</option>
|
||||
}
|
||||
</select>
|
||||
<span class="spacer"></span>
|
||||
@if (_tags is not null) { <span class="tb-count">@_tags.Count tag@(_tags.Count == 1 ? "" : "s")</span> }
|
||||
</div>
|
||||
|
||||
@if (_loading)
|
||||
{
|
||||
<p class="text-muted">Loading…</p>
|
||||
}
|
||||
else if (_tags is null || _tags.Count == 0)
|
||||
{
|
||||
<section class="panel notice rise" style="animation-delay:.02s">
|
||||
No virtual tags found for the selected filter.
|
||||
Open a cluster draft and use the <strong>Virtual Tags</strong> tab to author virtual tags.
|
||||
</section>
|
||||
}
|
||||
else
|
||||
{
|
||||
<section class="panel rise" style="animation-delay:.08s">
|
||||
<div class="panel-head">Virtual tags</div>
|
||||
<div class="table-wrap">
|
||||
<table class="data-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Equipment</th>
|
||||
<th>DataType</th>
|
||||
<th>Script</th>
|
||||
<th>Triggers</th>
|
||||
<th>Historize</th>
|
||||
<th>Enabled</th>
|
||||
<th class="num">Generation</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@foreach (var t in _tags)
|
||||
{
|
||||
<tr>
|
||||
<td><span class="mono">@t.Name</span></td>
|
||||
<td><span class="mono">@t.EquipmentId</span></td>
|
||||
<td>@t.DataType</td>
|
||||
<td><span class="mono">@t.ScriptId</span></td>
|
||||
<td>
|
||||
@if (t.ChangeTriggered) { <span class="chip chip-idle me-1">change</span> }
|
||||
@if (t.TimerIntervalMs.HasValue) { <span class="chip chip-idle">@t.TimerIntervalMs ms</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (t.Historize) { <span class="chip chip-ok">yes</span> }
|
||||
else { <span class="text-muted">—</span> }
|
||||
</td>
|
||||
<td>
|
||||
@if (t.Enabled) { <span class="chip chip-ok">enabled</span> }
|
||||
else { <span class="chip chip-idle">disabled</span> }
|
||||
</td>
|
||||
<td class="num">@t.GenerationId</td>
|
||||
<td>
|
||||
@if (DraftGenId(t.GenerationId) is { } draftUrl)
|
||||
{
|
||||
<a class="btn btn-sm btn-outline-secondary" href="@draftUrl">Edit draft</a>
|
||||
}
|
||||
</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
}
|
||||
|
||||
@code {
|
||||
private List<ServerCluster> _clusters = [];
|
||||
private List<ConfigGeneration> _generations = [];
|
||||
private List<VirtualTag>? _tags;
|
||||
private string _filterClusterId = string.Empty;
|
||||
private long _filterGenerationId;
|
||||
private bool _loading;
|
||||
|
||||
// Map generationId → (clusterId, isDraft) for the "Edit draft" link
|
||||
private Dictionary<long, (string clusterId, bool isDraft)> _genMap = [];
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
_clusters = await ClusterSvc.ListAsync(CancellationToken.None);
|
||||
await LoadTagsAsync();
|
||||
}
|
||||
|
||||
private async Task OnClusterChangedAsync()
|
||||
{
|
||||
_filterGenerationId = 0;
|
||||
_generations = string.IsNullOrEmpty(_filterClusterId)
|
||||
? []
|
||||
: await GenerationSvc.ListRecentAsync(_filterClusterId, 20, CancellationToken.None);
|
||||
await LoadTagsAsync();
|
||||
}
|
||||
|
||||
private async Task LoadTagsAsync()
|
||||
{
|
||||
_loading = true;
|
||||
_tags = null;
|
||||
try
|
||||
{
|
||||
var allTags = new List<VirtualTag>();
|
||||
_genMap = [];
|
||||
|
||||
// Determine which generations to query
|
||||
IEnumerable<ConfigGeneration> gens;
|
||||
if (!string.IsNullOrEmpty(_filterClusterId) && _filterGenerationId != 0)
|
||||
{
|
||||
// specific cluster + specific generation
|
||||
gens = _generations.Where(g => g.GenerationId == _filterGenerationId);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(_filterClusterId))
|
||||
{
|
||||
// specific cluster, all recent generations
|
||||
gens = await GenerationSvc.ListRecentAsync(_filterClusterId, 20, CancellationToken.None);
|
||||
_generations = gens.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
// all clusters — load generations per cluster
|
||||
var allGens = new List<ConfigGeneration>();
|
||||
foreach (var c in _clusters)
|
||||
{
|
||||
var cGens = await GenerationSvc.ListRecentAsync(c.ClusterId, 5, CancellationToken.None);
|
||||
allGens.AddRange(cGens);
|
||||
}
|
||||
gens = allGens;
|
||||
}
|
||||
|
||||
foreach (var g in gens)
|
||||
{
|
||||
_genMap[g.GenerationId] = (g.ClusterId, g.Status == ZB.MOM.WW.OtOpcUa.Configuration.Enums.GenerationStatus.Draft);
|
||||
var tags = await VirtualTagSvc.ListAsync(g.GenerationId, CancellationToken.None);
|
||||
allTags.AddRange(tags);
|
||||
}
|
||||
|
||||
_tags = allTags;
|
||||
}
|
||||
finally { _loading = false; }
|
||||
}
|
||||
|
||||
private string? DraftGenId(long generationId)
|
||||
{
|
||||
if (_genMap.TryGetValue(generationId, out var info) && info.isDraft)
|
||||
return $"/clusters/{info.clusterId}/draft/{generationId}";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
@* Server-side redirect to the login page for an unauthenticated request.
|
||||
Used by AuthorizeRouteView's NotAuthorized slot (Admin-001). The current
|
||||
path is carried through as returnUrl so the operator lands back where
|
||||
they aimed after signing in. *@
|
||||
@inject NavigationManager Nav
|
||||
|
||||
@code {
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
var returnUrl = Nav.ToBaseRelativePath(Nav.Uri);
|
||||
var target = string.IsNullOrEmpty(returnUrl) || returnUrl == "login"
|
||||
? "login"
|
||||
: $"login?returnUrl={Uri.EscapeDataString("/" + returnUrl)}";
|
||||
Nav.NavigateTo(target, forceLoad: true);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Components.Routing
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Layout
|
||||
|
||||
<Router AppAssembly="@typeof(Program).Assembly">
|
||||
<Found Context="routeData">
|
||||
@* AuthorizeRouteView (not a plain RouteView) is what makes a page-level
|
||||
[Authorize] attribute actually enforced — with RouteView the attribute
|
||||
is inert (Admin-001). Unauthenticated users hit the NotAuthorized slot
|
||||
and are bounced to /login; the route is preserved as returnUrl. *@
|
||||
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
|
||||
<NotAuthorized>
|
||||
@if (context.User.Identity?.IsAuthenticated != true)
|
||||
{
|
||||
<RedirectToLogin/>
|
||||
}
|
||||
else
|
||||
{
|
||||
<LayoutView Layout="@typeof(MainLayout)">
|
||||
<p class="text-danger">You do not have permission to view this page.</p>
|
||||
</LayoutView>
|
||||
}
|
||||
</NotAuthorized>
|
||||
<Authorizing>
|
||||
<LayoutView Layout="@typeof(MainLayout)"><p>Authorizing…</p></LayoutView>
|
||||
</Authorizing>
|
||||
</AuthorizeRouteView>
|
||||
</Found>
|
||||
<NotFound>
|
||||
<LayoutView Layout="@typeof(MainLayout)"><p>Not found.</p></LayoutView>
|
||||
</NotFound>
|
||||
</Router>
|
||||
@@ -1,16 +0,0 @@
|
||||
@using System.Net.Http
|
||||
@using Microsoft.AspNetCore.Components
|
||||
@using Microsoft.AspNetCore.Components.Forms
|
||||
@using Microsoft.AspNetCore.Components.Routing
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@using Microsoft.AspNetCore.Http
|
||||
@using Microsoft.JSInterop
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Layout
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Pages
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Pages.Clusters
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Components.Shared
|
||||
@using ZB.MOM.WW.OtOpcUa.Admin.Services
|
||||
@@ -1,37 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Pushes sticky alerts (crash-loop circuit trips, failed applies, reservation-release
|
||||
/// anomalies) to subscribed admin clients. Alerts don't auto-clear — the operator acks them
|
||||
/// from the UI via <see cref="AcknowledgeAsync"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <see cref="AuthorizeAttribute"/> gates the hub so failure-detail alert text is not pushed
|
||||
/// to anonymous connections (Admin-003).
|
||||
/// </remarks>
|
||||
[Authorize]
|
||||
public sealed class AlertHub : Hub
|
||||
{
|
||||
public const string AllAlertsGroup = "__alerts__";
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
await Groups.AddToGroupAsync(Context.ConnectionId, AllAlertsGroup);
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
/// <summary>Client-initiated ack. The server side of ack persistence is deferred — v2.1.</summary>
|
||||
public Task AcknowledgeAsync(string alertId) => Task.CompletedTask;
|
||||
}
|
||||
|
||||
public sealed record AlertMessage(
|
||||
string AlertId,
|
||||
string Severity,
|
||||
string Title,
|
||||
string Detail,
|
||||
DateTime RaisedAtUtc,
|
||||
string? ClusterId,
|
||||
string? NodeId);
|
||||
@@ -1,61 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Pushes per-node generation-apply state changes (<c>ClusterNodeGenerationState</c>) to
|
||||
/// subscribed browser clients. Clients call <c>SubscribeCluster(clusterId)</c> on connect to
|
||||
/// scope notifications; the server sends <c>NodeStateChanged</c> messages whenever the poller
|
||||
/// observes a delta.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <see cref="AuthorizeAttribute"/> gates the hub: an unauthenticated client cannot open the
|
||||
/// connection, so the fleet generation/role/resilience stream is not an anonymous
|
||||
/// information-disclosure channel (Admin-003).
|
||||
/// </remarks>
|
||||
[Authorize]
|
||||
public sealed class FleetStatusHub : Hub
|
||||
{
|
||||
public Task SubscribeCluster(string clusterId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(clusterId)) return Task.CompletedTask;
|
||||
return Groups.AddToGroupAsync(Context.ConnectionId, GroupName(clusterId));
|
||||
}
|
||||
|
||||
public Task UnsubscribeCluster(string clusterId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(clusterId)) return Task.CompletedTask;
|
||||
return Groups.RemoveFromGroupAsync(Context.ConnectionId, GroupName(clusterId));
|
||||
}
|
||||
|
||||
/// <summary>Clients call this once to also receive fleet-wide status — used by the dashboard.</summary>
|
||||
public Task SubscribeFleet() => Groups.AddToGroupAsync(Context.ConnectionId, FleetGroup);
|
||||
|
||||
public const string FleetGroup = "__fleet__";
|
||||
public static string GroupName(string clusterId) => $"cluster:{clusterId}";
|
||||
}
|
||||
|
||||
public sealed record NodeStateChangedMessage(
|
||||
string NodeId,
|
||||
string ClusterId,
|
||||
long? CurrentGenerationId,
|
||||
string? LastAppliedStatus,
|
||||
string? LastAppliedError,
|
||||
DateTime? LastAppliedAt,
|
||||
DateTime? LastSeenAt);
|
||||
|
||||
/// <summary>
|
||||
/// Pushed by <c>FleetStatusPoller</c> when it observes a change in a
|
||||
/// <c>DriverInstanceResilienceStatus</c> row. Closes the last Phase 6.1 Stream E.2/E.3
|
||||
/// deferral — lets the Admin <c>/hosts</c> page upsert the matching row without the
|
||||
/// 10-second polling round-trip. Keyed on (DriverInstanceId, HostName); the client
|
||||
/// fan-outs to the matching row by matching both.
|
||||
/// </summary>
|
||||
public sealed record ResilienceStatusChangedMessage(
|
||||
string DriverInstanceId,
|
||||
string HostName,
|
||||
int ConsecutiveFailures,
|
||||
DateTime? LastCircuitBreakerOpenUtc,
|
||||
int CurrentBulkheadDepth,
|
||||
DateTime? LastRecycleUtc);
|
||||
Binary file not shown.
@@ -1,228 +0,0 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Hubs;
|
||||
|
||||
/// <summary>
|
||||
/// Streams lines from the server's <c>scripts-*.log</c> file(s) to the Admin UI
|
||||
/// (Phase 7 Stream F.5). Clients call <see cref="TailLogAsync"/> with an optional
|
||||
/// <paramref name="scriptNameFilter"/> to see only events from a named script.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Log files are looked up at <c>ScriptLog:Directory</c> (appsettings.json) relative
|
||||
/// to the current working directory, defaulting to <c>logs</c>. The glob pattern
|
||||
/// <c>scripts-*.log</c> is applied and the most-recently-written file is tailed.
|
||||
/// If no matching file is found an empty stream is returned — the UI shows a notice.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Each streamed <see cref="ScriptLogLine"/> carries the raw text, an extracted level
|
||||
/// (parsed from the Serilog compact format <c>[INF]</c> / <c>[WRN]</c> / <c>[ERR]</c>),
|
||||
/// and the extracted <c>ScriptName</c> property value when present.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Tail semantics: up to <see cref="TailSeedLines"/> of the existing file are replayed
|
||||
/// first, then new lines are emitted as they are appended. The stream is cancelled when
|
||||
/// the client disconnects.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// <see cref="AuthorizeAttribute"/> gates the hub: only an authenticated operator can
|
||||
/// open the connection and tail the server <c>scripts-*.log</c> contents (Admin-003).
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
[Authorize]
|
||||
public sealed class ScriptLogHub(IConfiguration configuration, ILogger<ScriptLogHub> logger) : Hub
|
||||
{
|
||||
/// <summary>Number of existing lines to replay from the end of the file before live-tailing.</summary>
|
||||
public const int TailSeedLines = 200;
|
||||
|
||||
/// <summary>Poll cadence for new lines while the log is not being actively appended.</summary>
|
||||
private static readonly TimeSpan PollInterval = TimeSpan.FromSeconds(1);
|
||||
|
||||
/// <summary>
|
||||
/// SignalR server-to-client stream. The caller awaits <c>await foreach</c> on the returned
|
||||
/// <see cref="IAsyncEnumerable{T}"/> via the hub-stream protocol. Cancelled automatically
|
||||
/// when the client disconnects or the provided <paramref name="ct"/> fires.
|
||||
/// </summary>
|
||||
/// <param name="scriptNameFilter">
|
||||
/// Optional script name. When non-empty only lines whose <c>ScriptName</c> property
|
||||
/// matches (case-insensitive contains) are emitted.
|
||||
/// </param>
|
||||
/// <param name="ct">Hub-provided cancellation token, cancelled on disconnect.</param>
|
||||
public async IAsyncEnumerable<ScriptLogLine> TailLogAsync(
|
||||
string? scriptNameFilter,
|
||||
[EnumeratorCancellation] CancellationToken ct)
|
||||
{
|
||||
var logDir = configuration["ScriptLog:Directory"] ?? "logs";
|
||||
var pattern = "scripts-*.log";
|
||||
|
||||
// Find the most recently written matching file.
|
||||
string? logFile;
|
||||
try
|
||||
{
|
||||
logFile = Directory.GetFiles(logDir, pattern, SearchOption.TopDirectoryOnly)
|
||||
.OrderByDescending(File.GetLastWriteTimeUtc)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
logger.LogDebug("Script log directory '{Dir}' not found — yielding empty stream", logDir);
|
||||
yield break;
|
||||
}
|
||||
|
||||
if (logFile is null)
|
||||
{
|
||||
logger.LogDebug("No files matching '{Pattern}' in '{Dir}' — yielding empty stream", pattern, logDir);
|
||||
yield break;
|
||||
}
|
||||
|
||||
logger.LogDebug("Tailing script log '{File}' filter='{Filter}'", logFile, scriptNameFilter);
|
||||
|
||||
// Replay seed lines from the end of the current file, then tail.
|
||||
long seekPosition;
|
||||
var seedLines = ReadTailLines(logFile, TailSeedLines, out seekPosition);
|
||||
|
||||
foreach (var line in seedLines)
|
||||
{
|
||||
if (ct.IsCancellationRequested) yield break;
|
||||
var parsed = ParseLine(line);
|
||||
if (Matches(parsed, scriptNameFilter))
|
||||
yield return parsed;
|
||||
}
|
||||
|
||||
// Live-tail: poll for new bytes appended after seekPosition.
|
||||
while (!ct.IsCancellationRequested)
|
||||
{
|
||||
try { await Task.Delay(PollInterval, ct); }
|
||||
catch (OperationCanceledException) { yield break; }
|
||||
|
||||
IReadOnlyList<string> newLines;
|
||||
try
|
||||
{
|
||||
newLines = ReadNewLines(logFile, ref seekPosition);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "Error reading script log '{File}'", logFile);
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var line in newLines)
|
||||
{
|
||||
if (ct.IsCancellationRequested) yield break;
|
||||
var parsed = ParseLine(line);
|
||||
if (Matches(parsed, scriptNameFilter))
|
||||
yield return parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
private static readonly Regex LevelPattern =
|
||||
new(@"\[(?<lvl>VRB|DBG|INF|WRN|ERR|FTL)\]", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
|
||||
|
||||
// Serilog compact text format: ScriptName property appears as ScriptName="value" in the output.
|
||||
private static readonly Regex ScriptNamePattern =
|
||||
new(@"ScriptName=""(?<name>[^""]+)""", RegexOptions.Compiled, TimeSpan.FromMilliseconds(100));
|
||||
|
||||
internal static ScriptLogLine ParseLine(string raw)
|
||||
{
|
||||
var level = "INF";
|
||||
var lvlMatch = LevelPattern.Match(raw);
|
||||
if (lvlMatch.Success) level = lvlMatch.Groups["lvl"].Value;
|
||||
|
||||
string? scriptName = null;
|
||||
var snMatch = ScriptNamePattern.Match(raw);
|
||||
if (snMatch.Success) scriptName = snMatch.Groups["name"].Value;
|
||||
|
||||
return new ScriptLogLine(raw, level, scriptName, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
internal static bool Matches(ScriptLogLine line, string? filter)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(filter)) return true;
|
||||
return line.ScriptName is not null &&
|
||||
line.ScriptName.Contains(filter, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the last <paramref name="n"/> lines from <paramref name="path"/> using a
|
||||
/// shared-read stream (so the writer doesn't need an exclusive lock). Returns the lines
|
||||
/// and outputs the final byte offset so the caller can resume from there.
|
||||
/// </summary>
|
||||
internal static List<string> ReadTailLines(string path, int n, out long endPosition)
|
||||
{
|
||||
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
endPosition = fs.Length;
|
||||
|
||||
if (fs.Length == 0) return [];
|
||||
|
||||
// Walk backwards collecting newlines until we have n+1 occurrences (n lines from end).
|
||||
const int bufferSize = 4096;
|
||||
var position = fs.Length;
|
||||
var lineBreaks = 0;
|
||||
var chunks = new List<byte[]>();
|
||||
|
||||
while (position > 0 && lineBreaks <= n)
|
||||
{
|
||||
var readSize = (int)Math.Min(bufferSize, position);
|
||||
position -= readSize;
|
||||
fs.Seek(position, SeekOrigin.Begin);
|
||||
var buf = new byte[readSize];
|
||||
_ = fs.Read(buf, 0, readSize);
|
||||
chunks.Add(buf);
|
||||
|
||||
foreach (var b in buf)
|
||||
if (b == (byte)'\n') lineBreaks++;
|
||||
|
||||
if (lineBreaks > n) break;
|
||||
}
|
||||
|
||||
// Reassemble bytes in correct order.
|
||||
chunks.Reverse();
|
||||
var allBytes = new byte[chunks.Sum(c => c.Length)];
|
||||
var offset = 0;
|
||||
foreach (var chunk in chunks) { chunk.CopyTo(allBytes, offset); offset += chunk.Length; }
|
||||
|
||||
var fullText = System.Text.Encoding.UTF8.GetString(allBytes);
|
||||
var allLines = fullText.Split('\n', StringSplitOptions.RemoveEmptyEntries)
|
||||
.Select(l => l.TrimEnd('\r'))
|
||||
.Where(l => l.Length > 0)
|
||||
.ToArray();
|
||||
return allLines.Length <= n ? allLines.ToList() : allLines[^n..].ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads any bytes appended to <paramref name="path"/> beyond <paramref name="position"/>.
|
||||
/// Updates <paramref name="position"/> to the new end of file.
|
||||
/// </summary>
|
||||
internal static List<string> ReadNewLines(string path, ref long position)
|
||||
{
|
||||
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
if (fs.Length <= position) return [];
|
||||
|
||||
fs.Seek(position, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(fs, System.Text.Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true);
|
||||
var lines = new List<string>();
|
||||
string? line;
|
||||
while ((line = reader.ReadLine()) is not null)
|
||||
lines.Add(line);
|
||||
|
||||
position = fs.Position;
|
||||
return lines;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A single parsed script log line sent to the browser.</summary>
|
||||
/// <param name="Raw">Full raw text of the line.</param>
|
||||
/// <param name="Level">Serilog short level: VRB, DBG, INF, WRN, ERR, FTL.</param>
|
||||
/// <param name="ScriptName">Value of the <c>ScriptName</c> structured property, if present.</param>
|
||||
/// <param name="ReceivedAtUtc">Wall-clock time the Admin process forwarded this line.</param>
|
||||
public sealed record ScriptLogLine(
|
||||
string Raw,
|
||||
string Level,
|
||||
string? ScriptName,
|
||||
DateTime ReceivedAtUtc);
|
||||
@@ -1,191 +0,0 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using OpenTelemetry.Metrics;
|
||||
using Serilog;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Components;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Hubs;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Host.UseSerilog((ctx, cfg) => cfg
|
||||
.MinimumLevel.Information()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.File("logs/otopcua-admin-.log", rollingInterval: RollingInterval.Day));
|
||||
|
||||
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
||||
.AddCookie(o =>
|
||||
{
|
||||
o.Cookie.Name = "OtOpcUa.Admin";
|
||||
o.LoginPath = "/login";
|
||||
o.ExpireTimeSpan = TimeSpan.FromHours(8);
|
||||
})
|
||||
// Bearer-token scheme for the SignalR hub clients. A server-side Blazor circuit cannot
|
||||
// forward the browser's HttpOnly auth cookie to the loopback hub connection, so pages
|
||||
// authenticate to the hubs with a HubTokenService token instead (see HubTokenService).
|
||||
.AddScheme<Microsoft.AspNetCore.Authentication.AuthenticationSchemeOptions, HubTokenAuthenticationHandler>(
|
||||
HubTokenDefaults.AuthenticationScheme, _ => { });
|
||||
|
||||
builder.Services.AddSingleton<HubTokenService>();
|
||||
builder.Services.AddScoped<AdminHubConnectionFactory>();
|
||||
|
||||
// Secure-by-default: the fallback policy requires an authenticated user for any
|
||||
// endpoint (and any routable page) that carries no explicit authorization metadata,
|
||||
// so a newly added page cannot accidentally ship anonymously reachable (Admin-001/002).
|
||||
// Pages/endpoints that must stay anonymous opt out with [AllowAnonymous] — the login
|
||||
// page, the /auth/* endpoints and static files all do.
|
||||
builder.Services.AddAuthorizationBuilder()
|
||||
.AddPolicy("CanEdit", p => p.RequireRole(AdminRoles.ConfigEditor, AdminRoles.FleetAdmin))
|
||||
.AddPolicy("CanPublish", p => p.RequireRole(AdminRoles.FleetAdmin))
|
||||
// SignalR hubs accept either the browser auth cookie or a HubToken bearer token, so the
|
||||
// hub endpoint authorization must run both schemes (the default fallback policy is
|
||||
// cookie-only and would 401 the token-authenticated hub clients).
|
||||
.AddPolicy("HubClients", p => p
|
||||
.AddAuthenticationSchemes(
|
||||
CookieAuthenticationDefaults.AuthenticationScheme, HubTokenDefaults.AuthenticationScheme)
|
||||
.RequireAuthenticatedUser())
|
||||
.SetFallbackPolicy(new Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder()
|
||||
.RequireAuthenticatedUser()
|
||||
.Build());
|
||||
|
||||
builder.Services.AddCascadingAuthenticationState();
|
||||
|
||||
// Admin-004: the connection string is a secret — it is NOT committed to appsettings.json.
|
||||
// Supply it via user-secrets (dev) or the ConnectionStrings__ConfigDb environment variable
|
||||
// (prod). An empty/missing value is treated as "not configured" so a misconfigured deploy
|
||||
// fails fast with a clear message rather than a downstream SqlClient parse error.
|
||||
builder.Services.AddDbContext<OtOpcUaConfigDbContext>((sp, opt) =>
|
||||
{
|
||||
var connectionString = sp.GetRequiredService<IConfiguration>().GetConnectionString("ConfigDb");
|
||||
if (string.IsNullOrWhiteSpace(connectionString))
|
||||
throw new InvalidOperationException(
|
||||
"ConnectionStrings:ConfigDb is not configured. Set it via user-secrets " +
|
||||
"(dotnet user-secrets set \"ConnectionStrings:ConfigDb\" \"...\") or the " +
|
||||
"ConnectionStrings__ConfigDb environment variable.");
|
||||
opt.UseSqlServer(connectionString);
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<ClusterService>();
|
||||
builder.Services.AddScoped<GenerationService>();
|
||||
builder.Services.AddScoped<EquipmentService>();
|
||||
builder.Services.AddScoped<TagService>();
|
||||
builder.Services.AddScoped<UnsService>();
|
||||
builder.Services.AddScoped<NamespaceService>();
|
||||
builder.Services.AddScoped<DriverInstanceService>();
|
||||
builder.Services.AddScoped<FocasDriverDetailService>();
|
||||
|
||||
// #154 — Server diagnostics client. Default base URL points at the same machine's
|
||||
// HealthEndpointsHost (loopback :4841); deployments with remote Servers set
|
||||
// "DriverDiagnostics:ServerBaseUrl" in appsettings.json.
|
||||
builder.Services.AddHttpClient<DriverDiagnosticsClient>(client =>
|
||||
{
|
||||
var baseUrl = builder.Configuration["DriverDiagnostics:ServerBaseUrl"] ?? "http://localhost:4841/";
|
||||
client.BaseAddress = new Uri(baseUrl);
|
||||
});
|
||||
builder.Services.AddScoped<NodeAclService>();
|
||||
builder.Services.AddScoped<PermissionProbeService>();
|
||||
builder.Services.AddScoped<AclChangeNotifier>();
|
||||
builder.Services.AddScoped<ReservationService>();
|
||||
builder.Services.AddScoped<DraftValidationService>();
|
||||
builder.Services.AddScoped<AuditLogService>();
|
||||
builder.Services.AddScoped<HostStatusService>();
|
||||
builder.Services.AddScoped<ClusterNodeService>();
|
||||
builder.Services.AddSingleton<RedundancyMetrics>();
|
||||
builder.Services.AddScoped<EquipmentImportBatchService>();
|
||||
// EF-backed inner service registered under the keyed-service key so the resilient
|
||||
// singleton decorator resolves it per-scope without a captive-dependency issue.
|
||||
builder.Services.AddKeyedScoped<ZB.MOM.WW.OtOpcUa.Configuration.Services.ILdapGroupRoleMappingService,
|
||||
ZB.MOM.WW.OtOpcUa.Configuration.Services.LdapGroupRoleMappingService>(
|
||||
ZB.MOM.WW.OtOpcUa.Admin.Security.ResilientLdapGroupRoleMappingService.InnerServiceKey);
|
||||
// Resilient singleton decorator: timeout 2 s → retry 3× jittered → fallback to in-memory snapshot.
|
||||
// Uses IServiceScopeFactory to open a short-lived scope for each DB call.
|
||||
// The static LdapOptions.GroupToRole bootstrap dictionary in AdminRoleGrantResolver is the
|
||||
// lock-out-proof floor; this decorator only guards the DB-backed augmentation rows.
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.Configuration.Services.ILdapGroupRoleMappingService,
|
||||
ZB.MOM.WW.OtOpcUa.Admin.Security.ResilientLdapGroupRoleMappingService>();
|
||||
|
||||
// Phase 7 Stream F — scripting + virtual tag + scripted alarm draft services, test
|
||||
// harness, and historian diagnostics. The historian sink is the Null variant here —
|
||||
// the real SqliteStoreAndForwardSink lives in the server process. Admin reads status
|
||||
// from whichever sink is provided at composition time.
|
||||
builder.Services.AddScoped<ScriptService>();
|
||||
builder.Services.AddScoped<VirtualTagService>();
|
||||
builder.Services.AddScoped<ScriptedAlarmService>();
|
||||
builder.Services.AddScoped<ScriptTestHarnessService>();
|
||||
builder.Services.AddScoped<HistorianDiagnosticsService>();
|
||||
builder.Services.AddSingleton<ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian.IAlarmHistorianSink>(
|
||||
ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian.NullAlarmHistorianSink.Instance);
|
||||
|
||||
// Cert-trust management — reads the OPC UA server's PKI store root so rejected client certs
|
||||
// can be promoted to trusted via the Admin UI. Singleton: no per-request state, just
|
||||
// filesystem operations.
|
||||
builder.Services.Configure<CertTrustOptions>(builder.Configuration.GetSection(CertTrustOptions.SectionName));
|
||||
builder.Services.AddSingleton<CertTrustService>();
|
||||
|
||||
// LDAP auth — parity with ScadaLink's LdapAuthService (decision #102).
|
||||
builder.Services.Configure<LdapOptions>(
|
||||
builder.Configuration.GetSection("Authentication:Ldap"));
|
||||
builder.Services.AddScoped<ILdapAuthService, LdapAuthService>();
|
||||
|
||||
// Resolves Admin-role grants from LDAP groups at sign-in: the static appsettings bootstrap
|
||||
// dictionary augmented by the DB-backed LdapGroupRoleMapping rows (fleet-wide + cluster-scoped).
|
||||
builder.Services.AddScoped<IAdminRoleGrantResolver, AdminRoleGrantResolver>();
|
||||
|
||||
// SignalR real-time fleet status + alerts (admin-ui.md §"Real-Time Updates").
|
||||
builder.Services.AddHostedService<FleetStatusPoller>();
|
||||
|
||||
// OpenTelemetry Prometheus exporter — Meter stream from RedundancyMetrics + any future
|
||||
// Admin-side instrumentation lands on the /metrics endpoint Prometheus scrapes. Pull-based
|
||||
// means no OTel Collector deployment required for the common deploy-in-a-K8s case; appsettings
|
||||
// Metrics:Prometheus:Enabled=false disables the endpoint entirely for locked-down deployments.
|
||||
var metricsEnabled = builder.Configuration.GetValue("Metrics:Prometheus:Enabled", true);
|
||||
if (metricsEnabled)
|
||||
{
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithMetrics(m => m
|
||||
.AddMeter(RedundancyMetrics.MeterName)
|
||||
.AddPrometheusExporter());
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseSerilogRequestLogging();
|
||||
app.UseStaticFiles();
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAntiforgery();
|
||||
|
||||
// Admin-005: login + logout are minimal-API endpoints. The login does the LDAP bind,
|
||||
// grant resolution, cookie SignInAsync and redirect while the HTTP response is still
|
||||
// owned by the endpoint — a static-rendered Login.razor form posts here.
|
||||
app.MapAuthEndpoints();
|
||||
|
||||
// Admin-003: every SignalR hub requires an authenticated caller. The [Authorize] attribute
|
||||
// on each Hub class is the primary gate; .RequireAuthorization("HubClients") on the endpoint
|
||||
// is the belt-and-braces backstop AND the scheme gate — the "HubClients" policy runs both the
|
||||
// cookie and the HubToken scheme so server-side Blazor hub clients (which cannot present the
|
||||
// browser cookie) authenticate with a bearer token instead.
|
||||
app.MapHub<FleetStatusHub>("/hubs/fleet").RequireAuthorization("HubClients");
|
||||
app.MapHub<AlertHub>("/hubs/alerts").RequireAuthorization("HubClients");
|
||||
app.MapHub<ScriptLogHub>("/hubs/script-log").RequireAuthorization("HubClients");
|
||||
|
||||
if (metricsEnabled)
|
||||
{
|
||||
// Prometheus scrape endpoint — expose instrumentation registered in the OTel MeterProvider
|
||||
// above. Emits text-format metrics at /metrics; auth is intentionally NOT required (Prometheus
|
||||
// scrape jobs typically run on a trusted network). Operators who need auth put the endpoint
|
||||
// behind a reverse-proxy basic-auth gate per fleet-ops convention.
|
||||
app.MapPrometheusScrapingEndpoint();
|
||||
}
|
||||
|
||||
app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
|
||||
|
||||
await app.RunAsync();
|
||||
|
||||
public partial class Program;
|
||||
@@ -1,48 +0,0 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Default <see cref="IAdminRoleGrantResolver"/>. Merges the static <c>appsettings.json</c>
|
||||
/// bootstrap dictionary with the DB-backed <c>LdapGroupRoleMapping</c> rows. See
|
||||
/// <see cref="AdminRoleGrants"/> for the scope split and the decision-#150 control-plane note.
|
||||
/// </summary>
|
||||
public sealed class AdminRoleGrantResolver(
|
||||
ILdapGroupRoleMappingService mappingService,
|
||||
IOptions<LdapOptions> ldapOptions) : IAdminRoleGrantResolver
|
||||
{
|
||||
private readonly LdapOptions _ldap = ldapOptions.Value;
|
||||
|
||||
public async Task<AdminRoleGrants> ResolveAsync(
|
||||
IReadOnlyList<string> ldapGroups, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ldapGroups);
|
||||
if (ldapGroups.Count == 0) return AdminRoleGrants.Empty;
|
||||
|
||||
// Static bootstrap dictionary — always fleet-wide, lock-out-proof fallback.
|
||||
var fleet = new HashSet<string>(
|
||||
RoleMapper.Map(ldapGroups, _ldap.GroupToRole), StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// DB-backed grants stack additively. A system-wide row folds into the fleet set;
|
||||
// a cluster-scoped row becomes a (cluster, role) grant, deduped on that pair.
|
||||
var mappings = await mappingService.GetByGroupsAsync(ldapGroups, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
var cluster = new Dictionary<(string, string), ClusterRoleGrant>();
|
||||
foreach (var m in mappings)
|
||||
{
|
||||
var roleName = m.Role.ToString();
|
||||
if (m.IsSystemWide || string.IsNullOrEmpty(m.ClusterId))
|
||||
{
|
||||
fleet.Add(roleName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var key = (m.ClusterId, roleName);
|
||||
cluster[key] = new ClusterRoleGrant(m.ClusterId, roleName);
|
||||
}
|
||||
}
|
||||
|
||||
return new AdminRoleGrants([.. fleet], [.. cluster.Values]);
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>A cluster-scoped Admin-role grant — the <see cref="Role"/> binds only within <see cref="ClusterId"/>.</summary>
|
||||
public sealed record ClusterRoleGrant(string ClusterId, string Role);
|
||||
|
||||
/// <summary>
|
||||
/// The Admin roles a user holds after sign-in, split by scope. <see cref="FleetRoles"/> apply
|
||||
/// across every cluster; each entry in <see cref="ClusterRoles"/> binds only within its named
|
||||
/// cluster. Resolved by <see cref="IAdminRoleGrantResolver"/> from the user's LDAP groups.
|
||||
/// </summary>
|
||||
public sealed record AdminRoleGrants(
|
||||
IReadOnlyList<string> FleetRoles,
|
||||
IReadOnlyList<ClusterRoleGrant> ClusterRoles)
|
||||
{
|
||||
/// <summary>No grants — sign-in is blocked when a resolution yields this.</summary>
|
||||
public static readonly AdminRoleGrants Empty = new([], []);
|
||||
|
||||
/// <summary>True when the user holds no Admin role at any scope.</summary>
|
||||
public bool IsEmpty => FleetRoles.Count == 0 && ClusterRoles.Count == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the Admin-role grants a set of LDAP groups confers. Augments the static
|
||||
/// <see cref="LdapOptions.GroupToRole"/> bootstrap dictionary (always fleet-wide) with the
|
||||
/// DB-backed <c>LdapGroupRoleMapping</c> rows authored on the role-grants page — fleet-wide
|
||||
/// and cluster-scoped. The static dictionary is the lock-out-proof fallback; DB grants stack
|
||||
/// additively on top of it.
|
||||
/// </summary>
|
||||
public interface IAdminRoleGrantResolver
|
||||
{
|
||||
Task<AdminRoleGrants> ResolveAsync(IReadOnlyList<string> ldapGroups, CancellationToken cancellationToken);
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Minimal-API authentication endpoints. Admin-005: the login is a static-rendered HTML
|
||||
/// form (<c>Login.razor</c>, <c>data-enhance="false"</c>) POSTing here, so the LDAP bind,
|
||||
/// grant resolution, <see cref="AuthenticationHttpContextExtensions.SignInAsync(HttpContext,
|
||||
/// string?, ClaimsPrincipal)"/> cookie write and the redirect all happen while the endpoint
|
||||
/// still owns an unstarted HTTP response. Performing <c>SignInAsync</c> from an interactive
|
||||
/// Blazor circuit (the previous implementation) could not emit the auth cookie because the
|
||||
/// original HTTP response had already completed.
|
||||
/// </summary>
|
||||
public static class AuthEndpoints
|
||||
{
|
||||
/// <summary>Maps <c>POST /auth/login</c> and <c>POST /auth/logout</c>.</summary>
|
||||
public static IEndpointRouteBuilder MapAuthEndpoints(this IEndpointRouteBuilder endpoints)
|
||||
{
|
||||
// Anonymous: the login POST is the only way in, so the fallback authorization policy
|
||||
// (Admin-001) must not gate it. DisableAntiforgery — the static Login.razor form posts
|
||||
// with data-enhance="false" and renders no antiforgery token; the cookie scheme + LDAP
|
||||
// bind are the authentication gate here. Login is not a state-changing operation that
|
||||
// CSRF can abuse (the attacker cannot know the resulting cookie), so tokenless-login is
|
||||
// the standard Web pattern.
|
||||
endpoints.MapPost("/auth/login", (Delegate)LoginAsync)
|
||||
.AllowAnonymous()
|
||||
.DisableAntiforgery();
|
||||
|
||||
// Admin-006: the logout form in MainLayout.razor emits <AntiforgeryToken />.
|
||||
// The endpoint validates the token explicitly via IAntiforgery.ValidateRequestAsync
|
||||
// (minimal API endpoints do not participate in the UseAntiforgery() pipeline by default).
|
||||
// Calling .DisableAntiforgery() suppresses the middleware pass so the manual check in
|
||||
// LogoutAsync is the single validation point — duplicating both would cause double-reads
|
||||
// of the request body.
|
||||
endpoints.MapPost("/auth/logout", (Delegate)LogoutAsync)
|
||||
.DisableAntiforgery();
|
||||
|
||||
return endpoints;
|
||||
}
|
||||
|
||||
private static async Task<IResult> LoginAsync(
|
||||
HttpContext ctx,
|
||||
[FromForm] string? username,
|
||||
[FromForm] string? password,
|
||||
[FromForm] string? returnUrl,
|
||||
ILdapAuthService ldapAuth,
|
||||
IAdminRoleGrantResolver grantResolver,
|
||||
CancellationToken ct)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
|
||||
return RedirectToLogin("Username and password are required", returnUrl);
|
||||
|
||||
var result = await ldapAuth.AuthenticateAsync(username, password, ct);
|
||||
if (!result.Success)
|
||||
return RedirectToLogin(result.Error ?? "Sign-in failed", returnUrl);
|
||||
|
||||
// Grants come from the static bootstrap dictionary + DB-backed role grants;
|
||||
// result.Roles (static-only) is intentionally not consulted here.
|
||||
var grants = await grantResolver.ResolveAsync(result.Groups, ct);
|
||||
if (grants.IsEmpty)
|
||||
return RedirectToLogin(
|
||||
"Sign-in succeeded but no Admin roles mapped for your LDAP groups. Contact your administrator.",
|
||||
returnUrl);
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.Name, result.DisplayName ?? result.Username ?? username),
|
||||
new(ClaimTypes.NameIdentifier, username),
|
||||
};
|
||||
foreach (var role in grants.FleetRoles)
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
foreach (var clusterGrant in grants.ClusterRoles)
|
||||
claims.Add(new Claim(ClusterRoleClaims.ClaimType,
|
||||
ClusterRoleClaims.Encode(clusterGrant.ClusterId, clusterGrant.Role)));
|
||||
foreach (var group in result.Groups)
|
||||
claims.Add(new Claim("ldap_group", group));
|
||||
|
||||
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
await ctx.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
|
||||
new ClaimsPrincipal(identity));
|
||||
|
||||
return Results.Redirect(SafeReturnUrl(returnUrl));
|
||||
}
|
||||
|
||||
private static async Task<IResult> LogoutAsync(HttpContext ctx, IAntiforgery antiforgery)
|
||||
{
|
||||
try
|
||||
{
|
||||
await antiforgery.ValidateRequestAsync(ctx);
|
||||
}
|
||||
catch (AntiforgeryValidationException)
|
||||
{
|
||||
return Results.BadRequest("Invalid or missing antiforgery token.");
|
||||
}
|
||||
|
||||
await ctx.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
||||
return Results.Redirect("/login");
|
||||
}
|
||||
|
||||
private static IResult RedirectToLogin(string error, string? returnUrl)
|
||||
{
|
||||
var target = $"/login?error={Uri.EscapeDataString(error)}";
|
||||
if (!string.IsNullOrWhiteSpace(returnUrl) && IsLocalUrl(returnUrl))
|
||||
target += $"&returnUrl={Uri.EscapeDataString(returnUrl)}";
|
||||
return Results.Redirect(target);
|
||||
}
|
||||
|
||||
/// <summary>Open-redirect guard — only same-site relative paths are honoured.</summary>
|
||||
private static string SafeReturnUrl(string? returnUrl) =>
|
||||
!string.IsNullOrWhiteSpace(returnUrl) && IsLocalUrl(returnUrl) ? returnUrl : "/";
|
||||
|
||||
private static bool IsLocalUrl(string url) =>
|
||||
url.StartsWith('/') && !url.StartsWith("//") && !url.StartsWith("/\\");
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Encoding for the cluster-scoped role claim. A fleet-wide grant is a standard
|
||||
/// <see cref="ClaimTypes.Role"/> claim (so the existing <c>CanEdit</c>/<c>CanPublish</c>
|
||||
/// policies keep working); a cluster-scoped grant is a <see cref="ClaimType"/> claim whose
|
||||
/// value packs the cluster id and role together. A cluster-scoped role deliberately does NOT
|
||||
/// satisfy a fleet-wide <c>RequireRole</c> policy.
|
||||
/// </summary>
|
||||
public static class ClusterRoleClaims
|
||||
{
|
||||
/// <summary>Claim type carrying one cluster-scoped role grant.</summary>
|
||||
public const string ClaimType = "cluster_role";
|
||||
|
||||
// Unit separator (U+001F) — cannot occur in a cluster id or an AdminRole name.
|
||||
private const char Separator = '';
|
||||
|
||||
/// <summary>Pack a (cluster, role) pair into a claim value.</summary>
|
||||
public static string Encode(string clusterId, string role) => $"{clusterId}{Separator}{role}";
|
||||
|
||||
/// <summary>Unpack a claim value; null when the value is malformed.</summary>
|
||||
public static (string ClusterId, string Role)? Decode(string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) return null;
|
||||
var i = value.IndexOf(Separator);
|
||||
return i <= 0 || i == value.Length - 1
|
||||
? null
|
||||
: (value[..i], value[(i + 1)..]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="ClaimsPrincipal"/> helpers for cluster-scoped authorization. The effective role
|
||||
/// for a cluster is the highest of the user's fleet-wide roles and any cluster-scoped grant
|
||||
/// for that cluster.
|
||||
/// </summary>
|
||||
public static class ClaimsPrincipalClusterExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Highest <see cref="AdminRole"/> the user holds for <paramref name="clusterId"/>,
|
||||
/// combining fleet-wide and cluster-scoped grants; null when the user holds none.
|
||||
/// </summary>
|
||||
public static AdminRole? EffectiveClusterRole(this ClaimsPrincipal user, string clusterId)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
AdminRole? best = null;
|
||||
|
||||
foreach (var c in user.FindAll(ClaimTypes.Role))
|
||||
if (Enum.TryParse<AdminRole>(c.Value, out var role))
|
||||
best = Higher(best, role);
|
||||
|
||||
foreach (var c in user.FindAll(ClusterRoleClaims.ClaimType))
|
||||
{
|
||||
if (ClusterRoleClaims.Decode(c.Value) is not { } grant) continue;
|
||||
if (!string.Equals(grant.ClusterId, clusterId, StringComparison.OrdinalIgnoreCase)) continue;
|
||||
if (Enum.TryParse<AdminRole>(grant.Role, out var role))
|
||||
best = Higher(best, role);
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>True when the user's effective role for the cluster is at least <paramref name="minRole"/>.</summary>
|
||||
public static bool HasClusterRole(this ClaimsPrincipal user, string clusterId, AdminRole minRole)
|
||||
=> user.EffectiveClusterRole(clusterId) is { } role && role >= minRole;
|
||||
|
||||
// AdminRole ordinals ascend ConfigViewer < ConfigEditor < FleetAdmin, so >= is the hierarchy.
|
||||
private static AdminRole Higher(AdminRole? current, AdminRole candidate)
|
||||
=> current is { } c && c >= candidate ? c : candidate;
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>Scheme name for the SignalR hub bearer-token authentication.</summary>
|
||||
public static class HubTokenDefaults
|
||||
{
|
||||
public const string AuthenticationScheme = "HubToken";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Authenticates SignalR hub requests that carry a <see cref="HubTokenService"/>-minted
|
||||
/// token. SignalR supplies the token as an <c>Authorization: Bearer</c> header on the
|
||||
/// negotiate / long-poll requests and as an <c>access_token</c> query-string parameter on
|
||||
/// the WebSocket upgrade (custom headers cannot be set on a browser WebSocket handshake) —
|
||||
/// this handler accepts either.
|
||||
/// </summary>
|
||||
public sealed class HubTokenAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
|
||||
{
|
||||
private readonly HubTokenService _tokens;
|
||||
|
||||
public HubTokenAuthenticationHandler(
|
||||
IOptionsMonitor<AuthenticationSchemeOptions> options,
|
||||
ILoggerFactory logger,
|
||||
UrlEncoder encoder,
|
||||
HubTokenService tokens)
|
||||
: base(options, logger, encoder)
|
||||
{
|
||||
_tokens = tokens;
|
||||
}
|
||||
|
||||
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
var token = ExtractToken();
|
||||
if (string.IsNullOrEmpty(token))
|
||||
return Task.FromResult(AuthenticateResult.NoResult());
|
||||
|
||||
var principal = _tokens.Validate(token);
|
||||
if (principal is null)
|
||||
return Task.FromResult(AuthenticateResult.Fail("Invalid or expired hub token."));
|
||||
|
||||
return Task.FromResult(AuthenticateResult.Success(
|
||||
new AuthenticationTicket(principal, Scheme.Name)));
|
||||
}
|
||||
|
||||
private string? ExtractToken()
|
||||
{
|
||||
var header = Request.Headers.Authorization.ToString();
|
||||
if (header.StartsWith("Bearer ", StringComparison.OrdinalIgnoreCase))
|
||||
return header["Bearer ".Length..].Trim();
|
||||
|
||||
return Request.Query.TryGetValue("access_token", out var queryToken)
|
||||
? queryToken.ToString()
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using System.Text.Json;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Mints and validates short-lived bearer tokens that let the Admin UI's server-side
|
||||
/// Blazor <see cref="Microsoft.AspNetCore.SignalR.Client.HubConnection"/> clients
|
||||
/// authenticate to the <c>[Authorize]</c>-gated SignalR hubs.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Admin-003 fix gated every hub with authorization, but a server-side Blazor circuit
|
||||
/// cannot forward the browser's HttpOnly auth cookie to the loopback hub connection — so the
|
||||
/// hub negotiate would 401. Instead, a component mints a token here for its already-
|
||||
/// authenticated user and supplies it through <c>HttpConnectionOptions.AccessTokenProvider</c>;
|
||||
/// <see cref="HubTokenAuthenticationHandler"/> validates it on the hub endpoint.
|
||||
/// <para>
|
||||
/// The token is an ASP.NET Core Data Protection time-limited payload — the same
|
||||
/// primitive that already protects the auth cookie — so there is no signing-key
|
||||
/// management and no extra package. It is only ever presented loopback (the Admin
|
||||
/// server connecting to its own hub).
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public sealed class HubTokenService
|
||||
{
|
||||
private const string ProtectorPurpose = "ZB.MOM.WW.OtOpcUa.Admin.HubToken.v1";
|
||||
private static readonly TimeSpan TokenLifetime = TimeSpan.FromMinutes(30);
|
||||
|
||||
private readonly ITimeLimitedDataProtector _protector;
|
||||
|
||||
public HubTokenService(IDataProtectionProvider dataProtection)
|
||||
{
|
||||
_protector = dataProtection.CreateProtector(ProtectorPurpose).ToTimeLimitedDataProtector();
|
||||
}
|
||||
|
||||
/// <summary>Mints a token carrying the user's name and roles, valid for 30 minutes.</summary>
|
||||
public string Issue(ClaimsPrincipal user)
|
||||
{
|
||||
var payload = new HubTokenPayload(
|
||||
user.Identity?.Name,
|
||||
user.FindFirstValue(ClaimTypes.NameIdentifier),
|
||||
user.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray());
|
||||
return _protector.Protect(JsonSerializer.Serialize(payload), TokenLifetime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates a token and rebuilds the <see cref="ClaimsPrincipal"/>. Returns
|
||||
/// <c>null</c> when the token is missing, tampered with, or expired.
|
||||
/// </summary>
|
||||
public ClaimsPrincipal? Validate(string? token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token)) return null;
|
||||
|
||||
try
|
||||
{
|
||||
var payload = JsonSerializer.Deserialize<HubTokenPayload>(_protector.Unprotect(token));
|
||||
if (payload is null) return null;
|
||||
|
||||
var claims = new List<Claim>();
|
||||
if (!string.IsNullOrEmpty(payload.Name))
|
||||
claims.Add(new Claim(ClaimTypes.Name, payload.Name));
|
||||
if (!string.IsNullOrEmpty(payload.NameIdentifier))
|
||||
claims.Add(new Claim(ClaimTypes.NameIdentifier, payload.NameIdentifier));
|
||||
claims.AddRange((payload.Roles ?? []).Select(r => new Claim(ClaimTypes.Role, r)));
|
||||
|
||||
var identity = new ClaimsIdentity(
|
||||
claims, HubTokenDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
|
||||
return new ClaimsPrincipal(identity);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Crypto failure (tampered / expired / wrong key) or malformed JSON — unauthenticated.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record HubTokenPayload(string? Name, string? NameIdentifier, string[]? Roles);
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Polly;
|
||||
using Polly.Retry;
|
||||
using Polly.Timeout;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Services;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Resilience decorator for <see cref="ILdapGroupRoleMappingService"/> that wraps the
|
||||
/// hot-path <see cref="GetByGroupsAsync"/> call in the Phase 6.1-style pipeline:
|
||||
/// <b>timeout 2 s → retry 3× jittered → fallback to in-memory sealed snapshot</b>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>Registered as a singleton so the in-memory snapshot survives across sign-in
|
||||
/// requests. The inner <see cref="ILdapGroupRoleMappingService"/> is resolved via the
|
||||
/// keyed-service key <c>"inner"</c>, allowing the EF-backed scoped service to be
|
||||
/// registered as the "inner" implementation while this singleton decorator is the primary
|
||||
/// <see cref="ILdapGroupRoleMappingService"/> binding.</para>
|
||||
///
|
||||
/// <para>Because the inner service is scoped (it owns an EF <c>DbContext</c>), this
|
||||
/// singleton uses <see cref="IServiceScopeFactory"/> to open a short-lived scope for
|
||||
/// each DB call. The scope is disposed immediately after the call completes.</para>
|
||||
///
|
||||
/// <para>On each successful <see cref="GetByGroupsAsync"/> the result is stored in a
|
||||
/// <see cref="ConcurrentDictionary{TKey,TValue}"/> keyed by the canonicalised group set. On
|
||||
/// any failure (DB unreachable, SQL exception, timeout) after all retries, the cached
|
||||
/// result for that exact group set is returned. When no prior success exists for the group
|
||||
/// set, an empty list is returned — the static <see cref="LdapOptions.GroupToRole"/>
|
||||
/// bootstrap dictionary in <see cref="AdminRoleGrantResolver"/> is the lock-out-proof
|
||||
/// floor that remains functional regardless of DB state.</para>
|
||||
///
|
||||
/// <para>Write methods (<see cref="CreateAsync"/>, <see cref="DeleteAsync"/>) and
|
||||
/// <see cref="ListAllAsync"/> are passed through unchanged — the resilience layer is
|
||||
/// read-path only, consistent with the Phase 6.1 design decision that writes must fail
|
||||
/// hard on DB outage rather than landing against a stale state.</para>
|
||||
/// </remarks>
|
||||
public sealed class ResilientLdapGroupRoleMappingService : ILdapGroupRoleMappingService
|
||||
{
|
||||
/// <summary>
|
||||
/// DI keyed-service key used to register the inner (EF-backed) implementation so the
|
||||
/// decorator can resolve it without creating a circular dependency on itself.
|
||||
/// </summary>
|
||||
public const string InnerServiceKey = "LdapGroupRoleMappingService.Inner";
|
||||
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ResiliencePipeline _pipeline;
|
||||
private readonly ILogger<ResilientLdapGroupRoleMappingService> _logger;
|
||||
|
||||
// Keyed by the normalised group set (NUL-separated sorted group names, lower-case).
|
||||
private readonly ConcurrentDictionary<string, IReadOnlyList<LdapGroupRoleMapping>> _snapshot =
|
||||
new(StringComparer.Ordinal);
|
||||
|
||||
public ResilientLdapGroupRoleMappingService(
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ILogger<ResilientLdapGroupRoleMappingService> logger,
|
||||
TimeSpan? timeout = null,
|
||||
int retryCount = 3)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_logger = logger;
|
||||
|
||||
var builder = new ResiliencePipelineBuilder()
|
||||
.AddTimeout(new TimeoutStrategyOptions
|
||||
{
|
||||
Timeout = timeout ?? TimeSpan.FromSeconds(2),
|
||||
});
|
||||
|
||||
if (retryCount > 0)
|
||||
{
|
||||
builder.AddRetry(new RetryStrategyOptions
|
||||
{
|
||||
MaxRetryAttempts = retryCount,
|
||||
BackoffType = DelayBackoffType.Exponential,
|
||||
UseJitter = true,
|
||||
Delay = TimeSpan.FromMilliseconds(100),
|
||||
MaxDelay = TimeSpan.FromSeconds(1),
|
||||
ShouldHandle = new PredicateBuilder().Handle<Exception>(
|
||||
ex => ex is not OperationCanceledException),
|
||||
});
|
||||
}
|
||||
|
||||
_pipeline = builder.Build();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>
|
||||
/// Executed through the timeout → retry pipeline. On full failure the last snapshot
|
||||
/// for this group set (if any) is returned; otherwise an empty list. The static
|
||||
/// <c>appsettings.json</c> bootstrap dictionary in <see cref="AdminRoleGrantResolver"/>
|
||||
/// remains the ultimate fallback — a DB outage never causes a total login denial.
|
||||
/// </remarks>
|
||||
public async Task<IReadOnlyList<LdapGroupRoleMapping>> GetByGroupsAsync(
|
||||
IEnumerable<string> ldapGroups, CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(ldapGroups);
|
||||
|
||||
var groupList = ldapGroups.ToList();
|
||||
if (groupList.Count == 0) return [];
|
||||
|
||||
var cacheKey = CacheKey(groupList);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _pipeline.ExecuteAsync(async ct =>
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var inner = (ILdapGroupRoleMappingService)scope.ServiceProvider
|
||||
.GetRequiredKeyedService<ILdapGroupRoleMappingService>(InnerServiceKey);
|
||||
return await inner.GetByGroupsAsync(groupList, ct).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// Seal the snapshot so a subsequent DB outage can fall back to it.
|
||||
_snapshot[cacheKey] = result;
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
_logger.LogWarning(ex,
|
||||
"LDAP role-mapping DB read failed after retries; falling back to snapshot for group set [{Groups}]",
|
||||
string.Join(", ", groupList));
|
||||
|
||||
return _snapshot.TryGetValue(cacheKey, out var cached)
|
||||
? cached
|
||||
: [];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>Pass-through — not covered by the resilience pipeline (Admin UI listing only).</remarks>
|
||||
public async Task<IReadOnlyList<LdapGroupRoleMapping>> ListAllAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var inner = (ILdapGroupRoleMappingService)scope.ServiceProvider
|
||||
.GetRequiredKeyedService<ILdapGroupRoleMappingService>(InnerServiceKey);
|
||||
return await inner.ListAllAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>Pass-through — writes must fail hard on DB outage per Phase 6.1 design decision.</remarks>
|
||||
public async Task<LdapGroupRoleMapping> CreateAsync(LdapGroupRoleMapping row, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var inner = (ILdapGroupRoleMappingService)scope.ServiceProvider
|
||||
.GetRequiredKeyedService<ILdapGroupRoleMappingService>(InnerServiceKey);
|
||||
return await inner.CreateAsync(row, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>Pass-through — writes must fail hard on DB outage per Phase 6.1 design decision.</remarks>
|
||||
public async Task DeleteAsync(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
await using var scope = _scopeFactory.CreateAsyncScope();
|
||||
var inner = (ILdapGroupRoleMappingService)scope.ServiceProvider
|
||||
.GetRequiredKeyedService<ILdapGroupRoleMappingService>(InnerServiceKey);
|
||||
await inner.DeleteAsync(id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
// ── helpers ────────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Canonicalise a group set into a stable cache key: sort, lower-case, join with NUL.
|
||||
/// Two calls with the same groups in different orders produce the same key.
|
||||
/// </summary>
|
||||
internal static string CacheKey(IEnumerable<string> groups)
|
||||
=> string.Join('\0', groups
|
||||
.Select(g => g.ToLowerInvariant())
|
||||
.Order(StringComparer.Ordinal));
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Hubs;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Thin SignalR push helper for ACL + role-grant invalidation — slice 2 of task #196.
|
||||
/// Lets the Admin services + razor pages invalidate connected peers' views without each
|
||||
/// one having to know the hub wiring. Two message kinds: <c>NodeAclChanged</c> (cluster-scoped)
|
||||
/// and <c>RoleGrantsChanged</c> (fleet-wide — role mappings cross cluster boundaries).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Intentionally fire-and-forget — a failed hub send doesn't rollback the DB write that
|
||||
/// triggered it. Worst-case an operator sees stale data until their next poll or manual
|
||||
/// refresh; better than a transient hub blip blocking the authoritative write path.
|
||||
/// </remarks>
|
||||
public sealed class AclChangeNotifier(IHubContext<FleetStatusHub> fleetHub, ILogger<AclChangeNotifier> logger)
|
||||
{
|
||||
public async Task NotifyNodeAclChangedAsync(string clusterId, long generationId, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var msg = new NodeAclChangedMessage(ClusterId: clusterId, GenerationId: generationId, ObservedAtUtc: DateTime.UtcNow);
|
||||
await fleetHub.Clients.Group(FleetStatusHub.GroupName(clusterId))
|
||||
.SendAsync("NodeAclChanged", msg, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "NodeAclChanged push failed for cluster {ClusterId} gen {GenerationId}", clusterId, generationId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyRoleGrantsChangedAsync(CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var msg = new RoleGrantsChangedMessage(ObservedAtUtc: DateTime.UtcNow);
|
||||
await fleetHub.Clients.Group(FleetStatusHub.FleetGroup)
|
||||
.SendAsync("RoleGrantsChanged", msg, ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
logger.LogWarning(ex, "RoleGrantsChanged push failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record NodeAclChangedMessage(string ClusterId, long GenerationId, DateTime ObservedAtUtc);
|
||||
public sealed record RoleGrantsChangedMessage(DateTime ObservedAtUtc);
|
||||
@@ -1,55 +0,0 @@
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Security;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Builds <see cref="HubConnection"/>s to the Admin UI's own SignalR hubs with bearer-token
|
||||
/// authentication.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The hubs are <c>[Authorize]</c>-gated (Admin-003). A server-side Blazor circuit cannot
|
||||
/// forward the browser's HttpOnly auth cookie to the loopback hub connection, so every page
|
||||
/// that needs live updates resolves this factory and lets it wire an
|
||||
/// <c>AccessTokenProvider</c> that mints a <see cref="HubTokenService"/> token for the
|
||||
/// circuit's authenticated user. The provider re-mints on every (re)connect so a long-lived
|
||||
/// page outlives the token lifetime.
|
||||
/// </remarks>
|
||||
public sealed class AdminHubConnectionFactory
|
||||
{
|
||||
private readonly NavigationManager _nav;
|
||||
private readonly HubTokenService _tokens;
|
||||
private readonly AuthenticationStateProvider _authState;
|
||||
|
||||
public AdminHubConnectionFactory(
|
||||
NavigationManager nav,
|
||||
HubTokenService tokens,
|
||||
AuthenticationStateProvider authState)
|
||||
{
|
||||
_nav = nav;
|
||||
_tokens = tokens;
|
||||
_authState = authState;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates an auto-reconnecting connection to <paramref name="hubPath"/>
|
||||
/// (e.g. <c>/hubs/fleet</c>), authenticated as the current circuit's user.
|
||||
/// </summary>
|
||||
public HubConnection Create(string hubPath)
|
||||
{
|
||||
var hubUrl = _nav.ToAbsoluteUri(hubPath);
|
||||
return new HubConnectionBuilder()
|
||||
.WithUrl(hubUrl, options =>
|
||||
{
|
||||
options.AccessTokenProvider = async () =>
|
||||
{
|
||||
var state = await _authState.GetAuthenticationStateAsync();
|
||||
return _tokens.Issue(state.User);
|
||||
};
|
||||
})
|
||||
.WithAutomaticReconnect()
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The three admin roles per <c>admin-ui.md</c> §"Admin Roles" — mapped from LDAP groups at
|
||||
/// sign-in. Each role has a fixed set of capabilities (cluster CRUD, draft → publish, fleet
|
||||
/// admin). The ACL-driven runtime permissions (<c>NodePermissions</c>) govern OPC UA clients;
|
||||
/// these roles govern the Admin UI itself.
|
||||
/// </summary>
|
||||
public static class AdminRoles
|
||||
{
|
||||
public const string ConfigViewer = "ConfigViewer";
|
||||
public const string ConfigEditor = "ConfigEditor";
|
||||
public const string FleetAdmin = "FleetAdmin";
|
||||
|
||||
public static IReadOnlyList<string> All => [ConfigViewer, ConfigEditor, FleetAdmin];
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
public sealed class AuditLogService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<ConfigAuditLog>> ListRecentAsync(string? clusterId, int limit, CancellationToken ct)
|
||||
{
|
||||
var q = db.ConfigAuditLogs.AsNoTracking();
|
||||
if (clusterId is not null) q = q.Where(a => a.ClusterId == clusterId);
|
||||
return q.OrderByDescending(a => a.Timestamp).Take(limit).ToListAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Points the Admin UI at the OPC UA Server's PKI store root so
|
||||
/// <see cref="CertTrustService"/> can list and move certs between the
|
||||
/// <c>rejected/</c> and <c>trusted/</c> directories the server maintains. Must match the
|
||||
/// <c>OpcUaServer:PkiStoreRoot</c> the Server process is configured with.
|
||||
/// </summary>
|
||||
public sealed class CertTrustOptions
|
||||
{
|
||||
public const string SectionName = "CertTrust";
|
||||
|
||||
/// <summary>
|
||||
/// Absolute path to the PKI root. Defaults to
|
||||
/// <c>%ProgramData%\OtOpcUa\pki</c> — matches <c>OpcUaServerOptions.PkiStoreRoot</c>'s
|
||||
/// default so a standard side-by-side install needs no override.
|
||||
/// </summary>
|
||||
public string PkiStoreRoot { get; init; } =
|
||||
Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
|
||||
"OtOpcUa", "pki");
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Metadata for a certificate file found in one of the OPC UA server's PKI stores. The
|
||||
/// <see cref="FilePath"/> is the absolute path of the DER/CRT file the stack created when it
|
||||
/// rejected the cert (for <see cref="CertStoreKind.Rejected"/>) or when an operator trusted
|
||||
/// it (for <see cref="CertStoreKind.Trusted"/>).
|
||||
/// </summary>
|
||||
public sealed record CertInfo(
|
||||
string Thumbprint,
|
||||
string Subject,
|
||||
string Issuer,
|
||||
DateTime NotBefore,
|
||||
DateTime NotAfter,
|
||||
string FilePath,
|
||||
CertStoreKind Store);
|
||||
|
||||
public enum CertStoreKind
|
||||
{
|
||||
Rejected,
|
||||
Trusted,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filesystem-backed view over the OPC UA server's PKI store. The Opc.Ua stack uses a
|
||||
/// Directory-typed store — each cert is a <c>.der</c> file under <c>{root}/{store}/certs/</c>
|
||||
/// with a filename derived from subject + thumbprint. This service exposes operators for the
|
||||
/// Admin UI: list rejected, list trusted, trust a rejected cert (move to trusted), remove a
|
||||
/// rejected cert (delete), untrust a previously trusted cert (delete from trusted).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The Admin process is separate from the Server process; this service deliberately has no
|
||||
/// Opc.Ua dependency — it works on the on-disk layout directly so it can run on the Admin
|
||||
/// host even when the Server isn't installed locally, as long as the PKI root is reachable
|
||||
/// (typical deployment has Admin + Server side-by-side on the same machine).
|
||||
///
|
||||
/// Trust/untrust requires the Server to re-read its trust list. The Opc.Ua stack re-reads
|
||||
/// the Directory store on each new incoming connection, so there's no explicit signal
|
||||
/// needed — the next client handshake picks up the change. Operators should retry the
|
||||
/// rejected client's connection after trusting.
|
||||
/// </remarks>
|
||||
public sealed class CertTrustService
|
||||
{
|
||||
private readonly CertTrustOptions _options;
|
||||
private readonly ILogger<CertTrustService> _logger;
|
||||
|
||||
public CertTrustService(IOptions<CertTrustOptions> options, ILogger<CertTrustService> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string PkiStoreRoot => _options.PkiStoreRoot;
|
||||
|
||||
public IReadOnlyList<CertInfo> ListRejected() => ListStore(CertStoreKind.Rejected);
|
||||
public IReadOnlyList<CertInfo> ListTrusted() => ListStore(CertStoreKind.Trusted);
|
||||
|
||||
/// <summary>
|
||||
/// Move the cert with <paramref name="thumbprint"/> from the rejected store to the
|
||||
/// trusted store. No-op returns false if the rejected file doesn't exist (already moved
|
||||
/// by another operator, or thumbprint mismatch). Overwrites an existing trusted copy
|
||||
/// silently — idempotent.
|
||||
/// </summary>
|
||||
public bool TrustRejected(string thumbprint)
|
||||
{
|
||||
var cert = FindInStore(CertStoreKind.Rejected, thumbprint);
|
||||
if (cert is null) return false;
|
||||
|
||||
var trustedDir = CertsDir(CertStoreKind.Trusted);
|
||||
Directory.CreateDirectory(trustedDir);
|
||||
var destPath = Path.Combine(trustedDir, Path.GetFileName(cert.FilePath));
|
||||
File.Move(cert.FilePath, destPath, overwrite: true);
|
||||
_logger.LogInformation("Trusted cert {Thumbprint} (subject={Subject}) — moved {From} → {To}",
|
||||
cert.Thumbprint, cert.Subject, cert.FilePath, destPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DeleteRejected(string thumbprint) => DeleteFromStore(CertStoreKind.Rejected, thumbprint);
|
||||
public bool UntrustCert(string thumbprint) => DeleteFromStore(CertStoreKind.Trusted, thumbprint);
|
||||
|
||||
private bool DeleteFromStore(CertStoreKind store, string thumbprint)
|
||||
{
|
||||
var cert = FindInStore(store, thumbprint);
|
||||
if (cert is null) return false;
|
||||
File.Delete(cert.FilePath);
|
||||
_logger.LogInformation("Deleted cert {Thumbprint} (subject={Subject}) from {Store} store",
|
||||
cert.Thumbprint, cert.Subject, store);
|
||||
return true;
|
||||
}
|
||||
|
||||
private CertInfo? FindInStore(CertStoreKind store, string thumbprint) =>
|
||||
ListStore(store).FirstOrDefault(c =>
|
||||
string.Equals(c.Thumbprint, thumbprint, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
private IReadOnlyList<CertInfo> ListStore(CertStoreKind store)
|
||||
{
|
||||
var dir = CertsDir(store);
|
||||
if (!Directory.Exists(dir)) return [];
|
||||
|
||||
var results = new List<CertInfo>();
|
||||
foreach (var path in Directory.EnumerateFiles(dir))
|
||||
{
|
||||
// Skip CRL sidecars + private-key files — trust operations only concern public certs.
|
||||
var ext = Path.GetExtension(path);
|
||||
if (!ext.Equals(".der", StringComparison.OrdinalIgnoreCase) &&
|
||||
!ext.Equals(".crt", StringComparison.OrdinalIgnoreCase) &&
|
||||
!ext.Equals(".cer", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var cert = X509CertificateLoader.LoadCertificateFromFile(path);
|
||||
results.Add(new CertInfo(
|
||||
cert.Thumbprint, cert.Subject, cert.Issuer,
|
||||
cert.NotBefore.ToUniversalTime(), cert.NotAfter.ToUniversalTime(),
|
||||
path, store));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// A malformed file in the store shouldn't take down the page. Surface it in logs
|
||||
// but skip — operators see the other certs and can clean the bad file manually.
|
||||
_logger.LogWarning(ex, "Failed to parse cert at {Path} — skipping", path);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private string CertsDir(CertStoreKind store) =>
|
||||
Path.Combine(_options.PkiStoreRoot, store == CertStoreKind.Rejected ? "rejected" : "trusted", "certs");
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Read-side service for ClusterNode rows + their cluster-scoped redundancy view. Consumed
|
||||
/// by the RedundancyTab on the cluster detail page. Writes (role swap, node enable/disable)
|
||||
/// are not supported here — role swap happens through the RedundancyCoordinator apply-lease
|
||||
/// flow on the server side and would conflict with any direct DB mutation from Admin.
|
||||
/// </summary>
|
||||
public sealed class ClusterNodeService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
/// <summary>Stale-threshold matching <c>HostStatusService.StaleThreshold</c> — 30s of clock
|
||||
/// tolerance covers a missed heartbeat plus publisher GC pauses.</summary>
|
||||
public static readonly TimeSpan StaleThreshold = TimeSpan.FromSeconds(30);
|
||||
|
||||
public Task<List<ClusterNode>> ListByClusterAsync(string clusterId, CancellationToken ct) =>
|
||||
db.ClusterNodes.AsNoTracking()
|
||||
.Where(n => n.ClusterId == clusterId)
|
||||
.OrderByDescending(n => n.ServiceLevelBase)
|
||||
.ThenBy(n => n.NodeId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public static bool IsStale(ClusterNode node) =>
|
||||
node.LastSeenAt is null || DateTime.UtcNow - node.LastSeenAt.Value > StaleThreshold;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Cluster CRUD surface used by the Blazor pages. Writes go through stored procs in later
|
||||
/// phases; Phase 1 reads via EF Core directly (DENY SELECT on <c>dbo</c> schema means this
|
||||
/// service connects as a DB owner during dev — production swaps in a read-only view grant).
|
||||
/// </summary>
|
||||
public sealed class ClusterService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<ServerCluster>> ListAsync(CancellationToken ct) =>
|
||||
db.ServerClusters.AsNoTracking().OrderBy(c => c.ClusterId).ToListAsync(ct);
|
||||
|
||||
public Task<ServerCluster?> FindAsync(string clusterId, CancellationToken ct) =>
|
||||
db.ServerClusters.AsNoTracking().FirstOrDefaultAsync(c => c.ClusterId == clusterId, ct);
|
||||
|
||||
public async Task<ServerCluster> CreateAsync(ServerCluster cluster, string createdBy, CancellationToken ct)
|
||||
{
|
||||
cluster.CreatedAt = DateTime.UtcNow;
|
||||
cluster.CreatedBy = createdBy;
|
||||
db.ServerClusters.Add(cluster);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return cluster;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the managed <see cref="DraftValidator"/> against a draft's snapshot loaded from the
|
||||
/// Configuration DB. Used by the draft editor's inline validation panel and by the publish
|
||||
/// dialog's pre-check. Structural-only SQL checks live in <c>sp_ValidateDraft</c>; this layer
|
||||
/// owns the content / cross-generation / regex rules.
|
||||
/// </summary>
|
||||
public sealed class DraftValidationService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public async Task<IReadOnlyList<ValidationError>> ValidateAsync(long draftId, CancellationToken ct)
|
||||
{
|
||||
var draft = await db.ConfigGenerations.AsNoTracking()
|
||||
.FirstOrDefaultAsync(g => g.GenerationId == draftId, ct)
|
||||
?? throw new InvalidOperationException($"Draft {draftId} not found");
|
||||
|
||||
// Load the cluster row so path-length validation uses actual Enterprise/Site lengths.
|
||||
var cluster = await db.ServerClusters.AsNoTracking()
|
||||
.FirstOrDefaultAsync(c => c.ClusterId == draft.ClusterId, ct);
|
||||
|
||||
var snapshot = new DraftSnapshot
|
||||
{
|
||||
GenerationId = draft.GenerationId,
|
||||
ClusterId = draft.ClusterId,
|
||||
Enterprise = cluster?.Enterprise,
|
||||
Site = cluster?.Site,
|
||||
Namespaces = await db.Namespaces.AsNoTracking().Where(n => n.GenerationId == draftId).ToListAsync(ct),
|
||||
DriverInstances = await db.DriverInstances.AsNoTracking().Where(d => d.GenerationId == draftId).ToListAsync(ct),
|
||||
Devices = await db.Devices.AsNoTracking().Where(d => d.GenerationId == draftId).ToListAsync(ct),
|
||||
UnsAreas = await db.UnsAreas.AsNoTracking().Where(a => a.GenerationId == draftId).ToListAsync(ct),
|
||||
UnsLines = await db.UnsLines.AsNoTracking().Where(l => l.GenerationId == draftId).ToListAsync(ct),
|
||||
Equipment = await db.Equipment.AsNoTracking().Where(e => e.GenerationId == draftId).ToListAsync(ct),
|
||||
Tags = await db.Tags.AsNoTracking().Where(t => t.GenerationId == draftId).ToListAsync(ct),
|
||||
PollGroups = await db.PollGroups.AsNoTracking().Where(p => p.GenerationId == draftId).ToListAsync(ct),
|
||||
|
||||
PriorEquipment = await db.Equipment.AsNoTracking()
|
||||
.Where(e => e.GenerationId != draftId
|
||||
&& db.ConfigGenerations.Any(g => g.GenerationId == e.GenerationId && g.ClusterId == draft.ClusterId))
|
||||
.ToListAsync(ct),
|
||||
ActiveReservations = await db.ExternalIdReservations.AsNoTracking()
|
||||
.Where(r => r.ReleasedAt == null)
|
||||
.ToListAsync(ct),
|
||||
};
|
||||
|
||||
return DraftValidator.Validate(snapshot);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// #154 — Admin-side client for the Server's driver-diagnostics HTTP endpoints. Wraps
|
||||
/// <see cref="HttpClient"/> so Blazor pages can fetch per-driver runtime state from a
|
||||
/// remote Server process. The base URL is configured at registration time
|
||||
/// (typically read from <c>appsettings.json</c> at startup).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// One client instance per Server endpoint. Multi-server deployments register multiple
|
||||
/// keyed clients. Errors propagate as exceptions; pages catch and surface to the
|
||||
/// operator rather than swallowing.
|
||||
/// </remarks>
|
||||
public sealed class DriverDiagnosticsClient
|
||||
{
|
||||
private readonly HttpClient _http;
|
||||
|
||||
public DriverDiagnosticsClient(HttpClient http) => _http = http;
|
||||
|
||||
/// <summary>
|
||||
/// Fetch the current Modbus auto-prohibition list for the named driver instance.
|
||||
/// Returns null when the Server reports the driver doesn't exist or isn't a Modbus
|
||||
/// driver. Throws on transport / serialization failures.
|
||||
/// </summary>
|
||||
public async Task<ModbusAutoProhibitionsResponse?> GetModbusAutoProhibitedRangesAsync(
|
||||
string driverInstanceId, CancellationToken ct = default)
|
||||
{
|
||||
var resp = await _http.GetAsync(
|
||||
$"/diagnostics/drivers/{Uri.EscapeDataString(driverInstanceId)}/modbus/auto-prohibited", ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (resp.StatusCode is System.Net.HttpStatusCode.NotFound or System.Net.HttpStatusCode.BadRequest)
|
||||
return null;
|
||||
|
||||
resp.EnsureSuccessStatusCode();
|
||||
return await resp.Content.ReadFromJsonAsync<ModbusAutoProhibitionsResponse>(cancellationToken: ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Server response shape for the Modbus auto-prohibition diagnostic. Mirrors the JSON the
|
||||
/// <c>HealthEndpointsHost</c> serialises; fields are flat strings/numbers so the
|
||||
/// Admin-side client doesn't take a dependency on the Driver.Modbus assembly's
|
||||
/// <c>ModbusAutoProhibition</c> record.
|
||||
/// </summary>
|
||||
public sealed class ModbusAutoProhibitionsResponse
|
||||
{
|
||||
public string DriverInstanceId { get; set; } = string.Empty;
|
||||
public int Count { get; set; }
|
||||
public List<ModbusAutoProhibitionRow> Ranges { get; set; } = new();
|
||||
}
|
||||
|
||||
public sealed class ModbusAutoProhibitionRow
|
||||
{
|
||||
public byte UnitId { get; set; }
|
||||
public string Region { get; set; } = string.Empty;
|
||||
public ushort StartAddress { get; set; }
|
||||
public ushort EndAddress { get; set; }
|
||||
public DateTime LastProbedUtc { get; set; }
|
||||
public bool BisectionPending { get; set; }
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
public sealed class DriverInstanceService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<DriverInstance>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.DriverInstances.AsNoTracking()
|
||||
.Where(d => d.GenerationId == generationId)
|
||||
.OrderBy(d => d.DriverInstanceId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<DriverInstance> AddAsync(
|
||||
long draftId, string clusterId, string namespaceId, string name, string driverType,
|
||||
string driverConfigJson, CancellationToken ct)
|
||||
{
|
||||
var di = new DriverInstance
|
||||
{
|
||||
GenerationId = draftId,
|
||||
DriverInstanceId = $"drv-{Guid.NewGuid():N}"[..20],
|
||||
ClusterId = clusterId,
|
||||
NamespaceId = namespaceId,
|
||||
Name = name,
|
||||
DriverType = driverType,
|
||||
DriverConfig = driverConfigJson,
|
||||
};
|
||||
db.DriverInstances.Add(di);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return di;
|
||||
}
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// RFC 4180 CSV parser for equipment import per decision #95 and Phase 6.4 Stream B.1.
|
||||
/// Produces a validated <see cref="EquipmentCsvParseResult"/> the caller (CSV import
|
||||
/// modal + staging tables) consumes. Pure-parser concern — no DB access, no staging
|
||||
/// writes; those live in the follow-up Stream B.2 work.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para><b>Header contract</b>: line 1 must be exactly <c># OtOpcUaCsv v1</c> (version
|
||||
/// marker). Line 2 is the column header row. Unknown columns are rejected; required
|
||||
/// columns must all be present. The version bump handshake lets future shapes parse
|
||||
/// without ambiguity — v2 files go through a different parser variant.</para>
|
||||
///
|
||||
/// <para><b>Required columns</b> per decision #117 + admin-ui.md "Equipment CSV import"
|
||||
/// (Admin-012 — revised after adversarial review finding #4): ZTag, MachineCode, SAPID,
|
||||
/// EquipmentUuid, Name, UnsAreaName, UnsLineName. <b>No <c>EquipmentId</c> column</b> —
|
||||
/// it is system-derived as <c>'EQ-' + first 12 hex chars of EquipmentUuid</c> via
|
||||
/// <see cref="ZB.MOM.WW.OtOpcUa.Configuration.Validation.DraftValidator.DeriveEquipmentId"/>
|
||||
/// and is never accepted from the CSV: operator-supplied values would mint duplicate
|
||||
/// equipment identity on typos.</para>
|
||||
///
|
||||
/// <para><b>Optional columns</b> per decision #139: Manufacturer, Model, SerialNumber,
|
||||
/// HardwareRevision, SoftwareRevision, YearOfConstruction, AssetLocation,
|
||||
/// ManufacturerUri, DeviceManualUri.</para>
|
||||
///
|
||||
/// <para><b>Row validation</b>: blank required field → rejected; duplicate ZTag within
|
||||
/// the same file → rejected. Duplicate against the DB isn't detected here — the
|
||||
/// staged-import finalize step (Stream B.4) catches that.</para>
|
||||
/// </remarks>
|
||||
public static class EquipmentCsvImporter
|
||||
{
|
||||
public const string VersionMarker = "# OtOpcUaCsv v1";
|
||||
|
||||
public static IReadOnlyList<string> RequiredColumns { get; } = new[]
|
||||
{
|
||||
// Admin-012: no EquipmentId — derived from EquipmentUuid at finalise time.
|
||||
"ZTag", "MachineCode", "SAPID", "EquipmentUuid",
|
||||
"Name", "UnsAreaName", "UnsLineName",
|
||||
};
|
||||
|
||||
public static IReadOnlyList<string> OptionalColumns { get; } = new[]
|
||||
{
|
||||
"Manufacturer", "Model", "SerialNumber", "HardwareRevision", "SoftwareRevision",
|
||||
"YearOfConstruction", "AssetLocation", "ManufacturerUri", "DeviceManualUri",
|
||||
};
|
||||
|
||||
public static EquipmentCsvParseResult Parse(string csvText)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(csvText);
|
||||
|
||||
var rows = SplitLines(csvText);
|
||||
if (rows.Count == 0)
|
||||
throw new InvalidCsvFormatException("CSV is empty.");
|
||||
|
||||
if (!string.Equals(rows[0].Trim(), VersionMarker, StringComparison.Ordinal))
|
||||
throw new InvalidCsvFormatException(
|
||||
$"CSV header line 1 must be exactly '{VersionMarker}' — got '{rows[0]}'. " +
|
||||
"Files without the version marker are rejected so future-format files don't parse ambiguously.");
|
||||
|
||||
if (rows.Count < 2)
|
||||
throw new InvalidCsvFormatException("CSV has no column header row (line 2) or data rows.");
|
||||
|
||||
var headerCells = SplitCsvRow(rows[1]);
|
||||
ValidateHeader(headerCells);
|
||||
|
||||
var accepted = new List<EquipmentCsvRow>();
|
||||
var rejected = new List<EquipmentCsvRowError>();
|
||||
var ztagsSeen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
var colIndex = headerCells
|
||||
.Select((name, idx) => (name, idx))
|
||||
.ToDictionary(t => t.name, t => t.idx, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
for (var i = 2; i < rows.Count; i++)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rows[i])) continue;
|
||||
|
||||
try
|
||||
{
|
||||
var cells = SplitCsvRow(rows[i]);
|
||||
if (cells.Length != headerCells.Length)
|
||||
{
|
||||
rejected.Add(new EquipmentCsvRowError(
|
||||
LineNumber: i + 1,
|
||||
Reason: $"Column count {cells.Length} != header count {headerCells.Length}."));
|
||||
continue;
|
||||
}
|
||||
|
||||
var row = BuildRow(cells, colIndex);
|
||||
var missing = RequiredColumns.Where(c => string.IsNullOrWhiteSpace(GetCell(row, c))).ToList();
|
||||
if (missing.Count > 0)
|
||||
{
|
||||
rejected.Add(new EquipmentCsvRowError(i + 1, $"Blank required column(s): {string.Join(", ", missing)}"));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ztagsSeen.Add(row.ZTag))
|
||||
{
|
||||
rejected.Add(new EquipmentCsvRowError(i + 1, $"Duplicate ZTag '{row.ZTag}' within file."));
|
||||
continue;
|
||||
}
|
||||
|
||||
accepted.Add(row);
|
||||
}
|
||||
catch (InvalidCsvFormatException ex)
|
||||
{
|
||||
rejected.Add(new EquipmentCsvRowError(i + 1, ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
return new EquipmentCsvParseResult(accepted, rejected);
|
||||
}
|
||||
|
||||
private static void ValidateHeader(string[] headerCells)
|
||||
{
|
||||
var seen = new HashSet<string>(headerCells, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// Missing required
|
||||
var missingRequired = RequiredColumns.Where(r => !seen.Contains(r)).ToList();
|
||||
if (missingRequired.Count > 0)
|
||||
throw new InvalidCsvFormatException($"Header is missing required column(s): {string.Join(", ", missingRequired)}");
|
||||
|
||||
// Unknown columns (not in required ∪ optional)
|
||||
var known = new HashSet<string>(RequiredColumns.Concat(OptionalColumns), StringComparer.OrdinalIgnoreCase);
|
||||
var unknown = headerCells.Where(c => !known.Contains(c)).ToList();
|
||||
if (unknown.Count > 0)
|
||||
throw new InvalidCsvFormatException(
|
||||
$"Header has unknown column(s): {string.Join(", ", unknown)}. " +
|
||||
"Bump the version marker to define a new shape before adding columns.");
|
||||
|
||||
// Duplicates
|
||||
var dupe = headerCells.GroupBy(c => c, StringComparer.OrdinalIgnoreCase).FirstOrDefault(g => g.Count() > 1);
|
||||
if (dupe is not null)
|
||||
throw new InvalidCsvFormatException($"Header has duplicate column '{dupe.Key}'.");
|
||||
}
|
||||
|
||||
private static EquipmentCsvRow BuildRow(string[] cells, Dictionary<string, int> colIndex) => new()
|
||||
{
|
||||
ZTag = cells[colIndex["ZTag"]],
|
||||
MachineCode = cells[colIndex["MachineCode"]],
|
||||
SAPID = cells[colIndex["SAPID"]],
|
||||
EquipmentUuid = cells[colIndex["EquipmentUuid"]],
|
||||
Name = cells[colIndex["Name"]],
|
||||
UnsAreaName = cells[colIndex["UnsAreaName"]],
|
||||
UnsLineName = cells[colIndex["UnsLineName"]],
|
||||
Manufacturer = colIndex.TryGetValue("Manufacturer", out var mi) ? cells[mi] : null,
|
||||
Model = colIndex.TryGetValue("Model", out var moi) ? cells[moi] : null,
|
||||
SerialNumber = colIndex.TryGetValue("SerialNumber", out var si) ? cells[si] : null,
|
||||
HardwareRevision = colIndex.TryGetValue("HardwareRevision", out var hi) ? cells[hi] : null,
|
||||
SoftwareRevision = colIndex.TryGetValue("SoftwareRevision", out var swi) ? cells[swi] : null,
|
||||
YearOfConstruction = colIndex.TryGetValue("YearOfConstruction", out var yi) ? cells[yi] : null,
|
||||
AssetLocation = colIndex.TryGetValue("AssetLocation", out var ai) ? cells[ai] : null,
|
||||
ManufacturerUri = colIndex.TryGetValue("ManufacturerUri", out var mui) ? cells[mui] : null,
|
||||
DeviceManualUri = colIndex.TryGetValue("DeviceManualUri", out var dui) ? cells[dui] : null,
|
||||
};
|
||||
|
||||
private static string GetCell(EquipmentCsvRow row, string colName) => colName switch
|
||||
{
|
||||
"ZTag" => row.ZTag,
|
||||
"MachineCode" => row.MachineCode,
|
||||
"SAPID" => row.SAPID,
|
||||
"EquipmentUuid" => row.EquipmentUuid,
|
||||
"Name" => row.Name,
|
||||
"UnsAreaName" => row.UnsAreaName,
|
||||
"UnsLineName" => row.UnsLineName,
|
||||
_ => string.Empty,
|
||||
};
|
||||
|
||||
/// <summary>Split the raw text on line boundaries. Handles \r\n + \n + \r.</summary>
|
||||
private static List<string> SplitLines(string csv) =>
|
||||
csv.Split(["\r\n", "\n", "\r"], StringSplitOptions.None).ToList();
|
||||
|
||||
/// <summary>Split one CSV row with RFC 4180 quoted-field handling.</summary>
|
||||
private static string[] SplitCsvRow(string row)
|
||||
{
|
||||
var cells = new List<string>();
|
||||
var sb = new StringBuilder();
|
||||
var inQuotes = false;
|
||||
|
||||
for (var i = 0; i < row.Length; i++)
|
||||
{
|
||||
var ch = row[i];
|
||||
if (inQuotes)
|
||||
{
|
||||
if (ch == '"')
|
||||
{
|
||||
// Escaped quote "" inside quoted field.
|
||||
if (i + 1 < row.Length && row[i + 1] == '"')
|
||||
{
|
||||
sb.Append('"');
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
inQuotes = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(ch);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ch == ',')
|
||||
{
|
||||
cells.Add(sb.ToString());
|
||||
sb.Clear();
|
||||
}
|
||||
else if (ch == '"' && sb.Length == 0)
|
||||
{
|
||||
inQuotes = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cells.Add(sb.ToString());
|
||||
return cells.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One parsed equipment row with required + optional fields.</summary>
|
||||
public sealed class EquipmentCsvRow
|
||||
{
|
||||
// Required (decision #117). Admin-012: no EquipmentId here — derived from
|
||||
// EquipmentUuid at finalise time so a CSV typo cannot create a duplicate identity.
|
||||
public required string ZTag { get; init; }
|
||||
public required string MachineCode { get; init; }
|
||||
public required string SAPID { get; init; }
|
||||
public required string EquipmentUuid { get; init; }
|
||||
public required string Name { get; init; }
|
||||
public required string UnsAreaName { get; init; }
|
||||
public required string UnsLineName { get; init; }
|
||||
|
||||
// Optional (decision #139 — OPC 40010 Identification fields)
|
||||
public string? Manufacturer { get; init; }
|
||||
public string? Model { get; init; }
|
||||
public string? SerialNumber { get; init; }
|
||||
public string? HardwareRevision { get; init; }
|
||||
public string? SoftwareRevision { get; init; }
|
||||
public string? YearOfConstruction { get; init; }
|
||||
public string? AssetLocation { get; init; }
|
||||
public string? ManufacturerUri { get; init; }
|
||||
public string? DeviceManualUri { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>One row-level rejection captured by the parser. Line-number is 1-based in the source file.</summary>
|
||||
public sealed record EquipmentCsvRowError(int LineNumber, string Reason);
|
||||
|
||||
/// <summary>Parser output — accepted rows land in staging; rejected rows surface in the preview modal.</summary>
|
||||
public sealed record EquipmentCsvParseResult(
|
||||
IReadOnlyList<EquipmentCsvRow> AcceptedRows,
|
||||
IReadOnlyList<EquipmentCsvRowError> RejectedRows);
|
||||
|
||||
/// <summary>Thrown for file-level format problems (missing version marker, bad header, etc.).</summary>
|
||||
public sealed class InvalidCsvFormatException(string message) : Exception(message);
|
||||
@@ -1,434 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Staged-import orchestrator per Phase 6.4 Stream B.2-B.4. Covers the four operator
|
||||
/// actions: CreateBatch → StageRows (chunked) → FinaliseBatch (atomic apply into
|
||||
/// <see cref="Equipment"/>) → DropBatch (rollback of pre-finalise state).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>FinaliseBatch runs inside one EF transaction + bulk-inserts accepted rows into
|
||||
/// <see cref="Equipment"/>. Rejected rows stay behind as audit evidence; the batch row
|
||||
/// gains <see cref="EquipmentImportBatch.FinalisedAtUtc"/> so future writes know it's
|
||||
/// archived. DropBatch removes the batch + its cascaded rows.</para>
|
||||
///
|
||||
/// <para>Idempotence: calling FinaliseBatch twice throws <see cref="ImportBatchAlreadyFinalisedException"/>
|
||||
/// rather than double-inserting. Operator refreshes the admin page to see the first
|
||||
/// finalise completed.</para>
|
||||
///
|
||||
/// <para>ExternalIdReservation merging (ZTag + SAPID uniqueness) is NOT done here — a
|
||||
/// narrower follow-up wires it once the concurrent-insert test matrix is green.</para>
|
||||
/// </remarks>
|
||||
public sealed class EquipmentImportBatchService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
/// <summary>Create a new empty batch header. Returns the row with Id populated.</summary>
|
||||
public async Task<EquipmentImportBatch> CreateBatchAsync(string clusterId, string createdBy, CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(clusterId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(createdBy);
|
||||
|
||||
var batch = new EquipmentImportBatch
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
ClusterId = clusterId,
|
||||
CreatedBy = createdBy,
|
||||
CreatedAtUtc = DateTime.UtcNow,
|
||||
};
|
||||
db.EquipmentImportBatches.Add(batch);
|
||||
await db.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
return batch;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stage one chunk of rows into the batch. Caller usually feeds
|
||||
/// <see cref="EquipmentCsvImporter.Parse"/> output here — each
|
||||
/// <see cref="EquipmentCsvRow"/> becomes one accepted <see cref="EquipmentImportRow"/>,
|
||||
/// each rejected parser error becomes one row with <see cref="EquipmentImportRow.IsAccepted"/> false.
|
||||
/// </summary>
|
||||
public async Task StageRowsAsync(
|
||||
Guid batchId,
|
||||
IReadOnlyList<EquipmentCsvRow> acceptedRows,
|
||||
IReadOnlyList<EquipmentCsvRowError> rejectedRows,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var batch = await db.EquipmentImportBatches.FirstOrDefaultAsync(b => b.Id == batchId, ct).ConfigureAwait(false)
|
||||
?? throw new ImportBatchNotFoundException($"Batch {batchId} not found.");
|
||||
|
||||
if (batch.FinalisedAtUtc is not null)
|
||||
throw new ImportBatchAlreadyFinalisedException(
|
||||
$"Batch {batchId} finalised at {batch.FinalisedAtUtc:o}; no more rows can be staged.");
|
||||
|
||||
foreach (var row in acceptedRows)
|
||||
{
|
||||
// Admin-012: EquipmentId is not on the CSV row — derive it from EquipmentUuid
|
||||
// so the staging row carries the canonical 'EQ-' + first-12-hex form. Rows
|
||||
// with an unparseable UUID get an empty placeholder; the same finalise path
|
||||
// that re-derives the UUID (FinaliseBatchAsync) will overwrite it.
|
||||
var derivedEquipmentId = Guid.TryParse(row.EquipmentUuid, out var parsedUuid)
|
||||
? DraftValidator.DeriveEquipmentId(parsedUuid)
|
||||
: string.Empty;
|
||||
|
||||
db.EquipmentImportRows.Add(new EquipmentImportRow
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
BatchId = batchId,
|
||||
IsAccepted = true,
|
||||
ZTag = row.ZTag,
|
||||
MachineCode = row.MachineCode,
|
||||
SAPID = row.SAPID,
|
||||
EquipmentId = derivedEquipmentId,
|
||||
EquipmentUuid = row.EquipmentUuid,
|
||||
Name = row.Name,
|
||||
UnsAreaName = row.UnsAreaName,
|
||||
UnsLineName = row.UnsLineName,
|
||||
Manufacturer = row.Manufacturer,
|
||||
Model = row.Model,
|
||||
SerialNumber = row.SerialNumber,
|
||||
HardwareRevision = row.HardwareRevision,
|
||||
SoftwareRevision = row.SoftwareRevision,
|
||||
YearOfConstruction = row.YearOfConstruction,
|
||||
AssetLocation = row.AssetLocation,
|
||||
ManufacturerUri = row.ManufacturerUri,
|
||||
DeviceManualUri = row.DeviceManualUri,
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var error in rejectedRows)
|
||||
{
|
||||
db.EquipmentImportRows.Add(new EquipmentImportRow
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
BatchId = batchId,
|
||||
IsAccepted = false,
|
||||
RejectReason = error.Reason,
|
||||
LineNumberInFile = error.LineNumber,
|
||||
// Required columns need values for EF; reject rows use sentinel placeholders.
|
||||
ZTag = "", MachineCode = "", SAPID = "", EquipmentId = "", EquipmentUuid = "",
|
||||
Name = "", UnsAreaName = "", UnsLineName = "",
|
||||
});
|
||||
}
|
||||
|
||||
batch.RowsStaged += acceptedRows.Count + rejectedRows.Count;
|
||||
batch.RowsAccepted += acceptedRows.Count;
|
||||
batch.RowsRejected += rejectedRows.Count;
|
||||
|
||||
await db.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>Drop the batch (pre-finalise rollback). Cascaded row delete removes staged rows.</summary>
|
||||
public async Task DropBatchAsync(Guid batchId, CancellationToken ct)
|
||||
{
|
||||
var batch = await db.EquipmentImportBatches.FirstOrDefaultAsync(b => b.Id == batchId, ct).ConfigureAwait(false);
|
||||
if (batch is null) return;
|
||||
if (batch.FinalisedAtUtc is not null)
|
||||
throw new ImportBatchAlreadyFinalisedException(
|
||||
$"Batch {batchId} already finalised at {batch.FinalisedAtUtc:o}; cannot drop.");
|
||||
|
||||
db.EquipmentImportBatches.Remove(batch);
|
||||
await db.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Atomic finalise. Inserts every accepted row into the live
|
||||
/// <see cref="Equipment"/> table under the target generation + stamps
|
||||
/// <see cref="EquipmentImportBatch.FinalisedAtUtc"/>. Failure rolls the whole tx
|
||||
/// back — <see cref="Equipment"/> never partially mutates.
|
||||
/// </summary>
|
||||
public async Task FinaliseBatchAsync(
|
||||
Guid batchId, long generationId, string driverInstanceIdForRows, string unsLineIdForRows, CancellationToken ct)
|
||||
{
|
||||
var batch = await db.EquipmentImportBatches
|
||||
.Include(b => b.Rows)
|
||||
.FirstOrDefaultAsync(b => b.Id == batchId, ct)
|
||||
.ConfigureAwait(false)
|
||||
?? throw new ImportBatchNotFoundException($"Batch {batchId} not found.");
|
||||
|
||||
if (batch.FinalisedAtUtc is not null)
|
||||
throw new ImportBatchAlreadyFinalisedException(
|
||||
$"Batch {batchId} already finalised at {batch.FinalisedAtUtc:o}.");
|
||||
|
||||
// EF InMemory provider doesn't honour BeginTransaction; SQL Server provider does.
|
||||
// Tests run the happy path under in-memory; production SQL Server runs the atomic tx.
|
||||
var supportsTx = db.Database.IsRelational();
|
||||
Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction? tx = null;
|
||||
if (supportsTx)
|
||||
tx = await db.Database.BeginTransactionAsync(ct).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
// Snapshot active reservations that overlap this batch's ZTag + SAPID set — one
|
||||
// round-trip instead of N. Released rows (ReleasedAt IS NOT NULL) are ignored so
|
||||
// an explicitly-released value can be reused.
|
||||
var accepted = batch.Rows.Where(r => r.IsAccepted).ToList();
|
||||
var zTags = accepted.Where(r => !string.IsNullOrWhiteSpace(r.ZTag))
|
||||
.Select(r => r.ZTag).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
var sapIds = accepted.Where(r => !string.IsNullOrWhiteSpace(r.SAPID))
|
||||
.Select(r => r.SAPID).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
|
||||
|
||||
var existingReservations = await db.ExternalIdReservations
|
||||
.Where(r => r.ReleasedAt == null &&
|
||||
((r.Kind == ReservationKind.ZTag && zTags.Contains(r.Value)) ||
|
||||
(r.Kind == ReservationKind.SAPID && sapIds.Contains(r.Value))))
|
||||
.ToListAsync(ct).ConfigureAwait(false);
|
||||
var resByKey = existingReservations.ToDictionary(
|
||||
r => (r.Kind, r.Value.ToLowerInvariant()),
|
||||
r => r);
|
||||
|
||||
var nowUtc = DateTime.UtcNow;
|
||||
var firstPublishedBy = batch.CreatedBy;
|
||||
|
||||
foreach (var row in accepted)
|
||||
{
|
||||
var equipmentUuid = Guid.TryParse(row.EquipmentUuid, out var u) ? u : Guid.NewGuid();
|
||||
|
||||
// Admin-012: EquipmentId is always derived from the UUID that actually lands in
|
||||
// the Equipment row. Using the staging row's pre-derived value would diverge
|
||||
// when the staged UUID was blank and we just generated a fresh one above.
|
||||
var derivedEquipmentId = DraftValidator.DeriveEquipmentId(equipmentUuid);
|
||||
|
||||
db.Equipment.Add(new Equipment
|
||||
{
|
||||
EquipmentRowId = Guid.NewGuid(),
|
||||
GenerationId = generationId,
|
||||
EquipmentId = derivedEquipmentId,
|
||||
EquipmentUuid = equipmentUuid,
|
||||
DriverInstanceId = driverInstanceIdForRows,
|
||||
UnsLineId = unsLineIdForRows,
|
||||
Name = row.Name,
|
||||
MachineCode = row.MachineCode,
|
||||
ZTag = row.ZTag,
|
||||
SAPID = row.SAPID,
|
||||
Manufacturer = row.Manufacturer,
|
||||
Model = row.Model,
|
||||
SerialNumber = row.SerialNumber,
|
||||
HardwareRevision = row.HardwareRevision,
|
||||
SoftwareRevision = row.SoftwareRevision,
|
||||
YearOfConstruction = short.TryParse(row.YearOfConstruction, out var y) ? y : null,
|
||||
AssetLocation = row.AssetLocation,
|
||||
ManufacturerUri = row.ManufacturerUri,
|
||||
DeviceManualUri = row.DeviceManualUri,
|
||||
});
|
||||
|
||||
MergeReservation(row.ZTag, ReservationKind.ZTag, equipmentUuid, batch.ClusterId,
|
||||
firstPublishedBy, nowUtc, resByKey);
|
||||
MergeReservation(row.SAPID, ReservationKind.SAPID, equipmentUuid, batch.ClusterId,
|
||||
firstPublishedBy, nowUtc, resByKey);
|
||||
}
|
||||
|
||||
batch.FinalisedAtUtc = nowUtc;
|
||||
try
|
||||
{
|
||||
await db.SaveChangesAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (DbUpdateException ex) when (IsReservationUniquenessViolation(ex))
|
||||
{
|
||||
throw new ExternalIdReservationConflictException(
|
||||
"Finalise rejected: one or more ZTag/SAPID values were reserved by another operator " +
|
||||
"between batch preview and commit. Inspect active reservations + retry after resolving the conflict.",
|
||||
ex);
|
||||
}
|
||||
if (tx is not null) await tx.CommitAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (tx is not null) await tx.RollbackAsync(ct).ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (tx is not null) await tx.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Merge one external-ID reservation for an equipment row. Three outcomes:
|
||||
/// (1) value is empty → skip; (2) reservation exists for same <paramref name="equipmentUuid"/>
|
||||
/// → bump <c>LastPublishedAt</c>; (3) reservation exists for a different EquipmentUuid
|
||||
/// → throw <see cref="ExternalIdReservationConflictException"/> with the conflicting UUID
|
||||
/// so the caller sees which equipment already owns the value; (4) no reservation → create new.
|
||||
/// </summary>
|
||||
private void MergeReservation(
|
||||
string? value,
|
||||
ReservationKind kind,
|
||||
Guid equipmentUuid,
|
||||
string clusterId,
|
||||
string firstPublishedBy,
|
||||
DateTime nowUtc,
|
||||
Dictionary<(ReservationKind, string), ExternalIdReservation> cache)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) return;
|
||||
|
||||
var key = (kind, value.ToLowerInvariant());
|
||||
if (cache.TryGetValue(key, out var existing))
|
||||
{
|
||||
if (existing.EquipmentUuid != equipmentUuid)
|
||||
throw new ExternalIdReservationConflictException(
|
||||
$"{kind} '{value}' is already reserved by EquipmentUuid {existing.EquipmentUuid} " +
|
||||
$"(first published {existing.FirstPublishedAt:u} on cluster '{existing.ClusterId}'). " +
|
||||
$"Refusing to re-assign to {equipmentUuid}.");
|
||||
|
||||
existing.LastPublishedAt = nowUtc;
|
||||
return;
|
||||
}
|
||||
|
||||
var fresh = new ExternalIdReservation
|
||||
{
|
||||
ReservationId = Guid.NewGuid(),
|
||||
Kind = kind,
|
||||
Value = value,
|
||||
EquipmentUuid = equipmentUuid,
|
||||
ClusterId = clusterId,
|
||||
FirstPublishedAt = nowUtc,
|
||||
FirstPublishedBy = firstPublishedBy,
|
||||
LastPublishedAt = nowUtc,
|
||||
};
|
||||
db.ExternalIdReservations.Add(fresh);
|
||||
cache[key] = fresh;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when the <see cref="DbUpdateException"/> root-cause was the filtered-unique
|
||||
/// index <c>UX_ExternalIdReservation_KindValue_Active</c> — i.e. another transaction
|
||||
/// won the race between our cache-load + commit. SQL Server surfaces this as 2601 / 2627.
|
||||
/// </summary>
|
||||
private static bool IsReservationUniquenessViolation(DbUpdateException ex)
|
||||
{
|
||||
for (Exception? inner = ex; inner is not null; inner = inner.InnerException)
|
||||
{
|
||||
if (inner is Microsoft.Data.SqlClient.SqlException sql &&
|
||||
(sql.Number == 2601 || sql.Number == 2627) &&
|
||||
sql.Message.Contains("UX_ExternalIdReservation_KindValue_Active", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pre-checks active <see cref="ExternalIdReservation"/>s for the accepted rows in
|
||||
/// <paramref name="parseResult"/>. Rows whose ZTag or SAPID is already reserved by a
|
||||
/// <em>different</em> <see cref="ExternalIdReservation.EquipmentUuid"/> are moved from
|
||||
/// <see cref="EquipmentCsvParseResult.AcceptedRows"/> to
|
||||
/// <see cref="EquipmentCsvParseResult.RejectedRows"/> with a descriptive reason so the
|
||||
/// operator sees the conflict in the import preview rather than at finalise time.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Rows whose value matches a reservation owned by the <em>same</em>
|
||||
/// <see cref="ExternalIdReservation.EquipmentUuid"/> are not flagged — that is the
|
||||
/// normal re-publish of an asset keeping its identifier.
|
||||
///
|
||||
/// Released reservations (<see cref="ExternalIdReservation.ReleasedAt"/> IS NOT NULL)
|
||||
/// are ignored so an explicitly-released value is freely claimable.
|
||||
///
|
||||
/// One DB round-trip fetches all relevant active reservations before the per-row scan.
|
||||
/// </remarks>
|
||||
public async Task<EquipmentCsvParseResult> ApplyReservationPreCheckAsync(
|
||||
EquipmentCsvParseResult parseResult, CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(parseResult);
|
||||
|
||||
var accepted = parseResult.AcceptedRows;
|
||||
if (accepted.Count == 0) return parseResult; // nothing to check
|
||||
|
||||
// Collect ZTag + SAPID values that are non-empty across all accepted rows.
|
||||
var zTags = accepted
|
||||
.Where(r => !string.IsNullOrWhiteSpace(r.ZTag))
|
||||
.Select(r => r.ZTag)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
var sapIds = accepted
|
||||
.Where(r => !string.IsNullOrWhiteSpace(r.SAPID))
|
||||
.Select(r => r.SAPID)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
|
||||
if (zTags.Count == 0 && sapIds.Count == 0) return parseResult;
|
||||
|
||||
// Single round-trip: fetch all active reservations whose value appears in the import.
|
||||
var activeReservations = await db.ExternalIdReservations
|
||||
.AsNoTracking()
|
||||
.Where(r => r.ReleasedAt == null &&
|
||||
((r.Kind == ReservationKind.ZTag && zTags.Contains(r.Value)) ||
|
||||
(r.Kind == ReservationKind.SAPID && sapIds.Contains(r.Value))))
|
||||
.ToListAsync(ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
if (activeReservations.Count == 0) return parseResult;
|
||||
|
||||
// Build lookup: (kind, value-lower) → owning EquipmentUuid.
|
||||
var reservedBy = activeReservations.ToDictionary(
|
||||
r => (r.Kind, r.Value.ToLowerInvariant()),
|
||||
r => r.EquipmentUuid);
|
||||
|
||||
var stillAccepted = new List<EquipmentCsvRow>();
|
||||
var newRejections = new List<EquipmentCsvRowError>();
|
||||
|
||||
foreach (var row in accepted)
|
||||
{
|
||||
var rowUuid = Guid.TryParse(row.EquipmentUuid, out var u) ? u : Guid.Empty;
|
||||
string? conflictReason = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(row.ZTag) &&
|
||||
reservedBy.TryGetValue((ReservationKind.ZTag, row.ZTag.ToLowerInvariant()), out var zOwner) &&
|
||||
zOwner != rowUuid)
|
||||
{
|
||||
conflictReason =
|
||||
$"ZTag '{row.ZTag}' is already reserved by EquipmentUuid {zOwner}. " +
|
||||
"Release that reservation via the Reservations admin page before re-assigning this ZTag.";
|
||||
}
|
||||
|
||||
if (conflictReason is null &&
|
||||
!string.IsNullOrWhiteSpace(row.SAPID) &&
|
||||
reservedBy.TryGetValue((ReservationKind.SAPID, row.SAPID.ToLowerInvariant()), out var sOwner) &&
|
||||
sOwner != rowUuid)
|
||||
{
|
||||
conflictReason =
|
||||
$"SAPID '{row.SAPID}' is already reserved by EquipmentUuid {sOwner}. " +
|
||||
"Release that reservation via the Reservations admin page before re-assigning this SAPID.";
|
||||
}
|
||||
|
||||
if (conflictReason is not null)
|
||||
newRejections.Add(new EquipmentCsvRowError(LineNumber: 0, Reason: conflictReason));
|
||||
else
|
||||
stillAccepted.Add(row);
|
||||
}
|
||||
|
||||
if (newRejections.Count == 0) return parseResult; // fast path — no conflicts
|
||||
|
||||
return new EquipmentCsvParseResult(
|
||||
AcceptedRows: stillAccepted,
|
||||
RejectedRows: [..parseResult.RejectedRows, ..newRejections]);
|
||||
}
|
||||
|
||||
/// <summary>List batches created by the given user. Finalised batches are archived; include them on demand.</summary>
|
||||
public async Task<IReadOnlyList<EquipmentImportBatch>> ListByUserAsync(string createdBy, bool includeFinalised, CancellationToken ct)
|
||||
{
|
||||
var query = db.EquipmentImportBatches.AsNoTracking().Where(b => b.CreatedBy == createdBy);
|
||||
if (!includeFinalised)
|
||||
query = query.Where(b => b.FinalisedAtUtc == null);
|
||||
return await query.OrderByDescending(b => b.CreatedAtUtc).ToListAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ImportBatchNotFoundException(string message) : Exception(message);
|
||||
public sealed class ImportBatchAlreadyFinalisedException(string message) : Exception(message);
|
||||
|
||||
/// <summary>
|
||||
/// Thrown when a <c>FinaliseBatchAsync</c> call detects that one of its ZTag/SAPID values is
|
||||
/// already reserved by a different EquipmentUuid — either from a prior published generation
|
||||
/// or a concurrent finalise that won the race. The operator sees the message + the conflicting
|
||||
/// equipment ownership so they can resolve the conflict (pick a new ZTag, release the existing
|
||||
/// reservation via <c>sp_ReleaseExternalIdReservation</c>, etc.) and retry the finalise.
|
||||
/// </summary>
|
||||
public sealed class ExternalIdReservationConflictException : Exception
|
||||
{
|
||||
public ExternalIdReservationConflictException(string message) : base(message) { }
|
||||
public ExternalIdReservationConflictException(string message, Exception inner) : base(message, inner) { }
|
||||
}
|
||||
@@ -1,199 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Validation;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Equipment CRUD scoped to a generation. The Admin app writes against Draft generations only;
|
||||
/// Published generations are read-only (to create changes, clone to a new draft via
|
||||
/// <see cref="GenerationService.CreateDraftAsync"/>).
|
||||
/// </summary>
|
||||
public sealed class EquipmentService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<Equipment>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.Equipment.AsNoTracking()
|
||||
.Where(e => e.GenerationId == generationId)
|
||||
.OrderBy(e => e.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
/// <summary>
|
||||
/// Five-identifier ranked search across a cluster (all draft + published generations).
|
||||
/// Identifiers: ZTag, MachineCode, SAPID, EquipmentId, EquipmentUuid (decision #117).
|
||||
/// Scoring: exact match on any identifier = 100, prefix match = 50, fuzzy (opt-in) = 20.
|
||||
/// Tie-break: Published generation outranks Draft; within same status by Name ascending.
|
||||
/// Returns at most <paramref name="maxResults"/> rows.
|
||||
/// </summary>
|
||||
/// <param name="query">Search term (trimmed; empty returns empty results, not all rows).</param>
|
||||
/// <param name="clusterId">Cluster to scope the search to.</param>
|
||||
/// <param name="maxResults">Cap to prevent full-table dumps (default 50).</param>
|
||||
/// <param name="allowFuzzy">When true, LIKE-prefix suffix matches score 20 (opt-in per spec).</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
public async Task<IReadOnlyList<EquipmentSearchHit>> SearchAsync(
|
||||
string query,
|
||||
string clusterId,
|
||||
CancellationToken ct,
|
||||
int maxResults = 50,
|
||||
bool allowFuzzy = false)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(clusterId);
|
||||
query = query?.Trim() ?? string.Empty;
|
||||
if (string.IsNullOrEmpty(query))
|
||||
return [];
|
||||
|
||||
// Load candidates from DB — we filter generation to this cluster via the Join.
|
||||
// The scoring is pure-LINQ post-load because EF InMemory doesn't support CASE WHEN scoring
|
||||
// and the SQL-provider translation for this small set is acceptable (bounded by cluster).
|
||||
var candidates = await db.Equipment.AsNoTracking()
|
||||
.Join(db.ConfigGenerations.AsNoTracking(),
|
||||
e => e.GenerationId,
|
||||
g => g.GenerationId,
|
||||
(e, g) => new { Equipment = e, Generation = g })
|
||||
.Where(x => x.Generation.ClusterId == clusterId)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Equipment,
|
||||
IsPublished = x.Generation.Status == GenerationStatus.Published,
|
||||
})
|
||||
.ToListAsync(ct)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var lower = query.ToLowerInvariant();
|
||||
|
||||
var scored = candidates
|
||||
.Select(c =>
|
||||
{
|
||||
var (score, matchedField) = ScoreEquipment(c.Equipment, lower, allowFuzzy);
|
||||
return new
|
||||
{
|
||||
c.Equipment,
|
||||
c.IsPublished,
|
||||
Score = score,
|
||||
MatchedField = matchedField,
|
||||
};
|
||||
})
|
||||
.Where(x => x.Score > 0)
|
||||
// Tie-break: highest score → published before draft → name
|
||||
.OrderByDescending(x => x.Score)
|
||||
.ThenByDescending(x => x.IsPublished ? 1 : 0)
|
||||
.ThenBy(x => x.Equipment.Name)
|
||||
.Take(maxResults)
|
||||
.Select(x => new EquipmentSearchHit(x.Equipment, x.Score, x.MatchedField, x.IsPublished))
|
||||
.ToList();
|
||||
|
||||
return scored;
|
||||
}
|
||||
|
||||
/// <summary>Score one equipment row against the search term. Returns (score, matchedFieldName).</summary>
|
||||
private static (int Score, string? MatchedField) ScoreEquipment(Equipment e, string lower, bool allowFuzzy)
|
||||
{
|
||||
// Evaluate each identifier in priority order — first exact match wins with score 100.
|
||||
var identifiers = new (string FieldName, string? Value)[]
|
||||
{
|
||||
("ZTag", e.ZTag),
|
||||
("MachineCode", e.MachineCode),
|
||||
("SAPID", e.SAPID),
|
||||
("EquipmentId", e.EquipmentId),
|
||||
("EquipmentUuid", e.EquipmentUuid == Guid.Empty ? null : e.EquipmentUuid.ToString()),
|
||||
};
|
||||
|
||||
foreach (var (fieldName, value) in identifiers)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) continue;
|
||||
var v = value.ToLowerInvariant();
|
||||
if (v == lower)
|
||||
return (100, fieldName);
|
||||
}
|
||||
|
||||
// Prefix match — score 50
|
||||
foreach (var (fieldName, value) in identifiers)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) continue;
|
||||
var v = value.ToLowerInvariant();
|
||||
if (v.StartsWith(lower, StringComparison.Ordinal))
|
||||
return (50, fieldName);
|
||||
}
|
||||
|
||||
// Fuzzy (substring) match — score 20, opt-in only
|
||||
if (allowFuzzy)
|
||||
{
|
||||
foreach (var (fieldName, value) in identifiers)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) continue;
|
||||
var v = value.ToLowerInvariant();
|
||||
if (v.Contains(lower, StringComparison.Ordinal))
|
||||
return (20, fieldName);
|
||||
}
|
||||
}
|
||||
|
||||
return (0, null);
|
||||
}
|
||||
|
||||
public Task<Equipment?> FindAsync(long generationId, string equipmentId, CancellationToken ct) =>
|
||||
db.Equipment.AsNoTracking()
|
||||
.FirstOrDefaultAsync(e => e.GenerationId == generationId && e.EquipmentId == equipmentId, ct);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new equipment row in the given draft. The EquipmentId is auto-derived from
|
||||
/// a fresh EquipmentUuid per decision #125; operator-supplied IDs are rejected upstream.
|
||||
/// </summary>
|
||||
public async Task<Equipment> CreateAsync(long draftId, Equipment input, CancellationToken ct)
|
||||
{
|
||||
input.GenerationId = draftId;
|
||||
input.EquipmentUuid = input.EquipmentUuid == Guid.Empty ? Guid.NewGuid() : input.EquipmentUuid;
|
||||
input.EquipmentId = DraftValidator.DeriveEquipmentId(input.EquipmentUuid);
|
||||
db.Equipment.Add(input);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return input;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Equipment updated, CancellationToken ct)
|
||||
{
|
||||
// Only editable fields are persisted; EquipmentId + EquipmentUuid are immutable once set.
|
||||
var existing = await db.Equipment
|
||||
.FirstOrDefaultAsync(e => e.EquipmentRowId == updated.EquipmentRowId, ct)
|
||||
?? throw new InvalidOperationException($"Equipment row {updated.EquipmentRowId} not found");
|
||||
|
||||
existing.Name = updated.Name;
|
||||
existing.MachineCode = updated.MachineCode;
|
||||
existing.ZTag = updated.ZTag;
|
||||
existing.SAPID = updated.SAPID;
|
||||
existing.Manufacturer = updated.Manufacturer;
|
||||
existing.Model = updated.Model;
|
||||
existing.SerialNumber = updated.SerialNumber;
|
||||
existing.HardwareRevision = updated.HardwareRevision;
|
||||
existing.SoftwareRevision = updated.SoftwareRevision;
|
||||
existing.YearOfConstruction = updated.YearOfConstruction;
|
||||
existing.AssetLocation = updated.AssetLocation;
|
||||
existing.ManufacturerUri = updated.ManufacturerUri;
|
||||
existing.DeviceManualUri = updated.DeviceManualUri;
|
||||
existing.DriverInstanceId = updated.DriverInstanceId;
|
||||
existing.DeviceId = updated.DeviceId;
|
||||
existing.UnsLineId = updated.UnsLineId;
|
||||
existing.EquipmentClassRef = updated.EquipmentClassRef;
|
||||
existing.Enabled = updated.Enabled;
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid equipmentRowId, CancellationToken ct)
|
||||
{
|
||||
var row = await db.Equipment.FirstOrDefaultAsync(e => e.EquipmentRowId == equipmentRowId, ct);
|
||||
if (row is null) return;
|
||||
db.Equipment.Remove(row);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One hit from <see cref="EquipmentService.SearchAsync"/>.</summary>
|
||||
/// <param name="Equipment">The matched equipment row.</param>
|
||||
/// <param name="Score">Match score: 100 = exact, 50 = prefix, 20 = fuzzy.</param>
|
||||
/// <param name="MatchedField">Which identifier field produced the highest score.</param>
|
||||
/// <param name="IsPublished">True when the row is from a published generation (aids tie-break display).</param>
|
||||
public sealed record EquipmentSearchHit(
|
||||
Equipment Equipment,
|
||||
int Score,
|
||||
string? MatchedField,
|
||||
bool IsPublished);
|
||||
@@ -1,123 +0,0 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Per-instance detail view for FOCAS driver rows. Loads the latest
|
||||
/// <see cref="DriverInstance"/> row for the requested <c>DriverInstanceId</c> (most-recent
|
||||
/// draft wins when multiple rows exist across generations), parses the schemaless
|
||||
/// <c>DriverConfig</c> JSON into <see cref="FocasDriverConfigView"/>, and joins the
|
||||
/// per-device <see cref="DriverHostStatus"/> rows so the Admin page can render host
|
||||
/// state + consecutive-failure counters next to each configured device.
|
||||
/// </summary>
|
||||
public sealed class FocasDriverDetailService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOpts = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
};
|
||||
|
||||
public async Task<FocasDriverDetail?> GetAsync(string driverInstanceId, CancellationToken ct = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(driverInstanceId)) return null;
|
||||
|
||||
var instance = await db.DriverInstances.AsNoTracking()
|
||||
.Where(d => d.DriverInstanceId == driverInstanceId
|
||||
&& d.DriverType.ToLower() == "focas")
|
||||
.OrderByDescending(d => d.GenerationId)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
if (instance is null) return null;
|
||||
|
||||
FocasDriverConfigView? config = null;
|
||||
string? parseError = null;
|
||||
try { config = JsonSerializer.Deserialize<FocasDriverConfigView>(instance.DriverConfig, JsonOpts); }
|
||||
catch (JsonException ex) { parseError = ex.Message; }
|
||||
|
||||
var hostStatuses = await (from s in db.DriverHostStatuses.AsNoTracking()
|
||||
where s.DriverInstanceId == driverInstanceId
|
||||
join r in db.DriverInstanceResilienceStatuses.AsNoTracking()
|
||||
on new { s.DriverInstanceId, s.HostName }
|
||||
equals new { r.DriverInstanceId, r.HostName } into rj
|
||||
from r in rj.DefaultIfEmpty()
|
||||
orderby s.HostName
|
||||
select new FocasHostStatusRow(
|
||||
s.NodeId,
|
||||
s.HostName,
|
||||
s.State.ToString(),
|
||||
s.StateChangedUtc,
|
||||
s.LastSeenUtc,
|
||||
s.Detail,
|
||||
r != null ? r.ConsecutiveFailures : 0,
|
||||
r != null ? r.LastCircuitBreakerOpenUtc : null,
|
||||
r != null ? r.LastRecycleUtc : null)).ToListAsync(ct);
|
||||
|
||||
return new FocasDriverDetail(instance, config, parseError, hostStatuses);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Projected view of a FOCAS driver's parsed config. Unknown fields are ignored.</summary>
|
||||
public sealed record FocasDriverConfigView
|
||||
{
|
||||
public List<FocasDeviceView>? Devices { get; set; }
|
||||
public List<FocasTagView>? Tags { get; set; }
|
||||
public FocasProbeView? Probe { get; set; }
|
||||
public FocasAlarmProjectionView? AlarmProjection { get; set; }
|
||||
public FocasHandleRecycleView? HandleRecycle { get; set; }
|
||||
}
|
||||
|
||||
public sealed record FocasDeviceView
|
||||
{
|
||||
public string? HostAddress { get; set; }
|
||||
public string? DeviceName { get; set; }
|
||||
public string? Series { get; set; }
|
||||
}
|
||||
|
||||
public sealed record FocasTagView
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public string? DeviceHostAddress { get; set; }
|
||||
public string? Address { get; set; }
|
||||
public string? DataType { get; set; }
|
||||
public bool Writable { get; set; } = true;
|
||||
}
|
||||
|
||||
public sealed record FocasProbeView
|
||||
{
|
||||
public bool Enabled { get; set; } = true;
|
||||
public string? Interval { get; set; }
|
||||
}
|
||||
|
||||
public sealed record FocasAlarmProjectionView
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
public string? PollInterval { get; set; }
|
||||
}
|
||||
|
||||
public sealed record FocasHandleRecycleView
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
public string? Interval { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>Composite payload returned to the Admin page.</summary>
|
||||
public sealed record FocasDriverDetail(
|
||||
DriverInstance Instance,
|
||||
FocasDriverConfigView? Config,
|
||||
string? ParseError,
|
||||
IReadOnlyList<FocasHostStatusRow> HostStatuses);
|
||||
|
||||
public sealed record FocasHostStatusRow(
|
||||
string NodeId,
|
||||
string HostName,
|
||||
string State,
|
||||
DateTime StateChangedUtc,
|
||||
DateTime LastSeenUtc,
|
||||
string? Detail,
|
||||
int ConsecutiveFailures,
|
||||
DateTime? LastCircuitBreakerOpenUtc,
|
||||
DateTime? LastRecycleUtc);
|
||||
@@ -1,71 +0,0 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Owns the draft → diff → publish workflow (decision #89). Publish + rollback call into the
|
||||
/// stored procedures; diff queries <c>sp_ComputeGenerationDiff</c>.
|
||||
/// </summary>
|
||||
public sealed class GenerationService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public async Task<ConfigGeneration> CreateDraftAsync(string clusterId, string createdBy, CancellationToken ct)
|
||||
{
|
||||
var gen = new ConfigGeneration
|
||||
{
|
||||
ClusterId = clusterId,
|
||||
Status = GenerationStatus.Draft,
|
||||
CreatedBy = createdBy,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
};
|
||||
db.ConfigGenerations.Add(gen);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return gen;
|
||||
}
|
||||
|
||||
public Task<List<ConfigGeneration>> ListRecentAsync(string clusterId, int limit, CancellationToken ct) =>
|
||||
db.ConfigGenerations.AsNoTracking()
|
||||
.Where(g => g.ClusterId == clusterId)
|
||||
.OrderByDescending(g => g.GenerationId)
|
||||
.Take(limit)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task PublishAsync(string clusterId, long draftGenerationId, string? notes, CancellationToken ct)
|
||||
{
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.sp_PublishGeneration @ClusterId = {0}, @DraftGenerationId = {1}, @Notes = {2}",
|
||||
[clusterId, draftGenerationId, (object?)notes ?? DBNull.Value],
|
||||
ct);
|
||||
}
|
||||
|
||||
public async Task RollbackAsync(string clusterId, long targetGenerationId, string? notes, CancellationToken ct)
|
||||
{
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.sp_RollbackToGeneration @ClusterId = {0}, @TargetGenerationId = {1}, @Notes = {2}",
|
||||
[clusterId, targetGenerationId, (object?)notes ?? DBNull.Value],
|
||||
ct);
|
||||
}
|
||||
|
||||
public async Task<List<DiffRow>> ComputeDiffAsync(long from, long to, CancellationToken ct)
|
||||
{
|
||||
var results = new List<DiffRow>();
|
||||
await using var conn = (SqlConnection)db.Database.GetDbConnection();
|
||||
if (conn.State != System.Data.ConnectionState.Open) await conn.OpenAsync(ct);
|
||||
|
||||
await using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = "EXEC dbo.sp_ComputeGenerationDiff @FromGenerationId = @f, @ToGenerationId = @t";
|
||||
cmd.Parameters.AddWithValue("@f", from);
|
||||
cmd.Parameters.AddWithValue("@t", to);
|
||||
|
||||
await using var reader = await cmd.ExecuteReaderAsync(ct);
|
||||
while (await reader.ReadAsync(ct))
|
||||
results.Add(new DiffRow(reader.GetString(0), reader.GetString(1), reader.GetString(2)));
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record DiffRow(string TableName, string LogicalId, string ChangeKind);
|
||||
@@ -1,32 +0,0 @@
|
||||
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Surfaces the local-node historian queue health on the Admin UI's
|
||||
/// <c>/alarms/historian</c> diagnostics page (Phase 7 plan decisions #16/#21).
|
||||
/// Exposes queue depth / drain state / last-error, and lets the operator retry
|
||||
/// dead-lettered rows without restarting the node.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The sink injected here is the server-process <see cref="IAlarmHistorianSink"/>.
|
||||
/// When <see cref="NullAlarmHistorianSink"/> is bound (historian disabled for this
|
||||
/// deployment), <see cref="TryRetryDeadLettered"/> silently returns 0 and
|
||||
/// <see cref="GetStatus"/> reports <see cref="HistorianDrainState.Disabled"/>.
|
||||
/// </remarks>
|
||||
public sealed class HistorianDiagnosticsService(IAlarmHistorianSink sink)
|
||||
{
|
||||
public HistorianSinkStatus GetStatus() => sink.GetStatus();
|
||||
|
||||
/// <summary>
|
||||
/// Operator action from the UI's "Retry dead-lettered" button. Returns the number
|
||||
/// of rows revived so the UI can flash a confirmation. When the live sink doesn't
|
||||
/// implement retry (test doubles, Null sink), returns 0.
|
||||
/// </summary>
|
||||
public int TryRetryDeadLettered()
|
||||
{
|
||||
if (sink is SqliteStoreAndForwardSink concrete)
|
||||
return concrete.RetryDeadLettered();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// One row per <see cref="DriverHostStatus"/> record, enriched with the owning
|
||||
/// <c>ClusterNode.ClusterId</c> (left-join) + the per-<c>(DriverInstanceId, HostName)</c>
|
||||
/// <see cref="DriverInstanceResilienceStatus"/> counters (also left-join) so the Admin
|
||||
/// <c>/hosts</c> page renders the resilience surface inline with host state.
|
||||
/// </summary>
|
||||
public sealed record HostStatusRow(
|
||||
string NodeId,
|
||||
string? ClusterId,
|
||||
string DriverInstanceId,
|
||||
string HostName,
|
||||
DriverHostState State,
|
||||
DateTime StateChangedUtc,
|
||||
DateTime LastSeenUtc,
|
||||
string? Detail,
|
||||
int ConsecutiveFailures,
|
||||
DateTime? LastCircuitBreakerOpenUtc,
|
||||
int CurrentBulkheadDepth,
|
||||
DateTime? LastRecycleUtc);
|
||||
|
||||
/// <summary>
|
||||
/// Read-side service for the Admin UI's per-host drill-down. Loads
|
||||
/// <see cref="DriverHostStatus"/> rows (written by the Server process's
|
||||
/// <c>HostStatusPublisher</c>) and left-joins <c>ClusterNode</c> so each row knows which
|
||||
/// cluster it belongs to — the Admin UI groups by cluster for the fleet-wide view.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The publisher heartbeat is 10s (<c>HostStatusPublisher.HeartbeatInterval</c>). The
|
||||
/// Admin page also polls every ~10s and treats rows with <c>LastSeenUtc</c> older than
|
||||
/// <c>StaleThreshold</c> (30s) as stale — covers a missed heartbeat tolerance plus
|
||||
/// a generous buffer for clock skew and publisher GC pauses.
|
||||
/// </remarks>
|
||||
public sealed class HostStatusService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public static readonly TimeSpan StaleThreshold = TimeSpan.FromSeconds(30);
|
||||
|
||||
/// <summary>Consecutive-failure threshold at which <see cref="IsFlagged"/> returns <c>true</c>
|
||||
/// so the Admin UI can paint a red badge. Matches Phase 6.1 decision #143's conservative
|
||||
/// half-of-breaker-threshold convention — flags before the breaker actually opens.</summary>
|
||||
public const int FailureFlagThreshold = 3;
|
||||
|
||||
public async Task<IReadOnlyList<HostStatusRow>> ListAsync(CancellationToken ct = default)
|
||||
{
|
||||
// Two LEFT JOINs:
|
||||
// 1. ClusterNodes on NodeId — row persists even when its owning ClusterNode row
|
||||
// hasn't been created yet (first-boot bootstrap case).
|
||||
// 2. DriverInstanceResilienceStatuses on (DriverInstanceId, HostName) — resilience
|
||||
// counters haven't been sampled yet for brand-new hosts, so a missing row means
|
||||
// zero failures + never-opened breaker.
|
||||
var rows = await (from s in db.DriverHostStatuses.AsNoTracking()
|
||||
join n in db.ClusterNodes.AsNoTracking()
|
||||
on s.NodeId equals n.NodeId into nodeJoin
|
||||
from n in nodeJoin.DefaultIfEmpty()
|
||||
join r in db.DriverInstanceResilienceStatuses.AsNoTracking()
|
||||
on new { s.DriverInstanceId, s.HostName } equals new { r.DriverInstanceId, r.HostName } into resilJoin
|
||||
from r in resilJoin.DefaultIfEmpty()
|
||||
orderby s.NodeId, s.DriverInstanceId, s.HostName
|
||||
select new HostStatusRow(
|
||||
s.NodeId,
|
||||
n != null ? n.ClusterId : null,
|
||||
s.DriverInstanceId,
|
||||
s.HostName,
|
||||
s.State,
|
||||
s.StateChangedUtc,
|
||||
s.LastSeenUtc,
|
||||
s.Detail,
|
||||
r != null ? r.ConsecutiveFailures : 0,
|
||||
r != null ? r.LastCircuitBreakerOpenUtc : null,
|
||||
r != null ? r.CurrentBulkheadDepth : 0,
|
||||
r != null ? r.LastRecycleUtc : null)).ToListAsync(ct);
|
||||
return rows;
|
||||
}
|
||||
|
||||
public static bool IsStale(HostStatusRow row) =>
|
||||
DateTime.UtcNow - row.LastSeenUtc > StaleThreshold;
|
||||
|
||||
/// <summary>
|
||||
/// Red-badge predicate — <c>true</c> when the host has accumulated enough consecutive
|
||||
/// failures that an operator should take notice before the breaker trips.
|
||||
/// </summary>
|
||||
public static bool IsFlagged(HostStatusRow row) =>
|
||||
row.ConsecutiveFailures >= FailureFlagThreshold;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
public sealed class NamespaceService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<Namespace>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.Namespaces.AsNoTracking()
|
||||
.Where(n => n.GenerationId == generationId)
|
||||
.OrderBy(n => n.NamespaceId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<Namespace> AddAsync(
|
||||
long draftId, string clusterId, string namespaceUri, NamespaceKind kind, CancellationToken ct)
|
||||
{
|
||||
var ns = new Namespace
|
||||
{
|
||||
GenerationId = draftId,
|
||||
NamespaceId = $"ns-{Guid.NewGuid():N}"[..20],
|
||||
ClusterId = clusterId,
|
||||
NamespaceUri = namespaceUri,
|
||||
Kind = kind,
|
||||
};
|
||||
db.Namespaces.Add(ns);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return ns;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
public sealed class NodeAclService(OtOpcUaConfigDbContext db, AclChangeNotifier? notifier = null)
|
||||
{
|
||||
public Task<List<NodeAcl>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.NodeAcls.AsNoTracking()
|
||||
.Where(a => a.GenerationId == generationId)
|
||||
.OrderBy(a => a.LdapGroup)
|
||||
.ThenBy(a => a.ScopeKind)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<NodeAcl> GrantAsync(
|
||||
long draftId, string clusterId, string ldapGroup, NodeAclScopeKind scopeKind, string? scopeId,
|
||||
NodePermissions permissions, string? notes, CancellationToken ct)
|
||||
{
|
||||
var acl = new NodeAcl
|
||||
{
|
||||
GenerationId = draftId,
|
||||
NodeAclId = $"acl-{Guid.NewGuid():N}"[..20],
|
||||
ClusterId = clusterId,
|
||||
LdapGroup = ldapGroup,
|
||||
ScopeKind = scopeKind,
|
||||
ScopeId = scopeId,
|
||||
PermissionFlags = permissions,
|
||||
Notes = notes,
|
||||
};
|
||||
db.NodeAcls.Add(acl);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
if (notifier is not null)
|
||||
await notifier.NotifyNodeAclChangedAsync(clusterId, draftId, ct);
|
||||
|
||||
return acl;
|
||||
}
|
||||
|
||||
public async Task RevokeAsync(Guid nodeAclRowId, CancellationToken ct)
|
||||
{
|
||||
var row = await db.NodeAcls.FirstOrDefaultAsync(a => a.NodeAclRowId == nodeAclRowId, ct);
|
||||
if (row is null) return;
|
||||
db.NodeAcls.Remove(row);
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
if (notifier is not null)
|
||||
await notifier.NotifyNodeAclChangedAsync(row.ClusterId, row.GenerationId, ct);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Runs an ad-hoc permission probe against a draft or published generation's NodeAcl rows —
|
||||
/// "if LDAP group X asks for permission Y on node Z, would the trie grant it, and which
|
||||
/// rows contributed?" Powers the AclsTab "Probe this permission" form per the #196 sub-slice.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Thin wrapper over <see cref="PermissionTrieBuilder"/> + <see cref="PermissionTrie.CollectMatches"/> —
|
||||
/// the same code path the Server's dispatch layer uses at request time, so a probe result
|
||||
/// is guaranteed to match what the live server would decide. The probe is read-only + has
|
||||
/// no side effects; failing probes do NOT generate audit log rows.
|
||||
/// </remarks>
|
||||
public sealed class PermissionProbeService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
/// <summary>
|
||||
/// Evaluate <paramref name="required"/> against the NodeAcl rows of
|
||||
/// <paramref name="generationId"/> for a request by <paramref name="ldapGroup"/> at
|
||||
/// <paramref name="scope"/>. Returns whether the permission would be granted + the list
|
||||
/// of matching grants so the UI can show *why*.
|
||||
/// </summary>
|
||||
public async Task<PermissionProbeResult> ProbeAsync(
|
||||
long generationId,
|
||||
string ldapGroup,
|
||||
NodeScope scope,
|
||||
NodePermissions required,
|
||||
CancellationToken ct)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(ldapGroup);
|
||||
ArgumentNullException.ThrowIfNull(scope);
|
||||
|
||||
var rows = await db.NodeAcls.AsNoTracking()
|
||||
.Where(a => a.GenerationId == generationId && a.ClusterId == scope.ClusterId)
|
||||
.ToListAsync(ct).ConfigureAwait(false);
|
||||
|
||||
var trie = PermissionTrieBuilder.Build(scope.ClusterId, generationId, rows);
|
||||
var matches = trie.CollectMatches(scope, [ldapGroup]);
|
||||
|
||||
var effective = NodePermissions.None;
|
||||
foreach (var m in matches)
|
||||
effective |= m.PermissionFlags;
|
||||
|
||||
var granted = (effective & required) == required;
|
||||
return new PermissionProbeResult(
|
||||
Granted: granted,
|
||||
Required: required,
|
||||
Effective: effective,
|
||||
Matches: matches);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Outcome of a <see cref="PermissionProbeService.ProbeAsync"/> call.</summary>
|
||||
public sealed record PermissionProbeResult(
|
||||
bool Granted,
|
||||
NodePermissions Required,
|
||||
NodePermissions Effective,
|
||||
IReadOnlyList<MatchedGrant> Matches);
|
||||
@@ -1,102 +0,0 @@
|
||||
using System.Diagnostics.Metrics;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// OpenTelemetry-compatible instrumentation for the redundancy surface. Uses in-box
|
||||
/// <see cref="System.Diagnostics.Metrics"/> so no NuGet dependency is required to emit —
|
||||
/// any MeterListener (dotnet-counters, OpenTelemetry.Extensions.Hosting OTLP exporter,
|
||||
/// Prometheus exporter, etc.) picks up the instruments by the <see cref="MeterName"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Exporter configuration (OTLP, Prometheus, etc.) is intentionally NOT wired here —
|
||||
/// that's a deployment-ops decision that belongs in <c>Program.cs</c> behind an
|
||||
/// <c>appsettings</c> toggle. This class owns only the Meter + instruments so the
|
||||
/// production data stream exists regardless of exporter availability.
|
||||
///
|
||||
/// Counter + gauge names follow the otel-semantic-conventions pattern:
|
||||
/// <c>otopcua.redundancy.*</c> with tags for ClusterId + (for transitions) FromRole/ToRole/NodeId.
|
||||
/// </remarks>
|
||||
public sealed class RedundancyMetrics : IDisposable
|
||||
{
|
||||
public const string MeterName = "ZB.MOM.WW.OtOpcUa.Redundancy";
|
||||
|
||||
private readonly Meter _meter;
|
||||
private readonly Counter<long> _roleTransitions;
|
||||
private readonly object _gaugeLock = new();
|
||||
private readonly Dictionary<string, ClusterGaugeState> _gaugeState = new();
|
||||
|
||||
public RedundancyMetrics()
|
||||
{
|
||||
_meter = new Meter(MeterName, version: "1.0.0");
|
||||
_roleTransitions = _meter.CreateCounter<long>(
|
||||
"otopcua.redundancy.role_transition",
|
||||
unit: "{transition}",
|
||||
description: "Observed RedundancyRole changes per node — tagged FromRole, ToRole, NodeId, ClusterId.");
|
||||
|
||||
// Observable gauges — the callback reports whatever the last Observe*Count call stashed.
|
||||
_meter.CreateObservableGauge(
|
||||
"otopcua.redundancy.primary_count",
|
||||
ObservePrimaryCounts,
|
||||
unit: "{node}",
|
||||
description: "Count of Primary-role nodes per cluster (should be 1 for N+1 redundant clusters, 0 during failover).");
|
||||
_meter.CreateObservableGauge(
|
||||
"otopcua.redundancy.secondary_count",
|
||||
ObserveSecondaryCounts,
|
||||
unit: "{node}",
|
||||
description: "Count of Secondary-role nodes per cluster.");
|
||||
_meter.CreateObservableGauge(
|
||||
"otopcua.redundancy.stale_count",
|
||||
ObserveStaleCounts,
|
||||
unit: "{node}",
|
||||
description: "Count of cluster nodes whose LastSeenAt is older than StaleThreshold.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the per-cluster snapshot consumed by the ObservableGauges. Poller calls this
|
||||
/// at the end of every tick so the collectors see fresh numbers on the next observation
|
||||
/// window (by default 1s for dotnet-counters, configurable per exporter).
|
||||
/// </summary>
|
||||
public void SetClusterCounts(string clusterId, int primary, int secondary, int stale)
|
||||
{
|
||||
lock (_gaugeLock)
|
||||
{
|
||||
_gaugeState[clusterId] = new ClusterGaugeState(primary, secondary, stale);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Increment the role_transition counter when a node's RedundancyRole changes. Tags
|
||||
/// allow breakdowns by from/to roles (e.g. Primary → Secondary for planned failover vs
|
||||
/// Primary → Standalone for emergency recovery) + by cluster for multi-site fleets.
|
||||
/// </summary>
|
||||
public void RecordRoleTransition(string clusterId, string nodeId, string fromRole, string toRole)
|
||||
{
|
||||
_roleTransitions.Add(1,
|
||||
new KeyValuePair<string, object?>("cluster.id", clusterId),
|
||||
new KeyValuePair<string, object?>("node.id", nodeId),
|
||||
new KeyValuePair<string, object?>("from_role", fromRole),
|
||||
new KeyValuePair<string, object?>("to_role", toRole));
|
||||
}
|
||||
|
||||
public void Dispose() => _meter.Dispose();
|
||||
|
||||
private IEnumerable<Measurement<long>> ObservePrimaryCounts() => SnapshotGauge(s => s.Primary);
|
||||
private IEnumerable<Measurement<long>> ObserveSecondaryCounts() => SnapshotGauge(s => s.Secondary);
|
||||
private IEnumerable<Measurement<long>> ObserveStaleCounts() => SnapshotGauge(s => s.Stale);
|
||||
|
||||
private IEnumerable<Measurement<long>> SnapshotGauge(Func<ClusterGaugeState, int> selector)
|
||||
{
|
||||
List<Measurement<long>> results;
|
||||
lock (_gaugeLock)
|
||||
{
|
||||
results = new List<Measurement<long>>(_gaugeState.Count);
|
||||
foreach (var (cluster, state) in _gaugeState)
|
||||
results.Add(new Measurement<long>(selector(state),
|
||||
new KeyValuePair<string, object?>("cluster.id", cluster)));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private readonly record struct ClusterGaugeState(int Primary, int Secondary, int Stale);
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Fleet-wide external-ID reservation inspector + FleetAdmin-only release flow per
|
||||
/// <c>admin-ui.md §"Release an external-ID reservation"</c>. Release is audit-logged
|
||||
/// (<see cref="ConfigAuditLog"/>) via <c>sp_ReleaseExternalIdReservation</c>.
|
||||
/// </summary>
|
||||
public sealed class ReservationService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<ExternalIdReservation>> ListActiveAsync(CancellationToken ct) =>
|
||||
db.ExternalIdReservations.AsNoTracking()
|
||||
.Where(r => r.ReleasedAt == null)
|
||||
.OrderBy(r => r.Kind).ThenBy(r => r.Value)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public Task<List<ExternalIdReservation>> ListReleasedAsync(CancellationToken ct) =>
|
||||
db.ExternalIdReservations.AsNoTracking()
|
||||
.Where(r => r.ReleasedAt != null)
|
||||
.OrderByDescending(r => r.ReleasedAt)
|
||||
.Take(100)
|
||||
.ToListAsync(ct);
|
||||
|
||||
/// <summary>
|
||||
/// Releases an active reservation, recording <paramref name="releasedBy"/> (the signed-in
|
||||
/// Admin-UI operator) in <c>ExternalIdReservation.ReleasedBy</c> and the
|
||||
/// <c>ConfigAuditLog.Principal</c> column.
|
||||
///
|
||||
/// Both <paramref name="reason"/> and <paramref name="releasedBy"/> are required audit
|
||||
/// fields — the stored proc validates them and raises an error if either is null/empty.
|
||||
/// </summary>
|
||||
public async Task ReleaseAsync(string kind, string value, string reason, string releasedBy, CancellationToken ct)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(reason))
|
||||
throw new ArgumentException("ReleaseReason is required (audit invariant)", nameof(reason));
|
||||
if (string.IsNullOrWhiteSpace(releasedBy))
|
||||
throw new ArgumentException("ReleasedBy is required (audit invariant)", nameof(releasedBy));
|
||||
|
||||
await db.Database.ExecuteSqlRawAsync(
|
||||
"EXEC dbo.sp_ReleaseExternalIdReservation @Kind = {0}, @Value = {1}, @ReleaseReason = {2}, @ReleasedBy = {3}",
|
||||
[kind, value, reason, releasedBy],
|
||||
ct);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Draft-generation CRUD for <see cref="Script"/> rows — the C# source code referenced
|
||||
/// by Phase 7 virtual tags and scripted alarms. <see cref="Script.SourceHash"/> is
|
||||
/// recomputed on every save so Core.Scripting's compile cache sees a fresh key when
|
||||
/// source changes and reuses the compile when it doesn't.
|
||||
/// </summary>
|
||||
public sealed class ScriptService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<Script>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.Scripts.AsNoTracking()
|
||||
.Where(s => s.GenerationId == generationId)
|
||||
.OrderBy(s => s.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public Task<Script?> GetAsync(long generationId, string scriptId, CancellationToken ct) =>
|
||||
db.Scripts.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.GenerationId == generationId && s.ScriptId == scriptId, ct);
|
||||
|
||||
public async Task<Script> AddAsync(long generationId, string name, string sourceCode, CancellationToken ct)
|
||||
{
|
||||
var s = new Script
|
||||
{
|
||||
GenerationId = generationId,
|
||||
ScriptId = $"scr-{Guid.NewGuid():N}"[..20],
|
||||
Name = name,
|
||||
SourceCode = sourceCode,
|
||||
SourceHash = ComputeHash(sourceCode),
|
||||
};
|
||||
db.Scripts.Add(s);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return s;
|
||||
}
|
||||
|
||||
public async Task<Script> UpdateAsync(long generationId, string scriptId, string name, string sourceCode, CancellationToken ct)
|
||||
{
|
||||
var s = await db.Scripts.FirstOrDefaultAsync(x => x.GenerationId == generationId && x.ScriptId == scriptId, ct)
|
||||
?? throw new InvalidOperationException($"Script '{scriptId}' not found in generation {generationId}");
|
||||
s.Name = name;
|
||||
s.SourceCode = sourceCode;
|
||||
s.SourceHash = ComputeHash(sourceCode);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return s;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(long generationId, string scriptId, CancellationToken ct)
|
||||
{
|
||||
var s = await db.Scripts.FirstOrDefaultAsync(x => x.GenerationId == generationId && x.ScriptId == scriptId, ct);
|
||||
if (s is null) return;
|
||||
db.Scripts.Remove(s);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
internal static string ComputeHash(string source)
|
||||
{
|
||||
var bytes = SHA256.HashData(Encoding.UTF8.GetBytes(source ?? string.Empty));
|
||||
return Convert.ToHexString(bytes);
|
||||
}
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
using Serilog; // resolves Serilog.ILogger explicitly in signatures
|
||||
using Serilog.Core;
|
||||
using Serilog.Events;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
||||
using ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Dry-run harness for the Phase 7 scripting UI. Takes a script + a synthetic input
|
||||
/// map + evaluates once, returns the output (or rejection / exception) plus any
|
||||
/// logger emissions the script produced. Per Phase 7 plan decision #22: only inputs
|
||||
/// the <see cref="DependencyExtractor"/> identified can be supplied, so a dependency
|
||||
/// the harness can't prove statically surfaces as a harness error, not a runtime
|
||||
/// surprise later.
|
||||
/// </summary>
|
||||
public sealed class ScriptTestHarnessService
|
||||
{
|
||||
/// <summary>
|
||||
/// Evaluate <paramref name="source"/> as a virtual-tag script (return value is the
|
||||
/// tag's new value). <paramref name="inputs"/> supplies synthetic
|
||||
/// <see cref="DataValueSnapshot"/>s for every path the extractor found.
|
||||
/// </summary>
|
||||
public async Task<ScriptTestResult> RunVirtualTagAsync(
|
||||
string source, IDictionary<string, DataValueSnapshot> inputs, CancellationToken ct)
|
||||
{
|
||||
var deps = DependencyExtractor.Extract(source);
|
||||
if (!deps.IsValid)
|
||||
return ScriptTestResult.DependencyRejections(deps.Rejections);
|
||||
|
||||
var missing = deps.Reads.Where(r => !inputs.ContainsKey(r)).ToArray();
|
||||
if (missing.Length > 0)
|
||||
return ScriptTestResult.MissingInputs(missing);
|
||||
|
||||
var extra = inputs.Keys.Where(k => !deps.Reads.Contains(k)).ToArray();
|
||||
if (extra.Length > 0)
|
||||
return ScriptTestResult.UnknownInputs(extra);
|
||||
|
||||
ScriptEvaluator<HarnessVirtualTagContext, object?> evaluator;
|
||||
try
|
||||
{
|
||||
evaluator = ScriptEvaluator<HarnessVirtualTagContext, object?>.Compile(source);
|
||||
}
|
||||
catch (Exception compileEx)
|
||||
{
|
||||
return ScriptTestResult.Threw(compileEx.Message, []);
|
||||
}
|
||||
var capturing = new CapturingSink();
|
||||
var logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.Sink(capturing).CreateLogger();
|
||||
var ctx = new HarnessVirtualTagContext(inputs, logger);
|
||||
|
||||
try
|
||||
{
|
||||
var result = await evaluator.RunAsync(ctx, ct);
|
||||
return ScriptTestResult.Ok(result, ctx.Writes, capturing.Events);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ScriptTestResult.Threw(ex.Message, capturing.Events);
|
||||
}
|
||||
}
|
||||
|
||||
// Public so Roslyn's script compilation can reference the context type through the
|
||||
// ScriptGlobals<T> surface. The harness instantiates this directly; operators never see it.
|
||||
public sealed class HarnessVirtualTagContext(
|
||||
IDictionary<string, DataValueSnapshot> inputs, Serilog.ILogger logger) : ScriptContext
|
||||
{
|
||||
public Dictionary<string, object?> Writes { get; } = [];
|
||||
public override DataValueSnapshot GetTag(string path) =>
|
||||
inputs.TryGetValue(path, out var v)
|
||||
? v
|
||||
: new DataValueSnapshot(null, Ua.StatusCodes.BadNotFound, null, DateTime.UtcNow);
|
||||
public override void SetVirtualTag(string path, object? value) => Writes[path] = value;
|
||||
public override DateTime Now => DateTime.UtcNow;
|
||||
public override Serilog.ILogger Logger => logger;
|
||||
}
|
||||
|
||||
private sealed class CapturingSink : ILogEventSink
|
||||
{
|
||||
public List<LogEvent> Events { get; } = [];
|
||||
public void Emit(LogEvent e) => Events.Add(e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Harness outcome: outputs, write-set, logger events, or a rejection/throw reason.</summary>
|
||||
public sealed record ScriptTestResult(
|
||||
ScriptTestOutcome Outcome,
|
||||
object? Output,
|
||||
IReadOnlyDictionary<string, object?> Writes,
|
||||
IReadOnlyList<LogEvent> LogEvents,
|
||||
IReadOnlyList<string> Errors)
|
||||
{
|
||||
public static ScriptTestResult Ok(object? output, IReadOnlyDictionary<string, object?> writes, IReadOnlyList<LogEvent> logs) =>
|
||||
new(ScriptTestOutcome.Success, output, writes, logs, []);
|
||||
public static ScriptTestResult Threw(string reason, IReadOnlyList<LogEvent> logs) =>
|
||||
new(ScriptTestOutcome.Threw, null, new Dictionary<string, object?>(), logs, [reason]);
|
||||
public static ScriptTestResult DependencyRejections(IReadOnlyList<DependencyRejection> rejs) =>
|
||||
new(ScriptTestOutcome.DependencyRejected, null, new Dictionary<string, object?>(), [],
|
||||
rejs.Select(r => r.Message).ToArray());
|
||||
public static ScriptTestResult MissingInputs(string[] paths) =>
|
||||
new(ScriptTestOutcome.MissingInputs, null, new Dictionary<string, object?>(), [],
|
||||
paths.Select(p => $"Missing synthetic input: {p}").ToArray());
|
||||
public static ScriptTestResult UnknownInputs(string[] paths) =>
|
||||
new(ScriptTestOutcome.UnknownInputs, null, new Dictionary<string, object?>(), [],
|
||||
paths.Select(p => $"Input '{p}' is not referenced by the script — remove it").ToArray());
|
||||
}
|
||||
|
||||
public enum ScriptTestOutcome
|
||||
{
|
||||
Success,
|
||||
Threw,
|
||||
DependencyRejected,
|
||||
MissingInputs,
|
||||
UnknownInputs,
|
||||
}
|
||||
|
||||
file static class Ua
|
||||
{
|
||||
// Mirrors OPC UA StatusCodes.BadNotFound without pulling the OPC stack into Admin.
|
||||
public static class StatusCodes { public const uint BadNotFound = 0x803E0000; }
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>Draft-generation CRUD for <see cref="ScriptedAlarm"/> rows.</summary>
|
||||
public sealed class ScriptedAlarmService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<ScriptedAlarm>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.ScriptedAlarms.AsNoTracking()
|
||||
.Where(a => a.GenerationId == generationId)
|
||||
.OrderBy(a => a.EquipmentId).ThenBy(a => a.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<ScriptedAlarm> AddAsync(
|
||||
long generationId, string equipmentId, string name, string alarmType,
|
||||
int severity, string messageTemplate, string predicateScriptId,
|
||||
bool historizeToAveva, bool retain, CancellationToken ct)
|
||||
{
|
||||
var a = new ScriptedAlarm
|
||||
{
|
||||
GenerationId = generationId,
|
||||
ScriptedAlarmId = $"sal-{Guid.NewGuid():N}"[..20],
|
||||
EquipmentId = equipmentId,
|
||||
Name = name,
|
||||
AlarmType = alarmType,
|
||||
Severity = severity,
|
||||
MessageTemplate = messageTemplate,
|
||||
PredicateScriptId = predicateScriptId,
|
||||
HistorizeToAveva = historizeToAveva,
|
||||
Retain = retain,
|
||||
};
|
||||
db.ScriptedAlarms.Add(a);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return a;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(long generationId, string scriptedAlarmId, CancellationToken ct)
|
||||
{
|
||||
var a = await db.ScriptedAlarms.FirstOrDefaultAsync(x => x.GenerationId == generationId && x.ScriptedAlarmId == scriptedAlarmId, ct);
|
||||
if (a is null) return;
|
||||
db.ScriptedAlarms.Remove(a);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the persistent state row (ack/confirm/shelve) for this alarm identity —
|
||||
/// alarm state is NOT generation-scoped per Phase 7 plan decision #14, so the
|
||||
/// lookup is by <see cref="ScriptedAlarm.ScriptedAlarmId"/> only.
|
||||
/// </summary>
|
||||
public Task<ScriptedAlarmState?> GetStateAsync(string scriptedAlarmId, CancellationToken ct) =>
|
||||
db.ScriptedAlarmStates.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.ScriptedAlarmId == scriptedAlarmId, ct);
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// #155 — Tag CRUD scoped to a draft generation. Tags are the canonical signal definitions
|
||||
/// (one row per OPC UA variable) the Server materialises into the address space at startup.
|
||||
/// Mirrors the shape of <see cref="EquipmentService"/>; writes are restricted to draft
|
||||
/// generations only (published generations are immutable per the validation pipeline).
|
||||
/// </summary>
|
||||
public sealed class TagService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
/// <summary>Lists all tags in a generation, ordered by name. Optional driver / equipment filter.</summary>
|
||||
public Task<List<Tag>> ListAsync(long generationId,
|
||||
string? driverInstanceId = null,
|
||||
string? equipmentId = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var query = db.Tags.AsNoTracking().Where(t => t.GenerationId == generationId);
|
||||
if (!string.IsNullOrWhiteSpace(driverInstanceId))
|
||||
query = query.Where(t => t.DriverInstanceId == driverInstanceId);
|
||||
if (!string.IsNullOrWhiteSpace(equipmentId))
|
||||
query = query.Where(t => t.EquipmentId == equipmentId);
|
||||
return query.OrderBy(t => t.Name).ToListAsync(ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new tag row in the given draft. TagId is auto-derived as a GUID — the
|
||||
/// human-friendly Name is the user-facing identifier.
|
||||
/// </summary>
|
||||
public async Task<Tag> CreateAsync(long draftId, Tag input, CancellationToken ct)
|
||||
{
|
||||
input.GenerationId = draftId;
|
||||
if (string.IsNullOrWhiteSpace(input.TagId))
|
||||
input.TagId = Guid.NewGuid().ToString("N");
|
||||
db.Tags.Add(input);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return input;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Tag updated, CancellationToken ct)
|
||||
{
|
||||
var existing = await db.Tags
|
||||
.FirstOrDefaultAsync(t => t.TagRowId == updated.TagRowId, ct)
|
||||
?? throw new InvalidOperationException($"Tag row {updated.TagRowId} not found");
|
||||
|
||||
// Editable fields. TagId / GenerationId are immutable; the Validation pipeline rejects
|
||||
// changes that would break referential integrity (sp_ValidateDraft per decision #110).
|
||||
existing.Name = updated.Name;
|
||||
existing.DriverInstanceId = updated.DriverInstanceId;
|
||||
existing.DeviceId = updated.DeviceId;
|
||||
existing.EquipmentId = updated.EquipmentId;
|
||||
existing.FolderPath = updated.FolderPath;
|
||||
existing.DataType = updated.DataType;
|
||||
existing.AccessLevel = updated.AccessLevel;
|
||||
existing.WriteIdempotent = updated.WriteIdempotent;
|
||||
existing.PollGroupId = updated.PollGroupId;
|
||||
existing.TagConfig = updated.TagConfig;
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid tagRowId, CancellationToken ct)
|
||||
{
|
||||
var existing = await db.Tags.FirstOrDefaultAsync(t => t.TagRowId == tagRowId, ct);
|
||||
if (existing is null) return;
|
||||
db.Tags.Remove(existing);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -1,213 +0,0 @@
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Pure-function impact preview for UNS structural moves per Phase 6.4 Stream A.2. Given
|
||||
/// a <see cref="UnsMoveOperation"/> plus a snapshot of the draft's UNS tree and its
|
||||
/// equipment + tag counts, returns an <see cref="UnsImpactPreview"/> the Admin UI shows
|
||||
/// in a confirmation modal before committing the move.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>Stateless + deterministic — testable without EF or a live draft. The caller
|
||||
/// (Razor page) loads the draft's snapshot via the normal Configuration services, passes
|
||||
/// it in, and the analyzer counts + categorises the impact. The returned
|
||||
/// <see cref="UnsImpactPreview.RevisionToken"/> is the token the caller must re-check at
|
||||
/// confirm time; a mismatch means another operator mutated the draft between preview +
|
||||
/// confirm and the operation needs to be refreshed (decision on concurrent-edit safety
|
||||
/// in Phase 6.4 Scope).</para>
|
||||
///
|
||||
/// <para>Cross-cluster moves are rejected here (decision #82) — equipment is
|
||||
/// cluster-scoped; the UI disables the drop target and surfaces an Export/Import workflow
|
||||
/// toast instead.</para>
|
||||
/// </remarks>
|
||||
public static class UnsImpactAnalyzer
|
||||
{
|
||||
/// <summary>Run the analyzer. Returns a populated preview or throws for invalid operations.</summary>
|
||||
public static UnsImpactPreview Analyze(UnsTreeSnapshot snapshot, UnsMoveOperation move)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(snapshot);
|
||||
ArgumentNullException.ThrowIfNull(move);
|
||||
|
||||
// Cross-cluster guard — the analyzer refuses rather than silently re-homing.
|
||||
if (!string.Equals(move.SourceClusterId, move.TargetClusterId, StringComparison.OrdinalIgnoreCase))
|
||||
throw new CrossClusterMoveRejectedException(
|
||||
"Equipment is cluster-scoped (decision #82). Use Export → Import to migrate equipment " +
|
||||
"across clusters; drag/drop rejected.");
|
||||
|
||||
return move.Kind switch
|
||||
{
|
||||
UnsMoveKind.LineMove => AnalyzeLineMove(snapshot, move),
|
||||
UnsMoveKind.AreaRename => AnalyzeAreaRename(snapshot, move),
|
||||
UnsMoveKind.LineMerge => AnalyzeLineMerge(snapshot, move),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(move), move.Kind, $"Unsupported move kind {move.Kind}"),
|
||||
};
|
||||
}
|
||||
|
||||
private static UnsImpactPreview AnalyzeLineMove(UnsTreeSnapshot snapshot, UnsMoveOperation move)
|
||||
{
|
||||
var line = snapshot.FindLine(move.SourceLineId!)
|
||||
?? throw new UnsMoveValidationException($"Source line '{move.SourceLineId}' not found in draft {snapshot.DraftGenerationId}.");
|
||||
|
||||
var targetArea = snapshot.FindArea(move.TargetAreaId!)
|
||||
?? throw new UnsMoveValidationException($"Target area '{move.TargetAreaId}' not found in draft {snapshot.DraftGenerationId}.");
|
||||
|
||||
var warnings = new List<string>();
|
||||
if (targetArea.LineIds.Contains(line.LineId, StringComparer.OrdinalIgnoreCase))
|
||||
warnings.Add($"Target area '{targetArea.Name}' already contains line '{line.Name}' — dropping a no-op move.");
|
||||
|
||||
// If the target area has a line with the same display name as the mover, warn about
|
||||
// visual ambiguity even though the IDs differ (operators frequently reuse line names).
|
||||
if (targetArea.LineIds.Any(lid =>
|
||||
snapshot.FindLine(lid) is { } sibling &&
|
||||
string.Equals(sibling.Name, line.Name, StringComparison.OrdinalIgnoreCase) &&
|
||||
!string.Equals(sibling.LineId, line.LineId, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
warnings.Add($"Target area '{targetArea.Name}' already has a line named '{line.Name}'. Consider renaming before the move.");
|
||||
}
|
||||
|
||||
return new UnsImpactPreview
|
||||
{
|
||||
AffectedEquipmentCount = line.EquipmentCount,
|
||||
AffectedTagCount = line.TagCount,
|
||||
CascadeWarnings = warnings,
|
||||
RevisionToken = snapshot.RevisionToken,
|
||||
HumanReadableSummary =
|
||||
$"Moving line '{line.Name}' from area '{snapshot.FindAreaByLineId(line.LineId)?.Name ?? "?"}' " +
|
||||
$"to '{targetArea.Name}' will re-home {line.EquipmentCount} equipment + re-parent {line.TagCount} tags.",
|
||||
};
|
||||
}
|
||||
|
||||
private static UnsImpactPreview AnalyzeAreaRename(UnsTreeSnapshot snapshot, UnsMoveOperation move)
|
||||
{
|
||||
var area = snapshot.FindArea(move.SourceAreaId!)
|
||||
?? throw new UnsMoveValidationException($"Source area '{move.SourceAreaId}' not found in draft {snapshot.DraftGenerationId}.");
|
||||
|
||||
var affectedEquipment = area.LineIds
|
||||
.Select(lid => snapshot.FindLine(lid)?.EquipmentCount ?? 0)
|
||||
.Sum();
|
||||
var affectedTags = area.LineIds
|
||||
.Select(lid => snapshot.FindLine(lid)?.TagCount ?? 0)
|
||||
.Sum();
|
||||
|
||||
return new UnsImpactPreview
|
||||
{
|
||||
AffectedEquipmentCount = affectedEquipment,
|
||||
AffectedTagCount = affectedTags,
|
||||
CascadeWarnings = [],
|
||||
RevisionToken = snapshot.RevisionToken,
|
||||
HumanReadableSummary =
|
||||
$"Renaming area '{area.Name}' → '{move.NewName}' cascades to {area.LineIds.Count} lines / " +
|
||||
$"{affectedEquipment} equipment / {affectedTags} tags.",
|
||||
};
|
||||
}
|
||||
|
||||
private static UnsImpactPreview AnalyzeLineMerge(UnsTreeSnapshot snapshot, UnsMoveOperation move)
|
||||
{
|
||||
var src = snapshot.FindLine(move.SourceLineId!)
|
||||
?? throw new UnsMoveValidationException($"Source line '{move.SourceLineId}' not found.");
|
||||
var dst = snapshot.FindLine(move.TargetLineId!)
|
||||
?? throw new UnsMoveValidationException($"Target line '{move.TargetLineId}' not found.");
|
||||
|
||||
var warnings = new List<string>();
|
||||
if (!string.Equals(snapshot.FindAreaByLineId(src.LineId)?.AreaId,
|
||||
snapshot.FindAreaByLineId(dst.LineId)?.AreaId,
|
||||
StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
warnings.Add($"Lines '{src.Name}' and '{dst.Name}' are in different areas. The merge will re-parent equipment + tags into '{dst.Name}'s area.");
|
||||
}
|
||||
|
||||
return new UnsImpactPreview
|
||||
{
|
||||
AffectedEquipmentCount = src.EquipmentCount,
|
||||
AffectedTagCount = src.TagCount,
|
||||
CascadeWarnings = warnings,
|
||||
RevisionToken = snapshot.RevisionToken,
|
||||
HumanReadableSummary =
|
||||
$"Merging line '{src.Name}' into '{dst.Name}': {src.EquipmentCount} equipment + {src.TagCount} tags re-parent. " +
|
||||
$"The source line is deleted at commit.",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Kind of UNS structural move the analyzer understands.</summary>
|
||||
public enum UnsMoveKind
|
||||
{
|
||||
/// <summary>Drag a whole line from one area to another.</summary>
|
||||
LineMove,
|
||||
|
||||
/// <summary>Rename an area (cascades to the UNS paths of every equipment + tag below it).</summary>
|
||||
AreaRename,
|
||||
|
||||
/// <summary>Merge two lines into one; source line's equipment + tags are re-parented.</summary>
|
||||
LineMerge,
|
||||
}
|
||||
|
||||
/// <summary>One UNS structural move request.</summary>
|
||||
/// <param name="Kind">Move variant — selects which source + target fields are required.</param>
|
||||
/// <param name="SourceClusterId">Cluster of the source node. Must match <see cref="TargetClusterId"/> (decision #82).</param>
|
||||
/// <param name="TargetClusterId">Cluster of the target node.</param>
|
||||
/// <param name="SourceAreaId">Source area id for <see cref="UnsMoveKind.AreaRename"/>.</param>
|
||||
/// <param name="SourceLineId">Source line id for <see cref="UnsMoveKind.LineMove"/> / <see cref="UnsMoveKind.LineMerge"/>.</param>
|
||||
/// <param name="TargetAreaId">Target area id for <see cref="UnsMoveKind.LineMove"/>.</param>
|
||||
/// <param name="TargetLineId">Target line id for <see cref="UnsMoveKind.LineMerge"/>.</param>
|
||||
/// <param name="NewName">New display name for <see cref="UnsMoveKind.AreaRename"/>.</param>
|
||||
public sealed record UnsMoveOperation(
|
||||
UnsMoveKind Kind,
|
||||
string SourceClusterId,
|
||||
string TargetClusterId,
|
||||
string? SourceAreaId = null,
|
||||
string? SourceLineId = null,
|
||||
string? TargetAreaId = null,
|
||||
string? TargetLineId = null,
|
||||
string? NewName = null);
|
||||
|
||||
/// <summary>Snapshot of the UNS tree + counts the analyzer walks.</summary>
|
||||
public sealed class UnsTreeSnapshot
|
||||
{
|
||||
public required long DraftGenerationId { get; init; }
|
||||
public required DraftRevisionToken RevisionToken { get; init; }
|
||||
public required IReadOnlyList<UnsAreaSummary> Areas { get; init; }
|
||||
public required IReadOnlyList<UnsLineSummary> Lines { get; init; }
|
||||
|
||||
public UnsAreaSummary? FindArea(string areaId) =>
|
||||
Areas.FirstOrDefault(a => string.Equals(a.AreaId, areaId, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
public UnsLineSummary? FindLine(string lineId) =>
|
||||
Lines.FirstOrDefault(l => string.Equals(l.LineId, lineId, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
public UnsAreaSummary? FindAreaByLineId(string lineId) =>
|
||||
Areas.FirstOrDefault(a => a.LineIds.Contains(lineId, StringComparer.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
public sealed record UnsAreaSummary(string AreaId, string Name, IReadOnlyList<string> LineIds);
|
||||
|
||||
public sealed record UnsLineSummary(string LineId, string Name, int EquipmentCount, int TagCount);
|
||||
|
||||
/// <summary>
|
||||
/// Opaque per-draft revision fingerprint. Preview fetches the current token + stores it
|
||||
/// in the <see cref="UnsImpactPreview.RevisionToken"/>. Confirm compares the token against
|
||||
/// the draft's live value; mismatch means another operator mutated the draft between
|
||||
/// preview + commit — raise <c>409 Conflict / refresh-required</c> in the UI.
|
||||
/// </summary>
|
||||
public sealed record DraftRevisionToken(string Value)
|
||||
{
|
||||
/// <summary>Compare two tokens for equality; null-safe.</summary>
|
||||
public bool Matches(DraftRevisionToken? other) =>
|
||||
other is not null &&
|
||||
string.Equals(Value, other.Value, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <summary>Output of <see cref="UnsImpactAnalyzer.Analyze"/>.</summary>
|
||||
public sealed class UnsImpactPreview
|
||||
{
|
||||
public required int AffectedEquipmentCount { get; init; }
|
||||
public required int AffectedTagCount { get; init; }
|
||||
public required IReadOnlyList<string> CascadeWarnings { get; init; }
|
||||
public required DraftRevisionToken RevisionToken { get; init; }
|
||||
public required string HumanReadableSummary { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>Thrown when a move targets a different cluster than the source (decision #82).</summary>
|
||||
public sealed class CrossClusterMoveRejectedException(string message) : Exception(message);
|
||||
|
||||
/// <summary>Thrown when the move operation references a source / target that doesn't exist in the draft.</summary>
|
||||
public sealed class UnsMoveValidationException(string message) : Exception(message);
|
||||
@@ -1,180 +0,0 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
public sealed class UnsService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<UnsArea>> ListAreasAsync(long generationId, CancellationToken ct) =>
|
||||
db.UnsAreas.AsNoTracking()
|
||||
.Where(a => a.GenerationId == generationId)
|
||||
.OrderBy(a => a.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public Task<List<UnsLine>> ListLinesAsync(long generationId, CancellationToken ct) =>
|
||||
db.UnsLines.AsNoTracking()
|
||||
.Where(l => l.GenerationId == generationId)
|
||||
.OrderBy(l => l.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<UnsArea> AddAreaAsync(long draftId, string clusterId, string name, string? notes, CancellationToken ct)
|
||||
{
|
||||
var area = new UnsArea
|
||||
{
|
||||
GenerationId = draftId,
|
||||
UnsAreaId = $"area-{Guid.NewGuid():N}"[..20],
|
||||
ClusterId = clusterId,
|
||||
Name = name,
|
||||
Notes = notes,
|
||||
};
|
||||
db.UnsAreas.Add(area);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return area;
|
||||
}
|
||||
|
||||
public async Task<UnsLine> AddLineAsync(long draftId, string unsAreaId, string name, string? notes, CancellationToken ct)
|
||||
{
|
||||
var line = new UnsLine
|
||||
{
|
||||
GenerationId = draftId,
|
||||
UnsLineId = $"line-{Guid.NewGuid():N}"[..20],
|
||||
UnsAreaId = unsAreaId,
|
||||
Name = name,
|
||||
Notes = notes,
|
||||
};
|
||||
db.UnsLines.Add(line);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return line;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build the full UNS tree snapshot for the analyzer. Walks areas + lines in the draft
|
||||
/// and counts equipment + tags per line. Returns the snapshot plus a deterministic
|
||||
/// revision token computed by SHA-256'ing the sorted (kind, id, parent, name) tuples —
|
||||
/// stable across processes + changes whenever any row is added / modified / deleted.
|
||||
/// </summary>
|
||||
public async Task<UnsTreeSnapshot> LoadSnapshotAsync(long generationId, CancellationToken ct)
|
||||
{
|
||||
var areas = await db.UnsAreas.AsNoTracking()
|
||||
.Where(a => a.GenerationId == generationId)
|
||||
.OrderBy(a => a.UnsAreaId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var lines = await db.UnsLines.AsNoTracking()
|
||||
.Where(l => l.GenerationId == generationId)
|
||||
.OrderBy(l => l.UnsLineId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var equipmentCounts = await db.Equipment.AsNoTracking()
|
||||
.Where(e => e.GenerationId == generationId)
|
||||
.GroupBy(e => e.UnsLineId)
|
||||
.Select(g => new { LineId = g.Key, Count = g.Count() })
|
||||
.ToListAsync(ct);
|
||||
var equipmentByLine = equipmentCounts.ToDictionary(x => x.LineId, x => x.Count, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var lineSummaries = lines.Select(l =>
|
||||
new UnsLineSummary(
|
||||
LineId: l.UnsLineId,
|
||||
Name: l.Name,
|
||||
EquipmentCount: equipmentByLine.GetValueOrDefault(l.UnsLineId),
|
||||
TagCount: 0)).ToList();
|
||||
|
||||
var areaSummaries = areas.Select(a =>
|
||||
new UnsAreaSummary(
|
||||
AreaId: a.UnsAreaId,
|
||||
Name: a.Name,
|
||||
LineIds: lines.Where(l => string.Equals(l.UnsAreaId, a.UnsAreaId, StringComparison.OrdinalIgnoreCase))
|
||||
.Select(l => l.UnsLineId).ToList())).ToList();
|
||||
|
||||
return new UnsTreeSnapshot
|
||||
{
|
||||
DraftGenerationId = generationId,
|
||||
RevisionToken = ComputeRevisionToken(areas, lines),
|
||||
Areas = areaSummaries,
|
||||
Lines = lineSummaries,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Atomic re-parent of a line to a new area inside the same draft. The caller must pass
|
||||
/// the revision token it observed at preview time — a mismatch raises
|
||||
/// <see cref="DraftRevisionConflictException"/> so the UI can show the 409 concurrent-edit
|
||||
/// modal instead of silently overwriting a peer's work.
|
||||
/// </summary>
|
||||
public async Task MoveLineAsync(
|
||||
long generationId,
|
||||
DraftRevisionToken expected,
|
||||
string lineId,
|
||||
string targetAreaId,
|
||||
CancellationToken ct)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(expected);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(lineId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(targetAreaId);
|
||||
|
||||
var supportsTx = db.Database.IsRelational();
|
||||
Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction? tx = null;
|
||||
if (supportsTx) tx = await db.Database.BeginTransactionAsync(ct).ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
var areas = await db.UnsAreas
|
||||
.Where(a => a.GenerationId == generationId)
|
||||
.OrderBy(a => a.UnsAreaId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var lines = await db.UnsLines
|
||||
.Where(l => l.GenerationId == generationId)
|
||||
.OrderBy(l => l.UnsLineId)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var current = ComputeRevisionToken(areas, lines);
|
||||
if (!current.Matches(expected))
|
||||
throw new DraftRevisionConflictException(
|
||||
$"Draft {generationId} changed since preview. Expected revision {expected.Value}, saw {current.Value}. " +
|
||||
"Refresh + redo the move.");
|
||||
|
||||
var line = lines.FirstOrDefault(l => string.Equals(l.UnsLineId, lineId, StringComparison.OrdinalIgnoreCase))
|
||||
?? throw new InvalidOperationException($"Line '{lineId}' not found in draft {generationId}.");
|
||||
|
||||
if (!areas.Any(a => string.Equals(a.UnsAreaId, targetAreaId, StringComparison.OrdinalIgnoreCase)))
|
||||
throw new InvalidOperationException($"Target area '{targetAreaId}' not found in draft {generationId}.");
|
||||
|
||||
if (string.Equals(line.UnsAreaId, targetAreaId, StringComparison.OrdinalIgnoreCase))
|
||||
return; // no-op drop — same area
|
||||
|
||||
line.UnsAreaId = targetAreaId;
|
||||
await db.SaveChangesAsync(ct);
|
||||
if (tx is not null) await tx.CommitAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (tx is not null) await tx.RollbackAsync(ct).ConfigureAwait(false);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (tx is not null) await tx.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static DraftRevisionToken ComputeRevisionToken(IReadOnlyList<UnsArea> areas, IReadOnlyList<UnsLine> lines)
|
||||
{
|
||||
var sb = new StringBuilder(capacity: 256 + (areas.Count + lines.Count) * 80);
|
||||
foreach (var a in areas.OrderBy(a => a.UnsAreaId, StringComparer.Ordinal))
|
||||
sb.Append("A:").Append(a.UnsAreaId).Append('|').Append(a.Name).Append('|').Append(a.Notes ?? "").Append(';');
|
||||
foreach (var l in lines.OrderBy(l => l.UnsLineId, StringComparer.Ordinal))
|
||||
sb.Append("L:").Append(l.UnsLineId).Append('|').Append(l.UnsAreaId).Append('|').Append(l.Name).Append('|').Append(l.Notes ?? "").Append(';');
|
||||
|
||||
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(sb.ToString()));
|
||||
return new DraftRevisionToken(Convert.ToHexStringLower(hash)[..16]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Thrown when a UNS move's expected revision token no longer matches the live draft
|
||||
/// — another operator mutated the draft between preview + commit. Caller surfaces a 409-style
|
||||
/// "refresh required" modal in the Admin UI.</summary>
|
||||
public sealed class DraftRevisionConflictException(string message) : Exception(message);
|
||||
@@ -1,117 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Enums;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Draft-aware write surface over <see cref="NodeAcl"/>. Replaces direct
|
||||
/// <see cref="NodeAclService"/> CRUD for Admin UI grant authoring; the raw service stays
|
||||
/// as the read / delete surface. Enforces the invariants listed in Phase 6.2 Stream D.2:
|
||||
/// scope-uniqueness per (LdapGroup, ScopeKind, ScopeId, GenerationId), grant shape
|
||||
/// consistency, and no empty permission masks.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>Per decision #129 grants are additive — <see cref="NodePermissions.None"/> is
|
||||
/// rejected at write time. Explicit Deny is v2.1 and is not representable in the current
|
||||
/// <c>NodeAcl</c> row; attempts to express it (e.g. empty permission set) surface as
|
||||
/// <see cref="InvalidNodeAclGrantException"/>.</para>
|
||||
///
|
||||
/// <para>Draft scope: writes always target an unpublished (Draft-state) generation id.
|
||||
/// Once a generation publishes, its rows are frozen.</para>
|
||||
/// </remarks>
|
||||
public sealed class ValidatedNodeAclAuthoringService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
/// <summary>Add a new grant row to the given draft generation.</summary>
|
||||
public async Task<NodeAcl> GrantAsync(
|
||||
long draftGenerationId,
|
||||
string clusterId,
|
||||
string ldapGroup,
|
||||
NodeAclScopeKind scopeKind,
|
||||
string? scopeId,
|
||||
NodePermissions permissions,
|
||||
string? notes,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(clusterId);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(ldapGroup);
|
||||
|
||||
ValidateGrantShape(scopeKind, scopeId, permissions);
|
||||
await EnsureNoDuplicate(draftGenerationId, clusterId, ldapGroup, scopeKind, scopeId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var row = new NodeAcl
|
||||
{
|
||||
GenerationId = draftGenerationId,
|
||||
NodeAclId = $"acl-{Guid.NewGuid():N}"[..20],
|
||||
ClusterId = clusterId,
|
||||
LdapGroup = ldapGroup,
|
||||
ScopeKind = scopeKind,
|
||||
ScopeId = scopeId,
|
||||
PermissionFlags = permissions,
|
||||
Notes = notes,
|
||||
};
|
||||
db.NodeAcls.Add(row);
|
||||
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return row;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace an existing grant's permission set in place. Validates the new shape;
|
||||
/// rejects attempts to blank-out to None (that's a Revoke via <see cref="NodeAclService"/>).
|
||||
/// </summary>
|
||||
public async Task<NodeAcl> UpdatePermissionsAsync(
|
||||
Guid nodeAclRowId,
|
||||
NodePermissions newPermissions,
|
||||
string? notes,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (newPermissions == NodePermissions.None)
|
||||
throw new InvalidNodeAclGrantException(
|
||||
"Permission set cannot be None — revoke the row instead of writing an empty grant.");
|
||||
|
||||
var row = await db.NodeAcls.FirstOrDefaultAsync(a => a.NodeAclRowId == nodeAclRowId, cancellationToken).ConfigureAwait(false)
|
||||
?? throw new InvalidNodeAclGrantException($"NodeAcl row {nodeAclRowId} not found.");
|
||||
|
||||
row.PermissionFlags = newPermissions;
|
||||
if (notes is not null) row.Notes = notes;
|
||||
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
return row;
|
||||
}
|
||||
|
||||
private static void ValidateGrantShape(NodeAclScopeKind scopeKind, string? scopeId, NodePermissions permissions)
|
||||
{
|
||||
if (permissions == NodePermissions.None)
|
||||
throw new InvalidNodeAclGrantException(
|
||||
"Permission set cannot be None — grants must carry at least one flag (decision #129, additive only).");
|
||||
|
||||
if (scopeKind == NodeAclScopeKind.Cluster && !string.IsNullOrEmpty(scopeId))
|
||||
throw new InvalidNodeAclGrantException(
|
||||
"Cluster-scope grants must have null ScopeId. ScopeId only applies to sub-cluster scopes.");
|
||||
|
||||
if (scopeKind != NodeAclScopeKind.Cluster && string.IsNullOrEmpty(scopeId))
|
||||
throw new InvalidNodeAclGrantException(
|
||||
$"ScopeKind={scopeKind} requires a populated ScopeId.");
|
||||
}
|
||||
|
||||
private async Task EnsureNoDuplicate(
|
||||
long generationId, string clusterId, string ldapGroup, NodeAclScopeKind scopeKind, string? scopeId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var exists = await db.NodeAcls.AsNoTracking()
|
||||
.AnyAsync(a => a.GenerationId == generationId
|
||||
&& a.ClusterId == clusterId
|
||||
&& a.LdapGroup == ldapGroup
|
||||
&& a.ScopeKind == scopeKind
|
||||
&& a.ScopeId == scopeId,
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (exists)
|
||||
throw new InvalidNodeAclGrantException(
|
||||
$"A grant for (LdapGroup={ldapGroup}, ScopeKind={scopeKind}, ScopeId={scopeId}) already exists in generation {generationId}. " +
|
||||
"Update the existing row's permissions instead of inserting a duplicate.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Thrown when a <see cref="NodeAcl"/> grant authoring request violates an invariant.</summary>
|
||||
public sealed class InvalidNodeAclGrantException(string message) : Exception(message);
|
||||
@@ -1,53 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration;
|
||||
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Admin.Services;
|
||||
|
||||
/// <summary>Draft-generation CRUD for <see cref="VirtualTag"/> rows.</summary>
|
||||
public sealed class VirtualTagService(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
public Task<List<VirtualTag>> ListAsync(long generationId, CancellationToken ct) =>
|
||||
db.VirtualTags.AsNoTracking()
|
||||
.Where(v => v.GenerationId == generationId)
|
||||
.OrderBy(v => v.EquipmentId).ThenBy(v => v.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
public async Task<VirtualTag> AddAsync(
|
||||
long generationId, string equipmentId, string name, string dataType, string scriptId,
|
||||
bool changeTriggered, int? timerIntervalMs, bool historize, CancellationToken ct)
|
||||
{
|
||||
var v = new VirtualTag
|
||||
{
|
||||
GenerationId = generationId,
|
||||
VirtualTagId = $"vt-{Guid.NewGuid():N}"[..20],
|
||||
EquipmentId = equipmentId,
|
||||
Name = name,
|
||||
DataType = dataType,
|
||||
ScriptId = scriptId,
|
||||
ChangeTriggered = changeTriggered,
|
||||
TimerIntervalMs = timerIntervalMs,
|
||||
Historize = historize,
|
||||
};
|
||||
db.VirtualTags.Add(v);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return v;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(long generationId, string virtualTagId, CancellationToken ct)
|
||||
{
|
||||
var v = await db.VirtualTags.FirstOrDefaultAsync(x => x.GenerationId == generationId && x.VirtualTagId == virtualTagId, ct);
|
||||
if (v is null) return;
|
||||
db.VirtualTags.Remove(v);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<VirtualTag> UpdateEnabledAsync(long generationId, string virtualTagId, bool enabled, CancellationToken ct)
|
||||
{
|
||||
var v = await db.VirtualTags.FirstOrDefaultAsync(x => x.GenerationId == generationId && x.VirtualTagId == virtualTagId, ct)
|
||||
?? throw new InvalidOperationException($"VirtualTag '{virtualTagId}' not found in generation {generationId}");
|
||||
v.Enabled = enabled;
|
||||
await db.SaveChangesAsync(ct);
|
||||
return v;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<NoWarn>$(NoWarn);CS1591</NoWarn>
|
||||
<RootNamespace>ZB.MOM.WW.OtOpcUa.Admin</RootNamespace>
|
||||
<AssemblyName>OtOpcUa.Admin</AssemblyName>
|
||||
<!-- Admin-004: dev secrets (ConfigDb connection string, LDAP service-account password)
|
||||
live in user-secrets, not in the committed appsettings.json. -->
|
||||
<UserSecretsId>zb-mom-ww-otopcua-admin</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer"/>
|
||||
<PackageReference Include="Novell.Directory.Ldap.NETStandard"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client"/>
|
||||
<PackageReference Include="Serilog.AspNetCore"/>
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting"/>
|
||||
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Core\ZB.MOM.WW.OtOpcUa.Configuration\ZB.MOM.WW.OtOpcUa.Configuration.csproj"/>
|
||||
<ProjectReference Include="..\..\Core\ZB.MOM.WW.OtOpcUa.Core\ZB.MOM.WW.OtOpcUa.Core.csproj"/>
|
||||
<ProjectReference Include="..\..\Core\ZB.MOM.WW.OtOpcUa.Core.Scripting\ZB.MOM.WW.OtOpcUa.Core.Scripting.csproj"/>
|
||||
<ProjectReference Include="..\..\Core\ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian\ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian.csproj"/>
|
||||
<ProjectReference Include="..\..\Drivers\ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing\ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="ZB.MOM.WW.OtOpcUa.Admin.Tests"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-37gx-xxp4-5rgx"/>
|
||||
<NuGetAuditSuppress Include="https://github.com/advisories/GHSA-w3x6-4m5h-cxqf"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,33 +0,0 @@
|
||||
{
|
||||
"_secrets": "Admin-004: no secrets are committed here. Supply the ConfigDb connection string and the LDAP service-account password via user-secrets (dev) or environment variables / a secret store (prod). Env-var keys: ConnectionStrings__ConfigDb and Authentication__Ldap__ServiceAccountPassword. The connection string defaults to Encrypt=True (TLS); use a least-privilege SQL login, not 'sa'.",
|
||||
"ConnectionStrings": {
|
||||
"ConfigDb": ""
|
||||
},
|
||||
"Authentication": {
|
||||
"Ldap": {
|
||||
"Enabled": true,
|
||||
"Server": "localhost",
|
||||
"Port": 3893,
|
||||
"UseTls": false,
|
||||
"AllowInsecureLdap": true,
|
||||
"SearchBase": "dc=lmxopcua,dc=local",
|
||||
"ServiceAccountDn": "cn=serviceaccount,dc=lmxopcua,dc=local",
|
||||
"ServiceAccountPassword": "",
|
||||
"DisplayNameAttribute": "cn",
|
||||
"GroupAttribute": "memberOf",
|
||||
"GroupToRole": {
|
||||
"ReadOnly": "ConfigViewer",
|
||||
"ReadWrite": "ConfigEditor",
|
||||
"AlarmAck": "FleetAdmin"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Serilog": {
|
||||
"MinimumLevel": "Information"
|
||||
},
|
||||
"Metrics": {
|
||||
"Prometheus": {
|
||||
"Enabled": true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
@page "/"
|
||||
|
||||
<PageTitle>OtOpcUa</PageTitle>
|
||||
|
||||
<h1>OtOpcUa Admin</h1>
|
||||
<p>v2 fused host. Use the nav above to manage deployments.</p>
|
||||
<p class="text-muted">Most v1 admin pages were removed by the live-edit migration — see follow-up F15 for the per-page restoration plan.</p>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user