在pipIrr-web-remote模块中实现RTU上下行数据日志下载功能RtuLogCtrl.java,
为此在pipIrr-global模块中增加了ByteArrayHttpMessageConverter.java和ByteArrayHttpMessageConverterConfig.java
pipIrr-global模块的application-global.yml文件中增加了通信中间件相关URL
1 文件已复制
1 文件已重命名
2个文件已添加
2个文件已修改
188 ■■■■■ 已修改文件
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/ByteArrayHttpMessageConverter.java 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/ByteArrayHttpMessageConverterConfig.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayHttpMessageConverterConfig.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/RtuLogSupport.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/rtu/RtuLogCtrl.java 136 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/ByteArrayHttpMessageConverter.java
New file
@@ -0,0 +1,41 @@
package com.dy.pipIrrGlobal.config;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
 * @Author: liurunyu
 * @Date: 2024/8/28 15:58
 * @Description
 */
public class ByteArrayHttpMessageConverter extends AbstractHttpMessageConverter<byte[]> {
    public ByteArrayHttpMessageConverter() {
        super(MediaType.APPLICATION_OCTET_STREAM, new MediaType("application", "octet-stream", StandardCharsets.UTF_8));
    }
    @Override
    protected boolean supports(Class<?> clazz) {
        return byte[].class == clazz;
    }
    @Override
    protected byte[] readInternal(Class<? extends byte[]> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        // Read the byte array from the input message
        return StreamUtils.copyToByteArray(inputMessage.getBody());
    }
    @Override
    protected void writeInternal(byte[] bytes, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        // Write the byte array to the output message
        outputMessage.getBody().write(bytes);
    }
}
pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/ByteArrayHttpMessageConverterConfig.java
copy from pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayConverterConfig.java copy to pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/config/ByteArrayHttpMessageConverterConfig.java
File was copied from pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayConverterConfig.java
@@ -1,4 +1,4 @@
package com.dy.pipIrrMwTestWeb.rtuLog;
package com.dy.pipIrrGlobal.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -10,7 +10,7 @@
 * @Description
 */
@Configuration
public class ByteArrayConverterConfig {
public class ByteArrayHttpMessageConverterConfig {
    @Bean
    public HttpMessageConverter<byte[]> logFileByteArrayHttpMessageConverter() {
        return new ByteArrayHttpMessageConverter();
pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -149,8 +149,12 @@
    #命令发送地址(中缀是机构tag,其也是数据源后缀名称)
    ym:
        comSendUrl: "http://127.0.0.1:8070/rtuMw/com/send"
        rtuLogFileUrl: "http://127.0.0.1:8070/rtuMw/com/rtuLogFile"
        rtuLogTextUrl: "http://127.0.0.1:8070/rtuMw/com/rtuLogText"
    pj:
        comSendUrl: "http://127.0.0.1:8071/rtuMw/com/send"
        rtuLogFileUrl: "http://127.0.0.1:8071/rtuMw/com/rtuLogFile"
        rtuLogTextUrl: "http://127.0.0.1:8071/rtuMw/com/rtuLogText"
    #监测控制模块回调地址
    rtuCallbackUrl_rm: "http://127.0.0.1:8081/remote/comRes/receive"
    #微信小程序回调地址
pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayHttpMessageConverterConfig.java
File was renamed from pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayConverterConfig.java
@@ -10,7 +10,7 @@
 * @Description
 */
@Configuration
public class ByteArrayConverterConfig {
public class ByteArrayHttpMessageConverterConfig {
    @Bean
    public HttpMessageConverter<byte[]> logFileByteArrayHttpMessageConverter() {
        return new ByteArrayHttpMessageConverter();
pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/RtuLogSupport.java
@@ -3,7 +3,6 @@
import com.dy.common.webUtil.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.HttpRequest;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/rtu/RtuLogCtrl.java
New file
@@ -0,0 +1,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());
    }
}