ForeignHaiRoboticsService.php 18 KB

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