RorzeRobot751Handler.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Aitex.Core.RT.Device;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.Rorze
  11. {
  12. public class RorzeRobot751ProtocolTag
  13. {
  14. public const string tag_order = "o";
  15. public const string response_ack = "a";
  16. public const string response_nak = "n";
  17. public const string response_cancel = "c";
  18. public const string response_event = "e";
  19. }
  20. public abstract class RorzeRobot751Handler : HandlerBase
  21. {
  22. public RorzeRobot751 Device { get; }
  23. protected string _commandType;
  24. protected string _command;
  25. protected string _parameter;
  26. public RorzeRobot751Handler(RorzeRobot751 device,string command,string parameter =null)
  27. :base(BuildMessage(command,parameter,device))
  28. {
  29. Device = device;
  30. _command = command;
  31. _parameter = parameter;
  32. Name = command;
  33. AckTimeout = TimeSpan.FromSeconds(30);
  34. CompleteTimeout = TimeSpan.FromSeconds(60);
  35. }
  36. protected static string BuildMessage(string command, string parameter,RorzeRobot751 rbt)
  37. {
  38. return $"oTRB{rbt.RobotBodyNumber}.{command}{parameter??""}\r";
  39. }
  40. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  41. {
  42. RorzeRobot751Message response = msg as RorzeRobot751Message;
  43. ResponseMessage = msg;
  44. if (msg.IsError)
  45. {
  46. Device.NoteError(response.RawMessage);
  47. transactionComplete = true;
  48. return true;
  49. }
  50. if(msg.IsAck)
  51. {
  52. SetState(EnumHandlerState.Acked);
  53. }
  54. if (msg.IsComplete)
  55. {
  56. SetState(EnumHandlerState.Completed);
  57. Device.OnActionDone(null);
  58. transactionComplete = true;
  59. return true;
  60. }
  61. transactionComplete = false;
  62. return false;
  63. }
  64. }
  65. public class RorzeRobot751ReadHandler : RorzeRobot751Handler
  66. {
  67. public RorzeRobot751ReadHandler(RorzeRobot751 robot, string command,string parameter=null)
  68. :base(robot,command,parameter)
  69. {
  70. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  71. LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII.");
  72. }
  73. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  74. {
  75. RorzeRobot751Message response = msg as RorzeRobot751Message;
  76. if(!response.RawMessage.Contains(_command))
  77. {
  78. transactionComplete = false;
  79. return false;
  80. }
  81. if(!response.IsAck)
  82. {
  83. Device.OnError($"GetErrorMsg:{response.RawMessage}");
  84. transactionComplete = true;
  85. return true;
  86. }
  87. Device.ParseReadData(_command, response.Data);
  88. Device.OnActionDone(null);
  89. transactionComplete = true;
  90. return true;
  91. }
  92. }
  93. public class RorzeRobot751SetHandler : RorzeRobot751Handler
  94. {
  95. public RorzeRobot751SetHandler(RorzeRobot751 robot, string command, string parameter = null)
  96. : base(robot, command, parameter)
  97. {
  98. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  99. LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
  100. }
  101. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  102. {
  103. RorzeRobot751Message response = msg as RorzeRobot751Message;
  104. if (!response.RawMessage.Contains(_command))
  105. {
  106. transactionComplete = false;
  107. return false;
  108. }
  109. if (response.IsAck)
  110. {
  111. Device.OnActionDone(null);
  112. transactionComplete = true;
  113. return true;
  114. }
  115. transactionComplete = true;
  116. return false;
  117. }
  118. }
  119. public class RorzeRobot751MotionHandler : RorzeRobot751Handler
  120. {
  121. public RorzeRobot751MotionHandler(RorzeRobot751 robot, string command, string parameter = null)
  122. : base(robot, command, parameter)
  123. {
  124. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  125. LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
  126. }
  127. private string _motionCmd = "HOME,EXTD,LOAD, UNLD,TRNS, EXCH,CLMP, UCLM,WMAP, UTRN,MGET, MPUT,MGT1, " +
  128. "MGT2,MPT1, MPT2,WCHK, ALEX,ALLD, ALUL,ALGT, ALEA,ALMV,STEP";
  129. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  130. {
  131. RorzeRobot751Message response = msg as RorzeRobot751Message;
  132. ResponseMessage = msg;
  133. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  134. if (msg.IsError)
  135. {
  136. Device.NoteError(response.RawMessage);
  137. transactionComplete = true;
  138. return true;
  139. }
  140. if (msg.IsAck)
  141. {
  142. SetState(EnumHandlerState.Acked);
  143. }
  144. if(msg.IsComplete)
  145. {
  146. //Device.SenACK();
  147. }
  148. if(response.IsEvent)
  149. {
  150. if(_command == "ORGN" && response.RawMessage.Contains("STAT")
  151. && Device.IsOrgshCompleted && Device.CurrentOpStatus == RROpStatusEnum.Stop)
  152. {
  153. Device.OnActionDone(null);
  154. transactionComplete = true;
  155. return true;
  156. }
  157. if (_command == "INIT" && response.RawMessage.Contains("STAT")
  158. && Device.CurrentOpMode != RROpModeEnum.Initializing)
  159. {
  160. Device.OnActionDone(null);
  161. transactionComplete = true;
  162. return true;
  163. }
  164. if (_motionCmd.Contains(_command))
  165. {
  166. if(response.RawMessage.Contains("STAT") && !Device.IsOrgshCompleted)
  167. {
  168. Device.OnError($"GetErrorMsg:{ response.RawMessage} on command:{_command}");
  169. transactionComplete = true;
  170. return true;
  171. }
  172. if (response.RawMessage.Contains("STAT") && Device.CurrentOpStatus == RROpStatusEnum.Stop)
  173. {
  174. Device.OnActionDone(null);
  175. transactionComplete = true;
  176. return true;
  177. }
  178. }
  179. }
  180. transactionComplete = false;
  181. return false;
  182. }
  183. }
  184. }