PfeifferPumpA603Handler.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Aitex.Core.RT.Device;
  2. using MECF.Framework.Common.Communications;
  3. using Newtonsoft.Json.Linq;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.PfeifferPumpA603
  8. {
  9. public abstract class PfeifferPumpA603Handler : HandlerBase
  10. {
  11. public PfeifferPumpA603 Device { get; }
  12. public string _command;
  13. protected string _parameter;
  14. protected PfeifferPumpA603Handler(PfeifferPumpA603 device, string command, string parameter)
  15. : base(BuildMessage(device.ADR, command, parameter))
  16. {
  17. Device = device;
  18. _command = command;
  19. _parameter = parameter;
  20. Name = command;
  21. }
  22. private static string _startLine = "#";
  23. private static string _endLine = "\r";
  24. private static string BuildMessage(string address, string command, string parameter)
  25. {
  26. if (string.IsNullOrEmpty(parameter))
  27. return _startLine + address + command + _endLine;
  28. else
  29. return _startLine + address + command + parameter + _endLine;
  30. }
  31. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  32. {
  33. PfeifferPumpA603Message response = msg as PfeifferPumpA603Message;
  34. ResponseMessage = msg;
  35. if (response.IsAck)
  36. {
  37. SetState(EnumHandlerState.Acked);
  38. if (response.Address != Device.ADR)
  39. {
  40. Device.NoteError("Invalid address");
  41. msg.IsFormatError = true;
  42. }
  43. else if (response.Data.Contains("ERR"))
  44. {
  45. Device.NoteError($"Command '{_command}' Error: {response.Data}");
  46. msg.IsError = true;
  47. }
  48. else
  49. {
  50. SetState(EnumHandlerState.Completed);
  51. transactionComplete = true;
  52. Device.NoteError(null);
  53. return true;
  54. }
  55. }
  56. transactionComplete = false;
  57. return false;
  58. }
  59. }
  60. public class PfeifferPumpA603RawCommandHandler : PfeifferPumpA603Handler
  61. {
  62. public PfeifferPumpA603RawCommandHandler(PfeifferPumpA603 device, string command, string parameter)
  63. : base(device, command, parameter)
  64. {
  65. }
  66. public override bool HandleMessage(MessageBase msg, out bool handled)
  67. {
  68. if (base.HandleMessage(msg, out handled))
  69. {
  70. var result = msg as PfeifferPumpA603Message;
  71. Device.NoteRawCommandInfo(_command, result.RawMessage);
  72. }
  73. return true;
  74. }
  75. }
  76. public class PfeifferPumpA603SimpleSwitchHandler : PfeifferPumpA603Handler
  77. {
  78. public PfeifferPumpA603SimpleSwitchHandler(PfeifferPumpA603 device, string command, string parameter)
  79. : base(device, command, parameter)
  80. {
  81. }
  82. public override bool HandleMessage(MessageBase msg, out bool handled)
  83. {
  84. if (base.HandleMessage(msg, out handled))
  85. {
  86. Device.NoteSwitchCompleted(_command, _parameter.Contains("ON"));
  87. }
  88. return true;
  89. }
  90. }
  91. public class PfeifferPumpSetPumpParameterHandler : PfeifferPumpA603Handler
  92. {
  93. public PfeifferPumpSetPumpParameterHandler(PfeifferPumpA603 device, string parameter)
  94. : base(device, "SET", parameter)
  95. {
  96. }
  97. public override bool HandleMessage(MessageBase msg, out bool handled)
  98. {
  99. if (base.HandleMessage(msg, out handled))
  100. {
  101. Device.NoteSetParaCompleted();
  102. }
  103. return true;
  104. }
  105. }
  106. public class PfeifferPumpA603DisplayAlarmHandler : PfeifferPumpA603Handler
  107. {
  108. public PfeifferPumpA603DisplayAlarmHandler(PfeifferPumpA603 device, string parameter)
  109. : base(device, "DEF", parameter)
  110. {
  111. }
  112. public override bool HandleMessage(MessageBase msg, out bool handled)
  113. {
  114. if (base.HandleMessage(msg, out handled))
  115. {
  116. Device.NoteDisplayAlarmOrAlert(_parameter.Contains("0"));
  117. }
  118. return true;
  119. }
  120. }
  121. public class PfeifferPumpA603ReadPumpStatusHandler : PfeifferPumpA603Handler
  122. {
  123. public PfeifferPumpA603ReadPumpStatusHandler(PfeifferPumpA603 device)
  124. : base(device, "STA", null)
  125. {
  126. }
  127. public override bool HandleMessage(MessageBase msg, out bool handled)
  128. {
  129. if (base.HandleMessage(msg, out handled))
  130. {
  131. var result = msg as PfeifferPumpA603Message;
  132. if (result.Data.Length != 68)
  133. {
  134. Device.NoteError("Invalid 'RunParameter'response, the length is not 68");
  135. }
  136. else
  137. {
  138. Device.NotePumpStatus(result.Data);
  139. }
  140. }
  141. return true;
  142. }
  143. }
  144. }