ForeignHaiRoboticsService.php 6.9 KB

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