RTNotifier.cs 2.6 KB

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