Mini8DataDispatcher.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. namespace MinicsConsole.Helper;
  2. public class Mini8DataDispatcher
  3. {
  4. public Mini8DataDispatcher(ILog log,
  5. UISender uiNotifier,
  6. RTNotifier rtNotifer,
  7. PLCNotifier plcNotifier)
  8. //KanbanNotifier kanbanNotifier)
  9. {
  10. this._log = log;
  11. this._Connectors.TryAdd("PLC", plcNotifier);
  12. this._Connectors.TryAdd("UI", uiNotifier);
  13. this._Connectors.TryAdd("RT", rtNotifer);
  14. //this._Connectors.TryAdd("Kanban", kanbanNotifier);
  15. this._UINotifier = uiNotifier;
  16. this._PlcNotifier = plcNotifier;
  17. this._RTNotifier = rtNotifer;
  18. this._UIQueue = new(UIQueueHandler);
  19. this._RTQueue = new(RTQueueHandler);
  20. this._PLCQueue = new(PLCQueueHandler);
  21. }
  22. private readonly ILog _log;
  23. private readonly ConcurrentDictionary<string, IMini8DataNotifier> _Connectors = [];
  24. private readonly IMini8DataNotifier _UINotifier;
  25. private readonly IMini8DataNotifier _PlcNotifier;
  26. private readonly IMini8DataNotifier _RTNotifier;
  27. private readonly EventQueue<(byte mini8, byte channel, ChannelData channelData)> _UIQueue;
  28. private readonly EventQueue<(byte mini8, byte channel, ChannelData channelData)> _PLCQueue;
  29. private readonly EventQueue<(byte mini8, byte channel, ChannelData channelData)> _RTQueue;
  30. private void RTQueueHandler((byte mini8, byte channel, ChannelData channelData) t)
  31. {
  32. if (this._RTQueue.Count >= 2)
  33. return;
  34. try
  35. {
  36. this._RTNotifier?.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
  37. }
  38. catch
  39. {
  40. _log?.Warning($"DataDispatcher try send ChannelInfoNotify failed RT mini8 {t.mini8} channel {t.channel}");
  41. }
  42. }
  43. private void UIQueueHandler((byte mini8, byte channel, ChannelData channelData) t)
  44. {
  45. if (this._UIQueue.Count >= 2)
  46. return;
  47. try
  48. {
  49. this._UINotifier?.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
  50. }
  51. catch
  52. {
  53. _log?.Warning($"DataDispatcher try send ChannelInfoNotify failed UI mini8 {t.mini8} channel {t.channel}");
  54. }
  55. }
  56. private void PLCQueueHandler((byte mini8, byte channel, ChannelData channelData) t)
  57. {
  58. if (this._PLCQueue.Count >= 2)
  59. return;
  60. try
  61. {
  62. this._PlcNotifier?.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
  63. }
  64. catch
  65. {
  66. _log?.Warning($"DataDispatcher try send ChannelInfoNotify failed PLC mini8 {t.mini8} channel {t.channel}");
  67. }
  68. }
  69. public void ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  70. {
  71. if (channelData is null)
  72. return;
  73. _RTQueue.Enqueue((mini8, channel, channelData));
  74. _PLCQueue.Enqueue((mini8, channel, channelData));
  75. _UIQueue.Enqueue((mini8, channel, channelData));
  76. }
  77. public void AlarmNotify(byte mini8, byte channel, float temperature)
  78. {
  79. Parallel.ForEach(_Connectors.Values, item =>
  80. {
  81. try
  82. {
  83. item.AlarmNotify(mini8, channel, temperature);
  84. }
  85. catch
  86. {
  87. _log.Warning($"DataDispatcher try send AlarmNotify failed {item.Name} mini8 {mini8} channel {channel}");
  88. }
  89. });
  90. }
  91. public void AlarmTcBrockenNotify(byte mini8, byte channel)
  92. {
  93. Parallel.ForEach(_Connectors.Values, item =>
  94. {
  95. try
  96. {
  97. item.AlarmTcBrockenNotify(mini8, channel);
  98. }
  99. catch
  100. {
  101. _log.Warning($"DataDispatcher try send AlarmTcBrockenNotify failed {item.Name} mini8 {mini8} channel {channel}");
  102. }
  103. });
  104. }
  105. public void Mini8Connect(byte mini8Index)
  106. {
  107. Parallel.ForEach(_Connectors.Values, item =>
  108. {
  109. try
  110. {
  111. item.Mini8ConnectNotify(mini8Index, true);
  112. }
  113. catch
  114. {
  115. _log.Warning($"DataDispatcher try send Mini8Connect to failed {item.Name} mini8 {mini8Index}");
  116. }
  117. });
  118. }
  119. public void Mini8Disconnect(byte mini8Index)
  120. {
  121. Parallel.ForEach(_Connectors.Values, item =>
  122. {
  123. try
  124. {
  125. item.Mini8ConnectNotify(mini8Index, false);
  126. }
  127. catch
  128. {
  129. _log.Warning($"DataDispatcher try send Mini8Disconnect failed {item.Name} mini8 {mini8Index}");
  130. }
  131. });
  132. }
  133. }