package com.dy.pipIrrRemote.valve; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.aop.SsoAop; import com.dy.common.webUtil.BaseResponse; import com.dy.common.webUtil.BaseResponseUtils; import com.dy.common.webUtil.ResultCodeMsg; import com.dy.pipIrrRemote.utils.RestTemplateUtils; import com.dy.pipIrrRemote.valve.dto.DTOValve; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * @author ZhuBaoMin * @date 2024-05-07 14:59 * @LastEditTime 2024-05-07 14:59 * @Description */ @Slf4j @Tag(name = "分水房管理", description = "分水房操作") @RestController @RequestMapping(path="valve") @RequiredArgsConstructor public class ValveCtrl { private final RestTemplateUtils restTemplateUtils; private CompletableFuture futureValue = new CompletableFuture<>(); /** * 远程开关阀 * @param po 开关阀传入对象 * @param bindingResult * @return */ @Operation(summary = "远程开关阀", description = "远程开关阀") @ApiResponses(value = { @ApiResponse( responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, description = "操作结果:true:成功,false:失败(BaseResponse.content)", content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Boolean.class))} ) }) @PostMapping(path = "operate", consumes = MediaType.APPLICATION_JSON_VALUE) @Transactional(rollbackFor = Exception.class) @SsoAop() public BaseResponse open(@RequestBody @Valid DTOValve po, BindingResult bindingResult) throws ExecutionException, InterruptedException { if(bindingResult != null && bindingResult.hasErrors()){ return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); } String a = null; try { a = futureValue.get(10, TimeUnit.SECONDS); } catch (TimeoutException e) { return BaseResponseUtils.buildFail("1分钟后去查看结果"); } futureValue = new CompletableFuture<>(); Map param = new HashMap<>(); param.put("controllerType", "01"); param.put("projectNo", 100); param.put("rtuNewAddr", "202405061656120001"); Map postParams = new HashMap<>(); postParams.put("id", 2024050616450001L); postParams.put("protocol", "p1"); postParams.put("rtuAddr", "20001"); postParams.put("type", "outerCommand"); postParams.put("code", "10"); postParams.put("noRtMwDealRes", false); postParams.put("rtuResultSendWebUrl", "127.0.0.1/remote/"); postParams.put("param", param); Map headerParams = new HashMap<>(); JSONObject job_result = restTemplateUtils.post("http://localhost:8070/accMw/com/send", JSON.toJSONString(postParams), headerParams); return BaseResponseUtils.buildSuccess(a) ; } @GetMapping("/setValue") public String setValue(String name) { futureValue.complete(name); return "Value set"; } }