ForeignHaiRoboticsService.php 16 KB

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