| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using HardwareData;
- using MinicsConsole.Connector;
- using System.Diagnostics;
- using System.Net.NetworkInformation;
- using System.Threading.Channels;
- 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)
- {
- 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 _);
- }
- public void ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
- {
- if (channelData is null)
- return;
- foreach (var item in _ConnectorQueue)
- {
- 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)
- {
- Parallel.ForEach(_Connectors.Values, item =>
- {
- try
- {
- item.AlarmNotify(mini8, channel, temperature);
- }
- catch
- {
- log.Warning($"DataDispatcher try send AlarmNotify failed {item.Name} mini8 {mini8} channel {channel}");
- }
- });
- }
- public void AlarmTcBrockenNotify(byte mini8, byte channel)
- {
- Parallel.ForEach(_Connectors.Values, item =>
- {
- try
- {
- item.AlarmTcBrockenNotify(mini8, channel);
- }
- catch
- {
- log.Warning($"DataDispatcher try send AlarmTcBrockenNotify failed {item.Name} mini8 {mini8} channel {channel}");
- }
- });
- }
- public void Mini8Connect(byte mini8Index)
- {
- Parallel.ForEach(_Connectors.Values, item =>
- {
- try
- {
- item.Mini8ConnectNotify(mini8Index, true);
- }
- catch
- {
- log.Warning($"DataDispatcher try send Mini8Connect to failed {item.Name} mini8 {mini8Index}");
- }
- });
- }
- public void Mini8Disconnect(byte mini8Index)
- {
- Parallel.ForEach(_Connectors.Values, item =>
- {
- try
- {
- item.Mini8ConnectNotify(mini8Index, false);
- }
- catch
- {
- log.Warning($"DataDispatcher try send Mini8Disconnect failed {item.Name} mini8 {mini8Index}");
- }
- });
- }
- }
|