ForeignHaiRoboticsService.php 9.1 KB

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