SkyPumpHandler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Aitex.Core.RT.Device;
  2. using MECF.Framework.Common.Communications;
  3. using Newtonsoft.Json.Linq;
  4. using System.Linq;
  5. using System.Text;
  6. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkyPump
  7. {
  8. public abstract class SkyPumpHandler : HandlerBase
  9. {
  10. public SkyPump Device { get; }
  11. public string _command;
  12. protected string _completeEvent;
  13. protected SkyPumpHandler(SkyPump device, string command,string completeEvent)
  14. : base(BuildMessage(device.Address, command))
  15. {
  16. Device = device;
  17. _command = command;
  18. _completeEvent = completeEvent;
  19. Name = command;
  20. }
  21. private static string _startLine = "@";
  22. private static string _endLine = "\r";
  23. private static string BuildMessage(string address, string command)
  24. {
  25. return _startLine + address + command + _endLine;
  26. }
  27. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  28. {
  29. SkyPumpMessage response = msg as SkyPumpMessage;
  30. ResponseMessage = msg;
  31. if (response.IsAck)
  32. {
  33. SetState(EnumHandlerState.Acked);
  34. if (response.Address != Device.Address)
  35. {
  36. Device.NoteError("Invalid address");
  37. msg.IsFormatError = true;
  38. }
  39. else if (!response.Data.Contains(_completeEvent))
  40. {
  41. Device.NoteError("Invalid response, no expected command feedback");
  42. msg.IsFormatError = true;
  43. }
  44. else
  45. {
  46. SetState(EnumHandlerState.Completed);
  47. transactionComplete = true;
  48. Device.NoteError(null);
  49. return true;
  50. }
  51. }
  52. transactionComplete = false;
  53. return false;
  54. }
  55. }
  56. public class SkyPumpRawCommandHandler : SkyPumpHandler
  57. {
  58. public SkyPumpRawCommandHandler(SkyPump device, string command, string completeEvent)
  59. : base(device, command, completeEvent)
  60. {
  61. }
  62. public override bool HandleMessage(MessageBase msg, out bool handled)
  63. {
  64. if(base.HandleMessage(msg, out handled))
  65. {
  66. var result = msg as SkyPumpMessage;
  67. Device.NoteRawCommandInfo(_command, result.RawMessage);
  68. }
  69. return true;
  70. }
  71. }
  72. public class SkyPumpStartHandler : SkyPumpHandler
  73. {
  74. public SkyPumpStartHandler(SkyPump device)
  75. : base(device, "CON_FDP_ON", "FDP_ONOK")
  76. {
  77. }
  78. public override bool HandleMessage(MessageBase msg, out bool handled)
  79. {
  80. if(base.HandleMessage(msg, out handled))
  81. {
  82. Device.NoteStart(true);
  83. }
  84. return true;
  85. }
  86. }
  87. public class SkyPumpStopHandler : SkyPumpHandler
  88. {
  89. public SkyPumpStopHandler(SkyPump device)
  90. : base(device, "CON_FDP_OFF", "FDP_OFFOK")
  91. {
  92. }
  93. public override bool HandleMessage(MessageBase msg, out bool handled)
  94. {
  95. if (base.HandleMessage(msg, out handled))
  96. {
  97. Device.NoteStart(false);
  98. }
  99. return true;
  100. }
  101. }
  102. public class SkyPumpReadRunParaHandler : SkyPumpHandler
  103. {
  104. public SkyPumpReadRunParaHandler(SkyPump device)
  105. : base(device, "READ_RUN_PARA", "RUN_PARA")
  106. {
  107. }
  108. public override bool HandleMessage(MessageBase msg, out bool handled)
  109. {
  110. if (base.HandleMessage(msg, out handled))
  111. {
  112. var result = msg as SkyPumpMessage;
  113. if(result.Data.Length != 40)
  114. {
  115. Device.NoteError("Invalid 'RunParameter'response, the length is not 40");
  116. }
  117. else
  118. {
  119. Device.NoteRunPara(result.Data);
  120. }
  121. }
  122. return true;
  123. }
  124. }
  125. }