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
package com.dy.pipIrrMwTestWeb.rtuLog;
 
import com.dy.common.webUtil.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/8/28 14:58
 * @Description
 */
public class RtuLogSupport {
    protected static String mwUrlRtuLogFile = "http://127.0.0.1:8070/rtuMw/com/rtuLogFile" ;
    protected static String mwUrlRtuLogText = "http://127.0.0.1:8070/rtuMw/com/rtuLogText" ;
 
 
    @Autowired
    private RestTemplate restTemplate;
 
 
    /**
     * 发送命令
     * @return
     */
    protected byte[] requestMw4File(String rtuAddr, String mwUrl) throws Exception{
        String url = UriComponentsBuilder.fromUriString(mwUrl)
                .build()
                .toUriString();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                //.queryParam("paramTest", "test")
                .queryParam("rtuAddr", rtuAddr);
        String fullUrl = builder.toUriString();
        byte[] bs = restTemplate.getForObject(fullUrl, byte[].class);
        return bs ;
    }
 
    /**
     * 发送命令
     * @return
     */
    protected BaseResponse<List<String>> requestMw4Text(String rtuAddr, String mwUrl){
        String url = UriComponentsBuilder.fromUriString(mwUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                //.queryParam("paramTest", "test")
                .queryParam("rtuAddr", rtuAddr);
 
        String fullUrl = builder.toUriString();
 
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<?> httpEntity = new HttpEntity<>(headers);
        ResponseEntity<BaseResponse> response = null;
        try {
            // 通过Get方式调用接口
            response = restTemplate.exchange(fullUrl, HttpMethod.GET, httpEntity, BaseResponse.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response.getBody();
    }
}