Mini8DataDispatcher.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. Parallel.ForEach(_Connectors.Values, item =>
  18. {
  19. try
  20. {
  21. item?.ChannelInfoNotify(mini8, channel, channelData);
  22. }
  23. catch
  24. {
  25. log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
  26. }
  27. });
  28. }
  29. public void AlarmNotify(byte mini8, byte channel, float temperature)
  30. {
  31. Parallel.ForEach(_Connectors.Values, item =>
  32. {
  33. try
  34. {
  35. item.AlarmNotify(mini8, channel, temperature);
  36. }
  37. catch
  38. {
  39. log.Warning($"DataDispatcher try send AlarmNotify failed {item.Name} mini8 {mini8} channel {channel}");
  40. }
  41. });
  42. }
  43. public void AlarmTcBrockenNotify(byte mini8, byte channel)
  44. {
  45. Parallel.ForEach(_Connectors.Values, item =>
  46. {
  47. try
  48. {
  49. item.AlarmTcBrockenNotify(mini8, channel);
  50. }
  51. catch
  52. {
  53. log.Warning($"DataDispatcher try send AlarmTcBrockenNotify failed {item.Name} mini8 {mini8} channel {channel}");
  54. }
  55. });
  56. }
  57. public void Mini8Connect(byte mini8Index)
  58. {
  59. Parallel.ForEach(_Connectors.Values, item =>
  60. {
  61. try
  62. {
  63. item.Mini8ConnectNotify(mini8Index, true);
  64. }
  65. catch
  66. {
  67. log.Warning($"DataDispatcher try send Mini8Connect to failed {item.Name} mini8 {mini8Index}");
  68. }
  69. });
  70. }
  71. public void Mini8Disconnect(byte mini8Index)
  72. {
  73. Parallel.ForEach(_Connectors.Values, item =>
  74. {
  75. try
  76. {
  77. item.Mini8ConnectNotify(mini8Index, false);
  78. }
  79. catch
  80. {
  81. log.Warning($"DataDispatcher try send Mini8Disconnect failed {item.Name} mini8 {mini8Index}");
  82. }
  83. });
  84. }
  85. }