zhubaomin
2024-11-12 39be193dece5ad6314aa22a41a2c8675e8057be3
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
117
118
119
120
package com.dy.pipIrrGlobal.rtuMw;
 
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.mw.protocol.Command;
import com.dy.common.mw.protocol.CommandType;
import com.dy.common.softUpgrade.state.UpgradeTaskVo;
import com.dy.common.webUtil.BaseResponse;
import org.springframework.core.env.Environment;
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/10/23 11:45
 * @Description
 */
public abstract class ToRtuMwCom {
    /**
     * pro_mw:属性
     * tag从控制器中获取
     * key_mw:url的key
     */
    private static final String pro_mw = "mw";
    private static final String key_mw = "comSendUrl";
    private static final String keyUg_mw = "ugTaskSendUrl";
 
 
    /**
     * 得到向通信中间件发送数据的URL
     * @param env
     * @return
     */
    protected String getToMwUrl(Environment env) {
        return env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw);
    }
 
    /**
     * 得到向通信中间件发送数据的URL
     * @param env
     * @return
     */
    protected String getToMwUgUrl(Environment env) {
        return env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + keyUg_mw);
    }
 
    /**
     * 创建外部命令(发给控制器)
     * @param code 命令code
     * @return
     */
    protected Command createOuterCommand(String comId, String code) {
        Command com = new Command();
        com.id = comId;
        com.code = code ;
        com.type = CommandType.outerCommand;
        return com ;
    }
 
    /**
     * 创建内部
     * @param code 命令code
     * @return
     */
    protected Command createInnerCommand(String code) {
        Command com = new Command();
        com.id = Command.defaultId;
        com.code = code ;
        com.type = CommandType.innerCommand;
        return com ;
    }
 
    /**
     * 发送命令
     *
     * @return
     */
    protected BaseResponse sendCom2Mw(RestTemplate restTemplate, String comSendUrl, Command com) {
        String url = UriComponentsBuilder.fromUriString(comSendUrl)
                .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 response.getBody();
    }
 
 
    /**
     * 向通信中间件发送rtu远程升级任务
     * @param restTemplate
     * @param comSendUrl
     * @param vo
     * @return
     */
    protected BaseResponse sendUpgradeTask2Mw(RestTemplate restTemplate, String comSendUrl, UpgradeTaskVo vo) {
        String url = UriComponentsBuilder.fromUriString(comSendUrl)
                .build()
                .toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<UpgradeTaskVo> httpEntity = new HttpEntity<>(vo, headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Post方式调用接口
            response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.getBody();
    }
}