| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 | 
							- using System;
 
- using System.Text.RegularExpressions;
 
- using Aitex.Core.RT.Log;
 
- using Aitex.Core.RT.Event;
 
- using Aitex.Core.RT.DataCenter;
 
- using Aitex.Core.Util;
 
- using Aitex.Sorter.Common;
 
- using TSC = Aitex.Sorter.Common;
 
- using Aitex.Core.RT.Device;
 
- using MECF.Framework.Common.Communications;
 
- using MECF.Framework.Common.SubstrateTrackings;
 
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.NX100.AL;
 
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.SR100.AL;
 
- using MECF.Framework.Common.Equipment;
 
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot
 
- {
 
-   
 
-     public class Aligner : BaseDevice, IDevice,IConnection
 
-     {
 
-         public string Address
 
-         {
 
-             get { return _addr; }
 
-         }
 
-         public bool IsConnected
 
-         {
 
-             get { return _socket.IsConnected; }
 
-         }
 
-         public bool Disconnect()
 
-         {
 
-             return true;
 
-         }
 
-         public bool Connect()
 
-         {
 
-             return true;
 
-         }
 
-         public const string delimiter = "\r";
 
-         public int LastErrorCode { get; set; }
 
-         public int Status { get; set; }
 
-         public int ErrorCode { get; set; }
 
-         public int ElapseTime { get; set; }
 
-         public int Notch { get;  set; }
 
-         public bool Initalized { get; set; }
 
-         public bool Communication
 
-         {
 
-             get
 
-             {
 
-                 return !_commErr;
 
-             }
 
-         }
 
-         public bool Busy
 
-         {
 
-             get { return _backgroundHandler != null || _foregroundHandler != null; }
 
-         }
 
-         public bool Moving
 
-         {
 
-             get { return _backgroundHandler != null; }
 
-         }
 
-         
 
-         public bool Error
 
-         {
 
-             get
 
-             {
 
-                 return ErrorCode > 0 || _commErr;
 
-             }
 
-         }
 
-         //public string ErrorMessage
 
-         //{
 
-         //    get
 
-         //    {
 
-         //        return _factory.GetError(LastErrorCode);
 
-         //    }
 
-         //}
 
-         public DeviceState State
 
-         {
 
-             get
 
-             {
 
-                 if (!Initalized)
 
-                 {
 
-                     return DeviceState.Unknown;
 
-                 }
 
-                 if (Error)
 
-                 {
 
-                     return DeviceState.Error;
 
-                 }
 
-                 if (Busy)
 
-                     return DeviceState.Busy;
 
-                 return DeviceState.Idle;
 
-             }
 
-         }
 
-         public bool WaferOnAligner
 
-         {   
 
-             get
 
-             {
 
-                 return (Status & ((int)StateBit.WaferOnGrip | (int)StateBit.WaferOnCCD)) > 0; 
 
-             } 
 
-         }
 
-         
 
-         private static Object _locker = new Object();
 
-         private AsyncSocket _socket;
 
-         private IHandler _eventHandler = null;
 
-         private IHandler _backgroundHandler = null;  //moving
 
-         private IHandler _foregroundHandler = null;  //current handler
 
-         private IAlignerHandlerFactory _factory = null;
 
-         private bool _commErr = false;
 
-         //private bool _exceuteErr = false;
 
-         private string _addr;
 
-         private DeviceTimer _timerQuery = new DeviceTimer();
 
-         //private int _queryPeriod = 5000;    //ms
 
-         //private bool _queryState = true;
 
-         private RobotType _type = RobotType.SR100;
 
-         private string AlarmMechanicalAlignmentError = "MechanicalAlignmentError";
 
-         public Aligner(string module, string name, string display, string deviceId, string address, RobotType type = RobotType.SR100)
 
-             : base(module, name, display, deviceId)
 
-         {
 
-             _addr = address;
 
-             _socket = new AsyncSocket(address);
 
-             _type = type;
 
-             _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged); 
 
-             _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
 
-             Initalized = false;
 
-             _commErr = false;
 
-             WaferManager.Instance.SubscribeLocation(ModuleHelper.Converter(name), 1);
 
-         }
 
-         public bool Initialize()
 
-         {
 
-             switch (_type)
 
-             {
 
-                 case RobotType.NX100:
 
-                     _factory = new NX100AlignerHandlerFactory(this);
 
-                     break;
 
-                 default:
 
-                     _factory = new SR100AlignerHandlerFactory(this);
 
-                     break;
 
-             }
 
-             _eventHandler = _factory.Event();
 
-             ConnectionManager.Instance.Subscribe(Name, this);
 
-             _socket.Connect(this._addr);
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Init"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Init(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Init");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Home"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Home(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Home");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "AlignerHome"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Home(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Home");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Reset"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Clear(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Reset Error");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Grip"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Grip(0,out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Hold Wafer");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Release"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Release(0, out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Release Wafer");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "LiftUp"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = LiftUp(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Lifter Up");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "LiftDown"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = LiftDown(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Lifter Down");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Stop"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 bool ret = Stop(out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "Stop Align");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DEVICE.Register(String.Format("{0}.{1}", Name, "Align"), (out string reason, int time, object[] param) =>
 
-             {
 
-                 double angle = double.Parse((string)param[0]);
 
-                 bool ret = Align(angle, out reason);
 
-                 if (ret)
 
-                 {
 
-                     reason = string.Format("{0} {1}", Name, "PreAlign");
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             });
 
-             DATA.Subscribe($"{Name}.State", () => State);
 
-             DATA.Subscribe($"{Name}.AlignerState", () => State.ToString());
 
-             DATA.Subscribe($"{Name}.Busy", () => Busy);
 
-             DATA.Subscribe($"{Name}.ErrorCode", () => ErrorCode);
 
-             DATA.Subscribe($"{Name}.Error", () => Error);
 
-             DATA.Subscribe($"{Name}.ElapseTime", () => ElapseTime);
 
-             DATA.Subscribe($"{Name}.Notch", () => Notch);
 
-             DATA.Subscribe($"{Name}.WaferOnAligner", () => WaferOnAligner);
 
- //            string str = string.Empty;
 
-             EV.Subscribe(new EventItem("Event", AlarmMechanicalAlignmentError, "Aligner error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
 
- //            _timerQuery.Start(_queryPeriod);
 
-             return true;
 
-         }
 
-         public void Terminate()
 
-         {
 
-             _socket.Dispose();
 
-         }
 
-         public void Monitor()
 
-         {         
 
-         }
 
-         public void Reset()
 
-         {
 
-             lock (_locker)
 
-             {
 
-                 _foregroundHandler = null;
 
-                 _backgroundHandler = null;
 
-             }
 
-             
 
-             //_exceuteErr = false;
 
-             if (_commErr)
 
-             {
 
-                 _commErr = false;
 
-                 _socket.Connect(this._addr);
 
-             }
 
-         }
 
-         #region Command
 
-         public bool Init(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Init(), out reason);
 
-         }
 
-         public bool Home(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Home(), out reason);
 
-         }
 
-         public bool Clear(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Clear(), out reason);
 
-         }
 
-         public bool Grip(Hand hand, out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Grip(hand), out reason);
 
-         }
 
-         public bool Release(Hand hand, out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Release(hand), out reason);
 
-         }
 
-         public bool LiftUp(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.LiftUp(), out reason);
 
-         }
 
