RTNotifier.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. ST_CHANNEL data = new()
  30. {
  31. Mini8Index = mini8,
  32. ChannelIndex = channel,
  33. PV = channelData.PV,
  34. Caps = channelData.Caps,
  35. Floor = channelData.Floor,
  36. WorkingOutput = channelData.WorkingOutput,
  37. };
  38. this._server?.Send(Tags.ChannelRealtimeDataNotify, data);
  39. }
  40. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  41. {
  42. if (!hardwares.Mini8Channels.TryGetSubValue(mini8, channel, out ChannelData? channelData) || channelData is null)
  43. return;
  44. RTCommunicatorBase.AlarmType alarmType;
  45. if (temperature > channelData.Caps)
  46. alarmType = RTCommunicatorBase.AlarmType.CapsOverFlow;
  47. else if (temperature < channelData.Floor)
  48. alarmType = RTCommunicatorBase.AlarmType.FloorOverFlow;
  49. else
  50. alarmType = RTCommunicatorBase.AlarmType.Undefined;
  51. ST_ALARM alarm = new()
  52. {
  53. Mini8Index = mini8,
  54. ChannelIndex = channel,
  55. PV = temperature,
  56. Caps = channelData.Caps,
  57. Floor = channelData.Floor,
  58. WorkingOutput = channelData.WorkingOutput,
  59. AlarmType = alarmType
  60. };
  61. Task.Factory.StartNew(() =>
  62. {
  63. this._server?.Send(Tags.ChannelAlarmNotify, alarm);
  64. });
  65. }
  66. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  67. {
  68. ST_ALARM alarm = new()
  69. {
  70. Mini8Index = mini8,
  71. ChannelIndex = channel,
  72. AlarmType = RTCommunicatorBase.AlarmType.TcBroken,
  73. };
  74. this._server?.Send(Tags.ChannelAlarmNotify, alarm);
  75. }
  76. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  77. {
  78. byte tag = connected ? Tags.Mini8ConnectNotify : Tags.Mini8DisconnectNotify;
  79. this._server?.Send(tag, mini8);
  80. }
  81. }