SPaceTRanRobotHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.SUNWAY
  11. {
  12. public abstract class SPaceTRanRobotHandler : HandlerBase
  13. {
  14. public SPaceTRanRobot Device { get; }
  15. protected string _commandType;
  16. protected string _command;
  17. protected string _parameter;
  18. public SPaceTRanRobotHandler(SPaceTRanRobot device, string command, string parameter = null)
  19. : base(BuildMessage(command, parameter, device))
  20. {
  21. Device = device;
  22. _command = command;
  23. _parameter = parameter;
  24. Name = command;
  25. AckTimeout = TimeSpan.FromSeconds(30);
  26. CompleteTimeout = TimeSpan.FromSeconds(60);
  27. }
  28. protected static string BuildMessage(string command, string parameter, SPaceTRanRobot rbt)
  29. {
  30. return $"{command}{parameter ?? ""}" + "\r";
  31. }
  32. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  33. {
  34. SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
  35. ResponseMessage = msg;
  36. if (msg.IsError)
  37. {
  38. Device.NoteError(response.RawMessage);
  39. transactionComplete = true;
  40. return true;
  41. }
  42. if (msg.IsAck)
  43. {
  44. SetState(EnumHandlerState.Acked);
  45. }
  46. if (msg.IsComplete)
  47. {
  48. SetState(EnumHandlerState.Completed);
  49. Device.OnActionDone(null);
  50. transactionComplete = true;
  51. return true;
  52. }
  53. transactionComplete = false;
  54. return false;
  55. }
  56. }
  57. public class SPaceTRanRobotSetHandler : SPaceTRanRobotHandler
  58. {
  59. public SPaceTRanRobotSetHandler(SPaceTRanRobot robot, string command, string parameter = null)
  60. : base(robot, command, parameter)
  61. {
  62. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  63. LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII.");
  64. }
  65. }
  66. public class SPaceTRanRobotReadHandler : SPaceTRanRobotHandler
  67. {
  68. public SPaceTRanRobotReadHandler(SPaceTRanRobot robot, string command, string parameter = null)
  69. : base(robot, command, parameter)
  70. {
  71. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  72. LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII.");
  73. }
  74. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  75. {
  76. SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
  77. ResponseMessage = msg;
  78. if (msg.IsError)
  79. {
  80. Device.NoteError(response.RawMessage);
  81. transactionComplete = true;
  82. return true;
  83. }
  84. if (msg.IsAck)
  85. {
  86. SetState(EnumHandlerState.Acked);
  87. }
  88. Device.ParseReadData(_command, response.RawMessage);
  89. if (msg.IsComplete)
  90. {
  91. SetState(EnumHandlerState.Completed);
  92. Device.OnActionDone(null);
  93. transactionComplete = true;
  94. return true;
  95. }
  96. transactionComplete = false;
  97. return false;
  98. }
  99. }
  100. public class SPaceTRanRobotMotionHandler : SPaceTRanRobotHandler
  101. {
  102. public SPaceTRanRobotMotionHandler(SPaceTRanRobot robot, string command, string parameter = null)
  103. : base(robot, command, parameter)
  104. {
  105. string temp = string.IsNullOrEmpty(parameter) ? parameter : "";
  106. LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII.");
  107. }
  108. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  109. {
  110. SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage;
  111. ResponseMessage = msg;
  112. LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII.");
  113. if (msg.IsError)
  114. {
  115. Device.NoteError(response.RawMessage);
  116. transactionComplete = true;
  117. return true;
  118. }
  119. if (msg.IsAck)
  120. {
  121. SetState(EnumHandlerState.Acked);
  122. }
  123. if (msg.IsComplete)
  124. {
  125. SetState(EnumHandlerState.Completed);
  126. Device.OnActionDone(null);
  127. transactionComplete = true;
  128. return true;
  129. }
  130. transactionComplete = false;
  131. return false;
  132. }
  133. }
  134. }