ForeignHaiRoboticsService.php 22 KB

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