Handler.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using Aitex.Core.RT.Device;
  4. using Aitex.Core.RT.Log;
  5. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.LoadPorts.TDK
  6. {
  7. public interface IMsg
  8. {
  9. string deviceID { set; }
  10. bool background { get; }
  11. string package(params object[] args);
  12. string retry();
  13. /// </summary>
  14. /// return value, completed
  15. /// <param name="type"></param>
  16. /// <param name="cmd"></param>
  17. /// <returns></returns>
  18. bool unpackage(string type, string[] cmd);
  19. bool canhandle(string id);
  20. }
  21. public class handler<T> : IHandler where T : IMsg, new()
  22. {
  23. private static int retry_time = 3;
  24. private int retry_count = retry_time;
  25. private T _imp = new T();
  26. public int ID { get; set; }
  27. public int Unit { get; set; }
  28. public bool IsBackground { get { return _imp.background; } }
  29. private object[] _objs = null;
  30. private string _deviceID = string.Empty;
  31. private LoadPort _lpdevice;
  32. public handler(LoadPort lp, string deviceID, params object[] objs)
  33. {
  34. _deviceID = deviceID;
  35. _imp.deviceID = _deviceID;
  36. this._objs = objs;
  37. _lpdevice = lp;
  38. }
  39. public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
  40. {
  41. //_lpdevice.ExecuteError = false;
  42. retry_count = retry_time;
  43. return port.Write(string.Format("s00{0};\r", _imp.package(this._objs)));
  44. }
  45. /// <summary>
  46. /// return value: bhandle
  47. /// </summary>
  48. /// <typeparam name="TPort"></typeparam>
  49. /// <param name="port"></param>
  50. /// <param name="msg"></param>
  51. /// <param name="completed"></param>
  52. /// <returns></returns>
  53. public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
  54. {
  55. // message = "ACK:STATE/A000A400101000101000;";
  56. completed = false;
  57. try
  58. {
  59. string msg = message.TrimEnd(';');
  60. string[] words = Regex.Split(msg, ":");
  61. string type = words[0];
  62. string data = words[1];
  63. string[] items = Regex.Split(data, "/");
  64. if (!_imp.canhandle(items[0]))
  65. return false;
  66. if (type == "ABS")
  67. {
  68. _lpdevice.ExecuteError = true;
  69. throw (new ExcuteFailedException(message));
  70. }
  71. else if (type == "NAK") //process retry
  72. {
  73. if (items.Length > 1)
  74. {
  75. string cause = items[1];
  76. if (cause != "CKSUM" || cause != "CMDER")
  77. {
  78. string warning = string.Format("can't excute retry, failed cause is {0}", cause);
  79. LOG.Warning(warning);
  80. throw (new ExcuteFailedException(warning));
  81. }
  82. if (retry_count-- <= 0)
  83. {
  84. string warning = string.Format("retry over {0} times", retry_time);
  85. LOG.Warning(warning);
  86. throw (new ExcuteFailedException(warning));
  87. }
  88. port.Write(_imp.retry());
  89. return true;
  90. }
  91. }
  92. else
  93. {
  94. //_lpdevice.ExecuteError = false;
  95. completed = _imp.unpackage(type, items);
  96. return true;
  97. }
  98. }
  99. catch (ExcuteFailedException e)
  100. {
  101. throw (e);
  102. }
  103. catch (Exception ex)
  104. {
  105. LOG.Write(ex);
  106. throw (new InvalidPackageException(message));
  107. }
  108. return false;
  109. }
  110. }
  111. }