sirt factory connected

This commit is contained in:
Stoyan Zlatev
2025-01-28 14:39:42 +01:00
parent 1c2d2e70e9
commit db677b133c
26 changed files with 1333 additions and 618 deletions
@@ -21,13 +21,14 @@
this.COMPort = name;
this.readAwaiter = new ManualResetEventSlim();
this.closeAwaiter = new ManualResetEventSlim();
var sp = new SysSerialPort(this.COMPort, baudRate, parity, dataBits, stopBits);
serialPorts.AddOrUpdate(
key: this.COMPort,
addValue: sp,
updateValueFactory: (_1, _2) => sp);
serialPorts.AddOrUpdate(this.COMPort, sp, (key, value) =>
{
value?.Close();
return sp;
});
}
public event Action Connected;
@@ -36,9 +37,7 @@
public string COMPort { get; }
public bool IsOpen
=> serialPorts.TryGetValue(this.COMPort, out var sp)
&& sp.IsOpen;
public bool IsOpen => serialPorts.TryGetValue(this.COMPort, out var sp) && sp.IsOpen;
public void Close()
{
@@ -47,7 +46,7 @@
try
{
sp.Close();
this.closeAwaiter.Wait(3000);
this.closeAwaiter.Wait(5000);
this.closeAwaiter.Reset();
sp.DataReceived -= this.SP_DataReceived;
@@ -82,7 +81,7 @@
public IEnumerable<byte[]> ReadBytes()
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
var size = sp.ReadBufferSize;
var buffer = new byte[size];
var length = 0;
@@ -109,12 +108,14 @@
yield return buffer;
}
sp.Close();
}
}
public IEnumerable<string> ReadLines()
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
var text = default(string);
using (sp)
@@ -139,15 +140,17 @@
}
}
public void RemoveConnection()
public void Remove()
{
this.Close();
serialPorts.TryRemove(this.COMPort, out _);
if (serialPorts.TryRemove(this.COMPort, out var sp))
{
sp.Close();
}
}
public void Write(byte[] bytes)
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
try
{
@@ -161,7 +164,7 @@
public void Write(string text)
{
var sp = this.GetOpenSP();
var sp = this.GetOpenedSP();
try
{
@@ -173,7 +176,7 @@
}
}
private SysSerialPort GetOpenSP()
private SysSerialPort GetOpenedSP()
{
if (!serialPorts.TryGetValue(this.COMPort, out var sp))
{
@@ -182,6 +185,7 @@
if (!sp.IsOpen)
{
this.Close();
this.Open();
}
@@ -115,6 +115,8 @@
private void SerialPort_Connected()
{
this.IsConnected = true;
this.cancellationTokenSource = new CancellationTokenSource();
this.cancellationToken = this.cancellationTokenSource.Token;
@@ -130,6 +132,9 @@
private void SerialPort_Disconnected()
{
this.IsActivated = false;
this.IsConnected = false;
try
{
this.cancellationTokenSource?.Cancel();
@@ -158,6 +163,8 @@
private void StartProcessingTasksAsync()
{
this.IsActivated = true;
this.processingTask = Task.Factory.StartNew(() =>
{
var manualResetEventSlim = new ManualResetEventSlim();
@@ -28,6 +28,10 @@
public int Frequency { get; protected set; }
public bool IsConnected { get; protected set; }
public bool IsActivated { get; protected set; }
public void Close()
{
this.serialPort.Close();
@@ -84,6 +88,6 @@
}
public void RemoveConnection()
=> this.serialPort.RemoveConnection();
=> this.serialPort.Remove();
}
}
@@ -7,84 +7,95 @@
public class SIRTFactory
{
private readonly ConcurrentDictionary<string, SIRTBroadcaster> connections
private readonly ConcurrentDictionary<string, SIRTBroadcaster> broadcasters
= new ConcurrentDictionary<string, SIRTBroadcaster>();
public event Action<string> Logging;
public void CloseConnection(string portName)
{
if (this.connections.TryGetValue(portName, out var connection))
if (this.broadcasters.TryGetValue(portName, out var broadcaster))
{
connection.Close();
broadcaster.Logging -= this.NotifyMessage;
broadcaster.Close();
}
}
public void CloseConnections()
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.Close();
broadcaster.Logging -= this.NotifyMessage;
broadcaster.Close();
}
}
public void DequeueTasks(uint address)
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.DequeueTasks(address);
broadcaster.DequeueTasks(address);
}
}
public void OpenConnections()
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.Open();
broadcaster.Logging += this.NotifyMessage;
broadcaster.Open();
}
}
public void ConnectSIRT(string portName, Action<SIRTBroadcaster> callback)
{
if (!this.connections.TryGetValue(portName, out var broadcaster))
if (!this.broadcasters.TryGetValue(portName, out var broadcaster))
{
broadcaster = new SIRTBroadcaster(portName);
broadcaster.Logging += message => this.Logging?.Invoke(message);
broadcaster.Logging += this.NotifyMessage;
if (!this.connections.TryAdd(portName, broadcaster))
if (!this.broadcasters.TryAdd(portName, broadcaster))
{
broadcaster = null;
}
}
callback(broadcaster);
broadcaster?.Close();
broadcaster?.Open();
if (broadcaster?.IsActivated == false)
{
broadcaster?.Close();
broadcaster?.Open();
}
}
public void EnqueueTask(SIRTTask task)
{
foreach (var connection in this.GetConnections())
foreach (var broadcaster in this.GetConnections())
{
connection.EnqueueTask(task);
broadcaster.EnqueueTask(task);
}
}
public IEnumerable<string> GetPortNames()
=> this.connections.Keys.ToArray();
=> this.broadcasters.Keys.ToArray();
public void RemoveConnections()
{
foreach (var key in this.connections.Keys.ToArray())
foreach (var key in this.broadcasters.Keys.ToArray())
{
if (this.connections.TryRemove(key, out var broadcaster))
if (this.broadcasters.TryRemove(key, out var broadcaster))
{
broadcaster.Logging -= this.NotifyMessage;
broadcaster.RemoveConnection();
}
}
}
private void NotifyMessage(string message)
=> this.Logging?.Invoke(message);
private IEnumerable<SIRTBroadcaster> GetConnections()
=> this.connections.Values.ToArray();
=> this.broadcasters.Values.ToArray();
}
}