using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Sorter.Common; using MECF.Framework.Common.Communications; namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDKB { public class TDKBLoadPortHandler : HandlerBase { public TDKBLoadPort Device { get; set; } public string Command; protected TDKBLoadPortHandler(TDKBLoadPort device, string command, string para) : base(BuildMesage(command, para)) { Device = device; Command = command; Name = command; } public static string BuildMesage(string command, string para) { if(string.IsNullOrEmpty(para)) return $"s00{command};\r"; return $"s00{command}{para};\r"; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { TDKBLoadPortMessage response = msg as TDKBLoadPortMessage; ResponseMessage = msg; transactionComplete = false; if (response.IsAck) { SetState(EnumHandlerState.Acked); transactionComplete = true; } if (response.IsError) { SetState(EnumHandlerState.Completed); transactionComplete = true; } if (response.IsNak) { SetState(EnumHandlerState.Completed); transactionComplete = true; } if (response.IsEvent) { SendAck(); if (response.Command.Contains(Command)) { SetState(EnumHandlerState.Completed); transactionComplete = true; } } return true; } public void HandleEvent(string content) { } public void SendAck() { //Device.Connection.SendMessage(new byte[] { 0x06 }); } public void Retry() { } public virtual bool PaseData(byte[] data) { return true; } } public class TDKBSetHandler : TDKBLoadPortHandler { private string setcommand; public TDKBSetHandler(TDKBLoadPort device, string command, string para) : base(device, BuildData(command), para) { setcommand = command; } private static string BuildData(string command) { return "SET:" + command; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { TDKBLoadPortMessage response = msg as TDKBLoadPortMessage; ResponseMessage = msg; //Device.TaExecuteSuccss = true; transactionComplete = false; if (response.IsAck) { SetState(EnumHandlerState.Acked); transactionComplete = false; if (setcommand.Contains("FSB")) transactionComplete = true; } if (response.IsBusy) { SetState(EnumHandlerState.Completed); transactionComplete = true; } if (response.IsNak) { SetState(EnumHandlerState.Completed); transactionComplete = true; //ParseError(response.Data); } if (response.IsError) { SetState(EnumHandlerState.Completed); transactionComplete = true; } if (response.IsEvent) { SendAck(); if (response.Command.Contains(setcommand)) { SetState(EnumHandlerState.Completed); transactionComplete = true; } else HandleEvent(response.Command); transactionComplete = true; } //if (setcommand == "RESET") //{ // transactionComplete = true; //} return true; } } public class TDKBGetHandler : TDKBLoadPortHandler { public TDKBGetHandler(TDKBLoadPort device, string command, string para) : base(device, BuildData(command), para) { } private static string BuildData(string command) { return "GET:" + command; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { TDKBLoadPortMessage response = msg as TDKBLoadPortMessage; ResponseMessage = msg; //Device.TaExecuteSuccss = true; transactionComplete = false; if (response.IsAck) { SetState(EnumHandlerState.Acked); transactionComplete = true; ParseData(response.Command); } if (response.IsNak) { SetState(EnumHandlerState.Completed); transactionComplete = true; //ParseError(response.Data); } return true; } public void ParseData(string cmddata) { try { if (cmddata.Contains("STATE")) { Device.ParseState(cmddata); } if (cmddata.Contains("VERSN/")) { } if (cmddata.Contains("LEDST/")) { char[] ledstate = cmddata.Replace("LEDST/", "").ToArray(); if (ledstate.Length == 9) { IndicatorState[] lpledstate = new IndicatorState[] { Device.IndicatiorLoad, Device.IndicatiorUnload, Device.IndicatiorAccessManual, Device.IndicatiorPresence, Device.IndicatiorPlacement, Device.IndicatiorAccessAuto, Device.IndicatiorStatus1, Device.IndicatorAlarm, }; for (int i = 0; i < (lpledstate.Length > ledstate.Length ? ledstate.Length : lpledstate.Length); i++) { if (ledstate[i] == '0') lpledstate[i] = IndicatorState.OFF; if (ledstate[i] == '1') lpledstate[i] = IndicatorState.ON; if (ledstate[i] == '2') lpledstate[i] = IndicatorState.BLINK; } } else { IndicatorState[] lpledstate = new IndicatorState[] { Device.IndicatiorPresence, Device.IndicatiorPlacement, Device.IndicatiorLoad, Device.IndicatiorUnload, Device.IndicatiorOpAccess, Device.IndicatiorStatus1, Device.IndicatiorStatus2, }; for (int i = 0; i < (lpledstate.Length > ledstate.Length ? ledstate.Length : lpledstate.Length); i++) { if (ledstate[i] == '0') lpledstate[i] = IndicatorState.OFF; if (ledstate[i] == '1') lpledstate[i] = IndicatorState.ON; if (ledstate[i] == '2') lpledstate[i] = IndicatorState.BLINK; } } } if (cmddata.Contains("MAPRD/")) { string str1 = cmddata.Replace("MAPRD/", ""); Device.RecordCurrentSlotMap(str1); } if (cmddata.Contains("WFCNT/")) { Device.WaferCount = Convert.ToInt16(cmddata.Replace("WFCNT/", "")); } if (cmddata.Contains("MDAH/")) { } if (cmddata.Contains("LPIOI/")) { } if (cmddata.Contains("FSBxx/")) { if (cmddata.Contains("ON")) Device.IsFosbModeActual = true; if (cmddata.Contains("OF")) Device.IsFosbModeActual = false; } } catch(Exception ex) { LOG.Write(ex); } } private void SetIEDValue(int index, IndicatorState state) { switch (index) { case 0: Device.IndicatiorPresence = state; break; case 1: Device.IndicatiorPlacement = state; break; case 2: Device.IndicatiorLoad = state; break; case 3: Device.IndicatiorUnload = state; break; case 4: Device.IndicatiorOpAccess = state; break; case 5: Device.IndicatiorStatus1 = state; break; case 6: Device.IndicatiorStatus2 = state; break; default: break; } } } public class TDKBModHandler : TDKBLoadPortHandler { public TDKBModHandler(TDKBLoadPort device, string command, string para) : base(device, BuildData(command), para) { } private static string BuildData(string command) { return "MOD:" + command; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { TDKBLoadPortMessage response = msg as TDKBLoadPortMessage; ResponseMessage = msg; //Device.TaExecuteSuccss = true; transactionComplete = false; if (response.IsAck) { SetState(EnumHandlerState.Acked); transactionComplete = true; } if (response.IsNak) { SetState(EnumHandlerState.Completed); transactionComplete = true; //ParseError(response.Data); } return true; } public void ParseData(byte[] cmd) { } } public class TDKBMoveHandler : TDKBLoadPortHandler { private string moveCommand; public TDKBMoveHandler(TDKBLoadPort device, string command, string para) : base(device, BuildData(command), para) { moveCommand = command; } private static string BuildData(string command) { return "MOV:" + command; } public override bool HandleMessage(MessageBase msg, out bool transactionComplete) { TDKBLoadPortMessage response = msg as TDKBLoadPortMessage; ResponseMessage = msg; //Device.TaExecuteSuccss = true; transactionComplete = false; if (response.IsAck) { SetState(EnumHandlerState.Acked); transactionComplete = false; } if (response.IsNak) { Device.OnCommandFailed(moveCommand); SetState(EnumHandlerState.Completed); transactionComplete = true; //ParseError(response.Data); } if (response.IsError) { Device.OnCommandFailed(moveCommand); SetState(EnumHandlerState.Completed); transactionComplete = true; } if (response.IsEvent) { if (response.Command.Contains("ORGSH") || response.Command.Contains("ABORG")) { Device.OnHomed(); } if (response.Command.Contains(moveCommand)) { SetState(EnumHandlerState.Completed); transactionComplete = true; Device.OnCommandSuccess(moveCommand); } } return true; } public void ParseData(byte[] cmd) { } } }