sirt changes
This commit is contained in:
parent
5ef11a7f99
commit
81bc7e8b1a
@ -65,6 +65,7 @@
|
||||
<Compile Include="Parameters\SirtOperating.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SIRTBroadcaster.cs" />
|
||||
<Compile Include="SIRTChanel.cs" />
|
||||
<Compile Include="SIRTConstants.cs" />
|
||||
<Compile Include="SIRTExtensions.cs" />
|
||||
<Compile Include="SIRTHub.cs" />
|
||||
|
||||
@ -9,6 +9,8 @@
|
||||
|
||||
public class SIRTBroadcaster
|
||||
{
|
||||
private readonly SIRTChanel[] chanels;
|
||||
private readonly ConcurrentDictionary<uint, SIRTChanel> chanelsMap;
|
||||
private readonly ConcurrentDictionary<uint, SIRTTask> 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<uint, SIRTTask>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
57
Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs
Normal file
57
Common/Hardware/Common.Hardware.SIRT/SIRTChanel.cs
Normal 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
|
||||
Loading…
Reference in New Issue
Block a user