Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove IBP35Configurations #6

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions src/Smdn.Devices.BP35XX/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0008</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0008</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0008</DiagnosticId>
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
</Suppressions>
23 changes: 20 additions & 3 deletions src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35A1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Smdn.Net.SkStackIP;

namespace Smdn.Devices.BP35XX;

public class BP35A1 : BP35Base {
Expand Down Expand Up @@ -49,15 +51,30 @@ public static ValueTask<BP35A1> CreateAsync(
cancellationToken: cancellationToken
);

/// <summary>
/// Initializes a new instance of the <see cref="BP35A1"/> class with specifying configurations.
/// </summary>
/// <param name="configurations">
/// A <see cref="BP35A1Configurations"/> that holds the configurations to the <see cref="BP35A1"/> instance.
/// </param>
/// <param name="serviceProvider">
/// The <see cref="IServiceProvider"/>.
/// This constructor overload attempts to get a service of <see cref="IBP35SerialPortStreamFactory"/>, to create an <see cref="System.IO.Ports.SerialPort"/>.
/// </param>
private BP35A1(
IBP35Configurations configurations,
BP35A1Configurations configurations,
IServiceProvider? serviceProvider = null
)
: base(
configurations: configurations,
serialPortStreamFactory: serviceProvider?.GetService<IBP35SerialPortStreamFactory>(),
serialPortName: configurations.SerialPortName,
serialPortStreamFactory: serviceProvider?.GetService<IBP35SerialPortStreamFactory>() ?? new BP35A1SerialPortStreamFactory(configurations),
erxudpDataFormat: SkStackERXUDPDataFormat.Binary,
logger: serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<BP35A1>()
)
{
}

private protected class BP35A1SerialPortStreamFactory(BP35A1Configurations configurations) : SerialPortStreamFactory {
public override BP35UartBaudRate BaudRate { get; } = configurations.BaudRate;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using Smdn.Net.SkStackIP;

namespace Smdn.Devices.BP35XX;

public sealed class BP35A1Configurations : IBP35Configurations {
/// <inheritdoc cref="IBP35Configurations.SerialPortName"/>
public sealed class BP35A1Configurations {
/// <summary>
/// Gets or sets the <see cref="string"/> value that holds the serial port name for communicating with the device that implements the SKSTACK-IP protocol.
/// </summary>
public string? SerialPortName { get; set; }

/// <inheritdoc cref="IBP35Configurations.BaudRate"/>
/// <summary>
/// Gets or sets the <see cref="BP35UartBaudRate"/> value that specifies the baud rate of the serial port for communicating with the device.
/// </summary>
public BP35UartBaudRate BaudRate { get; set; } = BP35A1.DefaultValueForBP35UartBaudRate;

/// <inheritdoc cref="IBP35Configurations.TryLoadFlashMemory"/>
/// <summary>
/// Gets or sets a value indicating whether or not to attempt to load the configuration from flash memory during initialization.
/// </summary>
public bool TryLoadFlashMemory { get; set; } = BP35Base.DefaultValueForTryLoadFlashMemory;

SkStackERXUDPDataFormat IBP35Configurations.ERXUDPDataFormat => SkStackERXUDPDataFormat.Binary;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,32 @@ namespace Smdn.Devices.BP35XX;
#pragma warning disable IDE0040
partial class BP35Base {
#pragma warning restore IDE0040
private class DefaultSerialPortStreamFactory : IBP35SerialPortStreamFactory {
public static DefaultSerialPortStreamFactory Instance { get; } = new();
private protected abstract class SerialPortStreamFactory : IBP35SerialPortStreamFactory {
public abstract BP35UartBaudRate BaudRate { get; }

public Stream CreateSerialPortStream(IBP35Configurations configurations)
public Stream CreateSerialPortStream(string? serialPortName)
{
if (string.IsNullOrEmpty(configurations.SerialPortName)) {
if (string.IsNullOrEmpty(serialPortName)) {
throw new ArgumentException(
message: $"The {nameof(configurations.SerialPortName)} is not set for the {configurations.GetType().Name}",
paramName: nameof(configurations)
message: $"The {nameof(serialPortName)} must be a non-empty string.",
paramName: nameof(serialPortName)
);
}

const string CRLF = "\r\n";

#pragma warning disable CA2000
var port = new SerialPort(
portName: configurations.SerialPortName,
baudRate: configurations.BaudRate switch {
portName: serialPortName,
baudRate: BaudRate switch {
BP35UartBaudRate.Baud2400 => 2_400,
BP35UartBaudRate.Baud4800 => 4_800,
BP35UartBaudRate.Baud9600 => 9_600,
BP35UartBaudRate.Baud19200 => 19_200,
BP35UartBaudRate.Baud38400 => 38_400,
BP35UartBaudRate.Baud57600 => 57_600,
BP35UartBaudRate.Baud115200 => 115_200,
_ => throw new ArgumentException(
message: $"A valid {nameof(BP35UartBaudRate)} value is not set for the {configurations.GetType().Name}",
paramName: nameof(configurations)
),
_ => throw new InvalidOperationException($"A valid {nameof(BP35UartBaudRate)} value is not set for the {nameof(BaudRate)}"),
},
parity: Parity.None,
dataBits: 8,
Expand Down
20 changes: 11 additions & 9 deletions src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,28 @@ private protected static InvalidOperationException CreateNotInitializedException
/// <summary>
/// Initializes a new instance of the <see cref="BP35Base"/> class with specifying the serial port name.
/// </summary>
/// <param name="configurations">
/// A <see cref="IBP35Configurations"/> that holds the configurations to the <see cref="BP35Base"/> instance.
/// <param name="serialPortName">
/// A <see cref="string"/> that holds the serial port name to which <see cref="BP35Base"/> device is connected.
/// </param>
/// <param name="serialPortStreamFactory">
/// A <see cref="IBP35SerialPortStreamFactory"/> that provides the function to create the serial port stream according to the <paramref name="configurations"/>.
/// A <see cref="IBP35SerialPortStreamFactory"/> that provides the function to create the serial port stream.
/// </param>
/// <param name="erxudpDataFormat">
/// A <see cref="SkStackERXUDPDataFormat"/> that specifies the format of the data part received in the event <c>ERXUDP</c>. See <see cref="SkStackClient.ERXUDPDataFormat"/>.
/// </param>
/// <param name="logger">The <see cref="ILogger"/> to report the situation.</param>
#pragma warning disable IDE0290
private protected BP35Base(
IBP35Configurations configurations,
IBP35SerialPortStreamFactory? serialPortStreamFactory,
string? serialPortName,
IBP35SerialPortStreamFactory serialPortStreamFactory,
SkStackERXUDPDataFormat erxudpDataFormat,
ILogger? logger
)
#pragma warning restore IDE0290
: base(
stream: (serialPortStreamFactory ?? DefaultSerialPortStreamFactory.Instance).CreateSerialPortStream(
configurations ?? throw new ArgumentNullException(nameof(configurations))
),
stream: (serialPortStreamFactory ?? throw new ArgumentNullException(nameof(serialPortStreamFactory))).CreateSerialPortStream(serialPortName),
leaveStreamOpen: false, // should close the opened stream
erxudpDataFormat: configurations.ERXUDPDataFormat,
erxudpDataFormat: erxudpDataFormat,
logger: logger
)
{
Expand Down
29 changes: 0 additions & 29 deletions src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/IBP35Configurations.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
namespace Smdn.Devices.BP35XX;

public interface IBP35SerialPortStreamFactory {
Stream CreateSerialPortStream(IBP35Configurations configurations);
Stream CreateSerialPortStream(string? serialPortName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public PseudoSerialPortStreamFactory()
{
}

public Stream CreateSerialPortStream(IBP35Configurations configurations)
public Stream CreateSerialPortStream(string? serialPortName)
=> Stream;
}