OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
SerialPortConnectionListener.cs
1using System;
2using System.Threading.Tasks;
3using Microsoft.Extensions.Logging;
4
6
19{
20 private readonly string _portName;
21
29 string portName, int baudRate, ILoggerFactory loggerFactory = null) : base(baudRate, loggerFactory)
30 {
31 _portName = portName;
32 }
33
35 public override async Task Start(Func<IOsdpConnection, Task> newConnectionHandler)
36 {
37 IsRunning = true;
38
39 Logger?.LogInformation("Starting serial port listener on {Port} @ {BaudRate} baud", _portName, BaudRate);
40
41 await OpenSerialPort(newConnectionHandler);
42 }
43
48 private async Task OpenSerialPort(Func<IOsdpConnection, Task> newConnectionHandler)
49 {
50 try
51 {
52 var connection = new SerialPortOsdpConnection(_portName, BaudRate)
53 {
54 // An ACU discards its buffers before each command because it drives a strict
55 // command/reply cycle. A PD must not: this listener serves devices that reply to an
56 // incoming stream, and DiscardInBuffer throws away bytes of a command that is
57 // already arriving. Those bytes are dropped below the tracing layer, so the loss is
58 // invisible in a packet capture.
59 DiscardBuffersBeforeWrite = false
60 };
61 await connection.Open();
62
63 Logger?.LogDebug("Serial port {Port} opened successfully", _portName);
64
65 var task = Task.Run(async () =>
66 {
67 try
68 {
69 await newConnectionHandler(connection);
70 }
71 catch (Exception ex)
72 {
73 Logger?.LogError(ex, "Error in serial connection handler");
74 }
75 finally
76 {
77 // If still running, reopen the serial port after a brief delay
78 if (IsRunning)
79 {
80 Logger?.LogDebug("Serial connection closed, reopening port {Port}", _portName);
81 await Task.Delay(1000); // Brief delay before reopening
82 await OpenSerialPort(newConnectionHandler);
83 }
84 }
85 });
86
87 RegisterConnection(connection, task);
88 }
89 catch (Exception ex)
90 {
91 Logger?.LogError(ex, "Failed to open serial port {Port}", _portName);
92
93 // Retry after delay if still running
94 if (IsRunning)
95 {
96 await Task.Delay(5000); // Longer delay on error
97 await OpenSerialPort(newConnectionHandler);
98 }
99 }
100 }
101}
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.
bool IsRunning
Gets a value indicating whether the listener is currently running and accepting connections.