using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.Communications;
using MECF.Framework.Common.Equipment;
using System;
using MECF.Framework.Common.Gem;

namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.HwinRobot
{
    public abstract class HwinRobotHandler : HandlerBase
    {
        public HwinRobot Device { get; }
        public bool HasResponse { get; set; } = true;

        protected string _command;
        protected string _parameter;
        protected string _target;
        protected RobotArmEnum _blade;
        /// <summary>
        /// Handler执行完毕后执行的任务。
        /// </summary>
        protected Action _onHandlerEndTask;

        protected string _requestResponse = "";

        protected HwinRobotHandler(HwinRobot device, string command, string parameter = null, Action onHandlerEndTask = null)
            : base(BuildMessage(command, parameter))
        {
            Device = device;
            _command = command;
            _parameter = parameter;
            Name = command;
            _onHandlerEndTask = onHandlerEndTask;
        }
        private static string BuildMessage(string command, string parameter)
        {
            string msg = parameter == null ? $"{command}" : $"{command} {parameter}";

            return msg + "\r\n";
        }

        public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
        {
            var response = msg as HwinRobotMessage;
            ResponseMessage = msg;

            if (response.IsError)
            {
                Device.NoteError(response.Data);
            }

            if (response.IsComplete)
            {
                SetState(EnumHandlerState.Completed);
                transactionComplete = true;
                return true;
            }
            if (response.IsResponse)
            {
                _requestResponse = response.Data;
            }

            transactionComplete = false;
            return false;
        }
    }


    public class HwinRobotSTATHandler : HwinRobotHandler
    {
        // STAT
        public HwinRobotSTATHandler(HwinRobot device, int timeout = 5, Action onHandlerEndTask = null)
           : base(device, $"STAT", onHandlerEndTask: onHandlerEndTask)
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot STAT Timeout");
                return true;
            }

            if (result.Data == "?")
            {
                Device.NoteError("Robot STAT Response Error");
                return false;
            }

            //Wafer位置从机械手和Station之间转换
            if (Device.ParseStatusData(result.Data))
                _onHandlerEndTask?.Invoke();



            handled = true;
            return true;
        }
    }

    public class HwinRobotERRHandler : HwinRobotHandler
    {
        // STAT
        public HwinRobotERRHandler(HwinRobot device, int timeout = 5, Action onHandlerEndTask = null)
           : base(device, $"ERR 0", onHandlerEndTask:onHandlerEndTask)
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot ERR Timeout",false);
                return true;
            }

            if (result.Data == "?")
            {
                Device.NoteError("Robot ERR Response Error",false);
                return false;
            }

           if (Device.ParseErrData(result.Data))
                _onHandlerEndTask?.Invoke();

            handled = true;
            return true;
        }
    }


    public class HwinRobotGETAHandler : HwinRobotHandler
    {
        //GETA [module],[slot]
        public HwinRobotGETAHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"GETA {device.ModuleAssociateStationDic[module]} {slot+1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Picking,
                ArmTarget = RobotArm.ArmA, 
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot GETA Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                //Wafer位置从机械手和Station之间转换
                Device.PrasePutWaferData();
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot GETA Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotGETBHandler : HwinRobotHandler
    {
        //GETB [module],[slot]
        public HwinRobotGETBHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"GETB {device.ModuleAssociateStationDic[module]} {slot + 1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget =  module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot GETB Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                //Wafer位置从机械手和Station之间转换
                Device.PraseGetWaferData();
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot GETB Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotBGETBHandler : HwinRobotHandler
    {
        //GETB [module],[slot]
        string cModule = "";

        public HwinRobotBGETBHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"GETB {device.ModuleAssociateStationDic[module]} {slot + 1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            cModule = module;

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Picking,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot GETB Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.GetWaferData();
                Device.NotePickCompleted();

                Device.MoveInfo = new RobotMoveInfo()
                {
                    Action = RobotAction.Moving,
                    ArmTarget = RobotArm.ArmA,
                    BladeTarget = ModuleName.System.ToString(),
                };
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot GETB Response Error");
                return true;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotPUTAHandler : HwinRobotHandler
    {
        //PUTA [module],[slot]
        public HwinRobotPUTAHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"PUTA {device.ModuleAssociateStationDic[module]} {slot + 1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Placing,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot PUTA Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                //Wafer位置从机械手和Station之间转换
                //Device.PraseWaferData(result.Data);
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot PUTA Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotPUTBHandler : HwinRobotHandler
    {
        //PUTA [module],[slot]
        public HwinRobotPUTBHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"PUTB {device.ModuleAssociateStationDic[module]} {slot + 1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget =  module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot PUTB Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                //Wafer位置从机械手和Station之间转换
                //Device.PraseWaferData(result.Data);
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot PUTB Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotBPUTBHandler : HwinRobotHandler
    {
        //PUTA [module],[slot]
        string cModule = "";

        public HwinRobotBPUTBHandler(HwinRobot device, string module, int slot, int timeout = 60)
            : base(device, $"PUTB {device.ModuleAssociateStationDic[module]} {slot + 1}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            cModule = module;

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Placing,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot PUTB Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.PutWaferData();
                Device.NotePlaceCompleted();

                Device.MoveInfo = new RobotMoveInfo()
                {
                    Action = RobotAction.Moving,
                    ArmTarget = RobotArm.ArmA,
                    BladeTarget = ModuleName.System.ToString(),
                };

            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot PUTB Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotPUTSPHandler : HwinRobotHandler
    {
        //PUTSP [module],[slot],[step]
        //step:1:初始化位置  2:伸出  3:开真空并抬升  4:到位回缩
        string cModule = "";
        int cSlot = 0;
        int cStep = 0;
        public HwinRobotPUTSPHandler(HwinRobot device, string module, int slot,int step, int timeout = 60)
            : base(device, $"PUTSP {device.ModuleAssociateStationDic[module]} {slot + 1} {step}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            cModule = module;
            cSlot = slot;
            cStep = step;
            if (cStep == 2)
            {
                device.MoveInfo = new RobotMoveInfo()
                {
                    Action = RobotAction.Placing,
                    ArmTarget = RobotArm.ArmA,
                    BladeTarget = module,
                };
            }
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot PUTSP Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                if (cStep == 2)
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Placing,
                        ArmTarget = _blade == RobotArmEnum.Blade1 ? RobotArm.ArmA : RobotArm.ArmB,
                        BladeTarget = cModule,
                    };
                }
                else if (cStep == 3)
                {
                    Device.NoteActionCompleted();
                }
                else if (cStep == 4)
                {
                    Device.NotePlaceCompleted();
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Moving,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = ModuleName.System.ToString(),
                    };
                }
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot PUTSP Response Error");

                if (cStep == 2) // 伸出手臂
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Extending,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = cModule,
                    };
                }
                else if (cStep == 3) // 開啟真空並抬升取片
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Picking,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = cModule,
                    };
                }
                else //  1 = 縮回手臂至縮回位置並移動至取片的預備位置; 4 = 縮回手臂
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Moving,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = ModuleName.System.ToString(),
                    };
                }
                return false;
            }       
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotGETSPHandler : HwinRobotHandler
    {
        //PUTSP [module],[slot],[step]
        //step:1:初始化位置  2:伸出  3:关真空并抬升  4:到位回缩        
        string cModule = "";
        int cSlot = 0; 
        int cStep = 0;
        public HwinRobotGETSPHandler(HwinRobot device, string module, int slot, int step, int timeout = 60)
            : base(device, $"GETSP {device.ModuleAssociateStationDic[module]} {slot + 1} {step}")
        {
            cModule = module;
            cSlot = slot;
            cStep = step;
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            if (cStep == 2)
            {
                device.MoveInfo = new RobotMoveInfo()
                {
                    Action = RobotAction.Picking,
                    ArmTarget = RobotArm.ArmA,
                    BladeTarget = module,
                };
            }
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot GETSP Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                if (cStep == 2)
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Picking,
                        ArmTarget = _blade == RobotArmEnum.Blade1 ? RobotArm.ArmA : RobotArm.ArmB,
                        BladeTarget = cModule,
                    };
                    Device.NoteActionCompleted();
                }
                else if (cStep == 3)
                {
                    Device.NotePickCompleted();
                }
                else if (cStep == 4)
                {                    
                    Device.NoteActionCompleted(); 
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Moving,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = ModuleName.System.ToString(),
                    };
                }
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot GETSP Response Error");

                if (cStep == 2) // 伸出手臂
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Extending,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = cModule,
                    };
                }
                else if (cStep == 3) // 開啟真空並抬升取片
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Picking,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = cModule,
                    };
                }
                else //  1 = 縮回手臂至縮回位置並移動至取片的預備位置; 4 = 縮回手臂
                {
                    Device.MoveInfo = new RobotMoveInfo()
                    {
                        Action = RobotAction.Moving,
                        ArmTarget = RobotArm.ArmA,
                        BladeTarget = ModuleName.System.ToString(),
                    };
                }
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }



    public class HwinRobotINPUTHandler : HwinRobotHandler
    {
        // INPUT A 3
        public HwinRobotINPUTHandler(HwinRobot device, int timeout = 5,string com = "INPUT A 1")
            : base(device, com)
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot INPUT Timeout");
                return true;
            }
            if (result.Data.Length == 1)
            {
                Device.SetWaferData(result.Data);
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot INPUT Response Error");
                return false;
            }

            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };

            ResponseMessage = msg;
            //Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }

    public class HwinRobotBINPUTHandler : HwinRobotHandler
    {
        // INPUT A 3
        public HwinRobotBINPUTHandler(HwinRobotB device, int timeout = 5)
            : base(device, $"INPUT A 6")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot INPUT Timeout");
                return true;
            }
            if (result.Data.Length == 1)
            {
                Device.SetWaferData(result.Data);
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot INPUT Response Error");
                return false;
            }

            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };

            ResponseMessage = msg;
            Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }

    public class HwinRobotOutpOpenHandler : HwinRobotHandler
    {
        //开真空
        public HwinRobotOutpOpenHandler(HwinRobot device, int timeout = 5)
            : base(device, $"OUTP 1 1")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot OutP Timeout");
                return true;
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot OutP Response Error");
                return false;
            }

            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };

            ResponseMessage = msg;
            //Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }
    public class HwinRobotBOPTClosePHandler : HwinRobotHandler
    {
        //打开夹爪
        public HwinRobotBOPTClosePHandler(HwinRobot device, int timeout = 60)
            : base(device, $"OUTP 1 1")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot OUTP Timeout");
                return true;
            }
            if (result.Data == ">")
            {

            }
            else if (result.Data == "?")
            {
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }
    public class HwinRobotOutpCloseHandler : HwinRobotHandler
    {
        //关真空
        public HwinRobotOutpCloseHandler(HwinRobot device, int timeout = 5)
            : base(device, $"OUTP 1 0")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot OutP Timeout");
                return true;
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot OutP Response Error");
                return false;
            }

            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };

            ResponseMessage = msg;
            Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }
    public class HwinRobotBOPTPHandler : HwinRobotHandler
    {
        //打开夹爪
        public HwinRobotBOPTPHandler(HwinRobot device, int timeout = 60)
            : base(device, $"OUTP 1 0")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot OUTP Timeout");
                return true;
            }
            if (result.Data == ">")
            {

            }
            else if (result.Data == "?")
            {
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }
    public class HwinRobotMapHandler : HwinRobotHandler
    {
        //MAP [module]
        public HwinRobotMapHandler(HwinRobot device, string module, int timeout = 60)
            : base(device, $"MAP {device.ModuleAssociateStationDic[module].ToLower()}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Picking,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot MAP Timeout");
                return true;
            }
            if (result.Data == ">")
            {

            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot MAP Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotRSRHandler : HwinRobotHandler
    {
        //RSR
        public HwinRobotRSRHandler(HwinRobot device, int timeout = 60)
            : base(device, $"RSR")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot RSR Timeout");
                return true;
            }
            if (result.Data.Length == 25 )
            {
                Device.SetWaferData(result.Data);

                string Mappingesult = string.Join(",", result.Data.ToCharArray());
                GemManager.Instance.Equipment?.TriggerEvent("Mapping", new string[] { "CarrierID", "MappingResult"},
                    new object[] {Device.CurrentInteractModule.ToString(), Mappingesult });
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot RSR Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }

    public class HwinRobotBRSRHandler : HwinRobotHandler
    {
        //RSR
        public HwinRobotBRSRHandler(HwinRobot device, int timeout = 60)
            : base(device, $"RSR")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot RSR Timeout");
                return true;
            }
            if (result.Data.Length > 5)
            {
                Device.SetWaferData(result.Data);
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot RSR Response Error");
                return false;
            }

            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };

            ResponseMessage = msg;
            Device.NoteActionCompleted();
            handled = true;
            return true;
        }
    }

    public class HwinRobotRespHandler : HwinRobotHandler
    {
        //RESP 确认控制器是否成功建立与主控电脑的通信
        public HwinRobotRespHandler(HwinRobot device, int timeout = 60)
            : base(device, $"RESP")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot RESP Timeout");
                return true;
            }
            if (result.Data == ">")
            {

            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot RESP Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotRemsHandler : HwinRobotHandler
    {
        //RESP 确认控制器是否成功建立与主控电脑的通信
        public HwinRobotRemsHandler(HwinRobot device, int timeout = 60)
            : base(device, $"REMS")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot REMS Timeout");
                return true;
            }
            if (result.Data == ">")
            {
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot REMS Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotSVONHandler : HwinRobotHandler
    {
        //SVON 用以激磁位于机械手臂内之伺服马达
        public HwinRobotSVONHandler(HwinRobot device, int timeout = 60)
            : base(device, $"SVON")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot SVON Timeout");
                return true;
            }
            if (result.Data == ">")
            {
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot SVON Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }

    public class HwinRobotHomeHandler : HwinRobotHandler
    {
        //HOM
        public HwinRobotHomeHandler(HwinRobot device, int timeout = 60, Action onHandlerEndTask = null)
            : base(device, $"HOM", onHandlerEndTask:onHandlerEndTask)
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.Both,
                BladeTarget = "ArmA.System",
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot HOM Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.IsBusy = false;
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot HOME Response Error");
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            _onHandlerEndTask?.Invoke();
            Device.NoteActionCompleted();
            return true;
        }
    }

    public class HwinRobotMTCSPHandler : HwinRobotHandler
    {
        //前往位置
        public HwinRobotMTCSPHandler(HwinRobot device, string module, string strAction, int timeout = 60)
            : base(device, $"MTCS {device.ModuleAssociateStationDic[$"{module}.{strAction}"].ToUpper()}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = module,
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot MTCS Timeout");
                return true;
            }
            if (result.Data == ">")
            {

            }
            else if (result.Data == "?")
            {
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }
    public class HwinRobotMOVRGHandler : HwinRobotHandler
    {
        //抬升或下降 一定距离
        public HwinRobotMOVRGHandler(HwinRobot device, int rDistance, int timeout = 60)
            : base(device, $"MOVR Z {rDistance}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot MOVR Z Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.GetWaferData();
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot MOVR Z Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }
    public class HwinRobotMOVRPHandler : HwinRobotHandler
    {
        //抬升或下降 一定距离
        public HwinRobotMOVRPHandler(HwinRobotB device, int rDistance, int timeout = 60)
            : base(device, $"MOVR Z {rDistance}")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot MOVR Z Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.PutWaferData();
            }
            else if (result.Data == "?")
            {
                Device.NoteError("Robot MOVR Z Response Error");
                return false;
            }
            ResponseMessage = msg;
            handled = true;
            return true;
        }
    }
    public class HwinRobotRETHHandler : HwinRobotHandler
    {
        //缩回
        public HwinRobotRETHHandler(HwinRobotB device, int timeout = 60)
            : base(device, $"RETH")
        {
            AckTimeout = TimeSpan.FromSeconds(timeout);
            CompleteTimeout = TimeSpan.FromSeconds(timeout);

            device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = "ArmA.System",
            };
        }

        public override bool HandleMessage(MessageBase msg, out bool handled)
        {
            var result = msg as HwinRobotMessage;
            handled = false;
            if (!result.IsResponse)
            {
                Device.NoteError("Robot RETH Timeout");
                return true;
            }
            if (result.Data == ">")
            {
                Device.NotePickCompleted();
            }
            else if (result.Data == "?")
            {
                return false;
            }
            Device.MoveInfo = new RobotMoveInfo()
            {
                Action = RobotAction.Moving,
                ArmTarget = RobotArm.ArmA,
                BladeTarget = ModuleName.System.ToString(),
            };
            ResponseMessage = msg;
            handled = true;
            Device.NoteActionCompleted();
            return true;
        }
    }
}



