ForeignHaiRoboticsService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\ErrorException;
  4. use App\MaterialBox;
  5. use App\Station;
  6. use App\StationCacheShelfGrid;
  7. use App\StationTask;
  8. use App\StationTaskMaterialBox;
  9. use Carbon\Carbon;
  10. use Illuminate\Support\Collection;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Http;
  13. use App\Traits\ServiceAppAop;
  14. use Illuminate\Support\Facades\Log;
  15. class ForeignHaiRoboticsService
  16. {
  17. use ServiceAppAop;
  18. // protected $modelClass=ForeignHaiRobotics::class;
  19. /** @var $stationTaskMaterialBoxService StationTaskMaterialBoxService */
  20. private $stationTaskMaterialBoxService;
  21. /** @var $stationTaskBatchService StationTaskBatchService */
  22. private $stationTaskBatchService;
  23. /** @var $stationTaskCommoditiesService StationTaskCommodityService */
  24. private $stationTaskCommoditiesService;
  25. /** @var $materialBoxService MaterialBoxService */
  26. private $materialBoxService;
  27. /** @var $stationTaskService StationTaskService */
  28. private $stationTaskService;
  29. /** @var $stationService StationService */
  30. private $stationService;
  31. public function __construct()
  32. {
  33. $this->stationTaskMaterialBoxService=null;
  34. $this->materialBoxService=null;
  35. $this->stationService=null;
  36. }
  37. /**
  38. * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
  39. * @param string $fromLocation
  40. * @param string $toLocation
  41. * @param Collection $taskMaterialBoxes
  42. * @param string $groupId
  43. * @param int $priority
  44. * @param int $isSequenced
  45. * @return array
  46. */
  47. private function makeJson_move(
  48. Collection $taskMaterialBoxes,
  49. string $modeName,
  50. string $fromLocation='',
  51. string $toLocation='',
  52. $groupId=''
  53. , $priority=10
  54. , $isSequenced=1
  55. ): array
  56. {
  57. $timestampSuffix = microtime(true);
  58. $taskMode=(function()use($modeName){
  59. switch ($modeName){
  60. case '输送线入立架': return 1;
  61. case '立架出至输送线': return 2;
  62. case '立架出至缓存架':
  63. case '移动立架内位置': return 3;
  64. case '缓存架入立架':return 6;
  65. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  66. }
  67. })();
  68. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
  69. return [
  70. "taskCode" =>$taskMaterialBox['id'].'_'.$timestampSuffix,
  71. "binCode" => $taskMaterialBox['materialBox']['code'],
  72. "fromLocCode" => $fromLocation??'',
  73. "toLocCode" => $toLocation??'',
  74. ];
  75. });
  76. return [[
  77. "taskMode" =>$taskMode,
  78. "bins"=>$bins,
  79. "groupCode"=>$groupId.'_'.$timestampSuffix,
  80. "priority"=>$priority,
  81. "sequenceFlag"=>$isSequenced,
  82. ]];
  83. }
  84. private function makeJson_move_multi(
  85. Collection $taskMaterialBoxes,
  86. string $modeName,
  87. string $fromLocation='',
  88. string $toLocations=null,
  89. $groupId=''
  90. , $priority=10
  91. , $isSequenced=1
  92. ): array
  93. {
  94. $timestampSuffix = microtime(true);
  95. $taskMode=(function()use($modeName){
  96. switch ($modeName){
  97. case '输送线入立架': return 1;
  98. case '立架出至输送线': return 2;
  99. case '立架出至缓存架':
  100. case '移动立架内位置': return 3;
  101. case '缓存架入立架':return 6;
  102. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  103. }
  104. })();
  105. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox,$i)use($timestampSuffix,$fromLocation,$toLocations){
  106. return [
  107. "taskCode" =>$taskMaterialBox['id'].'_'.$timestampSuffix,
  108. "binCode" => $taskMaterialBox['materialBox']['code'],
  109. "fromLocCode" => $fromLocation??'',
  110. "toLocCode" => $toLocations[$i],
  111. ];
  112. });
  113. return [[
  114. "taskMode" =>$taskMode,
  115. "bins"=>$bins,
  116. "groupCode"=>$groupId.'_'.$timestampSuffix,
  117. "priority"=>$priority,
  118. "sequenceFlag"=>$isSequenced,
  119. ]];
  120. }
  121. public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix='',$mode='立架出至输送线'): bool
  122. {
  123. $dataToPost=$this->makeJson_move(
  124. $taskMaterialBoxes,
  125. $mode,
  126. '',
  127. $toLocation??'',
  128. $groupIdPrefix
  129. );
  130. return $this->controlHaiRobot($dataToPost,$taskMaterialBoxes,$mode);
  131. }
  132. /**
  133. * @param Collection $toLocations //库位和料箱任务一一对应,一个库位对应一个料箱
  134. * @param Collection $taskMaterialBoxes
  135. * @param string $groupIdPrefix
  136. * @param string $mode
  137. * @return bool
  138. * @throws ErrorException
  139. */
  140. public function fetchGroup_multiLocation(Collection $toLocations, Collection $taskMaterialBoxes, $groupIdPrefix='',$mode='立架出至输送线'): bool
  141. {
  142. $dataToPost=$this->makeJson_move_multi(
  143. $taskMaterialBoxes,
  144. $mode,
  145. '',
  146. $toLocations,
  147. $groupIdPrefix
  148. );
  149. return $this->controlHaiRobot($dataToPost,$taskMaterialBoxes,$mode);
  150. }
  151. public function moveBin(){
  152. }
  153. public function markBinProcessed(
  154. $workStation,
  155. $binCode,
  156. $success,
  157. $created_at,
  158. $exception,
  159. $is_in_plan
  160. ): bool
  161. {
  162. LogService::log('海柔请求','markBinProcessed1.1',
  163. '');
  164. $this->instant($this->stationService,'StationService');
  165. $this->instant($this->materialBoxService,'MaterialBoxService');
  166. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  167. $this->instant($this->stationTaskCommoditiesService,'StationTaskCommodityService');
  168. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  169. try{
  170. LogService::log('海柔请求','markBinProcessed1.2',
  171. json_encode([$binCode,$success,$exception,$is_in_plan]));
  172. if($failed
  173. =!$success)
  174. throw new ErrorException('海柔任务失败:'.$exception);
  175. LogService::log('海柔请求','markBinProcessed1.3',
  176. $failed);
  177. // if($NotInPlan
  178. // =!$is_in_plan)
  179. // throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  180. LogService::log('海柔请求','markBinProcessed1.4',
  181. '$NotInPlan');
  182. $materialBox=
  183. $this->materialBoxService->get(['code'=>$binCode])->first();
  184. LogService::log('海柔请求','markBinProcessed1.5',
  185. json_encode($materialBox));
  186. /** @var StationTaskMaterialBox $stationTaskMaterialBox */
  187. $stationTaskMaterialBox
  188. =(function()use($materialBox){
  189. return $stationTaskMaterialBox=
  190. StationTaskMaterialBox::query()
  191. ->where('material_box_id',$materialBox['id'])
  192. ->where('created_at','>',Carbon::now()->subDay())
  193. ->whereIn('status',['处理中','待处理','异常','处理队列'])
  194. ->orderBy('id','desc')
  195. ->first();
  196. })();
  197. LogService::log('海柔请求','markBinProcessed1.6',
  198. json_encode($stationTaskMaterialBox));
  199. if(!$stationTaskMaterialBox){
  200. throw new ErrorException($binCode.'该料箱没有安排在处理队列中.');
  201. }
  202. LogService::log('海柔请求','markBinProcessed1.7',
  203. json_encode($stationTaskMaterialBox));
  204. DB::transaction(function ()use($stationTaskMaterialBox){
  205. $stationTaskMaterialBox_next=
  206. $this->stationTaskMaterialBoxService
  207. ->processNextQueued($stationTaskMaterialBox); //找到队列中下一个料箱,并标记为处理中
  208. $this->stationTaskCommoditiesService
  209. ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
  210. LogService::log('海柔请求','markBinProcessed1.8',
  211. json_encode($stationTaskMaterialBox));
  212. if($stationTaskMaterialBox_next)
  213. $this->stationTaskCommoditiesService
  214. ->markProcessing($stationTaskMaterialBox_next['stationTaskCommodities']);//因为上边商品任务被标记完成了,所以这里要将队列中找出正在处理的料箱对应的标记为“处理中”
  215. $this->stationTaskMaterialBoxService
  216. ->markProcessed($stationTaskMaterialBox);
  217. $notProcessedBoxTasks = $this->stationTaskMaterialBoxService->getNotProcessedSiblings($stationTaskMaterialBox);
  218. if($notProcessedBoxTasks->isEmpty()){
  219. $this->instant($this->stationTaskService,'StationTaskService');
  220. LogService::log('海柔请求','markBinProcessed1.81',
  221. json_encode($stationTaskMaterialBox['stationTaskBatch']));
  222. $stationTaskMaterialBox->loadMissing('stationTaskBatch');
  223. $this->stationTaskBatchService->markProcessed($stationTaskMaterialBox['stationTaskBatch']);
  224. LogService::log('海柔请求','markBinProcessed1.82',
  225. json_encode($stationTaskMaterialBox['stationTaskBatch']));
  226. $this->stationTaskService->markProcessed($stationTaskMaterialBox['stationTask']);
  227. }
  228. $this->storeBox($stationTaskMaterialBox)
  229. ?true
  230. :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
  231. LogService::log('海柔请求','markBinProcessed1.9',
  232. json_encode($stationTaskMaterialBox));
  233. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  234. LogService::log('海柔请求','markBinProcessed1.99',
  235. json_encode($stationTaskMaterialBox));
  236. });
  237. return true;
  238. }catch (\Exception $e){
  239. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  240. $this->instant($this->materialBoxService,'MaterialBoxService');
  241. $box=$this->materialBoxService->firstOrCreate(['code'=>$binCode]);
  242. $stationTaskMaterialBox_toStore=
  243. $this->stationTaskMaterialBoxService->create([
  244. 'station_id' => $this->stationService->getStation_byType('立库')['id'],
  245. 'material_box_id' => $box['id'],
  246. 'status' => '处理中'
  247. ] );
  248. $dataToPost=$this->makeJson_move(
  249. collect([$stationTaskMaterialBox_toStore]),
  250. '输送线入立架',
  251. 'BIN-IN1',//TODO:这里应该是动态取得,参考出立架getULineExit()方法,不然不能从站获得对应的出口,而且要改Station的child为children
  252. '',
  253. $stationTaskMaterialBox_toStore['stationTaskBatch']['id']
  254. );
  255. $this->controlHaiRobot($dataToPost,collect([$stationTaskMaterialBox_toStore]),'输送线入立架');
  256. $stationTaskMaterialBox = $stationTaskMaterialBox_toStore??$materialBox??null;
  257. if($stationTaskMaterialBox && get_class($stationTaskMaterialBox)==MaterialBox::class){
  258. $stationTaskMaterialBox = StationTaskMaterialBox::query()
  259. ->where('material_box_id',$stationTaskMaterialBox['id'])
  260. ->where('status','<>','完成')
  261. ->where('created_at','>',now()->format('Y-m-d'))
  262. ->first();
  263. }
  264. if($stationTaskMaterialBox)
  265. $this->stationTaskMaterialBoxService
  266. ->excepted($stationTaskMaterialBox);
  267. return $e->getMessage();
  268. }
  269. }
  270. public function storeBox(?StationTaskMaterialBox $stationTaskMaterialBox): bool
  271. {
  272. LogService::log('海柔请求','putBinToStore1',
  273. '');
  274. $this->instant($this->stationService,'StationService');
  275. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  276. $stationTaskMaterialBox_toStore=
  277. $this->stationTaskMaterialBoxService->firstOrCreate([
  278. 'station_id' => $this->stationService->getStation_byType('立库')['id'],
  279. 'material_box_id' => $stationTaskMaterialBox['materialBox']['id'],
  280. 'status' => '处理中',
  281. 'type' => '放',
  282. ]);
  283. LogService::log('海柔请求','putBinToStore2',
  284. json_encode($stationTaskMaterialBox));
  285. $dataToPost=$this->makeJson_move(
  286. collect([$stationTaskMaterialBox_toStore]),
  287. '输送线入立架',
  288. 'BIN-IN1',//TODO:这里应该是动态取得,参考出立架getULineExit()方法,不然不能从站获得对应的出口,而且要改Station的child为children
  289. '',
  290. $stationTaskMaterialBox['stationTaskBatch']['id']
  291. );
  292. LogService::log('海柔请求','putBinToStore3',
  293. json_encode($dataToPost));
  294. $controlSuccess = $this->controlHaiRobot($dataToPost,collect([$stationTaskMaterialBox_toStore]),'输送线入立架');
  295. return $controlSuccess;
  296. }
  297. /** 缓存架入立架 料箱 任务
  298. * @param StationTaskMaterialBox|null $stationTaskMaterialBox
  299. * @param string|null $formLocation
  300. * @return bool
  301. * @throws ErrorException
  302. */
  303. public function putBinToStore_fromCacheShelf(?StationTaskMaterialBox $stationTaskMaterialBox,
  304. string $formLocation): bool
  305. {
  306. LogService::log('海柔请求','putBinToStore_fromCacheShelf1', '');
  307. LogService::log('海柔请求','putBinToStore_fromCacheShelf2', json_encode($stationTaskMaterialBox));
  308. $dataToPost=$this->makeJson_move(
  309. collect([$stationTaskMaterialBox]),
  310. '缓存架入立架',
  311. $formLocation,
  312. ''
  313. );
  314. LogService::log('海柔请求','putBinToStore_fromCacheShelf3', json_encode($dataToPost));
  315. $controlSuccess = $this->controlHaiRobot($dataToPost,collect([$stationTaskMaterialBox]),'缓存架入立架');
  316. LogService::log('海柔请求','putBinToStore_fromCacheShelf4', 'controlHaiRobot '. json_encode($controlSuccess));
  317. if($controlSuccess){
  318. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  319. $this->stationTaskMaterialBoxService->set($stationTaskMaterialBox,[
  320. 'status' => '处理中',
  321. ]);
  322. LogService::log('海柔请求','putBinToStore_fromCacheShelf5', 'controlHaiRobot '. json_encode($stationTaskMaterialBox));
  323. }
  324. return $controlSuccess;
  325. }
  326. public function taskUpdate(
  327. // $groupCode,
  328. $stationTaskMaterialBox_id, //实际对应传入的字段 taskCode, 这里用料箱任务号做taskCode
  329. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  330. $status, //0:任务成功1:任务失败
  331. $binCode
  332. ):bool{
  333. LogService::log('海柔请求','taskUpdateIn',
  334. json_encode([
  335. $stationTaskMaterialBox_id,
  336. $updateEventType,
  337. $status,
  338. $binCode
  339. ]));
  340. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  341. try{
  342. if(($failed
  343. =$status)==1){
  344. throw new ErrorException('海柔任务失败');
  345. }
  346. if($料箱不匹配=
  347. !$stationTaskMaterialBox
  348. =(function()use($stationTaskMaterialBox_id,$binCode){
  349. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$stationTaskMaterialBox_id);
  350. if($stationTaskMaterialBox['materialBox']['code']==$binCode)return $stationTaskMaterialBox;
  351. return null;
  352. })()){
  353. throw new ErrorException('发回的料箱和任务号(ID)不匹配:$stationTaskMaterialBox_id:'
  354. .$stationTaskMaterialBox_id.' $binCode:'.$binCode. ' '.
  355. StationTaskMaterialBox::query()
  356. ->where('id', $id=$stationTaskMaterialBox_id)
  357. ->get()
  358. ->toJson());
  359. }
  360. if(($标记已放置=
  361. function()use($updateEventType,$stationTaskMaterialBox){
  362. if(($isPut
  363. =$updateEventType)==1){
  364. $this->stationTaskMaterialBoxService->markHasPut($stationTaskMaterialBox);
  365. return true;
  366. }return false;
  367. })())
  368. return true;
  369. ($标记已取出=
  370. function()use($updateEventType,$stationTaskMaterialBox){
  371. if(($isGet
  372. =$updateEventType)==0){
  373. $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
  374. }
  375. })();
  376. }catch (\Exception $e){
  377. $this->excepted($stationTaskMaterialBox_id, $binCode, $e->getMessage());
  378. return false;
  379. }
  380. return true;
  381. }
  382. // public function markHasPut($taskCode,$binCode):bool{
  383. // try{
  384. // //标记料箱进入位置
  385. //// $taskMaterialBoxesService->markDone();//
  386. // }catch (\Exception $e){
  387. // switch ($e->getCode()){
  388. // case 'taskBinNotMatch';
  389. // case 'taskGetFailed';
  390. // }
  391. // }
  392. //
  393. // }
  394. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  395. try{
  396. throw new ErrorException(
  397. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  398. );
  399. }catch (\Exception $e){
  400. return true;
  401. }
  402. }
  403. /**
  404. * @param array $dataToPost
  405. * @return bool
  406. */
  407. public function controlHaiRobot(array $dataToPost,Collection $taskMaterialBoxes,$modeName): bool
  408. {
  409. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c1:'.json_encode($dataToPost));
  410. try{
  411. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c1.51:');
  412. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  413. if(isset($response->json()['code'])&&$response->json()['code']==500)
  414. throw new ErrorException('机器人500错误:'.json_encode($response->json()));
  415. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c1.52:');
  416. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.53:'.json_encode($response->json()));
  417. }catch (\Exception $e){
  418. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c1.54:'.json_encode($dataToPost).$e->getMessage());
  419. throw new ErrorException('海柔机器人任务执行失败:'.json_encode($dataToPost).$e->getMessage());
  420. }
  421. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c2:'.json_encode($dataToPost));
  422. $errMsg = (function () use ($response) {
  423. if ($response->ok()) return '';
  424. $errMsg = '错误: ';
  425. if (!$response) {
  426. return $errMsg . '没有返回内容,检查连接或目标服务器';
  427. }
  428. switch (((string)$response["code"])[0]) {
  429. case 5:
  430. $errMsg .= '目标服务器代码错误,请联系对方';
  431. break;
  432. case 4:
  433. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  434. break;
  435. default:
  436. $errMsg .= '出现未知请求错误';
  437. break;
  438. }
  439. $responseDetails = ' code:' . $response["code"]
  440. . ' header:' . $response->body()
  441. . ' response:' . json_encode($response->headers());
  442. return $errMsg . $responseDetails;
  443. })();
  444. LogService::log('海柔请求','runMany','波次任务分配6.r5f2c3:'.json_encode($errMsg));
  445. LogService::log(__METHOD__, __FUNCTION__,
  446. $errMsg ?? ''
  447. . '请求:' . json_encode($dataToPost)
  448. . '调用堆栈c:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  449. );
  450. $isSuccess = !$errMsg;
  451. $标记料箱状态=(function() use ($taskMaterialBoxes,$modeName){
  452. foreach ($taskMaterialBoxes as $taskMaterialBox){
  453. switch ($modeName){
  454. case '缓存架入立架':
  455. case '输送线入立架':
  456. case '移动立架内位置':
  457. $taskMaterialBox->materialBox['status']='在入库中';break;
  458. case '立架出至输送线':
  459. case '立架出至缓存架':
  460. $taskMaterialBox->materialBox['status']='在出库中';break;
  461. default:
  462. $taskMaterialBox->materialBox['status']='未知';break;
  463. }
  464. $taskMaterialBox->materialBox->update();
  465. }
  466. })();
  467. return $isSuccess;
  468. }
  469. /**
  470. * put cache rack box to warehousing(将缓存架料箱入库)
  471. *
  472. * @param string $fromLocation
  473. * @param string $toLocation
  474. * @param integer $boxId
  475. *
  476. * @throws ErrorException
  477. *
  478. * @return bool
  479. */
  480. public function putWareHousing(string $fromLocation, string $toLocation, $boxId):bool
  481. {
  482. $modelName = '缓存架入立架';
  483. $station = Station::query()->select("id")->where("code",$fromLocation)->first();
  484. if (!$station)return false;
  485. /** @var \stdClass $task */
  486. $task = StationTask::query()->create([
  487. 'status' => "待处理",
  488. 'station_id' => $station->id,
  489. ]);
  490. $collection = new Collection();
  491. $collection->add(StationTaskMaterialBox::query()->create([
  492. 'station_id' => $station->id,
  493. 'material_box_id'=>$boxId,
  494. 'status'=>"待处理",
  495. 'type' => '放',
  496. 'station_task_id' => $task->id,
  497. ]));
  498. $dataToPost = $this->makeJson_move($collection,$modelName,$fromLocation,$toLocation);
  499. return $this->controlHaiRobot($dataToPost,$collection,$modelName);
  500. }
  501. }