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.HRC100Robots.GM201LVPRobot { public abstract class GM201LVPRobotHandler : HandlerBase { public GM201LVPRobot Device { get; } public string _commandType; public string _command; public string _parameter; public GM201LVPRobotHandler(GM201LVPRobot device, string command, string parameter = null) : base(BuildMessage(command, parameter)) { Device = device; _command = command; _parameter = parameter; Name = command; } protected static string BuildMessage(string command, string parameter) { if (string.IsNullOrEmpty(parameter)) return command + "\r"; return $"{command},{parameter}\r"; } private static string Checksum(byte[] bytes) { int sum = 0; foreach (byte code in bytes) { sum += code; } string hex = String.Format("{0:X2}", sum % 256); return hex; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { GM201LVPRobotMessage response = msg as GM201LVPRobotMessage; ResponseMessage = msg; if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (msg.IsAck) { SetState(EnumHandlerState.Acked); } if (msg.IsComplete && response.RawMessage.Split(',')[0] == "!") { SetState(EnumHandlerState.Completed); //Device.SenACK(); Device.OnActionDone(null); transactionComplete = true; return true; } transactionComplete = false; return false; } protected virtual void NoteCommandResult(YaskawaRobotMessage response) { } public List replayWithErrorAtCmd = new List() { "INIT", "MTRS", "MTCH", "MABS", "MREL" }; } public class GM201LVPRobotReadHandler : GM201LVPRobotHandler { public GM201LVPRobotReadHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60) : base(robot, command, parameter) { AckTimeout = TimeSpan.FromSeconds(timeout); CompleteTimeout = TimeSpan.FromSeconds(timeout); 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) { GM201LVPRobotMessage response = msg as GM201LVPRobotMessage; if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (!response.RawMessage.Contains(_command)) { transactionComplete = false; return false; } if (Device.ParseReadData(_command, response.Data)) { Device.OnActionDone(null); transactionComplete = true; return true; } response.IsError = true; return base.HandleMessage(response, out transactionComplete); } } public class GM201LVPRobotSetHandler : GM201LVPRobotHandler { public GM201LVPRobotSetHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60) : base(robot, command, parameter) { AckTimeout = TimeSpan.FromSeconds(timeout); CompleteTimeout = TimeSpan.FromSeconds(timeout); string temp = string.IsNullOrEmpty(parameter) ? parameter : ""; LOG.Write($"{robot.Name} execute set command {command} {temp} in ASCII."); } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { GM201LVPRobotMessage response = msg as GM201LVPRobotMessage; if (msg.IsError) { Device.NoteError(response.RawMessage); transactionComplete = true; return true; } if (!response.RawMessage.Contains(_command)) { transactionComplete = false; return false; } if (response.IsAck) { Device.OnActionDone(null); transactionComplete = true; return true; } return base.HandleMessage(response, out transactionComplete); } } public class GM201LVPRobotMotionHandler : GM201LVPRobotHandler { public GM201LVPRobotMotionHandler(GM201LVPRobot robot, string command, string parameter = null, int timeout = 60) : base(robot, command, parameter) { AckTimeout = TimeSpan.FromSeconds(timeout); CompleteTimeout = TimeSpan.FromSeconds(timeout); 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) { GM201LVPRobotMessage response = msg as GM201LVPRobotMessage; 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) { //Device.SenACK(); } if (IsAcked && msg.IsComplete) { SetState(EnumHandlerState.Completed); Device.OnActionDone(null); transactionComplete = true; return true; } transactionComplete = false; return false; } } }