@page "/clusters/{ClusterId}/drivers/new/s7" @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.S7 @inject IDbContextFactory DbFactory @inject NavigationManager Nav

@(IsNew ? "New Siemens S7 driver" : "Edit Siemens S7 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
PLC IP address or hostname.
ISO-on-TCP; usually 102.
@foreach (var v in Enum.GetValues()) { }
Controls ISO-TSAP slot byte during handshake.
Almost always 0.
S7-300/400 = 2; S7-1200/1500 = 0.
@* Probe *@
Connectivity probe
Test Connect timeout (1–60 s).
@* Tags *@ NameAddressTypeWritable @t.Name@t.Address @t.DataType@(t.Writable ? "yes" : "no")
Only for String type. Max 254.
} @code { [Parameter] public string ClusterId { get; set; } = ""; [Parameter] public string? DriverInstanceId { get; set; } private const string DriverTypeKey = "S7"; 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, Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() }, }; 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 Tags is a collection — edited via the CollectionEditor modal. 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 S7DriverOptions()); } 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 S7DriverOptions(); _form = FormModel.FromOptions(opts); _form.ResilienceConfig = _existing.ResilienceConfig; _form.RowVersion = _existing.RowVersion; _tags = opts.Tags.Select(S7TagRow.FromDefinition).ToList(); } } _loaded = true; } private async Task SubmitAsync() { _busy = true; _error = null; try { var opts = _form.ToOptions(_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(_tags.Select(r => r.ToDefinition()).ToList()), _jsonOpts); private static S7DriverOptions? TryDeserialize(string json) { try { return System.Text.Json.JsonSerializer.Deserialize(json, _jsonOpts); } catch { return null; } } // Mutable VM for the modal editor — S7TagDefinition is an immutable record. public sealed class S7TagRow { public string Name { get; set; } = ""; public string Address { get; set; } = ""; public S7DataType DataType { get; set; } = S7DataType.Int16; public bool Writable { get; set; } = true; public int StringLength { get; set; } = 254; // Original record (null for newly-added rows). Preserves fields the editor doesn't expose // (WriteIdempotent) across a load→save. private S7TagDefinition? _source; public S7TagRow Clone() => (S7TagRow)MemberwiseClone(); // _source is an immutable record ref — safe to share public static S7TagRow FromDefinition(S7TagDefinition d) => new() { Name = d.Name, Address = d.Address, DataType = d.DataType, Writable = d.Writable, StringLength = d.StringLength, _source = d, }; public S7TagDefinition ToDefinition() { var baseDef = _source ?? new S7TagDefinition(Name.Trim(), Address.Trim(), DataType); return baseDef with { Name = Name.Trim(), Address = Address.Trim(), DataType = DataType, Writable = Writable, StringLength = StringLength, }; } public static string? ValidateRow(S7TagRow 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. // Collection (Tags) is kept on the component (_tags) and passed in on ToOptions(). public sealed class FormModel { // Connection public string Host { get; set; } = "127.0.0.1"; public int Port { get; set; } = 102; public S7CpuType CpuType { get; set; } = S7CpuType.S71500; public short Rack { get; set; } = 0; public short Slot { get; set; } = 0; public int TimeoutSeconds { get; set; } = 5; // Probe public bool ProbeEnabled { get; set; } = true; public int ProbeIntervalSeconds { get; set; } = 5; public int ProbeTimeoutSeconds { get; set; } = 2; public int AdminProbeTimeoutSeconds { get; set; } = 5; // Common public string? ResilienceConfig { get; set; } public byte[] RowVersion { get; set; } = []; public static FormModel FromOptions(S7DriverOptions o) => new() { Host = o.Host, Port = o.Port, CpuType = o.CpuType, Rack = o.Rack, Slot = o.Slot, TimeoutSeconds = (int)o.Timeout.TotalSeconds, ProbeEnabled = o.Probe.Enabled, ProbeIntervalSeconds = (int)o.Probe.Interval.TotalSeconds, ProbeTimeoutSeconds = (int)o.Probe.Timeout.TotalSeconds, AdminProbeTimeoutSeconds = o.ProbeTimeoutSeconds, }; public S7DriverOptions ToOptions(IReadOnlyList tags) => new() { Host = Host, Port = Port, CpuType = CpuType, Rack = Rack, Slot = Slot, Timeout = TimeSpan.FromSeconds(TimeoutSeconds), Probe = new S7ProbeOptions { Enabled = ProbeEnabled, Interval = TimeSpan.FromSeconds(ProbeIntervalSeconds), Timeout = TimeSpan.FromSeconds(ProbeTimeoutSeconds), }, ProbeTimeoutSeconds = AdminProbeTimeoutSeconds, Tags = tags, }; } }