da5fdf0e63
Brings the Data Connections admin page up to the same UX standard as the Topology page: - Search box with dim non-matches (opacity 0.4, shape preserved) - Toolbar: + Connection (disabled until a site is selected), Refresh, Expand, Collapse - Site context menu gains "Add Connection here" that navigates with ?siteId= so the form preselects + locks the Site field - Form gains "Primary Endpoint" / "Backup Endpoint" h6 subsection headers matching the SiteForm convention; Failover Retry Count moved inside the Backup subsection - URL renamed: /admin/connections (primary) + /admin/data-connections (legacy secondary @page). Same dual-route treatment on the form - Nav label: "Data Connections" -> "Connections" - Adds DataConnectionsPageTests bUnit suite (6 tests)
216 lines
8.7 KiB
Plaintext
216 lines
8.7 KiB
Plaintext
@page "/admin/connections/create"
|
|
@page "/admin/connections/{Id:int}/edit"
|
|
@page "/admin/data-connections/create"
|
|
@page "/admin/data-connections/{Id:int}/edit"
|
|
@using ScadaLink.Security
|
|
@using ScadaLink.Commons.Entities.Sites
|
|
@using ScadaLink.Commons.Interfaces.Repositories
|
|
@attribute [Authorize(Policy = AuthorizationPolicies.RequireAdmin)]
|
|
@inject ISiteRepository SiteRepository
|
|
@inject NavigationManager NavigationManager
|
|
|
|
<div class="container-fluid mt-3">
|
|
<div class="d-flex align-items-center mb-3">
|
|
<button class="btn btn-outline-secondary btn-sm me-3" @onclick="GoBack">← Back</button>
|
|
<h4 class="mb-0">@(Id.HasValue ? "Edit Data Connection" : "Add Data Connection")</h4>
|
|
</div>
|
|
|
|
@if (_loading)
|
|
{
|
|
<LoadingSpinner IsLoading="true" />
|
|
}
|
|
else
|
|
{
|
|
<div class="card mb-3">
|
|
<div class="card-body">
|
|
<div class="mb-2">
|
|
<label class="form-label small">Site</label>
|
|
@if (_siteLocked)
|
|
{
|
|
<input type="text" class="form-control form-control-sm" value="@_siteName" disabled />
|
|
}
|
|
else
|
|
{
|
|
<select class="form-select form-select-sm" @bind="_formSiteId">
|
|
<option value="0">Select site...</option>
|
|
@foreach (var site in _sites)
|
|
{
|
|
<option value="@site.Id">@site.Name</option>
|
|
}
|
|
</select>
|
|
}
|
|
</div>
|
|
<div class="mb-2">
|
|
<label class="form-label small">Name</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formName" />
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label small">Protocol</label>
|
|
<select class="form-select form-select-sm" @bind="_formProtocol">
|
|
<option value="">Select...</option>
|
|
<option value="OpcUa">OPC UA</option>
|
|
<option value="Custom">Custom</option>
|
|
</select>
|
|
</div>
|
|
|
|
<h6 class="text-muted border-bottom pb-1">Primary Endpoint</h6>
|
|
<div class="mb-3">
|
|
<label class="form-label small">Configuration</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formConfiguration"
|
|
placeholder='e.g. {"endpoint":"opc.tcp://..."}' />
|
|
</div>
|
|
|
|
<h6 class="text-muted border-bottom pb-1">Backup Endpoint</h6>
|
|
@if (!_showBackup)
|
|
{
|
|
<div class="mb-3">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm"
|
|
@onclick="() => _showBackup = true">
|
|
Add Backup Endpoint
|
|
</button>
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="mb-2">
|
|
<label class="form-label small">Configuration</label>
|
|
<textarea class="form-control form-control-sm" rows="4"
|
|
@bind="_formBackupConfiguration"
|
|
placeholder='{"Host": "backup-host", "Port": 50101}' />
|
|
</div>
|
|
<div class="mb-2">
|
|
<label class="form-label small">Failover Retry Count</label>
|
|
<input type="number" class="form-control form-control-sm" style="max-width: 120px;"
|
|
min="1" max="20"
|
|
@bind="_formFailoverRetryCount" />
|
|
<div class="form-text">Retries on active endpoint before switching to backup (default: 3)</div>
|
|
</div>
|
|
<div class="mb-3">
|
|
<button type="button" class="btn btn-outline-danger btn-sm"
|
|
@onclick="RemoveBackup">
|
|
Remove Backup
|
|
</button>
|
|
</div>
|
|
}
|
|
|
|
@if (_formError != null)
|
|
{
|
|
<div class="text-danger small mt-2">@_formError</div>
|
|
}
|
|
<div class="mt-3">
|
|
<button class="btn btn-success btn-sm me-1" @onclick="SaveConnection">Save</button>
|
|
<button class="btn btn-outline-secondary btn-sm" @onclick="GoBack">Cancel</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
}
|
|
</div>
|
|
|
|
@code {
|
|
[Parameter] public int? Id { get; set; }
|
|
[SupplyParameterFromQuery] public int? SiteId { get; set; }
|
|
|
|
private bool _loading = true;
|
|
private DataConnection? _editingConnection;
|
|
private List<Site> _sites = new();
|
|
private int _formSiteId;
|
|
private string _siteName = string.Empty;
|
|
private bool _siteLocked;
|
|
private string _formName = string.Empty;
|
|
private string _formProtocol = string.Empty;
|
|
private string? _formConfiguration;
|
|
private bool _showBackup;
|
|
private string? _formBackupConfiguration;
|
|
private int _formFailoverRetryCount = 3;
|
|
private string? _formError;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
_sites = (await SiteRepository.GetAllSitesAsync()).ToList();
|
|
|
|
if (Id.HasValue)
|
|
{
|
|
try
|
|
{
|
|
_editingConnection = await SiteRepository.GetDataConnectionByIdAsync(Id.Value);
|
|
if (_editingConnection != null)
|
|
{
|
|
_formSiteId = _editingConnection.SiteId;
|
|
_siteName = _sites.FirstOrDefault(s => s.Id == _formSiteId)?.Name ?? $"Site {_formSiteId}";
|
|
_siteLocked = true;
|
|
_formName = _editingConnection.Name;
|
|
_formProtocol = _editingConnection.Protocol;
|
|
_formConfiguration = _editingConnection.PrimaryConfiguration;
|
|
_formBackupConfiguration = _editingConnection.BackupConfiguration;
|
|
_formFailoverRetryCount = _editingConnection.FailoverRetryCount;
|
|
_showBackup = _editingConnection.BackupConfiguration != null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Failed to load connection: {ex.Message}";
|
|
}
|
|
}
|
|
else if (SiteId.HasValue)
|
|
{
|
|
var site = _sites.FirstOrDefault(s => s.Id == SiteId.Value);
|
|
if (site != null)
|
|
{
|
|
_formSiteId = site.Id;
|
|
_siteName = site.Name;
|
|
_siteLocked = true;
|
|
}
|
|
}
|
|
_loading = false;
|
|
}
|
|
|
|
private async Task SaveConnection()
|
|
{
|
|
_formError = null;
|
|
if (_formSiteId == 0) { _formError = "Site is required."; return; }
|
|
if (string.IsNullOrWhiteSpace(_formName)) { _formError = "Name is required."; return; }
|
|
if (string.IsNullOrWhiteSpace(_formProtocol)) { _formError = "Protocol is required."; return; }
|
|
|
|
try
|
|
{
|
|
if (_editingConnection != null)
|
|
{
|
|
_editingConnection.Name = _formName.Trim();
|
|
_editingConnection.Protocol = _formProtocol;
|
|
_editingConnection.PrimaryConfiguration = _formConfiguration?.Trim();
|
|
_editingConnection.BackupConfiguration = _showBackup ? _formBackupConfiguration?.Trim() : null;
|
|
_editingConnection.FailoverRetryCount = _showBackup ? _formFailoverRetryCount : 3;
|
|
await SiteRepository.UpdateDataConnectionAsync(_editingConnection);
|
|
}
|
|
else
|
|
{
|
|
var conn = new DataConnection(_formName.Trim(), _formProtocol, _formSiteId)
|
|
{
|
|
PrimaryConfiguration = _formConfiguration?.Trim(),
|
|
BackupConfiguration = _showBackup ? _formBackupConfiguration?.Trim() : null,
|
|
FailoverRetryCount = _showBackup ? _formFailoverRetryCount : 3
|
|
};
|
|
await SiteRepository.AddDataConnectionAsync(conn);
|
|
}
|
|
await SiteRepository.SaveChangesAsync();
|
|
NavigationManager.NavigateTo("/admin/connections");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Save failed: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
private void RemoveBackup()
|
|
{
|
|
_showBackup = false;
|
|
_formBackupConfiguration = null;
|
|
_formFailoverRetryCount = 3;
|
|
}
|
|
|
|
private void GoBack()
|
|
{
|
|
NavigationManager.NavigateTo("/admin/connections");
|
|
}
|
|
}
|