583 lines
29 KiB
Plaintext
583 lines
29 KiB
Plaintext
@page "/clusters/{ClusterId}/drivers/new/focas"
|
||
@attribute [Microsoft.AspNetCore.Authorization.Authorize]
|
||
@rendermode RenderMode.InteractiveServer
|
||
@using Microsoft.AspNetCore.Components.Forms
|
||
@using Microsoft.EntityFrameworkCore
|
||
@using ZB.MOM.WW.OtOpcUa.AdminUI.Clients
|
||
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers
|
||
@using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers.Pickers
|
||
@using ZB.MOM.WW.OtOpcUa.Configuration
|
||
@using ZB.MOM.WW.OtOpcUa.Configuration.Entities
|
||
@using ZB.MOM.WW.OtOpcUa.Driver.FOCAS
|
||
@inject IDbContextFactory<OtOpcUaConfigDbContext> DbFactory
|
||
@inject NavigationManager Nav
|
||
|
||
<div class="d-flex justify-content-between align-items-center mb-3">
|
||
<h4 class="mb-0">@(IsNew ? "New Fanuc FOCAS driver" : "Edit Fanuc FOCAS driver") · <span class="mono">@ClusterId</span></h4>
|
||
<a href="/clusters/@ClusterId/drivers" class="btn btn-outline-secondary btn-sm">Cancel</a>
|
||
</div>
|
||
<ClusterNav ClusterId="@ClusterId" ActiveTab="drivers" />
|
||
|
||
@if (!_loaded)
|
||
{
|
||
<p>Loading…</p>
|
||
}
|
||
else if (!IsNew && _existing is null)
|
||
{
|
||
<section class="panel notice rise" style="animation-delay:.02s">
|
||
Driver instance <span class="mono">@DriverInstanceId</span> was not found in cluster <span class="mono">@ClusterId</span>.
|
||
</section>
|
||
}
|
||
else
|
||
{
|
||
<EditForm Model="_form" OnValidSubmit="SubmitAsync" FormName="focasDriverEdit">
|
||
<DataAnnotationsValidator />
|
||
<DriverFormShell IsNew="IsNew" Busy="_busy" Error="_error"
|
||
CancelHref="@($"/clusters/{ClusterId}/drivers")"
|
||
OnDelete="@(IsNew ? null : (EventCallback?)EventCallback.Factory.Create(this, DeleteAsync))">
|
||
|
||
<DriverIdentitySection Model="_identityModel" Namespaces="_namespaces" IsNew="IsNew" ShowDriverType="false" />
|
||
|
||
@if (!IsNew && !string.IsNullOrEmpty(DriverInstanceId))
|
||
{
|
||
<DriverStatusPanel DriverInstanceId="@DriverInstanceId" Enabled="@_identityModel.Enabled" />
|
||
}
|
||
|
||
<div class="mt-2 mb-3">
|
||
<DriverTestConnectButton DriverType="@DriverTypeKey"
|
||
GetConfigJson="@SerializeCurrentConfig"
|
||
TimeoutSeconds="@_form.AdminProbeTimeoutSeconds" />
|
||
<button type="button" class="btn btn-sm btn-outline-secondary mt-2"
|
||
@onclick="@(() => _showPicker = true)">
|
||
Pick address
|
||
</button>
|
||
</div>
|
||
|
||
<DriverTagPicker @bind-Visible="_showPicker"
|
||
Title="FOCAS address"
|
||
CurrentAddress="@_pickedAddress"
|
||
OnPickAddress="@OnAddressPicked">
|
||
<FOCASAddressPickerBody CurrentAddress="@_pickedAddress"
|
||
CurrentAddressChanged="@((s) => _pickedAddress = s)" />
|
||
</DriverTagPicker>
|
||
|
||
@* Connection *@
|
||
<section class="panel rise mt-3" style="animation-delay:.05s">
|
||
<div class="panel-head">Connection</div>
|
||
<div style="padding:1rem">
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasTimeoutSec">Timeout (seconds)</label>
|
||
<InputNumber id="focasTimeoutSec" @bind-Value="_form.TimeoutSeconds"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">Per-operation timeout. Default 2 s.</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
@* Probe *@
|
||
<section class="panel rise mt-3" style="animation-delay:.08s">
|
||
<div class="panel-head">Connectivity probe</div>
|
||
<div style="padding:1rem">
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<div class="form-check form-switch mt-2">
|
||
<InputCheckbox id="focasProbeEnabled" @bind-Value="_form.ProbeEnabled"
|
||
class="form-check-input" />
|
||
<label class="form-check-label" for="focasProbeEnabled">Probe enabled</label>
|
||
</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasProbeInterval">Probe interval (s)</label>
|
||
<InputNumber id="focasProbeInterval" @bind-Value="_form.ProbeIntervalSeconds"
|
||
class="form-control form-control-sm" />
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasProbeTimeout">Probe timeout (s)</label>
|
||
<InputNumber id="focasProbeTimeout" @bind-Value="_form.ProbeTimeoutSeconds"
|
||
class="form-control form-control-sm" />
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasAdminProbe">Admin probe timeout (s)</label>
|
||
<InputNumber id="focasAdminProbe" @bind-Value="_form.AdminProbeTimeoutSeconds"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">Test Connect timeout (1–60 s).</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
@* Alarm projection *@
|
||
<section class="panel rise mt-3" style="animation-delay:.11s">
|
||
<div class="panel-head">Alarm projection</div>
|
||
<div style="padding:1rem">
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<div class="form-check form-switch mt-2">
|
||
<InputCheckbox id="focasAlarmEnabled" @bind-Value="_form.AlarmProjectionEnabled"
|
||
class="form-check-input" />
|
||
<label class="form-check-label" for="focasAlarmEnabled">Alarm projection enabled</label>
|
||
</div>
|
||
<div class="form-text">Surfaces FOCAS alarms via IAlarmSource.</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasAlarmPoll">Alarm poll interval (s)</label>
|
||
<InputNumber id="focasAlarmPoll" @bind-Value="_form.AlarmProjectionPollIntervalSeconds"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">One cnc_rdalmmsg2 call per device per tick. Default 2 s.</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
@* Handle recycle *@
|
||
<section class="panel rise mt-3" style="animation-delay:.14s">
|
||
<div class="panel-head">Handle recycle</div>
|
||
<div style="padding:1rem">
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<div class="form-check form-switch mt-2">
|
||
<InputCheckbox id="focasRecycleEnabled" @bind-Value="_form.HandleRecycleEnabled"
|
||
class="form-check-input" />
|
||
<label class="form-check-label" for="focasRecycleEnabled">Handle recycle enabled</label>
|
||
</div>
|
||
<div class="form-text">Proactive FWLIB session recycle to prevent handle pool exhaustion. Default off.</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasRecycleInterval">Recycle interval (minutes)</label>
|
||
<InputNumber id="focasRecycleInterval" @bind-Value="_form.HandleRecycleIntervalMinutes"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">Typical: 30 min (shared pool) or 360 min (single client).</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
@* Fixed tree *@
|
||
<section class="panel rise mt-3" style="animation-delay:.17s">
|
||
<div class="panel-head">Fixed-node tree</div>
|
||
<div style="padding:1rem">
|
||
<div class="row">
|
||
<div class="col-md-3 mb-3">
|
||
<div class="form-check form-switch mt-2">
|
||
<InputCheckbox id="focasFixedTree" @bind-Value="_form.FixedTreeEnabled"
|
||
class="form-check-input" />
|
||
<label class="form-check-label" for="focasFixedTree">Fixed tree enabled</label>
|
||
</div>
|
||
<div class="form-text">Exposes Identity/, Axes/, etc. from cnc_sysinfo/cnc_rdaxisname/cnc_rddynamic2. Default off.</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasAxisPoll">Axis poll interval (ms)</label>
|
||
<InputNumber id="focasAxisPoll" @bind-Value="_form.FixedTreePollIntervalMs"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">cnc_rddynamic2 cadence per axis. Default 250 ms.</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasProgramPoll">Program poll interval (s)</label>
|
||
<InputNumber id="focasProgramPoll" @bind-Value="_form.FixedTreeProgramPollIntervalSeconds"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">Program/mode info cadence. 0 = disabled. Default 1 s.</div>
|
||
</div>
|
||
<div class="col-md-3 mb-3">
|
||
<label class="form-label" for="focasTimerPoll">Timer poll interval (s)</label>
|
||
<InputNumber id="focasTimerPoll" @bind-Value="_form.FixedTreeTimerPollIntervalSeconds"
|
||
class="form-control form-control-sm" />
|
||
<div class="form-text">Power-on/cutting/cycle timer cadence. 0 = disabled. Default 30 s.</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
|
||
@* Devices *@
|
||
<CollectionEditor TRow="FocasDeviceRow" Items="_devices" Title="Devices" ItemNoun="device"
|
||
AnimationDelay=".20s"
|
||
NewRow="@(() => new FocasDeviceRow())" Clone="@(r => r.Clone())"
|
||
Validate="FocasDeviceRow.ValidateRow">
|
||
<HeaderTemplate>
|
||
<tr><th>Host address</th><th>CNC series</th><th>Device name</th><th></th></tr>
|
||
</HeaderTemplate>
|
||
<RowTemplate Context="d">
|
||
<td class="mono">@d.HostAddress</td><td>@d.Series</td>
|
||
<td>@(string.IsNullOrWhiteSpace(d.DeviceName) ? "—" : d.DeviceName)</td>
|
||
</RowTemplate>
|
||
<EditTemplate Context="d">
|
||
<div class="row g-3">
|
||
<div class="col-md-6"><label class="form-label">Host address</label>
|
||
<input class="form-control form-control-sm mono" @bind="d.HostAddress"
|
||
placeholder="192.168.0.10:8193" /></div>
|
||
<div class="col-md-3"><label class="form-label">CNC series</label>
|
||
<select class="form-select form-select-sm" @bind="d.Series">
|
||
@foreach (var e in Enum.GetValues<FocasCncSeries>()) { <option value="@e">@e</option> }
|
||
</select></div>
|
||
<div class="col-md-3"><label class="form-label">Device name</label>
|
||
<input class="form-control form-control-sm" @bind="d.DeviceName" /></div>
|
||
</div>
|
||
</EditTemplate>
|
||
</CollectionEditor>
|
||
|
||
@* Tags *@
|
||
<CollectionEditor TRow="FocasTagRow" Items="_tags" Title="Tags" ItemNoun="tag"
|
||
AnimationDelay=".23s"
|
||
NewRow="@(() => new FocasTagRow())" Clone="@(r => r.Clone())"
|
||
Validate="FocasTagRow.ValidateRow">
|
||
<HeaderTemplate>
|
||
<tr><th>Name</th><th>Device</th><th>Address</th><th>Type</th><th>Writable</th><th></th></tr>
|
||
</HeaderTemplate>
|
||
<RowTemplate Context="t">
|
||
<td class="mono">@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>
|
||
</RowTemplate>
|
||
<EditTemplate Context="t">
|
||
<div class="row g-3">
|
||
<div class="col-md-6"><label class="form-label">Name</label>
|
||
<input class="form-control form-control-sm" @bind="t.Name" /></div>
|
||
<div class="col-md-6"><label class="form-label">Device host address</label>
|
||
<input class="form-control form-control-sm mono" @bind="t.DeviceHostAddress"
|
||
placeholder="192.168.0.10:8193" /></div>
|
||
<div class="col-md-6"><label class="form-label">Address</label>
|
||
<input class="form-control form-control-sm mono" @bind="t.Address"
|
||
placeholder="e.g. X0.0, R100, PARAM:1815/0, MACRO:500" /></div>
|
||
<div class="col-md-3"><label class="form-label">Data type</label>
|
||
<select class="form-select form-select-sm" @bind="t.DataType">
|
||
@foreach (var e in Enum.GetValues<FocasDataType>()) { <option value="@e">@e</option> }
|
||
</select></div>
|
||
<div class="col-md-3"><div class="form-check form-switch mt-4">
|
||
<input type="checkbox" class="form-check-input" @bind="t.Writable" id="tagWritable" />
|
||
<label class="form-check-label" for="tagWritable">Writable</label></div></div>
|
||
</div>
|
||
</EditTemplate>
|
||
</CollectionEditor>
|
||
|
||
<DriverResilienceSection @bind-ResilienceConfig="_form.ResilienceConfig" />
|
||
</DriverFormShell>
|
||
</EditForm>
|
||
}
|
||
|
||
@code {
|
||
[Parameter] public string ClusterId { get; set; } = "";
|
||
[Parameter] public string? DriverInstanceId { get; set; }
|
||
|
||
private const string DriverTypeKey = "Focas";
|
||
|
||
private bool IsNew => string.IsNullOrEmpty(DriverInstanceId);
|
||
|
||
private static readonly System.Text.Json.JsonSerializerOptions _jsonOpts = new()
|
||
{
|
||
PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
|
||
UnmappedMemberHandling = System.Text.Json.Serialization.JsonUnmappedMemberHandling.Skip,
|
||
WriteIndented = false,
|
||
};
|
||
|
||
private FormModel _form = new();
|
||
private DriverIdentitySection.DriverIdentityModel _identityModel = new() { DriverType = DriverTypeKey };
|
||
private DriverInstance? _existing;
|
||
private List<Namespace> _namespaces = new();
|
||
private bool _loaded, _busy;
|
||
private string? _error;
|
||
|
||
// Address picker state
|
||
private bool _showPicker;
|
||
private string _pickedAddress = "";
|
||
|
||
private void OnAddressPicked(string address) => _pickedAddress = address;
|
||
|
||
// Held separately because Devices/Tags are collections — edited via the CollectionEditor modal.
|
||
private List<FocasDeviceRow> _devices = [];
|
||
private List<FocasTagRow> _tags = [];
|
||
|
||
protected override async Task OnInitializedAsync()
|
||
{
|
||
await using var db = await DbFactory.CreateDbContextAsync();
|
||
_namespaces = await db.Namespaces.AsNoTracking()
|
||
.Where(n => n.ClusterId == ClusterId)
|
||
.OrderBy(n => n.NamespaceId).ToListAsync();
|
||
|
||
if (IsNew)
|
||
{
|
||
_identityModel = new() { DriverType = DriverTypeKey, NamespaceId = _namespaces.FirstOrDefault()?.NamespaceId ?? "", Enabled = true };
|
||
_form = FormModel.FromOptions(new FocasDriverOptions());
|
||
}
|
||
else
|
||
{
|
||
_existing = await db.DriverInstances.AsNoTracking()
|
||
.FirstOrDefaultAsync(d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||
if (_existing is not null)
|
||
{
|
||
_identityModel = new()
|
||
{
|
||
DriverInstanceId = _existing.DriverInstanceId,
|
||
Name = _existing.Name,
|
||
DriverType = _existing.DriverType,
|
||
NamespaceId = _existing.NamespaceId,
|
||
Enabled = _existing.Enabled,
|
||
};
|
||
var opts = TryDeserialize(_existing.DriverConfig) ?? new FocasDriverOptions();
|
||
_form = FormModel.FromOptions(opts);
|
||
_form.ResilienceConfig = _existing.ResilienceConfig;
|
||
_form.RowVersion = _existing.RowVersion;
|
||
_devices = opts.Devices.Select(FocasDeviceRow.FromDefinition).ToList();
|
||
_tags = opts.Tags.Select(FocasTagRow.FromDefinition).ToList();
|
||
}
|
||
}
|
||
_loaded = true;
|
||
}
|
||
|
||
private async Task SubmitAsync()
|
||
{
|
||
_busy = true; _error = null;
|
||
try
|
||
{
|
||
var opts = _form.ToOptions(
|
||
_devices.Select(r => r.ToDefinition()).ToList(),
|
||
_tags.Select(r => r.ToDefinition()).ToList());
|
||
var configJson = System.Text.Json.JsonSerializer.Serialize(opts, _jsonOpts);
|
||
await using var db = await DbFactory.CreateDbContextAsync();
|
||
if (IsNew)
|
||
{
|
||
if (await db.DriverInstances.AnyAsync(d => d.DriverInstanceId == _identityModel.DriverInstanceId))
|
||
{
|
||
_error = $"Driver instance '{_identityModel.DriverInstanceId}' already exists."; return;
|
||
}
|
||
db.DriverInstances.Add(new DriverInstance
|
||
{
|
||
DriverInstanceId = _identityModel.DriverInstanceId,
|
||
ClusterId = ClusterId,
|
||
NamespaceId = _identityModel.NamespaceId,
|
||
Name = _identityModel.Name,
|
||
DriverType = DriverTypeKey,
|
||
Enabled = _identityModel.Enabled,
|
||
DriverConfig = configJson,
|
||
ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig,
|
||
});
|
||
}
|
||
else
|
||
{
|
||
var entity = await db.DriverInstances.FirstOrDefaultAsync(
|
||
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||
if (entity is null) { _error = "Row no longer exists."; return; }
|
||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||
entity.NamespaceId = _identityModel.NamespaceId;
|
||
entity.Name = _identityModel.Name;
|
||
entity.Enabled = _identityModel.Enabled;
|
||
entity.DriverConfig = configJson;
|
||
entity.ResilienceConfig = string.IsNullOrWhiteSpace(_form.ResilienceConfig) ? null : _form.ResilienceConfig;
|
||
}
|
||
await db.SaveChangesAsync();
|
||
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
_error = "Another user changed this driver instance while you were editing. Reload to see the latest values, then re-apply your changes.";
|
||
}
|
||
catch (Exception ex) { _error = ex.Message; }
|
||
finally { _busy = false; }
|
||
}
|
||
|
||
private async Task DeleteAsync()
|
||
{
|
||
if (IsNew) return;
|
||
_busy = true; _error = null;
|
||
try
|
||
{
|
||
await using var db = await DbFactory.CreateDbContextAsync();
|
||
var entity = await db.DriverInstances.FirstOrDefaultAsync(
|
||
d => d.ClusterId == ClusterId && d.DriverInstanceId == DriverInstanceId);
|
||
if (entity is null) { Nav.NavigateTo($"/clusters/{ClusterId}/drivers"); return; }
|
||
db.Entry(entity).Property(e => e.RowVersion).OriginalValue = _form.RowVersion;
|
||
db.DriverInstances.Remove(entity);
|
||
await db.SaveChangesAsync();
|
||
Nav.NavigateTo($"/clusters/{ClusterId}/drivers");
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
_error = "Another user changed this driver instance while you were viewing it. Reload before deleting.";
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_error = $"Delete failed: {ex.Message}. (Likely because equipment/tags still reference this driver — remove them first.)";
|
||
}
|
||
finally { _busy = false; }
|
||
}
|
||
|
||
private string SerializeCurrentConfig()
|
||
=> System.Text.Json.JsonSerializer.Serialize(
|
||
_form.ToOptions(
|
||
_devices.Select(r => r.ToDefinition()).ToList(),
|
||
_tags.Select(r => r.ToDefinition()).ToList()),
|
||
_jsonOpts);
|
||
|
||
private static FocasDriverOptions? TryDeserialize(string json)
|
||
{
|
||
try { return System.Text.Json.JsonSerializer.Deserialize<FocasDriverOptions>(json, _jsonOpts); }
|
||
catch { return null; }
|
||
}
|
||
|
||
// Mutable VM for the modal editor — FocasDeviceOptions is an immutable record.
|
||
public sealed class FocasDeviceRow
|
||
{
|
||
public string HostAddress { get; set; } = "";
|
||
public FocasCncSeries Series { get; set; } = FocasCncSeries.Unknown;
|
||
public string? DeviceName { get; set; }
|
||
|
||
// Original record (null for newly-added rows). Preserves any fields the editor doesn't
|
||
// expose across a load→save.
|
||
private FocasDeviceOptions? _source;
|
||
|
||
public FocasDeviceRow Clone() => (FocasDeviceRow)MemberwiseClone(); // _source is an immutable record ref — safe to share
|
||
|
||
public static FocasDeviceRow FromDefinition(FocasDeviceOptions d) => new()
|
||
{
|
||
HostAddress = d.HostAddress, Series = d.Series, DeviceName = d.DeviceName,
|
||
_source = d,
|
||
};
|
||
|
||
public FocasDeviceOptions ToDefinition()
|
||
{
|
||
var baseDef = _source ?? new FocasDeviceOptions(HostAddress.Trim());
|
||
return baseDef with
|
||
{
|
||
HostAddress = HostAddress.Trim(),
|
||
Series = Series,
|
||
DeviceName = string.IsNullOrWhiteSpace(DeviceName) ? null : DeviceName.Trim(),
|
||
};
|
||
}
|
||
|
||
public static string? ValidateRow(FocasDeviceRow row, IReadOnlyList<FocasDeviceRow> all, int? editIndex)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(row.HostAddress)) return "Host address is required.";
|
||
for (var i = 0; i < all.Count; i++)
|
||
if (i != editIndex && string.Equals(all[i].HostAddress, row.HostAddress, StringComparison.OrdinalIgnoreCase))
|
||
return $"Duplicate device host address '{row.HostAddress}'.";
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// Mutable VM for the modal editor — FocasTagDefinition is an immutable record.
|
||
public sealed class FocasTagRow
|
||
{
|
||
public string Name { get; set; } = "";
|
||
public string DeviceHostAddress { get; set; } = "";
|
||
public string Address { get; set; } = "";
|
||
public FocasDataType DataType { get; set; } = FocasDataType.Int32;
|
||
public bool Writable { get; set; } = true;
|
||
|
||
// Original record (null for newly-added rows). Preserves fields the editor doesn't expose
|
||
// (WriteIdempotent) across a load→save.
|
||
private FocasTagDefinition? _source;
|
||
|
||
public FocasTagRow Clone() => (FocasTagRow)MemberwiseClone(); // _source is an immutable record ref — safe to share
|
||
|
||
public static FocasTagRow FromDefinition(FocasTagDefinition d) => new()
|
||
{
|
||
Name = d.Name, DeviceHostAddress = d.DeviceHostAddress, Address = d.Address,
|
||
DataType = d.DataType, Writable = d.Writable,
|
||
_source = d,
|
||
};
|
||
|
||
public FocasTagDefinition ToDefinition()
|
||
{
|
||
var baseDef = _source ?? new FocasTagDefinition(Name.Trim(), DeviceHostAddress.Trim(), Address.Trim(), DataType);
|
||
return baseDef with
|
||
{
|
||
Name = Name.Trim(),
|
||
DeviceHostAddress = DeviceHostAddress.Trim(),
|
||
Address = Address.Trim(),
|
||
DataType = DataType,
|
||
Writable = Writable,
|
||
};
|
||
}
|
||
|
||
public static string? ValidateRow(FocasTagRow row, IReadOnlyList<FocasTagRow> all, int? editIndex)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(row.Name)) return "Name is required.";
|
||
for (var i = 0; i < all.Count; i++)
|
||
if (i != editIndex && string.Equals(all[i].Name, row.Name, StringComparison.OrdinalIgnoreCase))
|
||
return $"Duplicate tag name '{row.Name}'.";
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// Flat mutable model — all scalar properties settable for Blazor @bind-Value.
|
||
// Collections (Devices, Tags) are kept on the component and passed in on ToOptions().
|
||
public sealed class FormModel
|
||
{
|
||
// Connection
|
||
public int TimeoutSeconds { get; set; } = 2;
|
||
|
||
// Probe
|
||
public bool ProbeEnabled { get; set; } = true;
|
||
public int ProbeIntervalSeconds { get; set; } = 5;
|
||
public int ProbeTimeoutSeconds { get; set; } = 2;
|
||
public int AdminProbeTimeoutSeconds { get; set; } = 10;
|
||
|
||
// Alarm projection
|
||
public bool AlarmProjectionEnabled { get; set; } = false;
|
||
public int AlarmProjectionPollIntervalSeconds { get; set; } = 2;
|
||
|
||
// Handle recycle
|
||
public bool HandleRecycleEnabled { get; set; } = false;
|
||
public int HandleRecycleIntervalMinutes { get; set; } = 60;
|
||
|
||
// Fixed tree
|
||
public bool FixedTreeEnabled { get; set; } = false;
|
||
public int FixedTreePollIntervalMs { get; set; } = 250;
|
||
public int FixedTreeProgramPollIntervalSeconds { get; set; } = 1;
|
||
public int FixedTreeTimerPollIntervalSeconds { get; set; } = 30;
|
||
|
||
// Common
|
||
public string? ResilienceConfig { get; set; }
|
||
public byte[] RowVersion { get; set; } = [];
|
||
|
||
public static FormModel FromOptions(FocasDriverOptions o) => new()
|
||
{
|
||
TimeoutSeconds = (int)o.Timeout.TotalSeconds,
|
||
ProbeEnabled = o.Probe.Enabled,
|
||
ProbeIntervalSeconds = (int)o.Probe.Interval.TotalSeconds,
|
||
ProbeTimeoutSeconds = (int)o.Probe.Timeout.TotalSeconds,
|
||
AdminProbeTimeoutSeconds = o.ProbeTimeoutSeconds,
|
||
AlarmProjectionEnabled = o.AlarmProjection.Enabled,
|
||
AlarmProjectionPollIntervalSeconds = (int)o.AlarmProjection.PollInterval.TotalSeconds,
|
||
HandleRecycleEnabled = o.HandleRecycle.Enabled,
|
||
HandleRecycleIntervalMinutes = (int)o.HandleRecycle.Interval.TotalMinutes,
|
||
FixedTreeEnabled = o.FixedTree.Enabled,
|
||
FixedTreePollIntervalMs = (int)o.FixedTree.PollInterval.TotalMilliseconds,
|
||
FixedTreeProgramPollIntervalSeconds = (int)o.FixedTree.ProgramPollInterval.TotalSeconds,
|
||
FixedTreeTimerPollIntervalSeconds = (int)o.FixedTree.TimerPollInterval.TotalSeconds,
|
||
};
|
||
|
||
public FocasDriverOptions ToOptions(
|
||
IReadOnlyList<FocasDeviceOptions> devices,
|
||
IReadOnlyList<FocasTagDefinition> tags) => new()
|
||
{
|
||
Timeout = TimeSpan.FromSeconds(TimeoutSeconds),
|
||
Probe = new FocasProbeOptions
|
||
{
|
||
Enabled = ProbeEnabled,
|
||
Interval = TimeSpan.FromSeconds(ProbeIntervalSeconds),
|
||
Timeout = TimeSpan.FromSeconds(ProbeTimeoutSeconds),
|
||
},
|
||
ProbeTimeoutSeconds = AdminProbeTimeoutSeconds,
|
||
AlarmProjection = new FocasAlarmProjectionOptions
|
||
{
|
||
Enabled = AlarmProjectionEnabled,
|
||
PollInterval = TimeSpan.FromSeconds(AlarmProjectionPollIntervalSeconds),
|
||
},
|
||
HandleRecycle = new FocasHandleRecycleOptions
|
||
{
|
||
Enabled = HandleRecycleEnabled,
|
||
Interval = TimeSpan.FromMinutes(HandleRecycleIntervalMinutes),
|
||
},
|
||
FixedTree = new FocasFixedTreeOptions
|
||
{
|
||
Enabled = FixedTreeEnabled,
|
||
PollInterval = TimeSpan.FromMilliseconds(FixedTreePollIntervalMs),
|
||
ProgramPollInterval = TimeSpan.FromSeconds(FixedTreeProgramPollIntervalSeconds),
|
||
TimerPollInterval = TimeSpan.FromSeconds(FixedTreeTimerPollIntervalSeconds),
|
||
},
|
||
Devices = devices,
|
||
Tags = tags,
|
||
};
|
||
}
|
||
}
|