123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Sorter.Common;
- using TSC = Aitex.Sorter.Common;
- using Aitex.Core.RT.Device;
- using Aitex.Sorter.RT.Device.Robot;
- using Aitex.Core.RT.SCCore;
- using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM;
- using System.Net;
- using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM.EventArgs;
- namespace Aitex.Sorter.RT.Device.Reader
- {
- public class WIDReader : 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
- {
- lock (_locker)
- {
- return _handlers.Count > 0 || _foregroundHandler != null;
- }
- }
- }
- public bool Error
- {
- get
- {
- return _commErr;
- }
- }
- public bool ReadLaserMaker { get; set; }
- public Guid OCRGuid { get; set; }
- public string JobName { get; set; }
- public string LaserMaker { get; set; }
- public string LaserMark1 { get; set; }
- public string LaserMark1Score { get; set; }
- public string LaserMark1ReadTime { get; set; }
- public string T7Code { get; set; }
- public string LaserMark2 { get; set; }
- public string LaserMark2Score { get; set; }
- public string LaserMark2ReadTime { get; set; }
- public bool ReadOK { get; set; }
- public string ImageFileName { get; set; }
- public string ImageStorePath { get; set; }
- private static Object _locker = new Object();
- private AsyncSocket _socket;
- private TcpSocketClient _client;
- 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;
- private string AlarmWaferIDReadError = "WaferIDReadError";
- public WIDReader(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);
- string[] ipport = address.Split(':');
- IPAddress _ip = IPAddress.Parse(ipport[0]);
- //_client = new TcpSocketClient(_ip, Convert.ToInt16(ipport[1]));
- //_client.ServerDataReceived += _client_ServerDataReceived;
- Initalized = false;
- }
- private void _client_ServerDataReceived(object sender, TcpServerDataReceivedEventArgs e)
- {
- throw new NotImplementedException();
- }
- public bool Initialize()
- {
- _socket.Connect(this._addr);
- DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.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, "Read LaserMark");
- return true;
- }
- return false;
- });
- DATA.Subscribe(Name, ParamName.WRIDReaderState, () => State);
- DATA.Subscribe(Name, ParamName.WRIDReaderBusy, () => Busy);
- DATA.Subscribe(Name, ParamName.LaserMaker1, () => LaserMaker);
- DATA.Subscribe(Name, ParamName.LaserMaker2, () => T7Code);
- EV.Subscribe(new EventItem("Event", AlarmWaferIDReadError, "LaserMark reader error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
- _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);
- }
- _commErr = !_socket.IsConnected;
- }
- public void Reset()
- {
- lock (_locker)
- {
- _foregroundHandler = null;
- _handlers.Clear();
- }
- if (_commErr)
- {
- //_commErr = false;
- _socket.Connect(this._addr);
- }
- }
- #region Command
- public bool ReadImage(string wid,out string reason)
- {
- reason = "";
- return true;
- }
- public bool Read(bool bLaserMaker, string jobName, out string reason)
- {
- _handlers.Clear();
- ReadOK = false;
- if (string.IsNullOrEmpty(jobName))
- {
- reason = "Job is undefine.";
- return false;
- }
- jobName = ConvetJobName(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);
- }
-
- /// <summary>
- /// 传入Jobs的Read动作.
- /// </summary>
- /// <param name="bLaserMaker">LM1 = True / LM2 = False</param>
- /// <param name="jobs">Jobs文件</param>
- /// <param name="reason">返回错误</param>
- /// <returns></returns>
- public bool Read(bool bLaserMaker, string[] jobs, 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(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 ReadOnHostMode(bool bLaserMaker, string[] jobnames, out string reason)
- {
- _handlers.Clear();
- string[] jobs = jobnames;
- if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
- {
- reason = "Job is undefine";
- return false;
- }
- string jobName = ConvetJobName(jobs[0]);
- _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 = Name== "WIDReader1" ? SC.GetConfigItem("OcrReader.ReaderJob1Main").StringValue : SC.GetConfigItem("OcrReader.ReaderJob1Slave").StringValue;
- }
- else
- {
- jobs = Name == "WIDReader1" ? SC.GetConfigItem("OcrReader.ReaderJob2Main").StringValue : SC.GetConfigItem("OcrReader.ReaderJob2Slave").StringValue;
- }
- 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)
- {
- _foregroundHandler = _handlers.Dequeue();
- if (!_foregroundHandler.Execute(ref _socket))
- {
- reason = " communication failed, please recovery it.";
- //LOG.Error(reason);
- EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】" + reason);
- _foregroundHandler = null;
- return false;
- }
-
- }
- }
- 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);
- OnError();
- }
- catch (InvalidPackageException ex)
- {
- EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
- OnError();
- }
- catch (System.Exception ex)
- {
- EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】has exception:" + ex.ToString());
- OnError();
- }
- }
- private void OnErrorHandler(ErrorEventArgs args)
- {
- _commErr = true;
- //Initalized = false;
- EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
- OnError();
- }
- public void OnError()
- {
- EV.Notify(AlarmWaferIDReadError);
- }
- private string ConvetJobName(string name)
- {
- name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
- name = name.IndexOf(".") >=0?name.Substring(0, name.LastIndexOf(".")):name; //remove expand
- return name;
- }
- }
- }
|