TongsLoosen.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using EaseDapper;
  2. using SHJX.Service.Control.Extends;
  3. using static Org.BouncyCastle.Math.EC.ECCurve;
  4. using System.Windows.Threading;
  5. namespace SHJX.Service.Control.Pipeline.Nodes.MoveNodes
  6. {
  7. public class TongsLoosen : INode
  8. {
  9. #region Fields
  10. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(TongsGrab));
  11. private static IDataManager _dataManager;
  12. private static TaskExtend _taskExtend;
  13. private static ReadConfigUtil _config;
  14. #endregion
  15. public TongsLoosen(IDataManager dataManager, TaskExtend taskExtend, ReadConfigUtil config)
  16. {
  17. Name = nameof(TongsGrab);
  18. _config = config;
  19. _dataManager = dataManager;
  20. _taskExtend = taskExtend;
  21. }
  22. public override INode Invoke()
  23. {
  24. bool writeResponse = true;
  25. if (_config.TongsFeedBack)
  26. {
  27. var judgeCount = 0;
  28. Policy.HandleResult<bool>(Arg => Arg.Equals(false)).Retry(3, (resultArg, retryCount) =>
  29. {
  30. #region 松开
  31. writeResponse = EquipmentNames.Tongs.RegisterClose();
  32. #endregion
  33. Thread.Sleep(500);
  34. #region 抓取
  35. writeResponse = writeResponse && EquipmentNames.Tongs.RegisterOpen();
  36. #endregion
  37. judgeCount = retryCount;
  38. }).Execute(() =>
  39. {
  40. Thread.Sleep(500);
  41. char[] readRes = EquipmentNames.Tongs.MotorRead(); //这里需要读取
  42. bool judgeRes = readRes[4].Equals('0');
  43. #region 任务取消,加入取消缓存
  44. if (judgeCount.Equals(3) && !judgeRes)
  45. {
  46. ExcuteNextStep();
  47. }
  48. #endregion
  49. writeResponse = judgeRes;
  50. return judgeRes;
  51. });
  52. if (!writeResponse)
  53. {
  54. logger.LogDebug($"任务取消后,不进行后续操作!!!");
  55. return null;
  56. }
  57. }
  58. EquipmentNames.Tongs.RegisterClose();
  59. Thread.Sleep(200);
  60. return this;
  61. }
  62. private void ExcuteNextStep()
  63. {
  64. EquipmentNames.Tongs.RegisterClose();
  65. bool seekhome = EquipmentNames.AxisZ.MotorGoBack();
  66. if (seekhome)
  67. {
  68. CurrentTask.Status = TaskState.Finished;
  69. _dataManager.Update(CurrentTask);
  70. logger.LogDebug("任务被取消,后续步骤不再执行!");
  71. }
  72. if (CurrentTask.To.In("D1", "L1"))
  73. {
  74. EquipmentArea releaseArea = _dataManager.Query<EquipmentArea>().Where(item => item.PointName.Equals(CurrentTask.To)).First();
  75. if (releaseArea is not null)
  76. {
  77. releaseArea.Enable = true;
  78. _dataManager.Update(releaseArea);
  79. logger.LogDebug($"任务取消后,{releaseArea.PointName}被释放!");
  80. }
  81. }
  82. RouteCache cache = _dataManager.Query<RouteCache>().Where(item => item.Source.Equals(CurrentTask.Source)).First();
  83. if (cache is not null)
  84. {
  85. _dataManager.Delete(cache);
  86. logger.LogDebug($"任务取消后,{CurrentTask.Source}缓存被删除!");
  87. }
  88. var rows = _dataManager.Update<StateMachine>().Invoke(it => it.Status.Set(0))(it => it.Name.Equals(StateMachineName.MOTOR_LOCK));
  89. if (rows > 0)
  90. {
  91. logger.LogDebug($"任务取消后,{StateMachineName.MOTOR_LOCK}状态被解锁!");
  92. }
  93. ThreadPool.QueueUserWorkItem(delegate
  94. {
  95. SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
  96. SynchronizationContext.Current?.Post(pl =>
  97. {
  98. _config.UpdateTaskRunning(false);
  99. Messager<string>.Send("ChangePauseIconSendMesg");
  100. UMessageBox.SingleBtnInfo("任务暂停\n当前抓取位置异常,未检测到杯子\n请检查相应位置是否正确或联系售后工程师!");
  101. }, null);
  102. });
  103. }
  104. }
  105. }