OcrReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using Aitex.Core.RT.DataCenter;
  5. using Aitex.Core.RT.Device;
  6. using Aitex.Core.RT.Event;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Sorter.Common;
  9. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  10. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
  11. {
  12. public class OcrReader : BaseDevice, IDevice
  13. {
  14. public DeviceState State
  15. {
  16. get
  17. {
  18. if (!Initalized)
  19. {
  20. return DeviceState.Unknown;
  21. }
  22. if (Error)
  23. {
  24. return DeviceState.Error;
  25. }
  26. if (Busy)
  27. return DeviceState.Busy;
  28. return DeviceState.Idle;
  29. }
  30. }
  31. public bool Initalized { get; set; }
  32. public bool Busy
  33. {
  34. get { return _handlers.Count > 0 || _foregroundHandler != null; }
  35. }
  36. public bool Error
  37. {
  38. get
  39. {
  40. return _commErr;
  41. }
  42. }
  43. public bool ReadLaserMaker { get; set; }
  44. public string JobName { get; set; }
  45. public string LaserMaker { get; set; }
  46. public string T7Code { get; set; }
  47. public bool ReadOK { get; set; }
  48. private static Object _locker = new Object();
  49. private AsyncSocket _socket;
  50. private IHandler _foregroundHandler = null; //current handler
  51. private Queue<IHandler> _handlers = new Queue<IHandler>();
  52. public const string delimiter = "\r\n";
  53. private bool _commErr = false;
  54. private string _addr;
  55. private bool _onlineInited;
  56. public OcrReader(string module, string name, string display, string deviceId, string address)
  57. : base(module, name, display, deviceId)
  58. {
  59. _addr = address;
  60. _socket = new AsyncSocket(address, delimiter);
  61. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  62. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  63. Initalized = false;
  64. }
  65. public bool Initialize()
  66. {
  67. _socket.Connect(this._addr);
  68. DEVICE.Register(String.Format("{0}.{1}", Name, "ReadWaferID"), (out string reason, int time, object[] param) =>
  69. {
  70. bool bLaser = bool.Parse((string)param[0]);
  71. string jobName = (string)param[1];
  72. bool ret = Read(bLaser, jobName, out reason);
  73. if (ret)
  74. {
  75. reason = string.Format("{0}", Name, "读WaferID");
  76. return true;
  77. }
  78. return false;
  79. });
  80. DATA.Subscribe(Name, "WRIDReaderState", () => State);
  81. DATA.Subscribe(Name, "WRIDReaderBusy", () => Busy);
  82. DATA.Subscribe(Name, "LaserMaker1", () => LaserMaker);
  83. DATA.Subscribe(Name, "LaserMaker2", () => T7Code);
  84. _handlers.Clear();
  85. Initalized = true;
  86. return true;
  87. }
  88. public void Terminate()
  89. {
  90. }
  91. public void Monitor()
  92. {
  93. if (!_onlineInited && _socket.IsConnected)
  94. {
  95. _onlineInited = true;
  96. string error = string.Empty;
  97. Online(true, out error);
  98. }
  99. }
  100. public void Reset()
  101. {
  102. lock (_locker)
  103. {
  104. _foregroundHandler = null;
  105. _handlers.Clear();
  106. }
  107. if (_commErr)
  108. {
  109. _commErr = false;
  110. _socket.Connect(this._addr);
  111. }
  112. }
  113. #region Command
  114. public bool Read(bool bLaserMaker, string jobName, out string reason)
  115. {
  116. _handlers.Clear();
  117. if (string.IsNullOrEmpty(jobName))
  118. {
  119. reason = "Job is undefine.";
  120. return false;
  121. }
  122. jobName = ConvetJobName(jobName);
  123. if (JobName != jobName)
  124. {
  125. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  126. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  127. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  128. }
  129. ReadOK = false;
  130. ReadLaserMaker = bLaserMaker;
  131. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  132. return execute(out reason);
  133. }
  134. public bool Read(bool bLaserMaker, out string reason)
  135. {
  136. _handlers.Clear();
  137. string[] jobs = GetJobName(bLaserMaker);
  138. if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
  139. {
  140. reason = "Job is undefine";
  141. return false;
  142. }
  143. string jobName = ConvetJobName(jobs[0]);
  144. if (JobName != jobName)
  145. {
  146. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  147. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  148. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  149. }
  150. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  151. if (jobs.Length > 1)
  152. {
  153. for (int i = 1; i < jobs.Length; i++)
  154. {
  155. jobName = ConvetJobName(jobs[i]);
  156. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  157. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  158. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  159. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  160. }
  161. }
  162. ReadOK = false;
  163. ReadLaserMaker = bLaserMaker;
  164. return execute(out reason);
  165. }
  166. public bool Read(out string reason)
  167. {
  168. reason = string.Empty;
  169. return execute(new handler(new ReadHandler(this)), out reason);
  170. }
  171. public bool ReadJobName(out string reason)
  172. {
  173. reason = string.Empty;
  174. return execute(new handler(new GetJobHandler(this)), out reason);
  175. }
  176. public bool Online(bool online, out string reason)
  177. {
  178. reason = string.Empty;
  179. return execute(new handler(new OnlineHandler(this), online), out reason);
  180. }
  181. public bool LoadJob(string jobName, out string reason)
  182. {
  183. reason = string.Empty;
  184. return execute(new handler(new LoadJobHandler(this), jobName), out reason);
  185. }
  186. #endregion
  187. private string[] GetJobName(bool bLaserMark1)
  188. {
  189. string jobs = string.Empty;
  190. if (bLaserMark1)
  191. {
  192. jobs = SC.GetStringValue("OcrReader.ReaderJob1Main");
  193. }
  194. else
  195. {
  196. jobs = SC.GetStringValue("OcrReader.ReaderJob2Main");
  197. }
  198. return jobs.Split(';');
  199. }
  200. private bool execute(IHandler handler, out string reason)
  201. {
  202. reason = string.Empty;
  203. lock (_locker)
  204. {
  205. if (!handler.Execute(ref _socket))
  206. {
  207. reason = "Communication failed, please recovery it.";
  208. return false;
  209. }
  210. _foregroundHandler = handler;
  211. }
  212. return true;
  213. }
  214. private bool execute(out string reason)
  215. {
  216. reason = string.Empty;
  217. lock (_locker)
  218. {
  219. if (_handlers.Count > 0)
  220. {
  221. IHandler handler = _handlers.Dequeue();
  222. if (!handler.Execute(ref _socket))
  223. {
  224. reason = " communication failed, please recovery it.";
  225. //LOG.Error(reason);
  226. EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】" + reason);
  227. return false;
  228. }
  229. _foregroundHandler = handler;
  230. }
  231. }
  232. return true;
  233. }
  234. private void OnDataChanged(string package)
  235. {
  236. try
  237. {
  238. package = package.ToUpper();
  239. string[] msgs = Regex.Split(package, delimiter);
  240. foreach (string msg in msgs)
  241. {
  242. if (msg.Length > 0)
  243. {
  244. bool completed = false;
  245. string resp = msg;
  246. lock (_locker)
  247. {
  248. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  249. {
  250. if (completed)
  251. {
  252. _foregroundHandler = null;
  253. if (ReadOK)
  254. {
  255. _handlers.Clear();
  256. }
  257. else
  258. {
  259. string reason = string.Empty;
  260. execute(out reason);
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. }
  268. catch (ExcuteFailedException ex)
  269. {
  270. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  271. }
  272. catch (InvalidPackageException ex)
  273. {
  274. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  275. }
  276. catch (System.Exception ex)
  277. {
  278. EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】has exception:" + ex.ToString());
  279. }
  280. }
  281. public void OnLaskerMarkerRead()
  282. {
  283. }
  284. private void OnErrorHandler(ErrorEventArgs args)
  285. {
  286. _commErr = true;
  287. Initalized = false;
  288. EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
  289. //EV.PostMessage(Name, EventEnum.DefaultWarning, string.Format("{0} Communication failed,please check it.{1}", Name, args.Reason));
  290. }
  291. private string ConvetJobName(string name)
  292. {
  293. name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
  294. name = name.Substring(0, name.LastIndexOf(".")); //remove expand
  295. return name;
  296. }
  297. }
  298. }