Handler.cs 7.4 KB

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