RTNotifier.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. }