ForeignHaiRoboticsService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\ErrorException;
  4. use App\Exceptions\PanicException;
  5. use App\MaterialBox;
  6. use App\StationTaskMaterialBox;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Http;
  11. use App\Traits\ServiceAppAop;
  12. class ForeignHaiRoboticsService
  13. {
  14. use ServiceAppAop;
  15. /** @var $stationTaskMaterialBoxService StationTaskMaterialBoxService */
  16. private $stationTaskMaterialBoxService;
  17. /** @var $materialBoxService MaterialBoxService */
  18. private $materialBoxService;
  19. /** @var $stationService StationService */
  20. private $stationService;
  21. public function __construct()
  22. {
  23. $this->stationTaskMaterialBoxService=null;
  24. $this->materialBoxService=null;
  25. $this->stationService=null;
  26. }
  27. /**
  28. * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
  29. * @param string $fromLocation
  30. * @param string $toLocation
  31. * @param Collection $taskMaterialBoxes
  32. * @param string $groupId
  33. * @param int $priority
  34. * @param int $isSequenced
  35. * @return array
  36. */
  37. private function makeJson_move(
  38. Collection $taskMaterialBoxes,
  39. string $modeName,
  40. string $fromLocation='',
  41. string $toLocation='',
  42. $groupId=''
  43. , $priority=10
  44. , $isSequenced=1
  45. ): array
  46. {
  47. $timestampSuffix = microtime(true);
  48. $taskMode=(function()use($modeName){
  49. switch ($modeName){
  50. case '输送线入立架': return 1;
  51. case '立架出至输送线': return 2;
  52. case '移动立架内位置': return 3;
  53. case '缓存架入立架':
  54. case '立架出至缓存架': return 6;
  55. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  56. }
  57. })();
  58. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
  59. return [
  60. "taskCode" =>$taskMaterialBox['id'],
  61. "binCode" => $taskMaterialBox['materialBox']['code'],
  62. "fromLocCode" => $fromLocation??'',
  63. "toLocCode" => $toLocation??'',
  64. ];
  65. });
  66. return [
  67. "taskMode" =>$taskMode,
  68. "bins"=>$bins,
  69. "groupCode"=>$groupId.$timestampSuffix,
  70. "priority"=>$priority,
  71. "sequenceFlag"=>$isSequenced,
  72. ];
  73. }
  74. public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix=''): bool
  75. {
  76. $dataToPost=$this->makeJson_move(
  77. $taskMaterialBoxes,
  78. '立架出至输送线',
  79. '',
  80. $toLocation??'',
  81. $groupIdPrefix
  82. );
  83. return $this->controlHaiRobot($dataToPost);
  84. }
  85. public function moveBin(){
  86. }
  87. public function markBinProcessed(
  88. $workStation,
  89. $binCode,
  90. $success,
  91. $created_at,
  92. $exception,
  93. $is_in_plan
  94. ): bool
  95. {
  96. try{
  97. if($failed
  98. =!$success)
  99. throw new ErrorException('海柔任务失败:'.$exception);
  100. if($NotInPlan
  101. =!$is_in_plan)
  102. throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  103. $materialBox=
  104. $this->materialBoxService->get(['code'=>$binCode]);
  105. if($stationTaskMaterialBox
  106. =(function()use($materialBox){
  107. return $stationTaskMaterialBox=
  108. $this->stationTaskMaterialBoxService
  109. ->get([
  110. 'material_box_id'=>$materialBox['id'],
  111. 'status'=>'处理中',
  112. ])->first();
  113. })()){
  114. throw new ErrorException('该料箱没有安排在处理队列中');
  115. }
  116. $this->putBinToStore($stationTaskMaterialBox)
  117. ?true
  118. :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
  119. $this->stationTaskMaterialBoxService
  120. ->markProcessed($stationTaskMaterialBox);
  121. return true;
  122. }catch (\Exception $e){
  123. $this->stationTaskMaterialBoxService
  124. ->excepted($stationTaskMaterialBox??$materialBox??null);
  125. return $e->getMessage();
  126. }
  127. }
  128. public function putBinToStore(StationTaskMaterialBox $stationTaskMaterialBox): bool
  129. {
  130. $this->instant($this->stationService,'StationService');
  131. $dataToPost=$this->makeJson_move(
  132. collect($stationTaskMaterialBox),
  133. '缓存架入立架',
  134. $this->stationService->getULineExit($stationTaskMaterialBox['station'])['name']??'',
  135. '',
  136. $stationTaskMaterialBox['stationTaskBatch']['id']
  137. );
  138. return $this->controlHaiRobot($dataToPost);
  139. }
  140. public function taskUpdate(
  141. // $groupCode,
  142. $taskCode,
  143. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  144. $status, //0:任务成功1:任务失败
  145. $binCode
  146. ):bool{
  147. try{
  148. if(($failed
  149. =$status)==1){
  150. throw new ErrorException('海柔任务失败');
  151. }
  152. if($boxNotMatch=
  153. !$stationTaskMaterialBox
  154. =(function()use($taskCode,$binCode){
  155. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$taskCode);
  156. if($stationTaskMaterialBox['code']==$binCode)return $stationTaskMaterialBox;
  157. return null;
  158. })()){
  159. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  160. }
  161. ($标记已放置在库外=
  162. function()use($updateEventType,$stationTaskMaterialBox){
  163. if(($isPut
  164. =$updateEventType)==1){
  165. $this->stationTaskMaterialBoxService->markHasPut($stationTaskMaterialBox);
  166. }
  167. })();
  168. ($标记已入立架=
  169. function()use($updateEventType,$stationTaskMaterialBox){
  170. if(($isGet
  171. =$updateEventType)==0){
  172. $this->stationTaskMaterialBoxService->markTaken($stationTaskMaterialBox);
  173. }
  174. })();
  175. }catch (\Exception $e){
  176. $this->excepted($taskCode, $binCode, $e->getMessage());
  177. return false;
  178. }
  179. return true;
  180. }
  181. // public function markHasPut($taskCode,$binCode):bool{
  182. // try{
  183. // //标记料箱进入位置
  184. //// $taskMaterialBoxesService->markDone();//
  185. // }catch (\Exception $e){
  186. // switch ($e->getCode()){
  187. // case 'taskBinNotMatch';
  188. // case 'taskGetFailed';
  189. // }
  190. // }
  191. //
  192. // }
  193. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  194. try{
  195. throw new ErrorException(
  196. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  197. );
  198. }catch (\Exception $e){
  199. return true;
  200. }
  201. }
  202. /**
  203. * @param array $dataToPost
  204. * @return bool
  205. */
  206. public function controlHaiRobot(array $dataToPost): bool
  207. {
  208. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  209. $errMsg = (function () use ($response) {
  210. if ($response->ok()) return '';
  211. $errMsg = '错误: ';
  212. if (!$response) {
  213. return $errMsg . '没有返回内容,检查连接或目标服务器';
  214. }
  215. switch (((string)$response["code"])[0]) {
  216. case 5:
  217. $errMsg .= '目标服务器代码错误,请联系对方';
  218. break;
  219. case 4:
  220. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  221. break;
  222. default:
  223. $errMsg .= '出现未知请求错误';
  224. break;
  225. }
  226. $responseDetails = ' code:' . $response["code"]
  227. . ' header:' . $response->body()
  228. . ' response:' . json_encode($response->headers());
  229. return $errMsg . $responseDetails;
  230. })();
  231. LogService::log(__METHOD__, __FUNCTION__,
  232. $errMsg ?? ''
  233. . '请求:' . json_encode($dataToPost)
  234. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  235. );
  236. return $isSuccess = !$errMsg;
  237. }
  238. }