@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 DbFactory @inject NavigationManager Nav

@(IsNew ? "New Fanuc FOCAS driver" : "Edit Fanuc FOCAS driver") · @ClusterId

Cancel
@if (!_loaded) {

Loading…

} else if (!IsNew && _existing is null) {
Driver instance @DriverInstanceId was not found in cluster @ClusterId.
} else { @if (!IsNew && !string.IsNullOrEmpty(DriverInstanceId)) { }
@* Connection *@
Connection
Per-operation timeout. Default 2 s.
@* Probe *@
Connectivity probe
Test Connect timeout (1–60 s).
@* Alarm projection *@
Alarm projection
Surfaces FOCAS alarms via IAlarmSource.
One cnc_rdalmmsg2 call per device per tick. Default 2 s.
@* Handle recycle *@
Handle recycle
Proactive FWLIB session recycle to prevent handle pool exhaustion. Default off.
Typical: 30 min (shared pool) or 360 min (single client).
@* Fixed tree *@
Fixed-node tree
Exposes Identity/, Axes/, etc. from cnc_sysinfo/cnc_rdaxisname/cnc_rddynamic2. Default off.
cnc_rddynamic2 cadence per axis. Default 250 ms.
Program/mode info cadence. 0 = disabled. Default 1 s.
Power-on/cutting/cycle timer cadence. 0 = disabled. Default 30 s.
@* Devices *@ Host addressCNC seriesDevice name @d.HostAddress@d.Series @(string.IsNullOrWhiteSpace(d.DeviceName) ? "—" : d.DeviceName)
@* Tags *@ NameDeviceAddressTypeWritable @t.Name@t.DeviceHostAddress @t.Address@t.DataType@(t.Writable ? "yes" : "no")
} @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 _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 _devices = []; private List _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(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 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 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 devices, IReadOnlyList 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, }; } }