Handler.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Aitex.Core.RT.Log;
  4. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders
  6. {
  7. public interface IReaderMsg
  8. {
  9. string package(params object[] args);
  10. /// </summary>
  11. /// return value, completed
  12. /// <param name="type"></param>
  13. /// <param name="cmd"></param>
  14. /// <returns></returns>
  15. bool unpackage(string type, string[] items);
  16. bool background { get; }
  17. }
  18. public class handler : IHandler
  19. {
  20. public int ID { get; set; }
  21. public int Unit { get; set; }
  22. public bool IsBackground { get { return _imp.background; } }
  23. private static int retry_time = 1;
  24. private int retry_count = retry_time;
  25. private IReaderMsg _imp ;
  26. private object[] _objs = null;
  27. public handler(IReaderMsg imp, params object[] objs)
  28. {
  29. _imp = imp;
  30. this._objs = objs;
  31. }
  32. public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
  33. {
  34. retry_count = retry_time;
  35. //return port.Write(string.Format("{0}\r", _imp.package(this._objs)));
  36. return port.Write(string.Format("{0}\r\n", _imp.package(this._objs)));
  37. }
  38. /// <summary>
  39. /// return value: bhandle
  40. /// </summary>
  41. /// <typeparam name="TPort"></typeparam>
  42. /// <param name="port"></param>
  43. /// <param name="msg"></param>
  44. /// <param name="completed"></param>
  45. /// <returns></returns>
  46. public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
  47. {
  48. completed = false;
  49. try
  50. {
  51. string msg = message.Trim();
  52. if (msg.IndexOf("Welcome") >= 0)
  53. {
  54. return true;
  55. }
  56. msg = msg.Replace("User:","");
  57. if (string.IsNullOrWhiteSpace(msg))
  58. {
  59. return true;
  60. }
  61. if (msg.Length <= 2)
  62. {
  63. if (!msg.Equals("1")) //0: command failed
  64. {
  65. if (retry_count-- <= 0)
  66. {
  67. string warning = string.Format("retry over {0} times", retry_time);
  68. //LOG.Warning(warning);
  69. if (!IsBackground)
  70. throw (new ExcuteFailedException(warning));
  71. else
  72. {
  73. completed = true;
  74. return true;
  75. }
  76. }
  77. port.Write(string.Format("{0}\r\n", _imp.package(this._objs)));
  78. }
  79. if (!IsBackground)
  80. {
  81. _imp.unpackage("", null);
  82. completed = true;
  83. }
  84. return true;
  85. }
  86. if (IsBackground)
  87. {
  88. msg = message.TrimStart('[');
  89. msg = msg.TrimEnd(']');
  90. string[] data = Regex.Split(msg, ",");
  91. completed = _imp.unpackage("", data);
  92. // if (!completed)
  93. // {
  94. // port.Write(string.Format("{0}\r", _imp.package(this._objs))); //read failed. retry
  95. // }
  96. }
  97. return true;
  98. }
  99. catch (ExcuteFailedException e)
  100. {
  101. throw (e);
  102. }
  103. catch (Exception ex)
  104. {
  105. LOG.WriteExeption(ex);
  106. throw (new InvalidPackageException(message));
  107. }
  108. }
  109. }
  110. public class ReadHandler : IReaderMsg //common move
  111. {
  112. public bool background { get; private set; }
  113. private OcrReader _device ;
  114. public ReadHandler(OcrReader device)
  115. {
  116. _device = device;
  117. background = true;
  118. }
  119. public string package(params object[] args)
  120. {
  121. return string.Format("SM\"READ\"0 ");
  122. }
  123. public bool unpackage(string type, string[] items)
  124. {
  125. if (_device.ReadLaserMaker)
  126. {
  127. _device.LaserMaker = items[0];
  128. }
  129. else
  130. {
  131. _device.T7Code = items[0];
  132. }
  133. _device.ReadOK = double.Parse(items[2]) > 0;
  134. return true;
  135. }
  136. }
  137. public class OnlineHandler : IReaderMsg //common move
  138. {
  139. public bool background { get; private set; }
  140. private OcrReader _device ;
  141. private bool _online = false;
  142. public OnlineHandler(OcrReader device)
  143. {
  144. _device = device;
  145. background = false;
  146. }
  147. public string package(params object[] args)
  148. {
  149. _online = (bool)args[0];
  150. if(_online)
  151. return string.Format("SO1");
  152. return string.Format("SO0");
  153. }
  154. public bool unpackage(string type, string[] items)
  155. {
  156. return true;
  157. }
  158. }
  159. public class GetJobHandler : IReaderMsg
  160. {
  161. public bool background { get; private set; }
  162. private OcrReader _device ;
  163. public GetJobHandler(OcrReader device)
  164. {
  165. _device = device;
  166. background = true;
  167. }
  168. public string package(params object[] args)
  169. {
  170. return string.Format("GF");
  171. }
  172. public bool unpackage(string type, string[] items)
  173. {
  174. _device.JobName = (string)items[0];
  175. return true;
  176. }
  177. }
  178. public class LoadJobHandler : IReaderMsg //common move
  179. {
  180. public bool background { get; private set; }
  181. private OcrReader _device ;
  182. private string _job;
  183. public LoadJobHandler(OcrReader device)
  184. {
  185. _device = device;
  186. background = false;
  187. }
  188. public string package(params object[] args)
  189. {
  190. _job = (string)args[0]; //full path
  191. // _job = _job.Substring(_job.LastIndexOf("\\") + 1); //remove dir
  192. // _job = _job.Substring(0, _job.LastIndexOf(".")); //remove expand
  193. return string.Format("LF{0}.job",_job);
  194. }
  195. public bool unpackage(string type, string[] items)
  196. {
  197. _device.JobName = _job;
  198. return true;
  199. }
  200. }
  201. }