ForeignHaiRoboticsService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\Facades\Http;
  10. use Ramsey\Collection\Collection;
  11. class ForeignHaiRoboticsService
  12. {
  13. /** @var $stationTaskMaterialBoxService StationTaskMaterialBoxService */
  14. private $stationTaskMaterialBoxService;
  15. public function __construct()
  16. {
  17. $stationTaskMaterialBoxService=null;
  18. }
  19. /**
  20. * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
  21. * @param string $fromLocation
  22. * @param string $toLocation
  23. * @param Collection $taskMaterialBoxes
  24. * @param string $groupId
  25. * @param int $priority
  26. * @param int $isSequenced
  27. * @return array
  28. */
  29. private function makeJson_move(
  30. Collection $taskMaterialBoxes,
  31. string $modeName,
  32. string $fromLocation='',
  33. string $toLocation='',
  34. $groupId=''
  35. , $priority=10
  36. , $isSequenced=1
  37. ){
  38. $timestampSuffix = microtime(true);
  39. $taskMode=(function()use($modeName){
  40. switch ($modeName){
  41. case '输送线入立架': return 1;
  42. case '立架出至输送线': return 2;
  43. case '移动立架内位置': return 3;
  44. case '缓存架入立架':
  45. case '立架出至缓存架': return 6;
  46. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  47. }
  48. })();
  49. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
  50. return [
  51. "taskCode" =>$taskMaterialBox['id'],
  52. "binCode" => $taskMaterialBox['materialBox']['code'],
  53. "fromLocCode" => $fromLocation??'',
  54. "toLocCode" => $toLocation??'',
  55. ];
  56. });
  57. return [
  58. "taskMode" =>$taskMode,
  59. "bins"=>$bins,
  60. "groupCode"=>$groupId.$timestampSuffix,
  61. "priority"=>$priority,
  62. "sequenceFlag"=>$isSequenced,
  63. ];
  64. }
  65. public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix=''): bool
  66. {
  67. $dataToPost=$this->makeJson_move(
  68. $taskMaterialBoxes,
  69. '立架出至输送线',
  70. '',
  71. $toLocation,
  72. $groupIdPrefix
  73. );
  74. $response = Http::post(config('api.haiq.storage.moveBin'),$dataToPost);
  75. $errMsg=(function()use($response){
  76. if($response->ok())return '';
  77. $errMsg = '错误: ';
  78. if (!$response){
  79. return $errMsg.'没有返回内容,检查连接或目标服务器';
  80. }
  81. switch (((string)$response["code"])[0]){
  82. case 5: $errMsg.='目标服务器代码错误,请联系对方';break;
  83. case 4: $errMsg.='权限不足以请求资源,请检查对方服务器规范';break;
  84. default: $errMsg.='出现未知请求错误';break;
  85. }
  86. $responseDetails=' code:'.$response["code"]
  87. .' header:'.$response->body()
  88. .' response:'.json_encode($response->headers());
  89. return $errMsg.$responseDetails;
  90. })();
  91. LogService::log(__METHOD__,__FUNCTION__,
  92. $errMsg??''
  93. .'请求:'.json_encode($dataToPost)
  94. .'调用堆栈:'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3))
  95. );
  96. return $isSuccess=!$errMsg;
  97. }
  98. public function moveBin(){
  99. }
  100. public function markBinProcessed(){
  101. }
  102. public function taskUpdate(
  103. // $groupCode,
  104. $taskCode,
  105. $updateEventType, //0:task_begin(取货)1:task_end(放货)
  106. $status, //0:任务成功1:任务失败
  107. $binCode
  108. ):bool{
  109. try{
  110. if(($failed
  111. =$status)==1){
  112. throw new ErrorException('海柔任务失败');
  113. }
  114. if($stationTaskMaterialBox
  115. =(function()use($taskCode,$binCode){
  116. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($taskCode);
  117. if($stationTaskMaterialBox['code']==$binCode)return $stationTaskMaterialBox;
  118. return null;
  119. })()){
  120. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  121. };
  122. if(($isPut
  123. =$updateEventType)==0){
  124. $this->stationTaskMaterialBoxService->markHasPut($stationTaskMaterialBox);
  125. }
  126. if(($isGet
  127. =$updateEventType)==1){
  128. $this->stationTaskMaterialBoxService->markHasTaken($stationTaskMaterialBox);
  129. }
  130. }catch (\Exception $e){
  131. $this->excepted($taskCode, $binCode);
  132. return false;
  133. }
  134. return true;
  135. }
  136. // public function markHasPut($taskCode,$binCode):bool{
  137. // try{
  138. // //标记料箱进入位置
  139. //// $taskMaterialBoxesService->markDone();//
  140. // }catch (\Exception $e){
  141. // switch ($e->getCode()){
  142. // case 'taskBinNotMatch';
  143. // case 'taskGetFailed';
  144. // }
  145. // }
  146. //
  147. // }
  148. public function excepted($taskCode,$binCode):bool{
  149. }
  150. }