using Aitex.Core.RT.Device; using Aitex.Core.RT.Log; using MECF.Framework.Common.Communications; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.SUNWAY { public abstract class SPaceTRanRobotHandler : HandlerBase { public SPaceTRanRobot Device { get; } protected string _commandType; protected string _command; protected string _parameter; public SPaceTRanRobotHandler(SPaceTRanRobot device, string command, string parameter = null) : base(BuildMessage(command, parameter, device)) { Device = device; _command = command; _parameter = parameter; Name = command; AckTimeout = TimeSpan.FromSeconds(30); CompleteTimeout = TimeSpan.FromSeconds(60); } protected static string BuildMessage(string command, string parameter, SPaceTRanRobot rbt) { return $"{command}{parameter ?? ""}" + "\r"; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage; ResponseMessage = msg; if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (msg.IsAck) { SetState(EnumHandlerState.Acked); } if (msg.IsComplete) { SetState(EnumHandlerState.Completed); Device.OnActionDone(null); transactionComplete = true; return true; } transactionComplete = false; return false; } } public class SPaceTRanRobotSetHandler : SPaceTRanRobotHandler { public SPaceTRanRobotSetHandler(SPaceTRanRobot robot, string command, string parameter = null) : base(robot, command, parameter) { string temp = string.IsNullOrEmpty(parameter) ? parameter : ""; LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII."); } } public class SPaceTRanRobotReadHandler : SPaceTRanRobotHandler { public SPaceTRanRobotReadHandler(SPaceTRanRobot robot, string command, string parameter = null) : base(robot, command, parameter) { string temp = string.IsNullOrEmpty(parameter) ? parameter : ""; LOG.Write($"{robot.Name} execute read command {command} {temp} in ASCII."); } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage; ResponseMessage = msg; if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (msg.IsAck) { SetState(EnumHandlerState.Acked); } Device.ParseReadData(_command, response.RawMessage); if (msg.IsComplete) { SetState(EnumHandlerState.Completed); Device.OnActionDone(null); transactionComplete = true; return true; } transactionComplete = false; return false; } } public class SPaceTRanRobotMotionHandler : SPaceTRanRobotHandler { public SPaceTRanRobotMotionHandler(SPaceTRanRobot robot, string command, string parameter = null) : base(robot, command, parameter) { string temp = string.IsNullOrEmpty(parameter) ? parameter : ""; LOG.Write($"{robot.Name} execute motion command {command} {temp} in ASCII."); } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { SPaceTRanRobotMessage response = msg as SPaceTRanRobotMessage; ResponseMessage = msg; LOG.Write($"{Device.Name} handler message:{response.RawMessage} in ASCII."); if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (msg.IsAck) { SetState(EnumHandlerState.Acked); } if (msg.IsComplete) { SetState(EnumHandlerState.Completed); Device.OnActionDone(null); transactionComplete = true; return true; } transactionComplete = false; return false; } } }