zhubaomin
6 天以前 00d54db361d697cf8fdf2f15940f4650b21b3185
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
109
110
111
112
113
114
115
116
package com.dy.pipIrrMwTestWeb.p206V202404test;
 
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.CommandType;
import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404;
import com.dy.common.mw.protocol.p206V202404.downVos.ComCdXyVo;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
/**
 * @Author: liurunyu
 * @Date: 2024/5/14 17:32
 * @Description
 */
public class ComSupportP206V202404 {
    //protected static final String mwIp = "romq9311819.vicp.fun" ; //http://romq9311819.vicp.fun:16583
    //protected static final String mwPort = "16583" ;
    protected static final String mwIp = "127.0.0.1" ;
    protected static final String mwPort = "65533" ;
 
    protected static String mwUrlTest = "http://" + mwIp + ":" + mwPort + "/rtuMw/com/test" ;
    protected static String mwUrlSendCom = "http://" + mwIp + ":" + mwPort + "/rtuMw/com/send" ;
 
    protected static String rtuAddr = "37142501020100229" ;
    protected static String rtuResultSendWebUrl = "http://127.0.0.1:65535/test/comRes/receive" ;
 
    protected static String controllerType = "02" ;//控制器类型 57水泵,02阀门
    protected static Integer projectNo = 10 ;//项目编码
 
    protected static String icCardAddr = "04BEA5BB" ;//IC卡地址
    protected static String icCardNo = "37142501020500001" ;//IC卡编号(用户卡序列号)
 
 
    @Autowired
    private RestTemplate restTemplate;
 
 
    protected ComCdXyVo comCdXyVo(){
        ComCdXyVo comVo = new ComCdXyVo() ;
        comVo.controllerType = controllerType ;
        comVo.projectNo = projectNo  ;
        return comVo ;
    }
 
    protected Command command(String code, Object param, String comId){
        Command com = new Command() ;
        com.id = comId==null?Command.defaultId:(comId.trim().equals("")?Command.defaultId:comId) ;//实际应用中,替换成数据库记录id
        com.protocol = ProtocolConstantV206V202404.protocolName ;
        com.code = code ;
        com.rtuAddr = rtuAddr ;
        com.type = CommandType.outerCommand ;
        com.rtuResultSendWebUrl = rtuResultSendWebUrl ;
 
        com.param = param ;
 
        return com ;
    }
 
    /**
     * 连接通信中间件测试
     * @return
     */
    protected BaseResponse sendTest(){
        String url = UriComponentsBuilder.fromUriString(mwUrlTest)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<?> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Get方式调用接口
            response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常");
        }
        if(response == null){
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常");
        }else{
            return response.getBody();
        }
    }
 
    /**
     * 发送命令
     * @return
     */
    protected BaseResponse sendCom2Mw(Command com){
        String url = UriComponentsBuilder.fromUriString(mwUrlSendCom)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<Command> httpEntity = new HttpEntity<>(com, headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Post方式调用接口
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常");
        }
        if(response == null){
            return BaseResponseUtils.buildError("后端系统出错,中间件调用异常");
        }else{
            return response.getBody();
        }
    }
 
}