TongsGrab.cs 4.7 KB

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