ForeignHaiRoboticsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 $stationService StationService */
  23. private $stationService;
  24. public function __construct()
  25. {
  26. $this->stationTaskMaterialBoxService=null;
  27. $this->materialBoxService=null;
  28. $this->stationService=null;
  29. }
  30. /**
  31. * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
  32. * @param string $fromLocation
  33. * @param string $toLocation
  34. * @param Collection $taskMaterialBoxes
  35. * @param string $groupId
  36. * @param int $priority
  37. * @param int $isSequenced
  38. * @return array
  39. */
  40. private function makeJson_move(
  41. Collection $taskMaterialBoxes,
  42. string $modeName,
  43. string $fromLocation='',
  44. string $toLocation='',
  45. $groupId=''
  46. , $priority=10
  47. , $isSequenced=1
  48. ): array
  49. {
  50. $timestampSuffix = microtime(true);
  51. $taskMode=(function()use($modeName){
  52. switch ($modeName){
  53. case '输送线入立架': return 1;
  54. case '立架出至输送线': return 2;
  55. case '移动立架内位置': return 3;
  56. case '缓存架入立架':
  57. case '立架出至缓存架': return 6;
  58. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  59. }
  60. })();
  61. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
  62. return [
  63. "taskCode" =>$taskMaterialBox['id'],
  64. "binCode" => $taskMaterialBox['materialBox']['code'],
  65. "fromLocCode" => $fromLocation??'',
  66. "toLocCode" => $toLocation??'',
  67. ];
  68. });
  69. return [
  70. "taskMode" =>$taskMode,
  71. "bins"=>$bins,
  72. "groupCode"=>$groupId.$timestampSuffix,
  73. "priority"=>$priority,
  74. "sequenceFlag"=>$isSequenced,
  75. ];
  76. }
  77. public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix=''): bool
  78. {
  79. $dataToPost=$this->makeJson_move(
  80. $taskMaterialBoxes,
  81. '立架出至输送线',
  82. '',
  83. $toLocation??'',
  84. $groupIdPrefix
  85. );
  86. return $this->controlHaiRobot($dataToPost);
  87. }
  88. public function moveBin(){
  89. }
  90. public function markBinProcessed(
  91. $workStation,
  92. $binCode,
  93. $success,
  94. $created_at,
  95. $exception,
  96. $is_in_plan
  97. ): bool
  98. {
  99. $this->instant($this->stationService,'StationService');
  100. $this->instant($this->materialBoxService,'MaterialBoxService');
  101. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  102. $this->instant($this->stationTaskCommoditiesService,'StationTaskCommodityService');
  103. try{
  104. if($failed
  105. =!$success)
  106. throw new ErrorException('海柔任务失败:'.$exception);
  107. if($NotInPlan
  108. =!$is_in_plan)
  109. throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  110. $materialBox=
  111. $this->materialBoxService->get(['code'=>$binCode])->first();
  112. $stationTaskMaterialBox
  113. =(function()use($materialBox){
  114. return $stationTaskMaterialBox=
  115. $this->stationTaskMaterialBoxService
  116. ->get(
  117. [
  118. 'material_box_id'=>$materialBox['id'],
  119. 'status'=>['处理中','待处理','异常'],
  120. ]
  121. )->first();
  122. })();
  123. if(!$stationTaskMaterialBox){
  124. throw new ErrorException('该料箱没有安排在处理队列中');
  125. }
  126. $this->putBinToStore($stationTaskMaterialBox)
  127. ?true
  128. :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
  129. $this->stationTaskMaterialBoxService
  130. ->markProcessed($stationTaskMaterialBox);
  131. $stationTaskMaterialBox_next=
  132. $this->stationTaskMaterialBoxService
  133. ->processNextQueued($stationTaskMaterialBox); //找到队列中下一个料箱,并标记为处理中
  134. $this->stationTaskCommoditiesService
  135. ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
  136. if($stationTaskMaterialBox_next)
  137. $this->stationTaskCommoditiesService
  138. ->markProcessing($stationTaskMaterialBox_next['stationTaskCommodities']);//因为上边商品任务被标记完成了,所以这里要将队列中找出正在处理的料箱对应的标记为“处理中”
  139. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  140. return true;
  141. }catch (\Exception $e){
  142. $stationTaskMaterialBox = $stationTaskMaterialBox??$materialBox??null;
  143. if($stationTaskMaterialBox && get_class($stationTaskMaterialBox)==MaterialBox::class){
  144. $stationTaskMaterialBox = StationTaskMaterialBox::query()
  145. ->where('material_box_id',$stationTaskMaterialBox['id'])
  146. ->where('status','<>','完成')
  147. ->where('created_at','>',now()->format('Y-m-d'))
  148. ->first();
  149. }
  150. if($stationTaskMaterialBox)
  151. $this->stationTaskMaterialBoxService
  152. ->excepted($stationTaskMaterialBox);
  153. return $e->getMessage();
  154. }
  155. }
  156. public function putBinToStore(?StationTaskMaterialBox $stationTaskMaterialBox): bool
  157. {
  158. $this->instant($this->stationService,'StationService');
  159. $dataToPost=$this->makeJson_move(
  160. collect([$stationTaskMaterialBox]),
  161. '缓存架入立架',
  162. $this->stationService->getULineExit($stationTaskMaterialBox['station'])['name']??'',
  163. '',
  164. $stationTaskMaterialBox['stationTaskBatch']['id']
  165. );
  166. return $this->controlHaiRobot($dataToPost);
  167. }
  168. public function taskUpdate(
  169. // $groupCode,
  170. $stationTaskMaterialBox_id, //实际对应传入的字段 taskCode, 这里用料箱任务号做taskCode
  171. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  172. $status, //0:任务成功1:任务失败
  173. $binCode
  174. ):bool{
  175. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  176. try{
  177. if(($failed
  178. =$status)==1){
  179. throw new ErrorException('海柔任务失败');
  180. }
  181. if($料箱不匹配=
  182. !$stationTaskMaterialBox
  183. =(function()use($stationTaskMaterialBox_id,$binCode){
  184. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$stationTaskMaterialBox_id);
  185. if($stationTaskMaterialBox['materialBox']['code']==$binCode)return $stationTaskMaterialBox;
  186. return null;
  187. })()){
  188. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  189. }
  190. ($标记已放置在库外=
  191. function()use($updateEventType,$stationTaskMaterialBox){
  192. if(($isPut
  193. =$updateEventType)==0){
  194. $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
  195. }
  196. })();
  197. ($标记已入立架=
  198. function()use($updateEventType,$stationTaskMaterialBox){
  199. if(($isGet
  200. =$updateEventType)==1){
  201. $this->stationTaskMaterialBoxService->markPutStored($stationTaskMaterialBox);
  202. }
  203. })();
  204. }catch (\Exception $e){
  205. $this->excepted($stationTaskMaterialBox_id, $binCode, $e->getMessage());
  206. return false;
  207. }
  208. return true;
  209. }
  210. // public function markHasPut($taskCode,$binCode):bool{
  211. // try{
  212. // //标记料箱进入位置
  213. //// $taskMaterialBoxesService->markDone();//
  214. // }catch (\Exception $e){
  215. // switch ($e->getCode()){
  216. // case 'taskBinNotMatch';
  217. // case 'taskGetFailed';
  218. // }
  219. // }
  220. //
  221. // }
  222. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  223. try{
  224. throw new ErrorException(
  225. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  226. );
  227. }catch (\Exception $e){
  228. return true;
  229. }
  230. }
  231. /**
  232. * @param array $dataToPost
  233. * @return bool
  234. */
  235. public function controlHaiRobot(array $dataToPost): bool
  236. {
  237. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  238. $errMsg = (function () use ($response) {
  239. if ($response->ok()) return '';
  240. $errMsg = '错误: ';
  241. if (!$response) {
  242. return $errMsg . '没有返回内容,检查连接或目标服务器';
  243. }
  244. switch (((string)$response["code"])[0]) {
  245. case 5:
  246. $errMsg .= '目标服务器代码错误,请联系对方';
  247. break;
  248. case 4:
  249. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  250. break;
  251. default:
  252. $errMsg .= '出现未知请求错误';
  253. break;
  254. }
  255. $responseDetails = ' code:' . $response["code"]
  256. . ' header:' . $response->body()
  257. . ' response:' . json_encode($response->headers());
  258. return $errMsg . $responseDetails;
  259. })();
  260. LogService::log(__METHOD__, __FUNCTION__,
  261. $errMsg ?? ''
  262. . '请求:' . json_encode($dataToPost)
  263. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  264. );
  265. return $isSuccess = !$errMsg;
  266. }
  267. }