Mag7RobotHandlerBase.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. completed = _imp.unpackage(type, words);
  73. throw (new ExcuteFailedException(warning));
  74. }
  75. if (retry_count-- <= 0)
  76. {
  77. string warning = string.Format("retry over {0} times", retry_time);
  78. LOG.Warning(warning);
  79. throw (new ExcuteFailedException(warning));
  80. }
  81. return true;
  82. }
  83. else if (type == ProtocolTag.resp_tag_event)
  84. {
  85. string evtType = words[3];
  86. string evtInfo = words[5];
  87. if (_imp.evt)
  88. completed = _imp.unpackage(type, words);
  89. return _imp.evt;
  90. }
  91. else if (type == ProtocolTag.resp_tag_excute)
  92. {
  93. completed = _imp.unpackage(type, words);
  94. if (completed)
  95. {
  96. _generator.release(ID);
  97. return true;
  98. }
  99. return true;
  100. }
  101. else
  102. {
  103. completed = _imp.unpackage(type, words);
  104. if (completed)
  105. {
  106. _generator.release(ID);
  107. return true;
  108. }
  109. return true;
  110. }
  111. }
  112. catch (ExcuteFailedException e)
  113. {
  114. throw (e);
  115. }
  116. catch (InvalidPackageException e)
  117. {
  118. throw e;
  119. }
  120. catch (Exception ex)
  121. {
  122. LOG.Write(ex);
  123. throw (new InvalidPackageException(message));
  124. }
  125. }
  126. private string package()
  127. {
  128. //Commands<CR>
  129. string data = string.Empty;
  130. data = _imp.package(this._objs);
  131. return data;
  132. }
  133. }
  134. }