From 2b4fc2b16efdc887b71855b0b4d5b5680453d088 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 29 十一月 2024 11:15:52 +0800
Subject: [PATCH] 1、实现强制停止RTU远程升级任务功能; 2、业务系统与通信中间件web通信模块优化,使变得更通用与简单。

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java |  172 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 170 insertions(+), 2 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java
index 9abfa02..142a879 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java
@@ -1,18 +1,32 @@
 package com.dy.rtuMw.web.com;
 
+import com.dy.common.softUpgrade.state.UpgradeTaskVo;
+import com.dy.common.webUtil.ResultCodeMsg;
+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;
 import com.dy.rtuMw.server.local.ReturnCommand;
-import com.dy.rtuMw.server.tasks.RtuDownTask;
+import com.dy.rtuMw.server.tasks.WebDownComTask;
 import com.dy.common.mw.core.CoreUnit;
 import com.dy.common.mw.protocol.Command;
 import com.dy.common.mw.protocol.CommandType;
 import com.dy.common.webUtil.BaseResponse;
 import com.dy.common.webUtil.BaseResponseUtils;
+import com.dy.rtuMw.server.upgrade.UpgradeUnit;
+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.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 
 /**
@@ -32,6 +46,146 @@
         return BaseResponseUtils.buildSuccess("ok");
     }
 
+    /**
+     * 涓嬭浇鎺у埗鍣紙RTU锛変笂涓嬭鏁版嵁鐨刲og鏃ュ織鏂囦欢
+     * @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()){
+            //鍦⊿pring Boot涓紝application/octet-stream;charset=UTF-8閫氬父琛ㄧず鍝嶅簲鐨勫唴瀹规槸瀛楄妭娴侊紝
+            //骞朵笖瀛楃闆嗘槸UTF-8銆傚浜庤繖绉嶇被鍨嬬殑鍝嶅簲锛孲pring Boot榛樿浣跨敤ByteArrayHttpMessageConverter鏉ュ鐞嗭紝
+            //鍥犱负瀹冨彲浠ュ鐞嗘墍鏈塧pplication/octet-stream绫诲瀷鐨勫搷搴斻��
+            //鐒惰�岋紝ByteArrayHttpMessageConverter骞朵笉鐩存帴澶勭悊瀛楃闆嗭紙charset锛夈��
+            //瀛楃闆嗛�氬父鐢ㄤ簬鏂囨湰鍐呭锛岃�宎pplication/octet-stream閫氬父鐢ㄤ簬浜岃繘鍒跺唴瀹癸紝鍥犳鍦ㄨ繖绉嶆儏鍐典笅鎸囧畾瀛楃闆嗗彲鑳芥槸涓嶅悎閫傜殑銆�
+            //涓嶈繃锛屽鏋滀綘纭疄闇�瑕佸鐞嗗甫鏈夌壒瀹氬瓧绗﹂泦鐨刟pplication/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锛変笂涓嬭鏁版嵁鐨刲og鏃ュ織鏂囦欢
+     * @param rtuAddr
+     */
+    @GetMapping("/rtuLogText")
+    public BaseResponse<List<String>> rtuLogText(String rtuAddr){
+        List<String> list ;
+        File logFile = ResourceUnit.getInstance().getLogFile(rtuAddr + ".log") ;
+        if(logFile != null && logFile.exists()){
+            BufferedReader reader = null ;
+            try {
+                reader = new BufferedReader(new FileReader(logFile)) ;
+                //鏂扮殑瀹炵幇鏂规硶
+                Stream<String> linesStream = reader.lines() ;
+                //list = linesStream.toList() ; //鎸夊師鏉ラ『搴�
+                list = linesStream.sorted(Comparator.reverseOrder()).collect(Collectors.toList()) ;//鍊掑簭
+                /* 鍘熸潵鐨勫疄鐜版柟娉�
+                list = new ArrayList() ;
+                String line ;
+                while((line = reader.readLine()) != null){
+                    list.add(line) ;
+                }
+                */
+                return BaseResponseUtils.buildSuccess(list);
+            } catch (Exception e) {
+                list = new ArrayList() ;
+                list.add("璇诲彇鎺у埗鍣紙" + rtuAddr + "锛夌殑鏃ュ織鏂囦欢寮傚父锛�" + (e.getMessage() == null?"":("锛�" + e.getMessage()))) ;
+                return BaseResponseUtils.buildSuccess(list);
+            }finally{
+                if(reader != null){
+                    try{
+                        reader.close();
+                    }catch(Exception e){
+                    }
+                }
+            }
+        }else{
+            list = new ArrayList() ;
+            list.add("鏈緱鍒版帶鍒跺櫒锛�" + rtuAddr + "锛夌殑鏃ュ織鏂囦欢") ;
+            return BaseResponseUtils.buildSuccess(list);
+        }
+    }
+    /**
+     * 鎺ユ敹web绯荤粺鍙戞潵鍗囩骇浠诲姟
+     * @param vo
+     * @return
+     */
+    @PostMapping(path = "upgradeRtu", consumes = MediaType.APPLICATION_JSON_VALUE)
+    public BaseResponse<Boolean> upgradeRtu(@RequestBody UpgradeTaskVo vo) {
+        log.info("鏀跺埌RTU鍗囩骇浠诲姟锛歕n" + vo.toString()) ;
+        try{
+            UpgradeUnit.getInstance().setUpgradeTask(vo);
+        }catch (Exception e){
+            log.error("璁剧疆RTU鍗囩骇浠诲姟鏃跺彂鐢熷紓甯�", e);
+            return BaseResponseUtils.buildError("璁剧疆RTU鍗囩骇浠诲姟鏃跺彂鐢熷紓甯�" + (e.getMessage() == null?"":("锛�" + e.getMessage())));
+        }
+        return BaseResponseUtils.buildSuccess(true);
+    }
+
+    /**
+     * 鎺ユ敹web绯荤粺鍙戞潵鍋滄鍗囩骇浠诲姟
+     * @return
+     */
+    @PostMapping(path = "ugForceStop", consumes = MediaType.APPLICATION_JSON_VALUE)
+    public BaseResponse<String> ugForceStop() {
+        log.info("鏀跺埌鍋滄RTU鍗囩骇浠诲姟鍛戒护") ;
+        try{
+            String mes = UpgradeUnit.getInstance().overUpgradeTask();
+            if(mes == null){
+                mes = "鍋滄鍗囩骇浠诲姟鎴愬姛" ;
+            }
+            return BaseResponseUtils.buildResult(ResultCodeMsg.RsCode.SUCCESS_CODE, mes, mes);
+        }catch (Exception e){
+            log.error("鍋滄RTU鍗囩骇浠诲姟鏃跺彂鐢熷紓甯�", e);
+            return BaseResponseUtils.buildError("鍋滄RTU鍗囩骇浠诲姟鏃跺彂鐢熷紓甯�" + (e.getMessage() == null?"":("锛�" + e.getMessage())));
+        }
+    }
+
+
+    /**
+     * 鎺ユ敹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()) ;
@@ -87,6 +241,20 @@
     }
 
     /**
+     * 鏈湴璋冪敤锛屼緥濡備换鍔℃爲涓煇涓换鍔′笅鍙戠殑鍛戒护锛堝娓呯┖鍛戒护锛�
+     * @param com
+     * @return
+     */
+    public BaseResponse<Command> sendOutComFromLocal(Command com) {
+        try{
+            return this.dealOuterCommand(com) ;
+        }catch(Exception e){
+            return BaseResponseUtils.buildError(ReturnCommand.errored("澶勭悊鍙戝悜RTU鐨勫閮ㄥ懡浠ゅ嚭閿�" + (e.getMessage() == null?"":("锛�" + e.getMessage())), com.getId(), com.getCode()) );
+        }
+    }
+
+
+    /**
      * 澶勭悊鍙戝悜RTU鐨勫閮ㄥ懡浠�
      * @return 缁撴灉
      */
@@ -106,7 +274,7 @@
         }
 
         //鐢熸垚寮傛浠诲姟
-        RtuDownTask task = new RtuDownTask() ;
+        WebDownComTask task = new WebDownComTask() ;
         task.data = command ;
         try{
             log.info("鏋勯�犱笅鍙戣繙绋嬪懡浠�" + command.getCode() + "鐨勬牳蹇冧换鍔★紝骞舵斁鍏ヤ换鍔¢槦鍒椾腑");

--
Gitblit v1.8.0