sirt changes

This commit is contained in:
Stoyan Zlatev 2026-02-16 09:33:19 +01:00
parent 5ef11a7f99
commit 81bc7e8b1a
4 changed files with 123 additions and 5 deletions

View File

@ -65,6 +65,7 @@
<Compile Include="Parameters\SirtOperating.cs" /> <Compile Include="Parameters\SirtOperating.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SIRTBroadcaster.cs" /> <Compile Include="SIRTBroadcaster.cs" />
<Compile Include="SIRTChanel.cs" />
<Compile Include="SIRTConstants.cs" /> <Compile Include="SIRTConstants.cs" />
<Compile Include="SIRTExtensions.cs" /> <Compile Include="SIRTExtensions.cs" />
<Compile Include="SIRTHub.cs" /> <Compile Include="SIRTHub.cs" />

View File

@ -9,6 +9,8 @@
public class SIRTBroadcaster public class SIRTBroadcaster
{ {
private readonly SIRTChanel[] chanels;
private readonly ConcurrentDictionary<uint, SIRTChanel> chanelsMap;
private readonly ConcurrentDictionary<uint, SIRTTask> tasks; private readonly ConcurrentDictionary<uint, SIRTTask> tasks;
private readonly SIRTStream sirtStream; private readonly SIRTStream sirtStream;
private readonly bool log_RX_TX; private readonly bool log_RX_TX;
@ -18,6 +20,15 @@
public SIRTBroadcaster(string portName, bool log_RX_TX = true) public SIRTBroadcaster(string portName, bool log_RX_TX = true)
{ {
this.chanels = new SIRTChanel[]
{
new SIRTChanel(),
new SIRTChanel(),
new SIRTChanel(),
new SIRTChanel(),
new SIRTChanel(),
};
this.tasks = new ConcurrentDictionary<uint, SIRTTask>(); this.tasks = new ConcurrentDictionary<uint, SIRTTask>();
this.sirtStream = new SIRTStream(portName, false); this.sirtStream = new SIRTStream(portName, false);
this.log_RX_TX = log_RX_TX; this.log_RX_TX = log_RX_TX;
@ -58,6 +69,12 @@
this.LogMessage(e); this.LogMessage(e);
} }
foreach (var chanel in this.chanels)
{
chanel.Captured -= this.Chanel_Captured;
chanel.Released -= this.Chanel_Released;
}
this.Disconnected?.Invoke(this); this.Disconnected?.Invoke(this);
} }
@ -82,6 +99,12 @@
this.Connected?.Invoke(this); this.Connected?.Invoke(this);
this.LogMessage("Activated"); this.LogMessage("Activated");
foreach (var chanel in this.chanels)
{
chanel.Captured += this.Chanel_Captured;
chanel.Released += this.Chanel_Released;
}
} }
else else
{ {
@ -116,6 +139,15 @@
return false; return false;
} }
private void Chanel_Released(SIRTChanel chanel)
=> this.chanelsMap.TryRemove(chanel.Address, out var _);
private void Chanel_Captured(SIRTChanel chanel)
{
this.chanelsMap.TryRemove(chanel.Address, out var _);
this.chanelsMap.TryAdd(chanel.Address, chanel);
}
private void LogContinuationError(Task task) private void LogContinuationError(Task task)
{ {
if (task?.Exception != null) if (task?.Exception != null)
@ -136,6 +168,11 @@
message.Frequency = this.Frequency; message.Frequency = this.Frequency;
message.SirtId = this.Id; message.SirtId = this.Id;
if (this.chanelsMap.TryGetValue(message.Address, out var chanel))
{
chanel.Receive(message);
}
this.MessageReceived?.Invoke(message); this.MessageReceived?.Invoke(message);
if (this.tasks.TryGetValue(message.Address, out var task)) if (this.tasks.TryGetValue(message.Address, out var task))
@ -154,10 +191,7 @@
this.LogMessage(message); this.LogMessage(message);
} }
if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested();
{
break;
}
} }
this.LogMessage($"Message receiving stopped!"); this.LogMessage($"Message receiving stopped!");
@ -165,5 +199,31 @@
private void LogMessage(object message) private void LogMessage(object message)
=> SIRTLogger.LogMessage($"{this.PortName}|{nameof(SIRTBroadcaster)}|{message}"); => SIRTLogger.LogMessage($"{this.PortName}|{nameof(SIRTBroadcaster)}|{message}");
public bool TryCaptureChanel(uint address, out SIRTChanel chanel)
{
chanel = null;
if (this.chanelsMap.TryGetValue(address, out chanel))
{
return true;
}
foreach (var _chanel in this.chanels)
{
if (_chanel.IsFree)
{
_chanel.Capture(address);
this.chanelsMap.AddOrUpdate(address, _chanel, (_1, _2) => _chanel);
chanel = _chanel;
break;
}
}
return chanel != null;
}
} }
} }

View File

@ -0,0 +1,57 @@
namespace Common.Hardware.SIRT
{
using System;
using System.Collections.Generic;
using System.Threading;
public class SIRTChanel
{
private readonly ManualResetEventSlim awaiter = new ManualResetEventSlim();
private readonly Queue<SIRTMessage> messages = new Queue<SIRTMessage>();
public bool IsFree { get; private set; }
public uint Address { get; private set; }
public event Action<SIRTChanel> Captured;
public event Action<SIRTChanel> Released;
public void Capture(uint address)
{
this.IsFree = false;
this.Address = address;
this.messages.Clear();
this.Captured?.Invoke(this);
}
public IEnumerable<SIRTMessage> GetEnumerable()
{
while (true)
{
while (this.messages.Count > 0)
{
yield return this.messages.Dequeue();
}
this.awaiter.Reset();
this.awaiter.Wait();
}
}
public void Release()
{
this.Released?.Invoke(this);
this.messages.Clear();
this.Address = uint.MaxValue;
this.IsFree = true;
}
internal void Receive(SIRTMessage message)
{
this.messages.Enqueue(message);
this.awaiter.Set();
}
}
}

@ -1 +1 @@
Subproject commit e7aa3e2723a435f0a714df661dc5d86dfd45a71c Subproject commit 653498d43dcca1234a8fba95959ca02bb2c3f480