2using System.Threading.Tasks;
3using Microsoft.Extensions.Logging;
20 private readonly
string _portName;
29 string portName,
int baudRate, ILoggerFactory loggerFactory =
null) : base(baudRate, loggerFactory)
35 public override async Task
Start(Func<IOsdpConnection, Task> newConnectionHandler)
39 Logger?.LogInformation(
"Starting serial port listener on {Port} @ {BaudRate} baud", _portName,
BaudRate);
41 await OpenSerialPort(newConnectionHandler);
48 private async Task OpenSerialPort(Func<IOsdpConnection, Task> newConnectionHandler)
59 DiscardBuffersBeforeWrite =
false
61 await connection.Open();
63 Logger?.LogDebug(
"Serial port {Port} opened successfully", _portName);
65 var task = Task.Run(async () =>
69 await newConnectionHandler(connection);
73 Logger?.LogError(ex,
"Error in serial connection handler");
80 Logger?.LogDebug(
"Serial connection closed, reopening port {Port}", _portName);
81 await Task.Delay(1000);
82 await OpenSerialPort(newConnectionHandler);
91 Logger?.LogError(ex,
"Failed to open serial port {Port}", _portName);
96 await Task.Delay(5000);
97 await OpenSerialPort(newConnectionHandler);
void RegisterConnection(OsdpConnection connection, Task task)
Registers a new connection and its associated handling task with the listener.
OsdpConnectionListener(int baudRate, ILoggerFactory loggerFactory=null)
Initializes a new instance of the OsdpConnectionListener class.
ILogger Logger
Gets the logger instance for this listener if a logger factory was provided.
bool IsRunning
Gets a value indicating whether the listener is currently running and accepting connections.
int BaudRate
Gets the baud rate for serial connections.For TCP connections, this value may not be applicable.
SerialPortConnectionListener(string portName, int baudRate, ILoggerFactory loggerFactory=null)
Initializes a new instance of the SerialPortConnectionListener class.
override async Task Start(Func< IOsdpConnection, Task > newConnectionHandler)
Starts listening for incoming connections from ACUs.A task representing the asynchronous operation.
Connect using a serial port.
bool IsRunning
Gets a value indicating whether the listener is currently running and accepting connections.