Skip to content

Commit e6b096d

Browse files
authored
Merge pull request #6 from smdn/remove-IBP35Configurations
Remove IBP35Configurations
2 parents 95cd021 + 862c2d5 commit e6b096d

8 files changed

Lines changed: 140 additions & 63 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- https://learn.microsoft.com/en-us/dotnet/fundamentals/package-validation/diagnostic-ids -->
3+
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
4+
<Suppression>
5+
<DiagnosticId>CP0001</DiagnosticId>
6+
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
7+
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
8+
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
9+
<IsBaselineSuppression>true</IsBaselineSuppression>
10+
</Suppression>
11+
<Suppression>
12+
<DiagnosticId>CP0001</DiagnosticId>
13+
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
14+
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
15+
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
16+
<IsBaselineSuppression>true</IsBaselineSuppression>
17+
</Suppression>
18+
<Suppression>
19+
<DiagnosticId>CP0001</DiagnosticId>
20+
<Target>T:Smdn.Devices.BP35XX.IBP35Configurations</Target>
21+
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
22+
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
23+
<IsBaselineSuppression>true</IsBaselineSuppression>
24+
</Suppression>
25+
<Suppression>
26+
<DiagnosticId>CP0002</DiagnosticId>
27+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
28+
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
29+
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
30+
<IsBaselineSuppression>true</IsBaselineSuppression>
31+
</Suppression>
32+
<Suppression>
33+
<DiagnosticId>CP0002</DiagnosticId>
34+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
35+
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
36+
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
37+
<IsBaselineSuppression>true</IsBaselineSuppression>
38+
</Suppression>
39+
<Suppression>
40+
<DiagnosticId>CP0002</DiagnosticId>
41+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(Smdn.Devices.BP35XX.IBP35Configurations)</Target>
42+
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
43+
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
44+
<IsBaselineSuppression>true</IsBaselineSuppression>
45+
</Suppression>
46+
<Suppression>
47+
<DiagnosticId>CP0006</DiagnosticId>
48+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
49+
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
50+
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
51+
<IsBaselineSuppression>true</IsBaselineSuppression>
52+
</Suppression>
53+
<Suppression>
54+
<DiagnosticId>CP0006</DiagnosticId>
55+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
56+
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
57+
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
58+
<IsBaselineSuppression>true</IsBaselineSuppression>
59+
</Suppression>
60+
<Suppression>
61+
<DiagnosticId>CP0006</DiagnosticId>
62+
<Target>M:Smdn.Devices.BP35XX.IBP35SerialPortStreamFactory.CreateSerialPortStream(System.String)</Target>
63+
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
64+
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
65+
<IsBaselineSuppression>true</IsBaselineSuppression>
66+
</Suppression>
67+
<Suppression>
68+
<DiagnosticId>CP0008</DiagnosticId>
69+
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
70+
<Left>lib/net6.0/Smdn.Devices.BP35XX.dll</Left>
71+
<Right>lib/net6.0/Smdn.Devices.BP35XX.dll</Right>
72+
<IsBaselineSuppression>true</IsBaselineSuppression>
73+
</Suppression>
74+
<Suppression>
75+
<DiagnosticId>CP0008</DiagnosticId>
76+
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
77+
<Left>lib/net8.0/Smdn.Devices.BP35XX.dll</Left>
78+
<Right>lib/net8.0/Smdn.Devices.BP35XX.dll</Right>
79+
<IsBaselineSuppression>true</IsBaselineSuppression>
80+
</Suppression>
81+
<Suppression>
82+
<DiagnosticId>CP0008</DiagnosticId>
83+
<Target>T:Smdn.Devices.BP35XX.BP35A1Configurations</Target>
84+
<Left>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Left>
85+
<Right>lib/netstandard2.1/Smdn.Devices.BP35XX.dll</Right>
86+
<IsBaselineSuppression>true</IsBaselineSuppression>
87+
</Suppression>
88+
</Suppressions>

src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35A1.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
1010

