ForeignHaiRoboticsService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. use App\Traits\ServiceAppAop;
  12. class ForeignHaiRoboticsService
  13. {
  14. use ServiceAppAop;
  15. /** @var $stationTaskMaterialBoxService StationTaskMaterialBoxService */
  16. private $stationTaskMaterialBoxService;
  17. /** @var $materialBoxService MaterialBoxService */
  18. private $materialBoxService;
  19. public function __construct()
  20. {
  21. $stationTaskMaterialBoxService=null;
  22. $materialBoxService=null;
  23. }
  24. /**
  25. * @param string $modeName '输送线入立架'|'立架出至输送线'|'移动立架内位置'|'缓存架入立架'|'立架出至缓存架'
  26. * @param string $fromLocation
  27. * @param string $toLocation
  28. * @param Collection $taskMaterialBoxes
  29. * @param string $groupId
  30. * @param int $priority
  31. * @param int $isSequenced
  32. * @return array
  33. */
  34. private function makeJson_move(
  35. Collection $taskMaterialBoxes,
  36. string $modeName,
  37. string $fromLocation='',
  38. string $toLocation='',
  39. $groupId=''
  40. , $priority=10
  41. , $isSequenced=1
  42. ){
  43. $timestampSuffix = microtime(true);
  44. $taskMode=(function()use($modeName){
  45. switch ($modeName){
  46. case '输送线入立架': return 1;
  47. case '立架出至输送线': return 2;
  48. case '移动立架内位置': return 3;
  49. case '缓存架入立架':
  50. case '立架出至缓存架': return 6;
  51. default: throw new \Exception('发至海柔的移料箱请求,模式不存在');
  52. }
  53. })();
  54. $bins=$taskMaterialBoxes->map(function (StationTaskMaterialBox $taskMaterialBox)use($timestampSuffix,$fromLocation,$toLocation){
  55. return [
  56. "taskCode" =>$taskMaterialBox['id'],
  57. "binCode" => $taskMaterialBox['materialBox']['code'],
  58. "fromLocCode" => $fromLocation??'',
  59. "toLocCode" => $toLocation??'',
  60. ];
  61. });
  62. return [
  63. "taskMode" =>$taskMode,
  64. "bins"=>$bins,
  65. "groupCode"=>$groupId.$timestampSuffix,
  66. "priority"=>$priority,
  67. "sequenceFlag"=>$isSequenced,
  68. ];
  69. }
  70. public function fetchGroup($toLocation, Collection $taskMaterialBoxes, $groupIdPrefix=''): bool
  71. {
  72. $dataToPost=$this->makeJson_move(
  73. $taskMaterialBoxes,
  74. '立架出至输送线',
  75. '',
  76. $toLocation,
  77. $groupIdPrefix
  78. );
  79. $response = Http::post(config('api.haiq.storage.moveBin'),$dataToPost);
  80. $errMsg=(function()use($response){
  81. if($response->ok())return '';
  82. $errMsg = '错误: ';
  83. if (!$response){
  84. return $errMsg.'没有返回内容,检查连接或目标服务器';
  85. }
  86. switch (((string)$response["code"])[0]){
  87. case 5: $errMsg.='目标服务器代码错误,请联系对方';break;
  88. case 4: $errMsg.='权限不足以请求资源,请检查对方服务器规范';break;
  89. default: $errMsg.='出现未知请求错误';break;
  90. }
  91. $responseDetails=' code:'.$response["code"]
  92. .' header:'.$response->body()
  93. .' response:'.json_encode($response->headers());
  94. return $errMsg.$responseDetails;
  95. })();
  96. LogService::log(__METHOD__,__FUNCTION__,
  97. $errMsg??''
  98. .'请求:'.json_encode($dataToPost)
  99. .'调用堆栈:'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3))
  100. );
  101. return $isSuccess=!$errMsg;
  102. }
  103. public function moveBin(){
  104. }
  105. public function markBinProcessed(
  106. $workStation,
  107. $binCode,
  108. $success,
  109. $created_at,
  110. $exception,
  111. $is_in_plan
  112. ){
  113. try{
  114. if($failed
  115. =!$success)
  116. throw new ErrorException('海柔任务失败:'.$exception);
  117. if($NotInPlan
  118. =!$is_in_plan)
  119. throw new ErrorException('海柔认为是计划外的料箱:'.$exception);
  120. $materialBox=
  121. $this->materialBoxService->get(['code'=>$binCode]);
  122. if($stationTaskMaterialBox
  123. =(function()use($materialBox){
  124. return $stationTaskMaterialBox=
  125. $this->stationTaskMaterialBoxService
  126. ->get([
  127. 'material_box_id'=>$materialBox['id'],
  128. 'status'=>'处理中',
  129. ])->first();
  130. })()){
  131. throw new ErrorException('该料箱没有安排在处理队列中');
  132. }
  133. $this->stationTaskMaterialBoxService
  134. ->markProcessed($stationTaskMaterialBox);
  135. }catch (\Exception $e){
  136. $this->stationTaskMaterialBoxService
  137. ->excepted($stationTaskMaterialBox??$materialBox??null);
  138. return false;
  139. }
  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($stationTaskMaterialBox
  154. =(function()use($taskCode,$binCode){
  155. $stationTaskMaterialBox=StationTaskMaterialBox::query()->find($taskCode);
  156. if($stationTaskMaterialBox['code']==$binCode)return $stationTaskMaterialBox;
  157. return null;
  158. })()){
  159. throw new ErrorException('发回的料箱和任务号(ID)不匹配');
  160. }
  161. if(($isPut
  162. =$updateEventType)==1){
  163. $this->stationTaskMaterialBoxService->markHasPut($stationTaskMaterialBox);
  164. }
  165. if(($isGet
  166. =$updateEventType)==0){
  167. $this->stationTaskMaterialBoxService->markTaken($stationTaskMaterialBox);
  168. }
  169. }catch (\Exception $e){
  170. $this->excepted($taskCode, $binCode);
  171. return false;
  172. }
  173. return true;
  174. }
  175. // public function markHasPut($taskCode,$binCode):bool{
  176. // try{
  177. // //标记料箱进入位置
  178. //// $taskMaterialBoxesService->markDone();//
  179. // }catch (\Exception $e){
  180. // switch ($e->getCode()){
  181. // case 'taskBinNotMatch';
  182. // case 'taskGetFailed';
  183. // }
  184. // }
  185. //
  186. // }
  187. public function excepted($taskCode,$binCode):bool{
  188. }
  189. }