Mini8DataDispatcher.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. namespace MinicsConsole.Helper;
  2. public class Mini8DataDispatcher(ILog log)
  3. {
  4. private readonly ConcurrentDictionary<string, IMini8DataNotifier> _Connectors = [];
  5. public bool TryAddNotifier(string name, IMini8DataNotifier connector)
  6. {
  7. return _Connectors.TryAdd(name, connector);
  8. }
  9. public bool TryRemoveConnector(string name)
  10. {
  11. return _Connectors.TryRemove(name, out _);
  12. }
  13. public void ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  14. {
  15. if (channelData is null)
  16. return;
  17. Task.Factory.StartNew(() =>
  18. {
  19. Parallel.ForEach(_Connectors.Values, item =>
  20. {
  21. try
  22. {
  23. item?.ChannelInfoNotify(mini8, channel, channelData);
  24. }
  25. catch
  26. {
  27. log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
  28. }
  29. });
  30. });
  31. }
  32. public void AlarmNotify(byte mini8, byte channel, float temperature)
  33. {
  34. Parallel.ForEach(_Connectors.Values, item =>
  35. {
  36. try
  37. {
  38. item.AlarmNotify(mini8, channel, temperature);
  39. }
  40. catch
  41. {
  42. log.Warning($"DataDispatcher try send AlarmNotify failed {item.Name} mini8 {mini8} channel {channel}");
  43. }
  44. });
  45. }
  46. public void AlarmTcBrockenNotify(byte mini8, byte channel)
  47. {
  48. Parallel.ForEach(_Connectors.Values, item =>
  49. {
  50. try
  51. {
  52. item.AlarmTcBrockenNotify(mini8, channel);
  53. }
  54. catch
  55. {
  56. log.Warning($"DataDispatcher try send AlarmTcBrockenNotify failed {item.Name} mini8 {mini8} channel {channel}");
  57. }
  58. });
  59. }
  60. public void Mini8Connect(byte mini8Index)
  61. {
  62. Parallel.ForEach(_Connectors.Values, item =>
  63. {
  64. try
  65. {
  66. item.Mini8ConnectNotify(mini8Index, true);
  67. }
  68. catch
  69. {
  70. log.Warning($"DataDispatcher try send Mini8Connect to failed {item.Name} mini8 {mini8Index}");
  71. }
  72. });
  73. }
  74. public void Mini8Disconnect(byte mini8Index)
  75. {
  76. Parallel.ForEach(_Connectors.Values, item =>
  77. {
  78. try
  79. {
  80. item.Mini8ConnectNotify(mini8Index, false);
  81. }
  82. catch
  83. {
  84. log.Warning($"DataDispatcher try send Mini8Disconnect failed {item.Name} mini8 {mini8Index}");
  85. }
  86. });
  87. }
  88. }