-         public bool LiftDown(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.LiftDown(), out reason);
 
-         }
 
-         public bool MoveToReady(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.MoveToReadyPostion(), out reason);
 
-         }
 
-         public bool Stop(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             lock (_locker)
 
-             {
 
-                 _foregroundHandler = null;
 
-                 _backgroundHandler = null;
 
-             }
 
-             return execute(_factory.Stop(), out reason);
 
-         }
 
-         public bool Align(double angle, out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.Align(angle), out reason);
 
-         }
 
-         
 
-         public bool QueryState(out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             return execute(_factory.QueryState(), out reason);
 
-         }
 
-         #endregion
 
-         private bool execute(IHandler handler, out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             lock (_locker)
 
-             {
 
-                 if (_foregroundHandler != null)
 
-                 {
 
-                     reason = "System busy, please wait or reset system.";
 
-                     EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason));
 
-                     return false;
 
-                 }
 
-                 if (_backgroundHandler != null && handler.IsBackground)
 
-                 {
 
-                     reason = "System busy,one background command is running, please wait or reset system.";
 
-                     EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} {1} {2}", this.DeviceID, handler.ToString(), reason));
 
-                     return false;
 
-                 }
 
-                 handler.Unit = (int)Unit.Aligner;
 
-                 if(!handler.Execute(ref _socket))
 
