9cad9ed0fc
v2-ci / build (push) Failing after 41s
v2-ci / unit-tests (tests/Core/ZB.MOM.WW.OtOpcUa.Cluster.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.ControlPlane.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests) (push) Has been skipped
v2-ci / unit-tests (tests/Server/ZB.MOM.WW.OtOpcUa.Security.Tests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.Host.IntegrationTests) (push) Has been skipped
v2-ci / integration (tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests) (push) Has been skipped
Adds <summary>/<param>/<returns>/<inheritdoc> where missing and removes project bookkeeping IDs (task/tracking refs) from shipped code comments, so the docs read cleanly and CommentChecker is quiet except for known false positives (PLC/protocol terms, event/IEqualityComparer inheritdoc). Doc/comment-only; no logic changed; solution builds clean.
326 lines
13 KiB
C#
326 lines
13 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Hosting;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip;
|
|
|
|
/// <summary>
|
|
/// Static factory registration helper for <see cref="AbCipDriver"/>. Server's Program.cs
|
|
/// calls <see cref="Register"/> once at startup; the bootstrapper then
|
|
/// materialises AB CIP DriverInstance rows from the central config DB into live driver
|
|
/// instances. Mirrors <c>GalaxyProxyDriverFactoryExtensions</c>.
|
|
/// </summary>
|
|
public static class AbCipDriverFactoryExtensions
|
|
{
|
|
public const string DriverTypeName = "AbCip";
|
|
|
|
/// <summary>
|
|
/// Registers the AB CIP driver factory with the driver registry.
|
|
/// </summary>
|
|
/// <param name="registry">The driver factory registry to register with.</param>
|
|
public static void Register(DriverFactoryRegistry registry)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(registry);
|
|
registry.Register(DriverTypeName, CreateInstance);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an instance of the AB CIP driver from configuration.
|
|
/// </summary>
|
|
/// <param name="driverInstanceId">The unique identifier for this driver instance.</param>
|
|
/// <param name="driverConfigJson">The driver configuration as a JSON string.</param>
|
|
/// <returns>A configured <see cref="AbCipDriver"/> instance.</returns>
|
|
internal static AbCipDriver CreateInstance(string driverInstanceId, string driverConfigJson)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
|
|
var options = ParseOptions(driverInstanceId, driverConfigJson);
|
|
return new AbCipDriver(options, driverInstanceId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deserialise an AB CIP driver-config JSON document into <see cref="AbCipDriverOptions"/>.
|
|
/// Shared by <see cref="CreateInstance"/> (first construction) and
|
|
/// <see cref="AbCipDriver.InitializeAsync"/> / <see cref="AbCipDriver.ReinitializeAsync"/>
|
|
/// so a reinitialize with a changed config JSON (new device, new tag, changed timeout)
|
|
/// actually takes effect rather than being silently discarded.
|
|
/// </summary>
|
|
/// <param name="driverInstanceId">The unique identifier for this driver instance.</param>
|
|
/// <param name="driverConfigJson">The driver configuration as a JSON string.</param>
|
|
/// <returns>Parsed driver options.</returns>
|
|
internal static AbCipDriverOptions ParseOptions(string driverInstanceId, string driverConfigJson)
|
|
{
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(driverInstanceId);
|
|
ArgumentException.ThrowIfNullOrWhiteSpace(driverConfigJson);
|
|
|
|
var dto = JsonSerializer.Deserialize<AbCipDriverConfigDto>(driverConfigJson, JsonOptions)
|
|
?? throw new InvalidOperationException(
|
|
$"AB CIP driver config for '{driverInstanceId}' deserialised to null");
|
|
|
|
return new AbCipDriverOptions
|
|
{
|
|
Devices = dto.Devices is { Count: > 0 }
|
|
? [.. dto.Devices.Select(d => new AbCipDeviceOptions(
|
|
HostAddress: d.HostAddress ?? throw new InvalidOperationException(
|
|
$"AB CIP config for '{driverInstanceId}' has a device missing HostAddress"),
|
|
PlcFamily: ParseEnum<AbCipPlcFamily>(d.PlcFamily, "device", driverInstanceId, "PlcFamily",
|
|
fallback: AbCipPlcFamily.ControlLogix),
|
|
DeviceName: d.DeviceName,
|
|
AllowPacking: d.AllowPacking,
|
|
ConnectionSize: d.ConnectionSize))]
|
|
: [],
|
|
Tags = dto.Tags is { Count: > 0 }
|
|
? [.. dto.Tags.Select(t => BuildTag(t, driverInstanceId))]
|
|
: [],
|
|
Probe = new AbCipProbeOptions
|
|
{
|
|
Enabled = dto.Probe?.Enabled ?? true,
|
|
Interval = TimeSpan.FromMilliseconds(dto.Probe?.IntervalMs ?? 5_000),
|
|
Timeout = TimeSpan.FromMilliseconds(dto.Probe?.TimeoutMs ?? 2_000),
|
|
ProbeTagPath = dto.Probe?.ProbeTagPath,
|
|
},
|
|
Timeout = TimeSpan.FromMilliseconds(dto.TimeoutMs ?? 2_000),
|
|
EnableControllerBrowse = dto.EnableControllerBrowse ?? false,
|
|
EnableAlarmProjection = dto.EnableAlarmProjection ?? false,
|
|
AlarmPollInterval = TimeSpan.FromMilliseconds(dto.AlarmPollIntervalMs ?? 1_000),
|
|
EnableDeclarationOnlyUdtGrouping = dto.EnableDeclarationOnlyUdtGrouping ?? false,
|
|
};
|
|
}
|
|
|
|
private static AbCipTagDefinition BuildTag(AbCipTagDto t, string driverInstanceId) =>
|
|
new(
|
|
Name: t.Name ?? throw new InvalidOperationException(
|
|
$"AB CIP config for '{driverInstanceId}' has a tag missing Name"),
|
|
DeviceHostAddress: t.DeviceHostAddress ?? throw new InvalidOperationException(
|
|
$"AB CIP tag '{t.Name}' in '{driverInstanceId}' missing DeviceHostAddress"),
|
|
TagPath: t.TagPath ?? throw new InvalidOperationException(
|
|
$"AB CIP tag '{t.Name}' in '{driverInstanceId}' missing TagPath"),
|
|
DataType: ParseEnum<AbCipDataType>(t.DataType, t.Name, driverInstanceId, "DataType"),
|
|
Writable: t.Writable ?? true,
|
|
WriteIdempotent: t.WriteIdempotent ?? false,
|
|
Members: t.Members is { Count: > 0 }
|
|
? [.. t.Members.Select(m => new AbCipStructureMember(
|
|
Name: m.Name ?? throw new InvalidOperationException(
|
|
$"AB CIP tag '{t.Name}' in '{driverInstanceId}' has a member missing Name"),
|
|
DataType: ParseEnum<AbCipDataType>(m.DataType, t.Name, driverInstanceId,
|
|
$"Members[{m.Name}].DataType"),
|
|
Writable: m.Writable ?? true,
|
|
WriteIdempotent: m.WriteIdempotent ?? false,
|
|
ElementCount: m.ElementCount is > 0 ? m.ElementCount.Value : 1,
|
|
IsArray: m.IsArray))]
|
|
: null,
|
|
SafetyTag: t.SafetyTag ?? false,
|
|
ElementCount: t.ElementCount is > 0 ? t.ElementCount.Value : 1,
|
|
IsArray: t.IsArray);
|
|
|
|
private static T ParseEnum<T>(string? raw, string? tagName, string driverInstanceId, string field,
|
|
T? fallback = null) where T : struct, Enum
|
|
{
|
|
if (string.IsNullOrWhiteSpace(raw))
|
|
{
|
|
if (fallback.HasValue) return fallback.Value;
|
|
throw new InvalidOperationException(
|
|
$"AB CIP tag '{tagName ?? "<unnamed>"}' in '{driverInstanceId}' missing {field}");
|
|
}
|
|
return Enum.TryParse<T>(raw, ignoreCase: true, out var v)
|
|
? v
|
|
: throw new InvalidOperationException(
|
|
$"AB CIP 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 AbCipDriverConfigDto
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the timeout in milliseconds for operations.
|
|
/// </summary>
|
|
public int? TimeoutMs { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether controller browsing is enabled.
|
|
/// </summary>
|
|
public bool? EnableControllerBrowse { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether alarm projection is enabled.
|
|
/// </summary>
|
|
public bool? EnableAlarmProjection { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether declaration-only UDT grouping is enabled.
|
|
/// </summary>
|
|
public bool? EnableDeclarationOnlyUdtGrouping { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the alarm poll interval in milliseconds.
|
|
/// </summary>
|
|
public int? AlarmPollIntervalMs { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the list of devices to connect to.
|
|
/// </summary>
|
|
public List<AbCipDeviceDto>? Devices { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the list of tags to monitor.
|
|
/// </summary>
|
|
public List<AbCipTagDto>? Tags { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the probe configuration.
|
|
/// </summary>
|
|
public AbCipProbeDto? Probe { get; init; }
|
|
}
|
|
|
|
internal sealed class AbCipDeviceDto
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the host address of the device.
|
|
/// </summary>
|
|
public string? HostAddress { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the PLC family.
|
|
/// </summary>
|
|
public string? PlcFamily { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the device name.
|
|
/// </summary>
|
|
public string? DeviceName { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether packing is allowed.
|
|
/// </summary>
|
|
public bool? AllowPacking { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the connection size.
|
|
/// </summary>
|
|
public int? ConnectionSize { get; init; }
|
|
}
|
|
|
|
internal sealed class AbCipTagDto
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the tag name.
|
|
/// </summary>
|
|
public string? Name { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the device host address.
|
|
/// </summary>
|
|
public string? DeviceHostAddress { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the tag path.
|
|
/// </summary>
|
|
public string? TagPath { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the data type.
|
|
/// </summary>
|
|
public string? DataType { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether the tag is writable.
|
|
/// </summary>
|
|
public bool? Writable { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether write is idempotent.
|
|
/// </summary>
|
|
public bool? WriteIdempotent { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the list of structure members.
|
|
/// </summary>
|
|
public List<AbCipMemberDto>? Members { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether this is a safety tag.
|
|
/// </summary>
|
|
public bool? SafetyTag { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the explicit 1-D array signal — <c>true</c> ⟺ the tag is an array (even a
|
|
/// 1-element one). Mirrors <c>AbCipTagDefinition.IsArray</c>; optional (absent ⇒ scalar).
|
|
/// </summary>
|
|
[JsonPropertyName("isArray")]
|
|
public bool IsArray { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the number of elements for a 1-D array tag; <c>1</c> (or absent) for a scalar.
|
|
/// Mirrors <c>AbCipTagDefinition.ElementCount</c>; only positive values are honoured.
|
|
/// </summary>
|
|
[JsonPropertyName("elementCount")]
|
|
public int? ElementCount { get; init; }
|
|
}
|
|
|
|
internal sealed class AbCipMemberDto
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the member name.
|
|
/// </summary>
|
|
public string? Name { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the data type.
|
|
/// </summary>
|
|
public string? DataType { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether the member is writable.
|
|
/// </summary>
|
|
public bool? Writable { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets whether write is idempotent.
|
|
/// </summary>
|
|
public bool? WriteIdempotent { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the explicit 1-D array signal for the member — <c>true</c> ⟺ the member is
|
|
/// an array (even a 1-element one). Mirrors <c>AbCipStructureMember.IsArray</c>; optional.
|
|
/// </summary>
|
|
[JsonPropertyName("isArray")]
|
|
public bool IsArray { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the number of elements for a 1-D array member; <c>1</c> (or absent) for a
|
|
/// scalar. Mirrors <c>AbCipStructureMember.ElementCount</c>; only positive values are honoured.
|
|
/// </summary>
|
|
[JsonPropertyName("elementCount")]
|
|
public int? ElementCount { get; init; }
|
|
}
|
|
|
|
internal sealed class AbCipProbeDto
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets whether probing is enabled.
|
|
/// </summary>
|
|
public bool? Enabled { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the probe interval in milliseconds.
|
|
/// </summary>
|
|
public int? IntervalMs { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the probe timeout in milliseconds.
|
|
/// </summary>
|
|
public int? TimeoutMs { get; init; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the probe tag path.
|
|
/// </summary>
|
|
public string? ProbeTagPath { get; init; }
|
|
}
|
|
}
|