|
|
@@ -1,16 +1,50 @@
|
|
|
-namespace MinicsConsole.Helper;
|
|
|
+using HardwareData;
|
|
|
+using MinicsConsole.Connector;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.Net.NetworkInformation;
|
|
|
+using System.Threading.Channels;
|
|
|
|
|
|
-public class Mini8DataDispatcher(ILog log)
|
|
|
+namespace MinicsConsole.Helper;
|
|
|
+
|
|
|
+public class Mini8DataDispatcher
|
|
|
{
|
|
|
+ public Mini8DataDispatcher(ILog log)
|
|
|
+ {
|
|
|
+ this.log = log;
|
|
|
+ }
|
|
|
+ private readonly ILog log;
|
|
|
+
|
|
|
private readonly ConcurrentDictionary<string, IMini8DataNotifier> _Connectors = [];
|
|
|
+ private readonly ConcurrentDictionary<string, EventQueue<(string name, byte mini8, byte channel, ChannelData channelData)>> _ConnectorQueue = [];
|
|
|
+
|
|
|
+ private void QueueHandler((string name, byte mini8, byte channel, ChannelData channelData) t)
|
|
|
+ {
|
|
|
+ if (!_Connectors.TryGetValue(t.name, out IMini8DataNotifier? notifer) || notifer is null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ notifer.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
|
|
|
+ }
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {t.name} mini8 {t.mini8} channel {t.channel}");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public bool TryAddNotifier(string name, IMini8DataNotifier connector)
|
|
|
{
|
|
|
- return _Connectors.TryAdd(name, connector);
|
|
|
+ if (!_Connectors.TryAdd(name, connector))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ _ConnectorQueue[name] = new(QueueHandler);
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public bool TryRemoveConnector(string name)
|
|
|
{
|
|
|
+ _ConnectorQueue.TryRemove(name, out _);
|
|
|
return _Connectors.TryRemove(name, out _);
|
|
|
}
|
|
|
|
|
|
@@ -18,20 +52,27 @@ public class Mini8DataDispatcher(ILog log)
|
|
|
{
|
|
|
if (channelData is null)
|
|
|
return;
|
|
|
- Task.Factory.StartNew(() =>
|
|
|
+
|
|
|
+ foreach (var item in _ConnectorQueue)
|
|
|
{
|
|
|
- Parallel.ForEach(_Connectors.Values, item =>
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- item?.ChannelInfoNotify(mini8, channel, channelData);
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
|
|
|
- }
|
|
|
- });
|
|
|
- });
|
|
|
+ item.Value.Enqueue((item.Key, mini8, channel, channelData));
|
|
|
+ }
|
|
|
+
|
|
|
+ //Parallel.ForEach(_Connectors.Values, item =>
|
|
|
+ //{
|
|
|
+ // Task.Factory.StartNew(() =>
|
|
|
+ // {
|
|
|
+
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // item?.ChannelInfoNotify(mini8, channel, channelData);
|
|
|
+ // }
|
|
|
+ // catch
|
|
|
+ // {
|
|
|
+ // log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ //});
|
|
|
}
|
|
|
|
|
|
public void AlarmNotify(byte mini8, byte channel, float temperature)
|