Handler.cs 3.9 KB

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