feat(modbus-rtu): add ModbusTransportFactory switch on Transport mode
Claude-Session: https://claude.ai/code/session_01GASWkNEi68FSCtvr6rLoEW
This commit is contained in:
@@ -0,0 +1,37 @@
|
|||||||
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Single mapping point from <see cref="ModbusDriverOptions"/> to the concrete
|
||||||
|
/// <see cref="IModbusTransport"/> its <see cref="ModbusDriverOptions.Transport"/> selects.
|
||||||
|
/// Consumed by both <see cref="ModbusDriver"/>'s default transport-factory closure and the
|
||||||
|
/// AdminUI Test-Connect probe, so the <see cref="ModbusTransportMode"/> switch lives in
|
||||||
|
/// exactly one place.
|
||||||
|
/// </summary>
|
||||||
|
public static class ModbusTransportFactory
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the transport <paramref name="options"/>.<see cref="ModbusDriverOptions.Transport"/>
|
||||||
|
/// selects, threading every shared connection setting (Host/Port/Timeout/AutoReconnect/
|
||||||
|
/// KeepAlive/IdleDisconnectTimeout/Reconnect) through to whichever concrete transport is built.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">Driver configuration options.</param>
|
||||||
|
/// <returns>A <see cref="ModbusRtuOverTcpTransport"/> when <see cref="ModbusTransportMode.RtuOverTcp"/>
|
||||||
|
/// is selected; otherwise a <see cref="ModbusTcpTransport"/> (also the default/unrecognized fallback).</returns>
|
||||||
|
public static IModbusTransport Create(ModbusDriverOptions options)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(options);
|
||||||
|
return options.Transport switch
|
||||||
|
{
|
||||||
|
ModbusTransportMode.RtuOverTcp => new ModbusRtuOverTcpTransport(
|
||||||
|
options.Host, options.Port, options.Timeout, options.AutoReconnect,
|
||||||
|
keepAlive: options.KeepAlive,
|
||||||
|
idleDisconnect: options.IdleDisconnectTimeout,
|
||||||
|
reconnect: options.Reconnect),
|
||||||
|
_ => new ModbusTcpTransport(
|
||||||
|
options.Host, options.Port, options.Timeout, options.AutoReconnect,
|
||||||
|
keepAlive: options.KeepAlive,
|
||||||
|
idleDisconnect: options.IdleDisconnectTimeout,
|
||||||
|
reconnect: options.Reconnect),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using Shouldly;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
|
||||||
|
|
||||||
|
public sealed class ModbusTransportFactoryTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Tcp_mode_builds_tcp_transport()
|
||||||
|
=> ModbusTransportFactory.Create(new ModbusDriverOptions { Transport = ModbusTransportMode.Tcp })
|
||||||
|
.ShouldBeOfType<ModbusTcpTransport>();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RtuOverTcp_mode_builds_rtu_transport()
|
||||||
|
=> ModbusTransportFactory.Create(new ModbusDriverOptions { Transport = ModbusTransportMode.RtuOverTcp })
|
||||||
|
.ShouldBeOfType<ModbusRtuOverTcpTransport>();
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Default_options_build_tcp_transport()
|
||||||
|
=> ModbusTransportFactory.Create(new ModbusDriverOptions())
|
||||||
|
.ShouldBeOfType<ModbusTcpTransport>();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user