123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.SCCore;
- using Aitex.Sorter.Common;
- using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
- {
- public class OcrReader : BaseDevice, IDevice
- {
- public DeviceState State
- {
- get
- {
- if (!Initalized)
- {
- return DeviceState.Unknown;
- }
- if (Error)
- {
- return DeviceState.Error;
- }
- if (Busy)
- return DeviceState.Busy;
- return DeviceState.Idle;
- }
- }
- public bool Initalized { get; set; }
- public bool Busy
- {
- get { return _handlers.Count > 0 || _foregroundHandler != null; }
- }
- public bool Error
- {
- get
- {
- return _commErr;
- }
- }
- public bool ReadLaserMaker { get; set; }
- public string JobName { get; set; }
- public string LaserMaker { get; set; }
- public string T7Code { get; set; }
- public bool ReadOK { get; set; }
- private static Object _locker = new Object();
- private AsyncSocket _socket;
- private IHandler _foregroundHandler = null; //current handler
- private Queue<IHandler> _handlers = new Queue<IHandler>();
- public const string delimiter = "\r\n";
- private bool _commErr = false;
- private string _addr;
- private bool _onlineInited;
- public OcrReader(string module, string name, string display, string deviceId, string address)
- : base(module, name, display, deviceId)
- {
- _addr = address;
- _socket = new AsyncSocket(address, delimiter);
- _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
- _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
- Initalized = false;
- }
- public bool Initialize()
- {
- _socket.Connect(this._addr);
- DEVICE.Register(String.Format("{0}.{1}", Name, "ReadWaferID"), (out string reason, int time, object[] param) =>
- {
- bool bLaser = bool.Parse((string)param[0]);
- string jobName = (string)param[1];
- bool ret = Read(bLaser, jobName, out reason);
- if (ret)
- {
- reason = string.Format("{0}", Name, "读WaferID");
- return true;
- }
- return false;
- });
- DATA.Subscribe(Name, "WRIDReaderState", () => State);
- DATA.Subscribe(Name, "WRIDReaderBusy", () => Busy);
- DATA.Subscribe(Name, "LaserMaker1", () => LaserMaker);
- DATA.Subscribe(Name, "LaserMaker2", () => T7Code);
- _handlers.Clear();
- Initalized = true;
- return true;
- }
- public void Terminate()
- {
- }
- public void Monitor()
- {
- if (!_onlineInited && _socket.IsConnected)
- {
- _onlineInited = true;
- string error = string.Empty;
- Online(true, out error);
- }
- }
- public void Reset()
- {
- lock (_locker)
- {
- _foregroundHandler = null;
- _handlers.Clear();
- }
- if (_commErr)
- {
- _commErr = false;
- _socket.Connect(this._addr);
- }
- }
- #region Command
- public bool Read(bool bLaserMaker, string jobName, out string reason)
- {
- _handlers.Clear();
- if (string.IsNullOrEmpty(jobName))
- {
- reason = "Job is undefine.";
- return false;
- }
- jobName = ConvetJobName(jobName);
- if (JobName != jobName)
- {
- _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
- _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
- _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
- }
- ReadOK = false;
- ReadLaserMaker = bLaserMaker;
- _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
- return execute(out reason);
- }
- public bool Read(bool bLaserMaker, out string reason)
- {
- _handlers.Clear();
- string[] jobs = GetJobName(bLaserMaker);
- if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
- {
- reason = "Job is undefine";
- return false;
- }
- string jobName = ConvetJobName(jobs[0]);
- if (JobName != jobName)
- {
- _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
- _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
- _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
- }
- _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
- if (jobs.Length > 1)
- {
- for (int i = 1; i < jobs.Length; i++)
- {
- jobName = ConvetJobName(jobs[i]);
- _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
- _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
- _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
- _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
- }
- }
- ReadOK = false;
- ReadLaserMaker = bLaserMaker;
- return execute(out reason);
- }
- public bool Read(out string reason)
- {
- reason = string.Empty;
- return execute(new handler(new ReadHandler(this)), out reason);
- }
- public bool ReadJobName(out string reason)
- {
- reason = string.Empty;
- return execute(new handler(new GetJobHandler(this)), out reason);
- }
- public bool Online(bool online, out string reason)
- {
- reason = string.Empty;
- return execute(new handler(new OnlineHandler(this), online), out reason);
- }
- public bool LoadJob(string jobName, out string reason)
- {
- reason = string.Empty;
- return execute(new handler(new LoadJobHandler(this), jobName), out reason);
- }
- #endregion
- private string[] GetJobName(bool bLaserMark1)
- {
- string jobs = string.Empty;
- if (bLaserMark1)
- {
- jobs = SC.GetStringValue("OcrReader.ReaderJob1Main");
- }
- else
- {
- jobs = SC.GetStringValue("OcrReader.ReaderJob2Main");
- }
- return jobs.Split(';');
- }
- private bool execute(IHandler handler, out string reason)
- {
- reason = string.Empty;
- lock (_locker)
- {
- if (!handler.Execute(ref _socket))
- {
- reason = "Communication failed, please recovery it.";
- return false;
- }
- _foregroundHandler = handler;
- }
- return true;
- }
- private bool execute(out string reason)
- {
- reason = string.Empty;
- lock (_locker)
- {
- if (_handlers.Count > 0)
- {
- IHandler handler = _handlers.Dequeue();
- if (!handler.Execute(ref _socket))
- {
- reason = " communication failed, please recovery it.";
- //LOG.Error(reason);
- EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】" + reason);
- return false;
- }
- _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))
- {
- if (completed)
- {
- _foregroundHandler = null;
- if (ReadOK)
- {
- _handlers.Clear();
- }
- else
- {
- string reason = string.Empty;
- execute(out reason);
- }
- }
- }
- }
- }
- }
- }
- catch (ExcuteFailedException ex)
- {
- EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
- }
- catch (InvalidPackageException ex)
- {
- EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
- }
- catch (System.Exception ex)
- {
- EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】has exception:" + ex.ToString());
- }
- }
- public void OnLaskerMarkerRead()
- {
- }
- private void OnErrorHandler(ErrorEventArgs args)
- {
- _commErr = true;
- Initalized = false;
- EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
- //EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} Communication failed,please check it.{1}", Name, args.Reason));
- }
- private string ConvetJobName(string name)
- {
- name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
- name = name.Substring(0, name.LastIndexOf(".")); //remove expand
- return name;
- }
- }
- }
|