pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1_0_0/parse/Cd_66_Up.java
@@ -69,7 +69,7 @@ cdData.ip = ip1 + "." + ip2 + "." + ip3 + "." + ip4 ; String port = "" +ByteUtilUnsigned.bytes2Short_LE(bs, index++) ; String port = "" +ByteUtilUnsigned.bytes2Short_BE(bs, index++) ; cdData.port = Integer.parseInt(port) ; } pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/resource/ResourceUnit.java
@@ -123,7 +123,7 @@ /** * 得到一个日志文件 * @param fileName 文件名称 * @param fileName 文件名称([rtuAddr].log) */ @SuppressWarnings("unused") public File getLogFile(String fileName){ pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java
@@ -1,5 +1,6 @@ package com.dy.rtuMw.web.com; import com.dy.rtuMw.resource.ResourceUnit; import com.dy.rtuMw.server.ServerProperties; import com.dy.rtuMw.server.forTcp.TcpSessionCache; import com.dy.rtuMw.server.local.CommandInnerDeaLer; @@ -10,9 +11,16 @@ import com.dy.common.mw.protocol.CommandType; 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.http.MediaType; import org.springframework.web.bind.annotation.*; import java.io.*; import java.util.ArrayList; import java.util.List; /** @@ -32,6 +40,100 @@ return BaseResponseUtils.buildSuccess("ok"); } /** * 下载控制器(RTU)上下行数据的log日志文件 * @param rtuAddr * @param req * @param rep */ @GetMapping("/rtuLogFile") public void rtuLogFile(String rtuAddr, HttpServletRequest req, HttpServletResponse rep){ File logFile = ResourceUnit.getInstance().getLogFile(rtuAddr + ".log") ; if(logFile != null && logFile.exists()){ //在Spring Boot中,application/octet-stream;charset=UTF-8通常表示响应的内容是字节流, //并且字符集是UTF-8。对于这种类型的响应,Spring Boot默认使用ByteArrayHttpMessageConverter来处理, //因为它可以处理所有application/octet-stream类型的响应。 //然而,ByteArrayHttpMessageConverter并不直接处理字符集(charset)。 //字符集通常用于文本内容,而application/octet-stream通常用于二进制内容,因此在这种情况下指定字符集可能是不合适的。 //不过,如果你确实需要处理带有特定字符集的application/octet-stream响应,你可能需要自定义HttpMessageConverter。 rep.addHeader("content-type", "application/octet-stream;charset=UTF-8"); rep.addHeader("Content-Disposition", "attachment;fileName=" + (rtuAddr + ".log")) ; ServletOutputStream out = null; FileInputStream in = null ; try { out = rep.getOutputStream() ; } catch (Exception ee) { out = null ; }finally{ if(out != null){ byte[] bs = new byte[1024] ; int len = -1 ; try { in = new FileInputStream(logFile); len = in.read(bs) ; while(len != -1){ out.write(bs, 0, len); len = in.read(bs) ; } } catch (Exception eee) { } finally { if(out != null){ try{ out.flush(); out.close(); }catch(Exception e){ }finally{ if(in != null){ try{ in.close(); }catch(Exception e){ } } } } } } } } } /** * 下载控制器(RTU)上下行数据的log日志文件 * @param rtuAddr */ @GetMapping("/rtuLogText") public BaseResponse<List<String>> rtuLogText(String rtuAddr){ List<String> list = new ArrayList() ; File logFile = ResourceUnit.getInstance().getLogFile(rtuAddr + ".log") ; if(logFile != null && logFile.exists()){ BufferedReader reader = null ; try { reader = new BufferedReader(new FileReader(logFile)) ; String line ; while((line = reader.readLine()) != null){ list.add(line) ; } return BaseResponseUtils.buildSuccess(list); } catch (Exception e) { list.add("读取控制器(" + rtuAddr + ")的日志文件异常:" + (e.getMessage() == null?"":("," + e.getMessage()))) ; return BaseResponseUtils.buildSuccess(list); }finally{ if(reader != null){ try{ reader.close(); }catch(Exception e){ } } } }else{ list.add("未得到控制器(" + rtuAddr + ")的日志文件") ; return BaseResponseUtils.buildSuccess(list); } } /** * 接收web系统发来的命令 * @param com * @return */ @PostMapping(path = "send", consumes = MediaType.APPLICATION_JSON_VALUE) public BaseResponse<Command> send(@RequestBody Command com) { log.info("收到web系统发来的命令:\n" + com.toString()) ; pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/p206V1_0_0/ComSupportP206V1_0_0.java
@@ -20,6 +20,8 @@ public class ComSupportP206V1_0_0 { protected static String mwUrlTest = "http://127.0.0.1:8070/rtuMw/com/test" ; protected static String mwUrlSendCom = "http://127.0.0.1:8070/rtuMw/com/send" ; 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" ; protected static String rtuAddr = "532328059995" ; // protected static String mwUrlTest = "http://8.140.179.55:8071/rtuMw/com/test" ; pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayConverterConfig.java
New file @@ -0,0 +1,18 @@ package com.dy.pipIrrMwTestWeb.rtuLog; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; /** * @Author: liurunyu * @Date: 2024/8/28 16:02 * @Description */ @Configuration public class ByteArrayConverterConfig { @Bean public HttpMessageConverter<byte[]> logFileByteArrayHttpMessageConverter() { return new ByteArrayHttpMessageConverter(); } } pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/ByteArrayHttpMessageConverter.java
New file @@ -0,0 +1,40 @@ package com.dy.pipIrrMwTestWeb.rtuLog; 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 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 org.springframework.util.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-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/RtuLogCtrl.java
New file @@ -0,0 +1,60 @@ package com.dy.pipIrrMwTestWeb.rtuLog; 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.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.net.URLEncoder; import java.util.List; /** * @Author: liurunyu * @Date: 2024/8/28 14:55 * @Description */ @Slf4j @RestController @RequestMapping(path="rtuLog") public class RtuLogCtrl extends RtuLogSupport { @GetMapping(path = "logFile") public BaseResponse<List<String>> rtuLogFile(String rtuAddr, HttpServletRequest req, HttpServletResponse rep){ 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 ; } @GetMapping(path="logText") public BaseResponse<List<String>> rtuLogText(String rtuAddr){ BaseResponse<List<String>> response = this.requestMw4Text(rtuAddr, mwUrlRtuLogText) ; return response ; } } pipIrr-platform/pipIrr-web/pipIrr-mwTest-web/src/main/java/com/dy/pipIrrMwTestWeb/rtuLog/RtuLogSupport.java
New file @@ -0,0 +1,68 @@ 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.http.HttpRequest; 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(); } }