Mag7RobotHandlerBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Device;
  8. using Aitex.Core.RT.Log;
  9. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.MAG7
  10. {
  11. public class Mag7RobotHandlerBase<T> : IHandler where T : ITransferMsg, new()
  12. {
  13. public int ID { get; set; }
  14. public int Unit { get; set; }
  15. public bool IsBackground
  16. {
  17. get
  18. {
  19. return !_seqMode && _imp.background;
  20. }
  21. }
  22. private static int retry_time = 3;
  23. private int retry_count = retry_time;
  24. private object[] _objs = null;
  25. private TokenGenerator _generator;
  26. private T _imp = new T();
  27. private Robot _device;
  28. private bool _seqMode = true;
  29. public Mag7RobotHandlerBase(IDevice device)
  30. {
  31. _device = (Robot)device;
  32. _imp.Robot = device;
  33. }
  34. public Mag7RobotHandlerBase(IDevice device, ref TokenGenerator gen, params object[] objs)
  35. {
  36. _device = (Robot)device;
  37. _imp.Robot = device;
  38. this._generator = gen;
  39. this._objs = objs;
  40. }
  41. public bool Execute<TPort>(ref TPort port) where TPort : ICommunication
  42. {
  43. retry_count = retry_time;
  44. ID = _generator.create();
  45. return port.Write(string.Format("{0}{1}", package(), ProtocolTag.tag_end));
  46. }
  47. /// <summary>
  48. /// return value: bhandle
  49. /// </summary>
  50. /// <typeparam name="TPort"></typeparam>
  51. /// <param name="port"></param>
  52. /// <param name="msg"></param>
  53. /// <param name="completed"></param>
  54. /// <returns></returns>
  55. ///
  56. public bool OnMessage<TPort>(ref TPort port, string message, out bool completed) where TPort : ICommunication
  57. {
  58. try
  59. {
  60. completed = false;
  61. string package = message;
  62. string[] words = Regex.Split(package, ProtocolTag.cmd_token);
  63. string type = words[0];
  64. if (type == ProtocolTag.resp_tag_error)
  65. {
  66. int error = int.Parse(words[1]);
  67. _device.LastErrorCode = error;
  68. if (error != 0) //can't retry
  69. {
  70. string warning = string.Format("Error code {0:D4}", error);
  71. LOG.Warning(warning);
  72. throw (new ExcuteFailedException(warning));
  73. }
  74. if (retry_count-- <= 0)
  75. {
  76. string warning = string.Format("retry over {0} times", retry_time);
  77. LOG.Warning(warning);
  78. throw (new ExcuteFailedException(warning));
  79. }
  80. return true;
  81. }
  82. else if (type == ProtocolTag.resp_tag_event)
  83. {
  84. string evtType = words[3];
  85. string evtInfo = words[5];
  86. if (_imp.evt)
  87. completed = _imp.unpackage(type, words);
  88. return _imp.evt;
  89. }
  90. else if (type == ProtocolTag.resp_tag_excute)
  91. {
  92. completed = _imp.unpackage(type, words);
  93. if (completed)
  94. {
  95. _generator.release(ID);
  96. return true;
  97. }
  98. return true;
  99. }
  100. else
  101. {
  102. completed = _imp.unpackage(type, words);
  103. if (completed)
  104. {
  105. _generator.release(ID);
  106. return true;
  107. }
  108. return true;
  109. }
  110. }
  111. catch (ExcuteFailedException e)
  112. {
  113. throw (e);
  114. }
  115. catch (InvalidPackageException e)
  116. {
  117. throw e;
  118. }
  119. catch (Exception ex)
  120. {
  121. LOG.Write(ex);
  122. throw (new InvalidPackageException(message));
  123. }
  124. }
  125. private string package()
  126. {
  127. //Commands<CR>
  128. string data = string.Empty;
  129. data = _imp.package(this._objs);
  130. return data;
  131. }
  132. }
  133. }