Reader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.RT.DataCenter;
  6. using Aitex.Sorter.Common;
  7. using TSC = Aitex.Sorter.Common;
  8. using Aitex.Core.RT.Device;
  9. using Aitex.Sorter.RT.Device.Robot;
  10. using Aitex.Core.RT.SCCore;
  11. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM;
  12. using System.Net;
  13. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM.EventArgs;
  14. namespace Aitex.Sorter.RT.Device.Reader
  15. {
  16. public class WIDReader : BaseDevice, IDevice
  17. {
  18. public DeviceState State
  19. {
  20. get
  21. {
  22. if (!Initalized)
  23. {
  24. return DeviceState.Unknown;
  25. }
  26. if (Error)
  27. {
  28. return DeviceState.Error;
  29. }
  30. if (Busy)
  31. return DeviceState.Busy;
  32. return DeviceState.Idle;
  33. }
  34. }
  35. public bool Initalized { get; set; }
  36. public bool Busy
  37. {
  38. get
  39. {
  40. lock (_locker)
  41. {
  42. return _handlers.Count > 0 || _foregroundHandler != null;
  43. }
  44. }
  45. }
  46. public bool Error
  47. {
  48. get
  49. {
  50. return _commErr;
  51. }
  52. }
  53. public bool ReadLaserMaker { get; set; }
  54. public Guid OCRGuid { get; set; }
  55. public string JobName { get; set; }
  56. public string LaserMaker { get; set; }
  57. public string LaserMark1 { get; set; }
  58. public string LaserMark1Score { get; set; }
  59. public string LaserMark1ReadTime { get; set; }
  60. public string T7Code { get; set; }
  61. public string LaserMark2 { get; set; }
  62. public string LaserMark2Score { get; set; }
  63. public string LaserMark2ReadTime { get; set; }
  64. public bool ReadOK { get; set; }
  65. public string ImageFileName { get; set; }
  66. public string ImageStorePath { get; set; }
  67. private static Object _locker = new Object();
  68. private AsyncSocket _socket;
  69. private TcpSocketClient _client;
  70. private IHandler _foregroundHandler = null; //current handler
  71. private Queue<IHandler> _handlers = new Queue<IHandler>();
  72. public const string delimiter = "\r\n";
  73. private bool _commErr = false;
  74. private string _addr;
  75. private bool _onlineInited;
  76. private string AlarmWaferIDReadError = "WaferIDReadError";
  77. public WIDReader(string module, string name, string display, string deviceId, string address)
  78. : base(module, name, display, deviceId)
  79. {
  80. _addr = address;
  81. _socket = new AsyncSocket(address, delimiter);
  82. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataChanged);
  83. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHandler);
  84. string[] ipport = address.Split(':');
  85. IPAddress _ip = IPAddress.Parse(ipport[0]);
  86. //_client = new TcpSocketClient(_ip, Convert.ToInt16(ipport[1]));
  87. //_client.ServerDataReceived += _client_ServerDataReceived;
  88. Initalized = false;
  89. }
  90. private void _client_ServerDataReceived(object sender, TcpServerDataReceivedEventArgs e)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. public bool Initialize()
  95. {
  96. _socket.Connect(this._addr);
  97. DEVICE.Register(String.Format("{0}.{1}", Name, TSC.DeviceOperationName.ReadWaferID), (out string reason, int time, object[] param) =>
  98. {
  99. bool bLaser = bool.Parse((string)param[0]);
  100. string jobName = (string)param[1];
  101. bool ret = Read(bLaser, jobName, out reason);
  102. if (ret)
  103. {
  104. reason = string.Format("{0}", Name, "Read LaserMark");
  105. return true;
  106. }
  107. return false;
  108. });
  109. DATA.Subscribe(Name, ParamName.WRIDReaderState, () => State);
  110. DATA.Subscribe(Name, ParamName.WRIDReaderBusy, () => Busy);
  111. DATA.Subscribe(Name, ParamName.LaserMaker1, () => LaserMaker);
  112. DATA.Subscribe(Name, ParamName.LaserMaker2, () => T7Code);
  113. EV.Subscribe(new EventItem("Event", AlarmWaferIDReadError, "LaserMark reader error", EventLevel.Alarm, Aitex.Core.RT.Event.EventType.HostNotification));
  114. _handlers.Clear();
  115. Initalized = true;
  116. return true;
  117. }
  118. public void Terminate()
  119. {
  120. }
  121. public void Monitor()
  122. {
  123. if (!_onlineInited && _socket.IsConnected)
  124. {
  125. _onlineInited = true;
  126. string error = string.Empty;
  127. Online(true, out error);
  128. }
  129. _commErr = !_socket.IsConnected;
  130. }
  131. public void Reset()
  132. {
  133. lock (_locker)
  134. {
  135. _foregroundHandler = null;
  136. _handlers.Clear();
  137. }
  138. if (_commErr)
  139. {
  140. //_commErr = false;
  141. _socket.Connect(this._addr);
  142. }
  143. }
  144. #region Command
  145. public bool ReadImage(string wid,out string reason)
  146. {
  147. reason = "";
  148. return true;
  149. }
  150. public bool Read(bool bLaserMaker, string jobName, out string reason)
  151. {
  152. _handlers.Clear();
  153. ReadOK = false;
  154. if (string.IsNullOrEmpty(jobName))
  155. {
  156. reason = "Job is undefine.";
  157. return false;
  158. }
  159. jobName = ConvetJobName(jobName);
  160. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  161. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  162. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  163. ReadOK = false;
  164. ReadLaserMaker = bLaserMaker;
  165. _handlers.Enqueue(new handler( new ReadHandler(this))); //Read
  166. return execute(out reason);
  167. }
  168. /// <summary>
  169. /// 传入Jobs的Read动作.
  170. /// </summary>
  171. /// <param name="bLaserMaker">LM1 = True / LM2 = False</param>
  172. /// <param name="jobs">Jobs文件</param>
  173. /// <param name="reason">返回错误</param>
  174. /// <returns></returns>
  175. public bool Read(bool bLaserMaker, string[] jobs, out string reason)
  176. {
  177. _handlers.Clear();
  178. //string[] jobs = GetJobName(bLaserMaker);
  179. if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
  180. {
  181. reason = "Job is undefine";
  182. return false;
  183. }
  184. string jobName = ConvetJobName(jobs[0]);
  185. if (JobName != jobName)
  186. {
  187. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  188. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  189. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  190. }
  191. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  192. if (jobs.Length > 1)
  193. {
  194. for (int i = 1; i < jobs.Length; i++)
  195. {
  196. jobName = ConvetJobName(jobs[i]);
  197. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  198. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  199. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  200. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  201. }
  202. }
  203. ReadOK = false;
  204. ReadLaserMaker = bLaserMaker;
  205. return execute(out reason);
  206. }
  207. public bool Read(bool bLaserMaker, out string reason)
  208. {
  209. _handlers.Clear();
  210. string[] jobs = GetJobName(bLaserMaker);
  211. if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
  212. {
  213. reason = "Job is undefine";
  214. return false;
  215. }
  216. string jobName = ConvetJobName(jobs[0]);
  217. if (JobName != jobName)
  218. {
  219. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  220. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  221. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  222. }
  223. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  224. if (jobs.Length > 1)
  225. {
  226. for (int i = 1; i < jobs.Length; i++)
  227. {
  228. jobName = ConvetJobName(jobs[i]);
  229. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  230. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  231. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  232. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  233. }
  234. }
  235. ReadOK = false;
  236. ReadLaserMaker = bLaserMaker;
  237. return execute(out reason);
  238. }
  239. public bool ReadOnHostMode(bool bLaserMaker, string[] jobnames, out string reason)
  240. {
  241. _handlers.Clear();
  242. string[] jobs = jobnames;
  243. if (jobs.Length == 0 || string.IsNullOrEmpty(jobs[0]))
  244. {
  245. reason = "Job is undefine";
  246. return false;
  247. }
  248. string jobName = ConvetJobName(jobs[0]);
  249. _handlers.Enqueue(new handler(new OnlineHandler(this), false)); //offline
  250. _handlers.Enqueue(new handler(new LoadJobHandler(this), jobName)); //LoadJob
  251. _handlers.Enqueue(new handler(new OnlineHandler(this), true)); //online
  252. _handlers.Enqueue(new handler(new ReadHandler(this))); //Read
  253. ReadOK = false;
  254. ReadLaserMaker = bLaserMaker;
  255. return execute(out reason);
  256. }
  257. public bool Read(out string reason)
  258. {
  259. reason = string.Empty;
  260. return execute(new handler(new ReadHandler(this)), out reason);
  261. }
  262. public bool ReadJobName(out string reason)
  263. {
  264. reason = string.Empty;
  265. return execute(new handler(new GetJobHandler(this)), out reason);
  266. }
  267. public bool Online(bool online, out string reason)
  268. {
  269. reason = string.Empty;
  270. return execute(new handler(new OnlineHandler(this), online), out reason);
  271. }
  272. public bool LoadJob(string jobName, out string reason)
  273. {
  274. reason = string.Empty;
  275. return execute(new handler(new LoadJobHandler(this), jobName), out reason);
  276. }
  277. #endregion
  278. private string[] GetJobName(bool bLaserMark1)
  279. {
  280. string jobs = string.Empty;
  281. if (bLaserMark1)
  282. {
  283. jobs = Name== "WIDReader1" ? SC.GetConfigItem("OcrReader.ReaderJob1Main").StringValue : SC.GetConfigItem("OcrReader.ReaderJob1Slave").StringValue;
  284. }
  285. else
  286. {
  287. jobs = Name == "WIDReader1" ? SC.GetConfigItem("OcrReader.ReaderJob2Main").StringValue : SC.GetConfigItem("OcrReader.ReaderJob2Slave").StringValue;
  288. }
  289. return jobs.Split(';');
  290. }
  291. private bool execute(IHandler handler, out string reason)
  292. {
  293. reason = string.Empty;
  294. lock (_locker)
  295. {
  296. if (!handler.Execute(ref _socket))
  297. {
  298. reason = "Communication failed, please recovery it.";
  299. return false;
  300. }
  301. _foregroundHandler = handler;
  302. }
  303. return true;
  304. }
  305. private bool execute(out string reason)
  306. {
  307. reason = string.Empty;
  308. lock (_locker)
  309. {
  310. if (_handlers.Count > 0)
  311. {
  312. _foregroundHandler = _handlers.Dequeue();
  313. if (!_foregroundHandler.Execute(ref _socket))
  314. {
  315. reason = " communication failed, please recovery it.";
  316. //LOG.Error(reason);
  317. EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】" + reason);
  318. _foregroundHandler = null;
  319. return false;
  320. }
  321. }
  322. }
  323. return true;
  324. }
  325. private void OnDataChanged(string package)
  326. {
  327. try
  328. {
  329. package = package.ToUpper();
  330. string[] msgs = Regex.Split(package, delimiter);
  331. foreach (string msg in msgs)
  332. {
  333. if (msg.Length > 0)
  334. {
  335. bool completed = false;
  336. string resp = msg;
  337. lock (_locker)
  338. {
  339. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  340. {
  341. if (completed)
  342. {
  343. _foregroundHandler = null;
  344. if(ReadOK)
  345. {
  346. _handlers.Clear();
  347. }
  348. else
  349. {
  350. string reason = string.Empty;
  351. execute(out reason);
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. catch (ExcuteFailedException ex)
  360. {
  361. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  362. OnError();
  363. }
  364. catch (InvalidPackageException ex)
  365. {
  366. EV.PostMessage(DeviceID, EventEnum.DefaultWarning, ex.Message);
  367. OnError();
  368. }
  369. catch (System.Exception ex)
  370. {
  371. EV.PostMessage(Name, EventEnum.DefaultWarning, "【Reader】has exception:" + ex.ToString());
  372. OnError();
  373. }
  374. }
  375. private void OnErrorHandler(ErrorEventArgs args)
  376. {
  377. _commErr = true;
  378. //Initalized = false;
  379. EV.PostMessage(Module, EventEnum.CommunicationError, Display, args.Reason);
  380. OnError();
  381. }
  382. public void OnError()
  383. {
  384. EV.Notify(AlarmWaferIDReadError);
  385. }
  386. private string ConvetJobName(string name)
  387. {
  388. name = name.Substring(name.LastIndexOf("\\") + 1); //remove dir
  389. name = name.IndexOf(".") >=0?name.Substring(0, name.LastIndexOf(".")):name; //remove expand
  390. return name;
  391. }
  392. }
  393. }