ForeignHaiRoboticsService.php 20 KB

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