ForeignHaiRoboticsService.php 19 KB

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