11+
using Smdn.Net.SkStackIP;
12+
1113
namespace Smdn.Devices.BP35XX;
1214

1315
public class BP35A1 : BP35Base {
@@ -49,15 +51,30 @@ public static ValueTask<BP35A1> CreateAsync(
4951
cancellationToken: cancellationToken
5052
);
5153

54+
/// <summary>
55+
/// Initializes a new instance of the <see cref="BP35A1"/> class with specifying configurations.
56+
/// </summary>
57+
/// <param name="configurations">
58+
/// A <see cref="BP35A1Configurations"/> that holds the configurations to the <see cref="BP35A1"/> instance.
59+
/// </param>
60+
/// <param name="serviceProvider">
61+
/// The <see cref="IServiceProvider"/>.
62+
/// This constructor overload attempts to get a service of <see cref="IBP35SerialPortStreamFactory"/>, to create an <see cref="System.IO.Ports.SerialPort"/>.
63+
/// </param>
5264
private BP35A1(
53-
IBP35Configurations configurations,
65+
BP35A1Configurations configurations,
5466
IServiceProvider? serviceProvider = null
5567
)
5668
: base(
57-
configurations: configurations,
58-
serialPortStreamFactory: serviceProvider?.GetService<IBP35SerialPortStreamFactory>(),
69+
serialPortName: configurations.SerialPortName,
70+
serialPortStreamFactory: serviceProvider?.GetService<IBP35SerialPortStreamFactory>() ?? new BP35A1SerialPortStreamFactory(configurations),
71+
erxudpDataFormat: SkStackERXUDPDataFormat.Binary,
5972
logger: serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<BP35A1>()
6073
)
6174
{
6275
}
76+
77+
private protected class BP35A1SerialPortStreamFactory(BP35A1Configurations configurations) : SerialPortStreamFactory {
78+
public override BP35UartBaudRate BaudRate { get; } = configurations.BaudRate;
79+
}
6380
}
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3-
using Smdn.Net.SkStackIP;
4-
53
namespace Smdn.Devices.BP35XX;
64

