Complete OPC UA data flow: binding UI, flattening connections, real OPC UA client

- Add connection binding UI to Instances page (per-attribute and bulk assign)
- FlatteningService populates Connections dict from bound data connections
- Real OPC UA client using OPC Foundation SDK for live tag subscriptions
- DataConnectionFactory uses RealOpcUaClientFactory by default
- OpcUaDataConnection supports both "endpoint" and "EndpointUrl" config keys
This commit is contained in:
Joseph Doherty
2026-03-17 11:40:39 -04:00
parent dfb809a909
commit 8e1d0816b3
6 changed files with 366 additions and 2 deletions

View File

@@ -178,10 +178,67 @@
<button class="btn btn-outline-success btn-sm py-0 px-1 me-1"
@onclick="() => EnableInstance(inst)" disabled="@_actionInProgress">Enable</button>
}
<button class="btn btn-outline-info btn-sm py-0 px-1 me-1"
@onclick="() => ToggleBindings(inst)">Bindings</button>
<button class="btn btn-outline-danger btn-sm py-0 px-1"
@onclick="() => DeleteInstance(inst)" disabled="@_actionInProgress">Delete</button>
</td>
</tr>
@if (_bindingInstanceId == inst.Id)
{
<tr>
<td colspan="7" class="bg-light p-3">
<div class="d-flex justify-content-between align-items-center mb-2">
<strong>Connection Bindings for @inst.UniqueName</strong>
@if (_bindingDataSourceAttrs.Count > 0 && _siteConnections.Count > 0)
{
<div>
<select class="form-select form-select-sm d-inline-block me-1" style="width:auto;" @bind="_bulkConnectionId">
<option value="0">Select connection...</option>
@foreach (var c in _siteConnections)
{
<option value="@c.Id">@c.Name (@c.Protocol)</option>
}
</select>
<button class="btn btn-sm btn-outline-primary" @onclick="ApplyBulkBinding" disabled="@(_bulkConnectionId == 0)">Assign All</button>
</div>
}
</div>
@if (_bindingDataSourceAttrs.Count == 0)
{
<p class="text-muted small mb-0">No data-sourced attributes in this template.</p>
}
else
{
<table class="table table-sm table-bordered mb-2">
<thead class="table-light">
<tr><th>Attribute</th><th>Tag Path</th><th>Connection</th></tr>
</thead>
<tbody>
@foreach (var attr in _bindingDataSourceAttrs)
{
<tr>
<td class="small">@attr.Name</td>
<td class="small text-muted font-monospace">@attr.DataSourceReference</td>
<td>
<select class="form-select form-select-sm" value="@GetBindingConnectionId(attr.Name)"
@onchange="(e) => OnBindingChanged(attr.Name, e)">
<option value="0">— none —</option>
@foreach (var c in _siteConnections)
{
<option value="@c.Id">@c.Name</option>
}
</select>
</td>
</tr>
}
</tbody>
</table>
<button class="btn btn-success btn-sm" @onclick="SaveBindings" disabled="@_actionInProgress">Save Bindings</button>
}
</td>
</tr>
}
}
</tbody>
</table>
@@ -482,4 +539,100 @@
_createError = $"Create failed: {ex.Message}";
}
}
// Connection binding state
private int _bindingInstanceId;
private List<TemplateAttribute> _bindingDataSourceAttrs = new();
private List<DataConnection> _siteConnections = new();
private Dictionary<string, int> _bindingSelections = new();
private int _bulkConnectionId;
private async Task ToggleBindings(Instance inst)
{
if (_bindingInstanceId == inst.Id)
{
_bindingInstanceId = 0;
return;
}
_bindingInstanceId = inst.Id;
_bindingSelections.Clear();
_bulkConnectionId = 0;
// Load template attributes with DataSourceReference
var attrs = await TemplateEngineRepository.GetAttributesByTemplateIdAsync(inst.TemplateId);
_bindingDataSourceAttrs = attrs.Where(a => !string.IsNullOrEmpty(a.DataSourceReference)).ToList();
// Load data connections for this site
_siteConnections = (await SiteRepository.GetDataConnectionsBySiteIdAsync(inst.SiteId)).ToList();
if (_siteConnections.Count == 0)
{
// Also show unassigned connections (they may not be assigned to a site yet)
_siteConnections = (await SiteRepository.GetAllDataConnectionsAsync()).ToList();
}
// Load existing bindings
var existingBindings = await TemplateEngineRepository.GetBindingsByInstanceIdAsync(inst.Id);
foreach (var b in existingBindings)
{
_bindingSelections[b.AttributeName] = b.DataConnectionId;
}
}
private int GetBindingConnectionId(string attrName)
{
return _bindingSelections.GetValueOrDefault(attrName, 0);
}
private void OnBindingChanged(string attrName, ChangeEventArgs e)
{
var val = int.TryParse(e.Value?.ToString(), out var id) ? id : 0;
SetBinding(attrName, val);
}
private void SetBinding(string attrName, int connectionId)
{
if (connectionId == 0)
_bindingSelections.Remove(attrName);
else
_bindingSelections[attrName] = connectionId;
}
private void ApplyBulkBinding()
{
if (_bulkConnectionId == 0) return;
foreach (var attr in _bindingDataSourceAttrs)
{
_bindingSelections[attr.Name] = _bulkConnectionId;
}
}
private async Task SaveBindings()
{
_actionInProgress = true;
try
{
var bindings = _bindingSelections
.Select(kv => (kv.Key, kv.Value))
.ToList();
var result = await InstanceService.SetConnectionBindingsAsync(
_bindingInstanceId, bindings, "system");
if (result.IsSuccess)
{
_toast.ShowSuccess($"Saved {bindings.Count} connection bindings.");
_bindingInstanceId = 0;
}
else
{
_toast.ShowError($"Save failed: {result.Error}");
}
}
catch (Exception ex)
{
_toast.ShowError($"Save failed: {ex.Message}");
}
_actionInProgress = false;
}
}