152 lines
5.8 KiB
Plaintext
152 lines
5.8 KiB
Plaintext
@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">
|
|
@if (Id.HasValue)
|
|
{
|
|
<div class="mb-2">
|
|
<label class="form-label small">Site</label>
|
|
<input type="text" class="form-control form-control-sm" value="@(_siteName)" disabled />
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<div class="mb-2">
|
|
<label class="form-label small">Site</label>
|
|
<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-2">
|
|
<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="LmxProxy">LMX Proxy</option>
|
|
<option value="Custom">Custom</option>
|
|
</select>
|
|
</div>
|
|
<div class="mb-2">
|
|
<label class="form-label small">Configuration (JSON)</label>
|
|
<input type="text" class="form-control form-control-sm" @bind="_formConfiguration"
|
|
placeholder='e.g. {"endpoint":"opc.tcp://..."}' />
|
|
</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; }
|
|
|
|
private bool _loading = true;
|
|
private DataConnection? _editingConnection;
|
|
private List<Site> _sites = new();
|
|
private int _formSiteId;
|
|
private string _siteName = string.Empty;
|
|
private string _formName = string.Empty;
|
|
private string _formProtocol = string.Empty;
|
|
private string? _formConfiguration;
|
|
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}";
|
|
_formName = _editingConnection.Name;
|
|
_formProtocol = _editingConnection.Protocol;
|
|
_formConfiguration = _editingConnection.PrimaryConfiguration;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Failed to load connection: {ex.Message}";
|
|
}
|
|
}
|
|
_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();
|
|
await SiteRepository.UpdateDataConnectionAsync(_editingConnection);
|
|
}
|
|
else
|
|
{
|
|
var conn = new DataConnection(_formName.Trim(), _formProtocol, _formSiteId)
|
|
{
|
|
PrimaryConfiguration = _formConfiguration?.Trim()
|
|
};
|
|
await SiteRepository.AddDataConnectionAsync(conn);
|
|
}
|
|
await SiteRepository.SaveChangesAsync();
|
|
NavigationManager.NavigateTo("/admin/data-connections");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_formError = $"Save failed: {ex.Message}";
|
|
}
|
|
}
|
|
|
|
private void GoBack()
|
|
{
|
|
NavigationManager.NavigateTo("/admin/data-connections");
|
|
}
|
|
}
|