PortOperateImp.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using Polly;
  2. using System;
  3. using System.Threading;
  4. using SHJX.Service.ServerClient;
  5. using SHJX.Service.Model.Control;
  6. namespace SHJX.Service.Control.PortOperate.Interface
  7. {
  8. /// <summary>
  9. /// 泵操作
  10. /// </summary>
  11. public abstract class PortOperateImp
  12. {
  13. protected readonly OptClient _client;
  14. protected string OpName { get; set; }
  15. public PortOperateImp(OptClient optClient)
  16. {
  17. _client = optClient;
  18. }
  19. /// <summary>
  20. /// 操作液体的方式
  21. /// </summary>
  22. /// <param name="quantity">体积</param>
  23. /// <param name="pipeName"></param>
  24. public virtual bool LiquidOperation(string pipeName, long quantity)
  25. {
  26. CutPipe(pipeName);
  27. return MotorWrite(quantity); //吸液
  28. }
  29. /// <summary>
  30. /// 返回原点
  31. /// </summary>
  32. /// <param name="pipeName">管道名称</param>
  33. public virtual void GoBackOriginalPosition(string pipeName)
  34. {
  35. CutPipe(pipeName);
  36. GoBack();
  37. }
  38. /// <summary>
  39. /// 返回原点
  40. /// </summary>
  41. private void GoBack()
  42. {
  43. PortArgs gobackArg = new()
  44. {
  45. TypeName = OpName,
  46. WriteWay = WriteWay.GoBack
  47. };
  48. bool gb_write;
  49. do
  50. {
  51. gb_write = _client.Factory(OpName).Write(gobackArg);
  52. Thread.Sleep(2000);
  53. } while (!gb_write);
  54. bool res;
  55. do
  56. {
  57. char[] readRes = Read();
  58. if (readRes is null or { Length: 0 })
  59. {
  60. Thread.Sleep(1000);
  61. res = false;
  62. continue;
  63. };
  64. res = readRes[2].Equals('0');
  65. } while (!res);
  66. }
  67. /// <summary>
  68. /// 到达限位
  69. /// </summary>
  70. /// <returns></returns>
  71. public bool ArriveEndPoint()
  72. {
  73. var readRes = Read();
  74. if (readRes is null or { Length: 0 })
  75. return false;
  76. return readRes[2].Equals('0');
  77. }
  78. /// <summary>
  79. /// 读取寄存器
  80. /// </summary>
  81. /// <returns></returns>
  82. protected char[] Read()
  83. {
  84. PortArgs readArgs = new()
  85. {
  86. TypeName = OpName
  87. };
  88. var readRes = _client.Factory(OpName).Read(readArgs) as char[];
  89. return readRes;
  90. }
  91. /// <summary>
  92. /// 切换管道
  93. /// </summary>
  94. /// <param name="pipeName"></param>
  95. protected virtual void CutPipe(string pipeName)
  96. {
  97. var readRes = Read();
  98. if (readRes is null or { Length: 0 }) return;
  99. OptPipe(() =>
  100. {
  101. var readRes = Read();
  102. if (readRes is null or { Length: 0 })
  103. {
  104. Thread.Sleep(1000);
  105. return false;
  106. }
  107. var res = pipeName switch
  108. {
  109. "In" => readRes[5].Equals('0'),
  110. "Out" => readRes[6].Equals('0'),
  111. _ => false,
  112. };
  113. return res;
  114. });
  115. }
  116. /// <summary>
  117. /// 选择管道(出 or 吸)
  118. /// </summary>
  119. /// <param name="func"></param>
  120. protected void OptPipe(Func<bool> func)
  121. {
  122. var res = func.Invoke();
  123. if (res) return;
  124. #region 打开寄存器
  125. PortArgs switchArg = new()
  126. {
  127. TypeName = OpName,
  128. WriteWay = WriteWay.Normotopia
  129. };
  130. bool openRes;
  131. do
  132. {
  133. openRes = _client.Factory(OpName).Write(switchArg);
  134. Thread.Sleep(1000);
  135. } while (!openRes);
  136. #endregion
  137. Policy.HandleResult<bool>(arg => arg.Equals(false))
  138. .RetryForever(_ => { MotorWrite(-100); })
  139. .Execute(func);
  140. #region 关闭该寄存器
  141. switchArg.WriteWay = WriteWay.Antiposition;
  142. do
  143. {
  144. openRes = _client.Factory(OpName).Write(switchArg);
  145. Thread.Sleep(1000);
  146. } while (!openRes);
  147. #endregion
  148. }
  149. /// <summary>
  150. /// 写入脉冲
  151. /// </summary>
  152. /// <param name="distance"></param>
  153. protected bool MotorWrite(long distance)
  154. {
  155. PortArgs args = new()
  156. {
  157. TypeName = OpName,
  158. WriteWay = WriteWay.Move,
  159. Distance = distance
  160. };
  161. var res = _client.Factory(OpName).Write(args);
  162. int speedWaitTime = OpName switch
  163. {
  164. "SilverSulfate" => 300,
  165. _ => 500,
  166. };
  167. Thread.Sleep((int)(Math.Abs(distance) / 20000 * speedWaitTime));
  168. return res;
  169. }
  170. }
  171. }