ForeignHaiRoboticsService.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. try{
  100. if($failed
  101. =!$success)
  102. throw new ErrorException('海柔任务失败:'.$exception);
  103. if($NotInPlan
  104. =!$is_in_plan)
  105. throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  106. $materialBox=
  107. $this->materialBoxService->get(['code'=>$binCode]);
  108. if($stationTaskMaterialBox
  109. =(function()use($materialBox){
  110. return $stationTaskMaterialBox=
  111. $this->stationTaskMaterialBoxService
  112. ->get(
  113. [
  114. 'material_box_id'=>$materialBox['id'],
  115. 'status'=>'处理中',
  116. ],
  117. 'stationTaskCommodities'
  118. )->first();
  119. })()){
  120. throw new ErrorException('该料箱没有安排在处理队列中');
  121. }
  122. $this->putBinToStore($stationTaskMaterialBox)
  123. ?true
  124. :(function(){throw new ErrorException('呼叫机器人回收U型线料箱失败');})();
  125. $this->stationTaskMaterialBoxService
  126. ->markProcessed($stationTaskMaterialBox);
  127. $this->stationTaskCommoditiesService
  128. ->markProcessed($stationTaskMaterialBox['stationTaskCommodities']);
  129. return true;
  130. }catch (\Exception $e){
  131. $this->stationTaskMaterialBoxService
  132. ->excepted($stationTaskMaterialBox??$materialBox??null);
  133. return $e->getMessage();
  134. }
  135. }
  136. public function putBinToStore(StationTaskMaterialBox $stationTaskMaterialBox): bool
  137. {
  138. $this->instant($this->stationService,'StationService');
  139. $dataToPost=$this->makeJson_move(
  140. collect($stationTaskMaterialBox),
  141. '缓存架入立架',
  142. $this->stationService->getULineExit($stationTaskMaterialBox['station'])['name']??'',
  143. '',
  144. $stationTaskMaterialBox['stationTaskBatch']['id']
  145. );
  146. return $this->controlHaiRobot($dataToPost);
  147. }
  148. public function taskUpdate(
  149. // $groupCode,
  150. $taskCode,
  151. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  152. $status, //0:任务成功1:任务失败
  153. $binCode
  154. ):bool{
  155. try{
  156. if(($failed
  157. =$status)==1){
  158. throw new ErrorException('海柔任务失败');
  159. }
  160. if($boxNotMatch=
  161. !$stationTaskMaterialBox
  162. =(function()use($taskCode,$binCode){
  163. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($id=$taskCode);
  164. if($stationTaskMaterialBox['code']==$binCode)return $stationTaskMaterialBox;
  165. return null;
  166. })()){
  167. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  168. }
  169. ($标记已放置在库外=
  170. function()use($updateEventType,$stationTaskMaterialBox){
  171. if(($isPut
  172. =$updateEventType)==1){
  173. $this->stationTaskMaterialBoxService->markHasPut($stationTaskMaterialBox);
  174. }
  175. })();
  176. ($标记已入立架=
  177. function()use($updateEventType,$stationTaskMaterialBox){
  178. if(($isGet
  179. =$updateEventType)==0){
  180. $this->stationTaskMaterialBoxService->markTaken($stationTaskMaterialBox);
  181. }
  182. })();
  183. }catch (\Exception $e){
  184. $this->excepted($taskCode, $binCode, $e->getMessage());
  185. return false;
  186. }
  187. return true;
  188. }
  189. // public function markHasPut($taskCode,$binCode):bool{
  190. // try{
  191. // //标记料箱进入位置
  192. //// $taskMaterialBoxesService->markDone();//
  193. // }catch (\Exception $e){
  194. // switch ($e->getCode()){
  195. // case 'taskBinNotMatch';
  196. // case 'taskGetFailed';
  197. // }
  198. // }
  199. //
  200. // }
  201. public function excepted($taskCode='',$binCode='', $msg=''):bool{
  202. try{
  203. throw new ErrorException(
  204. "taskCode任务号:$taskCode , binCode箱号:$binCode 海柔运行报错: $msg"
  205. );
  206. }catch (\Exception $e){
  207. return true;
  208. }
  209. }
  210. /**
  211. * @param array $dataToPost
  212. * @return bool
  213. */
  214. public function controlHaiRobot(array $dataToPost): bool
  215. {
  216. $response = Http::post(config('api.haiq.storage.moveBin'), $dataToPost);
  217. $errMsg = (function () use ($response) {
  218. if ($response->ok()) return '';
  219. $errMsg = '错误: ';
  220. if (!$response) {
  221. return $errMsg . '没有返回内容,检查连接或目标服务器';
  222. }
  223. switch (((string)$response["code"])[0]) {
  224. case 5:
  225. $errMsg .= '目标服务器代码错误,请联系对方';
  226. break;
  227. case 4:
  228. $errMsg .= '权限不足以请求资源,请检查对方服务器规范';
  229. break;
  230. default:
  231. $errMsg .= '出现未知请求错误';
  232. break;
  233. }
  234. $responseDetails = ' code:' . $response["code"]
  235. . ' header:' . $response->body()
  236. . ' response:' . json_encode($response->headers());
  237. return $errMsg . $responseDetails;
  238. })();
  239. LogService::log(__METHOD__, __FUNCTION__,
  240. $errMsg ?? ''
  241. . '请求:' . json_encode($dataToPost)
  242. . '调用堆栈:' . json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), 0, 3))
  243. );
  244. return $isSuccess = !$errMsg;
  245. }
  246. }