-                 {
 
-                     reason = "Communication error,please check it.";
 
-                     return false;
 
-                 }
 
-                 if (handler.IsBackground)
 
-                     _backgroundHandler = handler;
 
-                 else
 
-                     _foregroundHandler = handler;
 
-             }
 
-             return true;
 
-         }
 
-         
 
-         private void OnDataChanged(string package)
 
-         {
 
-             try
 
-             {
 
-                 package = package.ToUpper();
 
-                 string[] msgs = Regex.Split(package, delimiter);
 
-                 foreach (string msg in msgs)
 
-                 {
 
-                     if (msg.Length > 0)
 
-                     {
 
-                         bool completed = false;
 
-                         string resp = msg;
 
-                         lock (_locker)
 
-                         {
 
-                             if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
 
-                             {
 
-                                  _foregroundHandler = null;
 
-                             }
 
-                             else if (_backgroundHandler != null && _backgroundHandler.OnMessage(ref _socket, resp, out completed))
 
-                             {
 
-                                 if (completed)
 
-                                 {
 
-                                     string reason = string.Empty;
 
-                                     QueryState(out reason);
 
-                                     _backgroundHandler = null;
 
-                                 }
 
-                             }
 
-                             else
 
-                             {
 
-                                 if (_eventHandler != null)
 
-                                 {
 
-                                     if (_eventHandler.OnMessage(ref _socket, resp, out completed))
 
-                                     {
 
-                                         if (completed)
 
-                                         {
 
-                                             EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format(" has error. {0:X}", ErrorCode));
 
-                                             //_exceuteErr = true;
 
-                                         }
 
-                                     }
 
-                                 }
 
-                             }
 
-                         }
 
-                     }
 
-                 }
 
-             }
 
-             catch (ExcuteFailedException e)
 
-             {
 
-                 EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format("executed failed. {0}", e.Message));
 
-                 OnError();
 
-             }
 
-             catch (InvalidPackageException e)
 
-             {
 
-                 EV.PostMessage("Aligner", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
 
-                 OnError();
 
-             }
 
-             catch (System.Exception ex)
 
-             {
 
-                 _commErr = true;
 
-                 LOG.WriteExeption("Aligner failed:" , ex);
 
-             }
 
-         }
 
-         private void OnErrorHandler(ErrorEventArgs args)
 
-         {
 
-             Initalized = false;
 
-             _commErr = true;
 
-             EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
 
-             OnError();
 
-             //LOG.Error(string.Format("{0} Communication failed, {1}", Name, args.Reason));
 
-         }
 
-         //private bool checkslot(int min, int max, int slot)
 
-         //{
 
-         //    return slot >= min && slot < max;
 
-         //}
 
-         private void OnError()
 
-         {
 
-             EV.Notify(AlarmMechanicalAlignmentError);
 
-         }
 
-     }
 
- }
 
-   
 
 
  |