ForeignHaiRoboticsService.php 8.8 KB

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