Administrator
2024-05-20 ba262a5e8ce59bcf661a40ce33bba60e4a0a2400
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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<String> 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<Boolean> 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<String, Object> param = new HashMap<>();
        param.put("controllerType", "01");
        param.put("projectNo", 100);
        param.put("rtuNewAddr", "202405061656120001");
 
        Map<String, Object> 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<String, String> 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";
    }
}