RorzeRobot751Handler.cs 7.4 KB

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