Files
lmxopcua/src/ZB.MOM.WW.OtOpcUa.Driver.AbLegacy/AbLegacyDriverFactoryExtensions.cs
2026-04-26 04:13:13 -04:00

251 lines
12 KiB
C#

using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Hosting;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.Import;
using ZB.MOM.WW.OtOpcUa.Driver.AbLegacy.PlcFamilies;
namespace ZB.MOM.WW.OtOpcUa.Driver.AbLegacy;
/// <summary>
/// Static factory registration helper for <see cref="AbLegacyDriver"/>. Server's Program.cs
/// calls <see cref="Register"/> once at startup; the bootstrapper (task #248) then
/// materialises AB Legacy DriverInstance rows from the central config DB into live
/// driver instances. Mirrors <c>GalaxyProxyDriverFactoryExtensions</c>.
/// </summary>
public static class AbLegacyDriverFactoryExtensions
{
public const string DriverTypeName = "AbLegacy";
public static void Register(DriverFactoryRegistry registry)
{
ArgumentNullException.ThrowIfNull(registry);
registry.Register(DriverTypeName, CreateInstance);
}
internal static AbLegacyDriver CreateInstance(string driverInstanceId, string driverConfigJson)
{
ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
ArgumentException.ThrowIfNullOrWhiteSpace(driverConfigJson);
var dto = JsonSerializer.Deserialize<AbLegacyDriverConfigDto>(driverConfigJson, JsonOptions)
?? throw new InvalidOperationException(
$"AB Legacy driver config for '{driverInstanceId}' deserialised to null");
var options = new AbLegacyDriverOptions
{
Devices = dto.Devices is { Count: > 0 }
? [.. dto.Devices.Select(d => new AbLegacyDeviceOptions(
HostAddress: d.HostAddress ?? throw new InvalidOperationException(
$"AB Legacy config for '{driverInstanceId}' has a device missing HostAddress"),
PlcFamily: ParseEnum<AbLegacyPlcFamily>(d.PlcFamily, driverInstanceId, "PlcFamily",
fallback: AbLegacyPlcFamily.Slc500),
DeviceName: d.DeviceName,
// PR 9 — per-device timeout / retry overrides. Device-level wins over driver-wide.
Timeout: d.TimeoutMs is int devMs ? TimeSpan.FromMilliseconds(devMs) : null,
Retries: d.Retries))]
: [],
Tags = dto.Tags is { Count: > 0 }
? [.. dto.Tags.Select(t => new AbLegacyTagDefinition(
Name: t.Name ?? throw new InvalidOperationException(
$"AB Legacy config for '{driverInstanceId}' has a tag missing Name"),
DeviceHostAddress: t.DeviceHostAddress ?? throw new InvalidOperationException(
$"AB Legacy tag '{t.Name}' in '{driverInstanceId}' missing DeviceHostAddress"),
Address: t.Address ?? throw new InvalidOperationException(
$"AB Legacy tag '{t.Name}' in '{driverInstanceId}' missing Address"),
DataType: ParseEnum<AbLegacyDataType>(t.DataType, driverInstanceId, "DataType",
tagName: t.Name),
Writable: t.Writable ?? true,
WriteIdempotent: t.WriteIdempotent ?? false,
ArrayLength: t.ArrayLength,
AbsoluteDeadband: t.AbsoluteDeadband,
PercentDeadband: t.PercentDeadband))]
: [],
Probe = new AbLegacyProbeOptions
{
Enabled = dto.Probe?.Enabled ?? true,
Interval = TimeSpan.FromMilliseconds(dto.Probe?.IntervalMs ?? 5_000),
Timeout = TimeSpan.FromMilliseconds(dto.Probe?.TimeoutMs ?? 2_000),
ProbeAddress = dto.Probe?.ProbeAddress ?? "S:0",
},
Timeout = TimeSpan.FromMilliseconds(dto.TimeoutMs ?? 2_000),
// PR 9 — driver-wide retry default. null ≡ 0 retries (single attempt). Per-device
// Retries on AbLegacyDeviceOptions still wins.
Retries = dto.Retries,
};
return new AbLegacyDriver(options, driverInstanceId);
}
/// <summary>
/// ablegacy-11 / #254 — append RSLogix CSV symbol-export rows to
/// <paramref name="options"/> as <see cref="AbLegacyTagDefinition"/> entries bound to
/// <paramref name="deviceHostAddress"/>. Returns a new <see cref="AbLegacyDriverOptions"/>
/// with the imported tags concatenated onto the existing <c>Tags</c> list — useful both
/// at startup-time (server-side bootstrap that wants to seed a device's address space
/// from a customer-supplied CSV) and from the CLI (<c>import-rslogix</c> emits the
/// resulting JSON fragment for hand-merging into an appsettings file).
/// </summary>
/// <remarks>
/// <para>
/// The importer is permissive by default — malformed rows are logged and skipped;
/// the resulting <see cref="RsLogixImportResult"/> counts surface on
/// <paramref name="result"/> for callers that want to assert "we got the row count
/// we expected".
/// </para>
/// <para>
/// RSLogix 500's <c>.RSS</c> + RSLogix 5's <c>.RSP</c> binary project files are
/// out of scope for v1 — the binary format is proprietary and undocumented; no
/// libplctag or community parser exists. Customers must export to text/CSV via
/// RSLogix's "Tools → Database → Save" or "Database Export" before pointing the
/// importer at the file. See <c>docs/drivers/AbLegacy-RSLogix-Import.md</c>.
/// </para>
/// </remarks>
public static AbLegacyDriverOptions AddRsLogixImport(
this AbLegacyDriverOptions options,
string path,
string deviceHostAddress,
out RsLogixImportResult result,
ImportOptions? importOptions = null,
ILogger<RsLogixSymbolImport>? logger = null)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(deviceHostAddress);
using var stream = File.OpenRead(path);
var importer = new RsLogixSymbolImport(logger ?? NullLogger<RsLogixSymbolImport>.Instance);
result = importer.Parse(stream, deviceHostAddress, importOptions);
// Concat onto whatever's already on the options — the importer is additive so
// hand-edited Tags rows (e.g., system-status fields not surfaced by RSLogix) keep
// sitting alongside the bulk-imported symbol rows. Use init-syntax with-expression
// so the returned options keeps every other field (Devices, Probe, Timeout, …)
// untouched.
var merged = new List<AbLegacyTagDefinition>(options.Tags.Count + result.Tags.Count);
merged.AddRange(options.Tags);
merged.AddRange(result.Tags);
return new AbLegacyDriverOptions
{
Devices = options.Devices,
Tags = merged,
Probe = options.Probe,
Timeout = options.Timeout,
Retries = options.Retries,
};
}
/// <summary>
/// CLI-friendly overload that returns the <see cref="RsLogixImportResult"/> alongside
/// the modified options as a tuple. Mirrors <see cref="AddRsLogixImport"/> but avoids
/// the <c>out</c> parameter for call sites that prefer pattern-matched destructuring.
/// </summary>
public static (AbLegacyDriverOptions Options, RsLogixImportResult Result) AddRsLogixImportWithResult(
this AbLegacyDriverOptions options,
string path,
string deviceHostAddress,
ImportOptions? importOptions = null,
ILogger<RsLogixSymbolImport>? logger = null)
{
var updated = options.AddRsLogixImport(path, deviceHostAddress, out var result, importOptions, logger);
return (updated, result);
}
private static T ParseEnum<T>(string? raw, string driverInstanceId, string field,
string? tagName = null, T? fallback = null) where T : struct, Enum
{
if (string.IsNullOrWhiteSpace(raw))
{
if (fallback.HasValue) return fallback.Value;
throw new InvalidOperationException(
$"AB Legacy {(tagName is null ? "config" : $"tag '{tagName}'")} in '{driverInstanceId}' missing {field}");
}
return Enum.TryParse<T>(raw, ignoreCase: true, out var v)
? v
: throw new InvalidOperationException(
$"AB Legacy {(tagName is null ? "config" : $"tag '{tagName}'")} has unknown {field} '{raw}'. " +
$"Expected one of {string.Join(", ", Enum.GetNames<T>())}");
}
private static readonly JsonSerializerOptions JsonOptions = new()
{
PropertyNameCaseInsensitive = true,
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
internal sealed class AbLegacyDriverConfigDto
{
public int? TimeoutMs { get; init; }
/// <summary>
/// PR 9 — driver-wide retry count for transient <c>BadCommunicationError</c> reads.
/// <c>null</c> ≡ <c>0</c> (single attempt). A per-device override on
/// <see cref="AbLegacyDeviceDto.Retries"/> wins.
/// </summary>
public int? Retries { get; init; }
public List<AbLegacyDeviceDto>? Devices { get; init; }
public List<AbLegacyTagDto>? Tags { get; init; }
public AbLegacyProbeDto? Probe { get; init; }
}
internal sealed class AbLegacyDeviceDto
{
public string? HostAddress { get; init; }
public string? PlcFamily { get; init; }
public string? DeviceName { get; init; }
/// <summary>
/// PR 9 — optional per-device timeout in ms. Wins over the driver-wide
/// <see cref="AbLegacyDriverConfigDto.TimeoutMs"/>. Tune this per chassis: SLC 5/01
/// RS-232 ≈ 5000, SLC 5/05 ≈ 2000, MicroLogix 1100 ≈ 3000.
/// </summary>
public int? TimeoutMs { get; init; }
/// <summary>
/// PR 9 — optional per-device retry count for transient <c>BadCommunicationError</c>
/// reads. Wins over the driver-wide <see cref="AbLegacyDriverConfigDto.Retries"/>.
/// <c>null</c> at both levels = single attempt.
/// </summary>
public int? Retries { get; init; }
}
internal sealed class AbLegacyTagDto
{
public string? Name { get; init; }
public string? DeviceHostAddress { get; init; }
public string? Address { get; init; }
public string? DataType { get; init; }
public bool? Writable { get; init; }
public bool? WriteIdempotent { get; init; }
/// <summary>
/// PR 7 — optional override for the parsed array suffix. When set and &gt; 1 the
/// driver issues a single contiguous PCCC block read for N elements.
/// </summary>
public int? ArrayLength { get; init; }
/// <summary>
/// PR 8 — optional absolute change filter for numeric tags. <c>OnDataChange</c> is
/// suppressed unless <c>|new - prev| &gt;= AbsoluteDeadband</c>. Booleans bypass;
/// strings + status changes always publish.
/// </summary>
public double? AbsoluteDeadband { get; init; }
/// <summary>
/// PR 8 — optional percent-of-previous change filter for numeric tags.
/// <c>OnDataChange</c> is suppressed unless <c>|new - prev| &gt;= |prev * Percent / 100|</c>.
/// <c>prev == 0</c> always publishes (avoids division-by-zero).
/// </summary>
public double? PercentDeadband { get; init; }
}
internal sealed class AbLegacyProbeDto
{
public bool? Enabled { get; init; }
public int? IntervalMs { get; init; }
public int? TimeoutMs { get; init; }
public string? ProbeAddress { get; init; }
}
}