ForeignHaiRoboticsService.php 22 KB

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