RTNotifier.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using RTCommunicatorBase;
  2. using RTCommunicatorTLV;
  3. namespace MinicsConsole.Connector;
  4. public class RTNotifier(Hardwares hardwares) : IMini8DataNotifier, IDisposable
  5. {
  6. private ITlvCommunicatorServer? _server;
  7. public string Name { get; set; } = "RT TLV";
  8. public bool Initialize(ITlvProvider tlvProvider)
  9. {
  10. if (this._server is not null)
  11. return false;
  12. this._server = TlvFactory.GetTlvServer();
  13. this._server.Initialize(tlvProvider);
  14. return true;
  15. }
  16. public bool Open(string IP, ushort port)
  17. {
  18. if (this._server is null)
  19. return false;
  20. return this._server.Open(IP, port);
  21. }
  22. public void Dispose()
  23. {
  24. this._server?.Dispose();
  25. this._server = null;
  26. }
  27. void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  28. {
  29. return;
  30. }
  31. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  32. {
  33. if (!hardwares.Mini8Channels.TryGetSubValue(mini8, channel, out ChannelData? channelData) || channelData is null)
  34. return;
  35. RTCommunicatorBase.AlarmType alarmType;
  36. if (temperature > channelData.Caps)
  37. alarmType = RTCommunicatorBase.AlarmType.CapsOverFlow;
  38. else if (temperature < channelData.Floor)
  39. alarmType = RTCommunicatorBase.AlarmType.FloorOverFlow;
  40. else
  41. alarmType = RTCommunicatorBase.AlarmType.Undefined;
  42. ST_ALARM alarm = new()
  43. {
  44. Mini8Index = mini8,
  45. ChannelIndex = channel,
  46. PV = temperature,
  47. Caps = channelData.Caps,
  48. Floor = channelData.Floor,
  49. AlarmType = alarmType
  50. };
  51. Task.Factory.StartNew(() =>
  52. {
  53. this._server?.Send(Tags.ChannelAlarmNotify, alarm);
  54. });
  55. }
  56. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  57. {
  58. ST_ALARM alarm = new()
  59. {
  60. Mini8Index = mini8,
  61. ChannelIndex = channel,
  62. AlarmType = RTCommunicatorBase.AlarmType.TcBroken,
  63. };
  64. this._server?.Send(Tags.ChannelAlarmNotify, alarm);
  65. }
  66. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  67. {
  68. byte tag = connected ? Tags.Mini8ConnectNotify : Tags.Mini8DisconnectNotify;
  69. this._server?.Send(tag, mini8);
  70. }
  71. }
  72. public class RTNotiferSignalR(Hardwares hardwares) : SenderBase_SignalR, IMini8DataNotifier
  73. {
  74. public string Name { get; set; } = "RT SignalR";
  75. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  76. {
  77. if (!hardwares.Mini8Channels.TryGetSubValue(mini8, channel, out ChannelData? channelData) || channelData is null)
  78. return;
  79. RTCommunicatorBase.AlarmType alarmType;
  80. if (temperature > channelData.Caps)
  81. alarmType = RTCommunicatorBase.AlarmType.CapsOverFlow;
  82. else if (temperature < channelData.Floor)
  83. alarmType = RTCommunicatorBase.AlarmType.FloorOverFlow;
  84. else
  85. alarmType = RTCommunicatorBase.AlarmType.Undefined;
  86. ST_ALARM alarm = new()
  87. {
  88. Mini8Index = mini8,
  89. ChannelIndex = channel,
  90. PV = temperature,
  91. Caps = channelData.Caps,
  92. Floor = channelData.Floor,
  93. AlarmType = alarmType
  94. };
  95. Task.Factory.StartNew(() =>
  96. {
  97. base.SendAll("AlarmNotify", alarm);
  98. });
  99. }
  100. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  101. {
  102. ST_ALARM alarm = new()
  103. {
  104. Mini8Index = mini8,
  105. ChannelIndex = channel,
  106. AlarmType = RTCommunicatorBase.AlarmType.TcBroken,
  107. };
  108. base.SendAll("AlarmNotify", alarm);
  109. }
  110. void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  111. {
  112. if (channelData.Inhibit == Inhibit.Disable)
  113. return;
  114. ChannelRealtimeInfo channelRealtime = channelData.Adapt<ChannelRealtimeInfo>();
  115. base.SendAll("ChannelInfoNotify", mini8, channel, channelRealtime);
  116. }
  117. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  118. {
  119. base.SendAll("Mini8ConnectNotify", mini8, connected);
  120. }
  121. }