ForeignHaiRoboticsService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. $this->stationTaskCommoditiesService
  132. ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
  133. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  134. return true;
  135. }catch (\Exception $e){
  136. $stationTaskMaterialBox = $stationTaskMaterialBox??$materialBox??null;
  137. if($stationTaskMaterialBox && get_class($stationTaskMaterialBox)==MaterialBox::class){
  138. $stationTaskMaterialBox = StationTaskMaterialBox::query()
  139. ->where('material_box_id',$stationTaskMaterialBox['id'])
  140. ->where('status','<>','完成')
  141. ->where('created_at','>',now()->format('Y-m-d'))
  142. ->first();
  143. }
  144. if($stationTaskMaterialBox)
  145. $this->stationTaskMaterialBoxService
  146. ->excepted($stationTaskMaterialBox);
  147. return $e->getMessage();
  148. }
  149. }
  150. public function putBinToStore(?StationTaskMaterialBox $stationTaskMaterialBox): bool
  151. {
  152. $this->instant($this->stationService,'StationService');
  153. $dataToPost=$this->makeJson_move(
  154. collect([$stationTaskMaterialBox]),
  155. '缓存架入立架',
  156. $this->stationService->getULineExit($stationTaskMaterialBox['station'])['name']??'',
  157. '',
  158. $stationTaskMaterialBox['stationTaskBatch']['id']
  159. );
  160. return $this->controlHaiRobot($dataToPost);
  161. }
  162. public function taskUpdate(
  163. // $groupCode,
  164. $stationTaskMaterialBox_id, //实际对应传入的字段 taskCode, 这里用料箱任务号做taskCode
  165. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  166. $status, //0:任务成功1:任务失败
  167. $binCode
  168. ):bool{
  169. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  170. try{
  171. if(($failed
  172. =$status)==1){
  173. throw new ErrorException('海柔任务失败');
  174. }
  175. if($料箱不匹配=
  176. !$stationTaskMaterialBox
  177. =(function()use($stationTaskMaterialBox_id,$binCode){
  178. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$stationTaskMaterialBox_id);
  179. if($stationTaskMaterialBox['materialBox']['code']==$binCode)return $stationTaskMaterialBox;
  180. return null;
  181. })()){
  182. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  183. }
  184. ($标记已放置在库外=
  185. function()use($updateEventType,$stationTaskMaterialBox){
  186. if(($isPut
  187. =$updateEventType)==0){
  188. $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
  189. }
  190. })();
  191. ($标记已入立架=
  192. function()use($updateEventType,$stationTaskMaterialBox){
  193. if(($isGet
  194. =$updateEventType)==1){
  195. $this->stationTaskMaterialBoxService->markPutStored($stationTaskMaterialBox);
  196. }
  197. })();
  198. }catch (\Exception $e){
  199. $this->excepted($stationTaskMaterialBox_id, $binCode, $e->getMessage());
  200. return false;
  201. }
  202. return true;
  203. }
  204. // public function markHasPut($taskCode,$binCode):bool{
  205. // try{
  206. // //标记料箱进入位置
  207. //// $taskMaterialBoxesService->markDone();//
  208. // }catch (\Exception $e){
  209. // switch ($e->getCode()){
  210. // case 'taskBinNotMatch';
  211. // case 'taskGetFailed';
  212. // }
  213. // }
  214. //
  215. // }
  216. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  217. try{
  218. throw new ErrorException(
  219. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  220. );
  221. }catch (\Exception $e){
  222. return true;
  223. }
  224. }
  225. /**
  226. * @param array $dataToPost
  227. * @return bool
  228. */
  229. public function controlHaiRobot(array $dataToPost): bool
  230. {
  231. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  232. $errMsg = (function () use ($response) {
  233. if ($response->ok()) return '';
  234. $errMsg = '错误: ';
  235. if (!$response) {
  236. return $errMsg . '没有返回内容,检查连接或目标服务器';
  237. }
  238. switch (((string)$response["code"])[0]) {
  239. case 5:
  240. $errMsg .= '目标服务器代码错误,请联系对方';
  241. break;
  242. case 4:
  243. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  244. break;
  245. default:
  246. $errMsg .= '出现未知请求错误';
  247. break;
  248. }
  249. $responseDetails = ' code:' . $response["code"]
  250. . ' header:' . $response->body()
  251. . ' response:' . json_encode($response->headers());
  252. return $errMsg . $responseDetails;
  253. })();
  254. LogService::log(__METHOD__, __FUNCTION__,
  255. $errMsg ?? ''
  256. . '请求:' . json_encode($dataToPost)
  257. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  258. );
  259. return $isSuccess = !$errMsg;
  260. }
  261. }