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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.dy.pipIrrRemote.rtu;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
 
import java.net.URLEncoder;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/8/29 9:22
 * @Description
 */
 
@Slf4j
@RestController
@RequestMapping(path="rtuLog")
public class RtuLogCtrl {
 
    private static final String pro_mw = "mw";
    private static final String key_mw_file = "rtuLogFileUrl";
    private static final String key_mw_text = "rtuLogTextUrl";
 
    private Environment env ;
    private RestTemplate restTemplate;
 
    @Autowired
    public RtuLogCtrl(Environment env, RestTemplate restTemplate) {
        this.env = env;
        this.restTemplate = restTemplate;
    }
 
    /**
     * 发送命令,请求下载rtu上下行数据日志文件
     * @param rtuAddr rtu地址
     * @return
     */
    @GetMapping(path = "file")
    @SsoAop()
    public BaseResponse<List<String>> rtuLogFile(String rtuAddr, HttpServletRequest req, HttpServletResponse rep){
        String mwUrlRtuLogFile = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw_file);
        ServletOutputStream out = null ;
        try{
            byte[] bs = this.requestMw4File(rtuAddr, mwUrlRtuLogFile) ;
            if(bs != null && bs.length > 0){
                String fileReName = rtuAddr + ".log" ;
                //URLEncoder.encode可以防止中文乱码
                fileReName = URLEncoder.encode(fileReName, "UTF-8").replaceAll("\\+", "%20");
                rep.addHeader("content-type", "application/octet-stream;charset=UTF-8");
                rep.addHeader("Content-Disposition", "attachment;fileName=" + fileReName);
                out = rep.getOutputStream() ;
                out.write(bs, 0, (bs==null?0:bs.length));
                out.flush();
            }else{
                return BaseResponseUtils.buildError("获取文件失败") ;
            }
        }catch (Exception e){
        }finally {
            if(out != null){
                try{
                    out.close();
                }catch(Exception e){
                }
            }
        }
        return null ;
    }
 
    /**
     * 发送命令,请求rtu上下行数据日志文件内容
     * @param rtuAddr rtu地址
     * @return
     */
    @GetMapping(path="text")
    @SsoAop()
    public BaseResponse<List<String>> rtuLogText(String rtuAddr){
        String mwUrlRtuLogText = env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + key_mw_text);
        return this.requestMw4Text(rtuAddr, mwUrlRtuLogText) ;
    }
 
 
    /**
     * 发送命令,请求下载rtu上下行数据日志文件
     * @param rtuAddr rtu地址
     * @param mwUrl mw服务器地址
     * @return
     */
    private byte[] requestMw4File(String rtuAddr, String mwUrl) throws Exception{
        String url = UriComponentsBuilder.fromUriString(mwUrl)
                .build()
                .toUriString();
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("rtuAddr", rtuAddr);
        return restTemplate.getForObject(builder.toUriString(), byte[].class);
    }
 
    /**
     * 发送命令,请求rtu上下行数据日志文件内容
     * @param rtuAddr rtu地址
     * @param mwUrl mw服务器地址
     * @return
     */
    private BaseResponse<List<String>> requestMw4Text(String rtuAddr, String mwUrl){
        String url = UriComponentsBuilder.fromUriString(mwUrl)
                .build()
                .toUriString();
 
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("rtuAddr", rtuAddr);
 
        ResponseEntity<BaseResponse> response = restTemplate.exchange(builder.toUriString(),
                HttpMethod.GET,
                new HttpEntity<>(new HttpHeaders()),
                BaseResponse.class);
 
        return (response==null?null:response.getBody());
    }
 
}