Mini8DataDispatcher.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using HardwareData;
  2. using MinicsConsole.Connector;
  3. using System.Diagnostics;
  4. using System.Net.NetworkInformation;
  5. using System.Threading.Channels;
  6. namespace MinicsConsole.Helper;
  7. public class Mini8DataDispatcher
  8. {
  9. public Mini8DataDispatcher(ILog log)
  10. {
  11. this.log = log;
  12. }
  13. private readonly ILog log;
  14. private readonly ConcurrentDictionary<string, IMini8DataNotifier> _Connectors = [];
  15. private readonly ConcurrentDictionary<string, EventQueue<(string name, byte mini8, byte channel, ChannelData channelData)>> _ConnectorQueue = [];
  16. private void QueueHandler((string name, byte mini8, byte channel, ChannelData channelData) t)
  17. {
  18. if (!_Connectors.TryGetValue(t.name, out IMini8DataNotifier? notifer) || notifer is null)
  19. return;
  20. try
  21. {
  22. notifer.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
  23. }
  24. catch
  25. {
  26. log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {t.name} mini8 {t.mini8} channel {t.channel}");
  27. }
  28. }
  29. public bool TryAddNotifier(string name, IMini8DataNotifier connector)
  30. {
  31. if (!_Connectors.TryAdd(name, connector))
  32. return false;
  33. _ConnectorQueue[name] = new(QueueHandler);
  34. return true;
  35. }
  36. public bool TryRemoveConnector(string name)
  37. {
  38. _ConnectorQueue.TryRemove(name, out _);
  39. return _Connectors.TryRemove(name, out _);
  40. }
  41. public void ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  42. {
  43. if (channelData is null)
  44. return;
  45. foreach (var item in _ConnectorQueue)
  46. {
  47. item.Value.Enqueue((item.Key, mini8, channel, channelData));
  48. }
  49. //Parallel.ForEach(_Connectors.Values, item =>
  50. //{
  51. // Task.Factory.StartNew(() =>
  52. // {
  53. // try
  54. // {
  55. // item?.ChannelInfoNotify(mini8, channel, channelData);
  56. // }
  57. // catch
  58. // {
  59. // log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
  60. // }
  61. // });
  62. //});
  63. }
  64. public void AlarmNotify(byte mini8, byte channel, float temperature)
  65. {
  66. Parallel.ForEach(_Connectors.Values, item =>
  67. {
  68. try
  69. {
  70. item.AlarmNotify(mini8, channel, temperature);
  71. }
  72. catch
  73. {
  74. log.Warning($"DataDispatcher try send AlarmNotify failed {item.Name} mini8 {mini8} channel {channel}");
  75. }
  76. });
  77. }
  78. public void AlarmTcBrockenNotify(byte mini8, byte channel)
  79. {
  80. Parallel.ForEach(_Connectors.Values, item =>
  81. {
  82. try
  83. {
  84. item.AlarmTcBrockenNotify(mini8, channel);
  85. }
  86. catch
  87. {
  88. log.Warning($"DataDispatcher try send AlarmTcBrockenNotify failed {item.Name} mini8 {mini8} channel {channel}");
  89. }
  90. });
  91. }
  92. public void Mini8Connect(byte mini8Index)
  93. {
  94. Parallel.ForEach(_Connectors.Values, item =>
  95. {
  96. try
  97. {
  98. item.Mini8ConnectNotify(mini8Index, true);
  99. }
  100. catch
  101. {
  102. log.Warning($"DataDispatcher try send Mini8Connect to failed {item.Name} mini8 {mini8Index}");
  103. }
  104. });
  105. }
  106. public void Mini8Disconnect(byte mini8Index)
  107. {
  108. Parallel.ForEach(_Connectors.Values, item =>
  109. {
  110. try
  111. {
  112. item.Mini8ConnectNotify(mini8Index, false);
  113. }
  114. catch
  115. {
  116. log.Warning($"DataDispatcher try send Mini8Disconnect failed {item.Name} mini8 {mini8Index}");
  117. }
  118. });
  119. }
  120. }