7-
public sealed class BP35A1Configurations : IBP35Configurations {
8-
/// <inheritdoc cref="IBP35Configurations.SerialPortName"/>
5+
public sealed class BP35A1Configurations {
6+
/// <summary>
7+
/// 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.
8+
/// </summary>
99
public string? SerialPortName { get; set; }
1010

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

14-
/// <inheritdoc cref="IBP35Configurations.TryLoadFlashMemory"/>
16+
/// <summary>
17+
/// Gets or sets a value indicating whether or not to attempt to load the configuration from flash memory during initialization.
18+
/// </summary>
1519
public bool TryLoadFlashMemory { get; set; } = BP35Base.DefaultValueForTryLoadFlashMemory;
16-
17-
SkStackERXUDPDataFormat IBP35Configurations.ERXUDPDataFormat => SkStackERXUDPDataFormat.Binary;
1820
}

src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35Base.DefaultSerialPortStreamFactory.cs renamed to src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35Base.SerialPortStreamFactory.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,32 @@ namespace Smdn.Devices.BP35XX;
1010
#pragma warning disable IDE0040
1111
partial class BP35Base {
1212
#pragma warning restore IDE0040
13-
private class DefaultSerialPortStreamFactory : IBP35SerialPortStreamFactory {
14-
public static DefaultSerialPortStreamFactory Instance { get; } = new();
13+
private protected abstract class SerialPortStreamFactory : IBP35SerialPortStreamFactory {
14+
public abstract BP35UartBaudRate BaudRate { get; }
1515

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

2525
const string CRLF = "\r\n";
2626

2727
#pragma warning disable CA2000
2828
var port = new SerialPort(
29-
portName: configurations.SerialPortName,
30-
baudRate: configurations.BaudRate switch {
29+
portName: serialPortName,
30+
baudRate: BaudRate switch {
3131
BP35UartBaudRate.Baud2400 => 2_400,
3232
BP35UartBaudRate.Baud4800 => 4_800,
3333
BP35UartBaudRate.Baud9600 => 9_600,
3434
BP35UartBaudRate.Baud19200 => 19_200,
3535
BP35UartBaudRate.Baud38400 => 38_400,
3636
BP35UartBaudRate.Baud57600 => 57_600,
3737
BP35UartBaudRate.Baud115200 => 115_200,
38-
_ => throw new ArgumentException(
39-
message: $"A valid {nameof(BP35UartBaudRate)} value is not set for the {configurations.GetType().Name}",
40-
paramName: nameof(configurations)
41-
),
38+
_ => throw new InvalidOperationException($"A valid {nameof(BP35UartBaudRate)} value is not set for the {nameof(BaudRate)}"),
4239
},
4340
parity: Parity.None,
4441
dataBits: 8,

src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/BP35Base.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,28 @@ private protected static InvalidOperationException CreateNotInitializedException
9393
/// <summary>
9494
/// Initializes a new instance of the <see cref="BP35Base"/> class with specifying the serial port name.
9595
/// </summary>
96-
/// <param name="configurations">
97-
/// A <see cref="IBP35Configurations"/> that holds the configurations to the <see cref="BP35Base"/> instance.
96+
/// <param name="serialPortName">
97+
/// A <see cref="string"/> that holds the serial port name to which <see cref="BP35Base"/> device is connected.
9898
/// </param>
9999
/// <param name="serialPortStreamFactory">
100-
/// A <see cref="IBP35SerialPortStreamFactory"/> that provides the function to create the serial port stream according to the <paramref name="configurations"/>.
100+
/// A <see cref="IBP35SerialPortStreamFactory"/> that provides the function to create the serial port stream.
101+
/// </param>
102+
/// <param name="erxudpDataFormat">
103+
/// A <see cref="SkStackERXUDPDataFormat"/> that specifies the format of the data part received in the event <c>ERXUDP</c>. See <see cref="SkStackClient.ERXUDPDataFormat"/>.
101104
/// </param>
102105
/// <param name="logger">The <see cref="ILogger"/> to report the situation.</param>
103106
#pragma warning disable IDE0290
104107
private protected BP35Base(
105-
IBP35Configurations configurations,
106-
IBP35SerialPortStreamFactory? serialPortStreamFactory,
108+
string? serialPortName,
109+
IBP35SerialPortStreamFactory serialPortStreamFactory,
110+
SkStackERXUDPDataFormat erxudpDataFormat,
107111
ILogger? logger
108112
)
109113
#pragma warning restore IDE0290
110114
: base(
111-
stream: (serialPortStreamFactory ?? DefaultSerialPortStreamFactory.Instance).CreateSerialPortStream(
112-
configurations ?? throw new ArgumentNullException(nameof(configurations))
113-
),
115+
stream: (serialPortStreamFactory ?? throw new ArgumentNullException(nameof(serialPortStreamFactory))).CreateSerialPortStream(serialPortName),
114116
leaveStreamOpen: false, // should close the opened stream
115-
erxudpDataFormat: configurations.ERXUDPDataFormat,
117+
erxudpDataFormat: erxudpDataFormat,
116118
logger: logger
117119
)
118120
{

src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/IBP35Configurations.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/IBP35SerialPortStreamFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
namespace Smdn.Devices.BP35XX;
66

77
public interface IBP35SerialPortStreamFactory {
8-
Stream CreateSerialPortStream(IBP35Configurations configurations);
8+
Stream CreateSerialPortStream(string? serialPortName);
99
}

tests/Smdn.Devices.BP35XX/Smdn.Devices.BP35XX/PseudoSerialPortStreamFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public PseudoSerialPortStreamFactory()
1111
{
1212
}
1313

14-
public Stream CreateSerialPortStream(IBP35Configurations configurations)
14+
public Stream CreateSerialPortStream(string? serialPortName)
1515
=> Stream;
1616
}

0 commit comments

Comments
 (0)