From 81bc7e8b1a69ace4ed04b59a5c5433d6a0d72216 Mon Sep 17 00:00:00 2001 From: Stoyan Zlatev Date: Mon, 16 Feb 2026 09:33:19 +0100 Subject: [PATCH] sirt changes --- .../Common.Hardware.SIRT.csproj | 1 + .../Common.Hardware.SIRT/SIRTBroadcaster.cs | 68 +++++++++++++++++-- .../Common.Hardware.SIRT/SIRTChanel.cs | 57 ++++++++++++++++ LaaProductionWeb/LaaPackages | 2 +- 4 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs diff --git a/Common/Hardware/Common.Hardware.SIRT/Common.Hardware.SIRT.csproj b/Common/Hardware/Common.Hardware.SIRT/Common.Hardware.SIRT.csproj index 00d9d18a..d1cb7a50 100644 --- a/Common/Hardware/Common.Hardware.SIRT/Common.Hardware.SIRT.csproj +++ b/Common/Hardware/Common.Hardware.SIRT/Common.Hardware.SIRT.csproj @@ -65,6 +65,7 @@ + diff --git a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.cs b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.cs index c817f7a9..f2a10626 100644 --- a/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.cs +++ b/Common/Hardware/Common.Hardware.SIRT/SIRTBroadcaster.cs @@ -9,6 +9,8 @@ public class SIRTBroadcaster { + private readonly SIRTChanel[] chanels; + private readonly ConcurrentDictionary chanelsMap; private readonly ConcurrentDictionary tasks; private readonly SIRTStream sirtStream; private readonly bool log_RX_TX; @@ -18,6 +20,15 @@ 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(); this.sirtStream = new SIRTStream(portName, false); this.log_RX_TX = log_RX_TX; @@ -58,6 +69,12 @@ this.LogMessage(e); } + foreach (var chanel in this.chanels) + { + chanel.Captured -= this.Chanel_Captured; + chanel.Released -= this.Chanel_Released; + } + this.Disconnected?.Invoke(this); } @@ -82,6 +99,12 @@ this.Connected?.Invoke(this); this.LogMessage("Activated"); + + foreach (var chanel in this.chanels) + { + chanel.Captured += this.Chanel_Captured; + chanel.Released += this.Chanel_Released; + } } else { @@ -116,6 +139,15 @@ 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) { if (task?.Exception != null) @@ -136,6 +168,11 @@ message.Frequency = this.Frequency; message.SirtId = this.Id; + if (this.chanelsMap.TryGetValue(message.Address, out var chanel)) + { + chanel.Receive(message); + } + this.MessageReceived?.Invoke(message); if (this.tasks.TryGetValue(message.Address, out var task)) @@ -154,10 +191,7 @@ this.LogMessage(message); } - if (cancellationToken.IsCancellationRequested) - { - break; - } + cancellationToken.ThrowIfCancellationRequested(); } this.LogMessage($"Message receiving stopped!"); @@ -165,5 +199,31 @@ private void LogMessage(object 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; + } } } diff --git a/Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs b/Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs new file mode 100644 index 00000000..32b1eeb2 --- /dev/null +++ b/Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs @@ -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 messages = new Queue(); + + public bool IsFree { get; private set; } + + public uint Address { get; private set; } + + public event Action Captured; + public event Action Released; + + public void Capture(uint address) + { + this.IsFree = false; + this.Address = address; + + this.messages.Clear(); + this.Captured?.Invoke(this); + } + + public IEnumerable 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(); + } + } +} \ No newline at end of file diff --git a/LaaProductionWeb/LaaPackages b/LaaProductionWeb/LaaPackages index e7aa3e27..653498d4 160000 --- a/LaaProductionWeb/LaaPackages +++ b/LaaProductionWeb/LaaPackages @@ -1 +1 @@ -Subproject commit e7aa3e2723a435f0a714df661dc5d86dfd45a71c +Subproject commit 653498d43dcca1234a8fba95959ca02bb2c3f480