ForeignHaiRoboticsService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace App\Services;
  3. use App\Exceptions\ErrorException;
  4. use App\Exceptions\PanicException;
  5. use App\MaterialBox;
  6. use App\StationTaskMaterialBox;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Response;
  9. use Illuminate\Support\Collection;
  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 $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'],
  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. $this->instant($this->stationService,'StationService');
  105. $this->instant($this->materialBoxService,'MaterialBoxService');
  106. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  107. $this->instant($this->stationTaskCommoditiesService,'StationTaskCommodityService');
  108. try{
  109. if($failed
  110. =!$success)
  111. throw new ErrorException('海柔任务失败:'.$exception);
  112. if($NotInPlan
  113. =!$is_in_plan)
  114. throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  115. $materialBox=
  116. $this->materialBoxService->get(['code'=>$binCode])->first();
  117. $stationTaskMaterialBox
  118. =(function()use($materialBox){
  119. return $stationTaskMaterialBox=
  120. $this->stationTaskMaterialBoxService
  121. ->get(
  122. [
  123. 'material_box_id'=>$materialBox['id'],
  124. 'status'=>['处理中','待处理','异常','处理队列'],
  125. ]
  126. )->first();
  127. })();
  128. if(!$stationTaskMaterialBox){
  129. throw new ErrorException('该料箱没有安排在处理队列中');
  130. }
  131. $this->putBinToStore($stationTaskMaterialBox)
  132. ?true
  133. :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
  134. $this->stationTaskMaterialBoxService
  135. ->markProcessed($stationTaskMaterialBox);
  136. $stationTaskMaterialBox_next=
  137. $this->stationTaskMaterialBoxService
  138. ->processNextQueued($stationTaskMaterialBox); //找到队列中下一个料箱,并标记为处理中
  139. $this->stationTaskCommoditiesService
  140. ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
  141. if($stationTaskMaterialBox_next)
  142. $this->stationTaskCommoditiesService
  143. ->markProcessing($stationTaskMaterialBox_next['stationTaskCommodities']);//因为上边商品任务被标记完成了,所以这里要将队列中找出正在处理的料箱对应的标记为“处理中”
  144. if(!$stationTaskMaterialBox_next){
  145. $this->instant($this->stationTaskService,'StationTaskService');
  146. $this->stationTaskService->markProcessed($stationTaskMaterialBox['stationTask']);
  147. }
  148. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  149. return true;
  150. }catch (\Exception $e){
  151. $stationTaskMaterialBox = $stationTaskMaterialBox??$materialBox??null;
  152. if($stationTaskMaterialBox && get_class($stationTaskMaterialBox)==MaterialBox::class){
  153. $stationTaskMaterialBox = StationTaskMaterialBox::query()
  154. ->where('material_box_id',$stationTaskMaterialBox['id'])
  155. ->where('status','<>','完成')
  156. ->where('created_at','>',now()->format('Y-m-d'))
  157. ->first();
  158. }
  159. if($stationTaskMaterialBox)
  160. $this->stationTaskMaterialBoxService
  161. ->excepted($stationTaskMaterialBox);
  162. return $e->getMessage();
  163. }
  164. }
  165. public function putBinToStore(?StationTaskMaterialBox $stationTaskMaterialBox): bool
  166. {
  167. $this->instant($this->stationService,'StationService');
  168. $dataToPost=$this->makeJson_move(
  169. collect([$stationTaskMaterialBox]),
  170. '缓存架入立架',
  171. $this->stationService->getULineExit($stationTaskMaterialBox['station'])['name']??'',
  172. '',
  173. $stationTaskMaterialBox['stationTaskBatch']['id']
  174. );
  175. return $this->controlHaiRobot($dataToPost);
  176. }
  177. public function taskUpdate(
  178. // $groupCode,
  179. $stationTaskMaterialBox_id, //实际对应传入的字段 taskCode, 这里用料箱任务号做taskCode
  180. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  181. $status, //0:任务成功1:任务失败
  182. $binCode
  183. ):bool{
  184. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  185. try{
  186. if(($failed
  187. =$status)==1){
  188. throw new ErrorException('海柔任务失败');
  189. }
  190. if($料箱不匹配=
  191. !$stationTaskMaterialBox
  192. =(function()use($stationTaskMaterialBox_id,$binCode){
  193. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$stationTaskMaterialBox_id);
  194. if($stationTaskMaterialBox['materialBox']['code']==$binCode)return $stationTaskMaterialBox;
  195. return null;
  196. })()){
  197. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  198. }
  199. ($标记已放置在库外=
  200. function()use($updateEventType,$stationTaskMaterialBox){
  201. if(($isPut
  202. =$updateEventType)==0){
  203. $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
  204. }
  205. })();
  206. ($标记已入立架=
  207. function()use($updateEventType,$stationTaskMaterialBox){
  208. if(($isGet
  209. =$updateEventType)==1){
  210. $this->stationTaskMaterialBoxService->markPutStored($stationTaskMaterialBox);
  211. }
  212. })();
  213. }catch (\Exception $e){
  214. $this->excepted($stationTaskMaterialBox_id, $binCode, $e->getMessage());
  215. return false;
  216. }
  217. return true;
  218. }
  219. // public function markHasPut($taskCode,$binCode):bool{
  220. // try{
  221. // //标记料箱进入位置
  222. //// $taskMaterialBoxesService->markDone();//
  223. // }catch (\Exception $e){
  224. // switch ($e->getCode()){
  225. // case 'taskBinNotMatch';
  226. // case 'taskGetFailed';
  227. // }
  228. // }
  229. //
  230. // }
  231. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  232. try{
  233. throw new ErrorException(
  234. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  235. );
  236. }catch (\Exception $e){
  237. return true;
  238. }
  239. }
  240. /**
  241. * @param array $dataToPost
  242. * @return bool
  243. */
  244. public function controlHaiRobot(array $dataToPost): bool
  245. {
  246. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1:'.json_encode($dataToPost));
  247. try{
  248. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.51:');
  249. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  250. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.52:');
  251. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.53:'.json_encode($response));
  252. }catch (\Exception $e){
  253. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c1.54:');
  254. throw new ErrorException('海柔机器人任务执行失败:'.json_encode($dataToPost));
  255. }
  256. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c2:'.json_encode($dataToPost));
  257. $errMsg = (function () use ($response) {
  258. if ($response->ok()) return '';
  259. $errMsg = '错误: ';
  260. if (!$response) {
  261. return $errMsg . '没有返回内容,检查连接或目标服务器';
  262. }
  263. switch (((string)$response["code"])[0]) {
  264. case 5:
  265. $errMsg .= '目标服务器代码错误,请联系对方';
  266. break;
  267. case 4:
  268. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  269. break;
  270. default:
  271. $errMsg .= '出现未知请求错误';
  272. break;
  273. }
  274. $responseDetails = ' code:' . $response["code"]
  275. . ' header:' . $response->body()
  276. . ' response:' . json_encode($response->headers());
  277. return $errMsg . $responseDetails;
  278. })();
  279. LogService::log(__METHOD__,'runMany','波次任务分配6.r5f2c3:'.json_encode($errMsg));
  280. LogService::log(__METHOD__, __FUNCTION__,
  281. $errMsg ?? ''
  282. . '请求:' . json_encode($dataToPost)
  283. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  284. );
  285. return $isSuccess = !$errMsg;
  286. }
  287. }