From 10238b43c767fba6a900680da3280f66994f3847 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期一, 11 十一月 2024 18:52:43 +0800
Subject: [PATCH] 继续实现通信中间件RTU嵌入式软件远程升级功能,并修改完善之前实现的部分。

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeTask.java |  270 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 183 insertions(+), 87 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeTask.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeTask.java
index 631aacd..e0b3d13 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeTask.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/upgrade/UpgradeTask.java
@@ -2,7 +2,9 @@
 
 import com.alibaba.fastjson2.annotation.JSONField;
 import com.dy.common.softUpgrade.parse.HexFileParse;
-import com.dy.common.softUpgrade.parse.HexFileVo;
+import com.dy.common.softUpgrade.state.UpgradeRtu;
+import com.dy.common.softUpgrade.state.UpgradeState;
+import com.dy.common.softUpgrade.state.UpgradeTaskVo;
 import com.dy.common.util.Callback;
 import com.dy.common.util.DateTime;
 import lombok.Data;
@@ -17,30 +19,37 @@
 @Data
 public class UpgradeTask {
 
+    protected static final String TaskOverType_Natural = "鑷劧" ;
+    protected static final String TaskOverType_Force = "寮哄埗" ;
+
     @JSONField(serialize = false)
     protected Integer failTryTimes ;//鍗囩骇澶辫触鍚庯紝閲嶆柊鍋胯瘯鍗囩骇娆℃暟锛�0琛ㄧず涓嶉噸鏂板伩璇曞崌绾�
     @JSONField(serialize = false)
     protected Integer ugMaxRtuSameTime ;//鍚屾椂鍗囩骇RTU鏈�澶т釜鏁�
 
     public String setupDt ;//璁剧疆鏃堕棿(yyyy-mm-dd HH:MM:SS)
+    @JSONField(serialize = false)
+    private Long setupDtLong ;//璁剧疆鏃堕棿
 
-    public String softFileName ;//鍗囩骇杞欢锛坔ex锛夋枃浠跺悕绉�
-    public String softStoreAddr ;//鍗囩骇绋嬪簭瀛樻斁鍦板潃(4瀛楄妭锛�8瀛楃HEX瀛楃涓�)锛屽崌绾х▼搴忓湪FLASH涓瓨鏀惧湴鍧�
-    public String softStartAddr ;//绋嬪簭瑕嗙洊璧峰鍦板潃(4瀛楄妭锛�8瀛楃HEX瀛楃涓�)锛岃鍒锋柊绋嬪簭鐨勮捣濮嬪湴鍧�楂樺瓧鑺傚湪鍓� 锛屼綆瀛楄妭鍦ㄥ悗
+    public UpgradeTaskVo taskVo ;//鍗囩骇浠诲姟鍊煎璞�
 
     @JSONField(serialize = false)
-    public byte[][] softData ;//鍗囩骇绋嬪簭鏁版嵁(姣忓寘鏁版嵁鏄�512瀛楄妭)
-    @JSONField(serialize = false)
-    public int softByteSrc16;//鍗囩骇绋嬪簭鏍¢獙鐮� CRC16
-
-    public int softBytesCalculate;//鍗囩骇绋嬪簭瀛楄妭鏁�(鎸夊叕寮忚绠�)
-
-    public List<String> rtuAddrList ;//闇�瑕佸崌绾х殑RTU鍦板潃闆嗗悎
+    protected byte[][] softFileDataGrp ;//浠�512瀛楄妭涓哄崟浣嶆妸鍗囩骇绋嬪簭鏁版嵁鍒嗙粍
 
     @JSONField(serialize = false)
-    public Map<String, UpgradeRtu> upgradeState ;//鍗囩骇鐘舵��
+    protected Map<String, UpgradeRtu> upgradeRtus;//鍗囩骇鐘舵��
+
+    public boolean taskIsOver = false ;//浠诲姟鏄惁瀹屾垚
+    public String taskOverType = "" ;//浠诲姟瀹屾垚鏂瑰紡锛堣嚜鐒讹紝寮哄埗锛�
+    public String taskOverDt = "" ;//浠诲姟瀹屾垚鏃堕棿(yyyy-mm-dd HH:MM:SS)
+
+    ///////////////////////////////////////////////////
+    //浠ヤ笅鍐呴儴鎺у埗鐢�
+    @JSONField(serialize = false)
+    private int curUgRunningRtuTotal = 0 ;//褰撳墠姝e湪鍗囩骇鐨凴TU涓暟
 
     public UpgradeTask() {
+        this.curUgRunningRtuTotal = 0 ;
     }
     /**
      *  鍒濆鍖栭厤缃俊鎭�
@@ -51,70 +60,112 @@
     }
     /**
      * 璁剧疆鍗囩骇浠诲姟
-     * @param softFileName 鍗囩骇绋嬪簭鏂囦欢鍚�
-     * @param softStoreAddr 鍗囩骇绋嬪簭瀛樻斁鍦板潃
-     * @param softStartAddr 绋嬪簭瑕嗙洊璧峰鍦板潃
-     * @param softFileData 鍗囩骇绋嬪簭瀛楄妭鏁扮粍
-     * @param softBytesCalculate 鍗囩骇绋嬪簭瀛楄妭鏁�(鎸夊叕寮忚绠�)
-     * @param rtuAddrList 鍗囩骇RTU
+     * @param taskVo UpgradeTaskVo 鍗囩骇浠诲姟瀵硅薄
      * @throws Exception
      */
-    public void setTask(String softFileName,
-                       String softStoreAddr,
-                       String softStartAddr,
-                       byte[] softFileData,
-                       Integer softBytesCalculate,
-                       List<String> rtuAddrList) throws Exception {
-        if(softFileName == null || softFileName.trim().length() == 0){
+    public void setTask(UpgradeTaskVo taskVo) throws Exception {
+        if(taskVo.id == null || taskVo.id.trim().length() == 0){
+            throw new Exception("鍗囩骇浠诲姟id蹇呴』鎻愪緵") ;
+        }
+       if(taskVo.softFileName == null || taskVo.softFileName.trim().length() == 0){
             throw new Exception("鍗囩骇杞欢锛坔ex锛夋枃浠跺悕绉板繀椤绘彁渚�") ;
         }
-        if(softStoreAddr == null || softStoreAddr.trim().length() != 8){
+        if(taskVo.softStoreAddr == null || taskVo.softStoreAddr.trim().length() != 8){
             throw new Exception("鍗囩骇绋嬪簭瀛樻斁鍦板潃涓嶅悎娉曪紝蹇呴』鏄�8瀛楃锛堝崄鍏繘鍒讹級鐨勫瓧绗︿覆") ;
         }
-        if(softStartAddr == null || softStartAddr.trim().length() != 8){
+        if(taskVo.softStartAddr == null || taskVo.softStartAddr.trim().length() != 8){
             throw new Exception("绋嬪簭瑕嗙洊璧峰鍦板潃涓嶅悎娉曪紝蹇呴』鏄�8瀛楃锛堝崄鍏繘鍒讹級鐨勫瓧绗︿覆") ;
         }
-        if(softFileData == null || softFileData.length <= 0){
+        if(taskVo.softFileData == null || taskVo.softFileData.length <= 0){
             throw new Exception("鍗囩骇绋嬪簭鍐呭蹇呴』鎻愪緵") ;
         }
-        if(rtuAddrList == null || rtuAddrList.size() <= 0){
+        if(taskVo.softBytesCalculate == null){
+            throw new Exception("鍏紡璁$畻鍗囩骇绋嬪簭鏈夋晥搴忓瓧鑺傛暟蹇呴』鎻愪緵") ;
+        }
+       if(taskVo.softByteSrc16 == null){
+            throw new Exception("鏈夋晥鍗囩骇绋嬪簭瀛楄妭鏁癈RC16鏍¢獙鍊煎繀椤绘彁渚�") ;
+        }
+        if(taskVo.rtuAddrList == null || taskVo.rtuAddrList.size() <= 0){
             throw new Exception("鍗囩骇璁惧RTU鍦板潃蹇呴』鎻愪緵") ;
         }
+        if(taskVo.callbackWebUrl == null || taskVo.callbackWebUrl.trim().equals("")){
+            throw new Exception("鍥炶皟缃戝潃蹇呴』鎻愪緵") ;
+        }
         this.setupDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;
-        this.softFileName = softFileName;
-        this.softStoreAddr = softStoreAddr;
-        this.softStartAddr = softStartAddr;
-        this.softBytesCalculate = softBytesCalculate;
-        this.rtuAddrList = rtuAddrList;
+        this.setupDtLong = System.currentTimeMillis() ;
+        this.taskVo = taskVo ;
 
-        this.upgradeState = new HashMap<>();
-        if(softFileData != null && softFileData.length >0){
-            HexFileVo vo = new HexFileParse().doParse(softFileData);
-            this.softData = vo.listByte512.toArray(new byte[0][]);
-            this.softByteSrc16 = vo.bytesCrc16 ;
+        this.upgradeRtus = new HashMap<>();
+        if(taskVo.softFileData != null && taskVo.softFileData.length >0){
+            List<byte[]> listBytes = new HexFileParse().splitBytesByUnit512(taskVo.softFileData);
+            this.softFileDataGrp = listBytes.toArray(new byte[0][]);
+            for(String rtuAddr : this.taskVo.rtuAddrList){
+                //姝ゆ椂鐘舵�佽缃垚绂荤嚎鐘舵��
+                UpgradeRtuDev ugRtu = new UpgradeRtuDev(this, rtuAddr, this.taskVo.softFileData.length, UpgradeRtuDev.STATE_OFFLINE) ;
+                this.upgradeRtus.put(rtuAddr, ugRtu) ;
+            }
         }
     }
     /**
      * RTU鏈変笂琛屾暟鎹簡锛岃Е鍙戜笅鍙戝崌绾ф暟鎹�
      * @param rtuAddr
      * @param code
-     * @param callback
+     * @param callbackCom
      */
-    public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callback){
-        if(upgradeState != null && upgradeState.size() > 0
-                && rtuAddrList != null && rtuAddrList.size() > 0){
-            UpgradeRtu info = upgradeState.get(rtuAddr) ;
-            if(info == null){
-                if(rtuAddrList.contains(rtuAddr)){
-                    info = new UpgradeRtu(this, rtuAddr, softData.length) ;
-                    upgradeState.put(rtuAddr, info) ;
+    public void trigger(String rtuAddr, String code, String protocolName, Short protocolVersion, Callback callbackCom){
+        if(this.upgradeRtus != null && this.upgradeRtus.size() > 0
+                && this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0){
+            UpgradeRtu ugRtu = this.upgradeRtus.get(rtuAddr) ;
+            if(ugRtu == null){
+                //鏍规嵁鏂规硶setTask鐨勯�昏緫锛屽彧瑕丷TU鍦ㄥ崌绾т箣鍒楋紝姝ゅugRtuState涓�瀹氫笉涓簄ull
+                //涓轰繚闄╋紝瀹炵幇涓嬮潰閫昏緫
+                if(taskVo.rtuAddrList.contains(rtuAddr)){
+                    ugRtu = new UpgradeRtuDev(this, rtuAddr, this.taskVo.softFileData.length) ;
+                    upgradeRtus.put(rtuAddr, ugRtu) ;
                 }else{
                     //rtu涓嶅湪鍗囩骇涔嬪垪
                     return ;
                 }
             }
-            if(info != null){
-                info.trigger(code, protocolName, protocolVersion, this.softData, callback) ;
+            if(ugRtu != null){
+                if(ugRtu.isOver){
+                    //褰撳墠RTU宸茬粡鍗囩骇瀹屾垚锛屾棤闇�鍐嶅崌绾�
+                    return;
+                }else{
+                    if(UpgradeUnit.confVo.ugMaxRtuAtOnce > 0){
+                        //璁剧疆浜嗗悓鏃跺崌绾х殑RTU鏈�澶ф暟閲忕殑闄愬埗
+                        if(ugRtu.state == UpgradeRtuDev.STATE_OFFLINE){
+                            //鍒濆鎬侊紝璇存槑鍗囩骇浠诲姟璁剧疆浠ユ潵锛岃RTU绗竴娆′笂琛屾暟鎹�
+                            if(this.curUgRunningRtuTotal <= UpgradeUnit.confVo.ugMaxRtuAtOnce){
+                                //褰撳墠姝e湪鍗囩骇鐨凴TU鏁伴噺杩樻湭鍙楅檺
+                                ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ;
+                            }else{
+                                //鍚屾椂鍗囩骇鐨凴TU鏁伴噺鍙楅檺锛岀瓑寰呬笅娆℃満浼�
+                                //浣嗗厛琛ㄦ槑涓�涓嬬姸鎬�
+                                ugRtu.state = UpgradeRtuDev.STATE_UNSTART ;
+                                return ;
+                            }
+                        }else if(ugRtu.state == UpgradeRtuDev.STATE_UNSTART){
+                            //鏍规嵁涓婇潰閫昏緫锛� 璇存槑蹇呯劧鍦ㄧ嚎浜�
+                            if(this.curUgRunningRtuTotal <= UpgradeUnit.confVo.ugMaxRtuAtOnce){
+                                //褰撳墠姝e湪鍗囩骇鐨凴TU鏁伴噺杩樻湭鍙楅檺
+                                ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ;
+                            }else{
+                                //鍚屾椂鍗囩骇鐨凴TU鏁伴噺鍙楅檺锛岀瓑寰呬笅娆℃満浼�
+                                return ;
+                            }
+                        }else{
+                            //RTU宸茬粡澶勪簬鍗囩骇杩囩▼涓�
+                            ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ;
+                        }
+                    }else{
+                        //娌℃湁璁剧疆鍚屾椂鍗囩骇鐨凴TU鏈�澶ф暟閲忕殑闄愬埗
+                         ugRtu.trigger(code, protocolName, protocolVersion, this.softFileDataGrp, callbackCom) ;
+                    }
+                }
+            }else{
+                //rtu涓嶅湪鍗囩骇涔嬪垪
+                return ;
             }
         }
     }
@@ -123,29 +174,11 @@
      * 寮哄埗缁撴潫鍗囩骇浠诲姟
      */
     public void forceOver(){
-        this.rtuAddrList.clear();
-        this.upgradeState.clear();
-    }
-
-    /**
-     * 鍗囩骇浠诲姟鏄惁瀹屾垚
-     * @return
-     */
-    public boolean isOver() {
-        boolean isOver = true ;
-        if(upgradeState != null && upgradeState.size() > 0){
-            Collection<UpgradeRtu> col = upgradeState.values() ;
-            for(UpgradeRtu info : col){
-                if(info.state == UpgradeRtu.STATE_UNSTART){
-                    isOver = false ;
-                    break ;
-                }else if(info.state == UpgradeRtu.STATE_RUNNING){
-                    isOver = false ;
-                    break ;
-                }
-            }
-        }
-        return isOver ;
+        this.taskIsOver = true ;//寮哄埗璁剧疆浠诲姟瀹屾垚
+        this.taskOverType = TaskOverType_Force ;//浠诲姟瀹屾垚鏂瑰紡锛堣嚜鐒讹紝寮哄埗锛�
+        this.taskOverDt = DateTime.yyyy_MM_dd_HH_mm_ss() ;//浠诲姟瀹屾垚鏃堕棿(yyyy-mm-dd HH:MM:SS)
+        //this.taskVo.rtuAddrList.clear();
+        //this.upgradeState.clear();
     }
 
     /**
@@ -154,24 +187,26 @@
      */
     public UpgradeState currentUpgradeState() {
         UpgradeState state = new UpgradeState() ;
-        if(rtuAddrList != null && rtuAddrList.size() > 0){
-            state.rtuTotal = rtuAddrList.size() ;
-            if(upgradeState != null && upgradeState.size() > 0){
-                Collection<UpgradeRtu> col = upgradeState.values() ;
+        if(this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0){
+            state.rtuTotal = this.taskVo.rtuAddrList.size() ;
+            if(this.upgradeRtus != null && this.upgradeRtus.size() > 0){
+                Collection<UpgradeRtu> col = this.upgradeRtus.values() ;
                 for(UpgradeRtu info : col){
-                    if(info.state == UpgradeRtu.STATE_UNSTART){
+                    if(info.state == UpgradeRtuDev.STATE_OFFLINE){
+                        state.offLineTotal ++ ;
+                    }else if(info.state == UpgradeRtuDev.STATE_UNSTART){
                         state.unStartTotal ++ ;
-                    }else if(info.state == UpgradeRtu.STATE_RUNNING){
+                    }else if(info.state == UpgradeRtuDev.STATE_RUNNING){
                         state.runningTotal ++ ;
-                    }else if(info.state == UpgradeRtu.STATE_SUCCESS) {
+                    }else if(info.state == UpgradeRtuDev.STATE_SUCCESS) {
                         state.successTotal++;
-                        state.overTotal++;
-                    }else if(info.state == UpgradeRtu.STATE_FAILONE) {
+                    }else if(info.state == UpgradeRtuDev.STATE_FAILONE) {
                         state.failOneTotal++;
                         state.failTotal++;
-                        state.overTotal++;
-                    }else if(info.state == UpgradeRtu.STATE_FAIL) {
+                    }else if(info.state == UpgradeRtuDev.STATE_FAIL) {
                         state.failTotal++;
+                    }
+                    if(info.isOver){
                         state.overTotal++;
                     }
                 }
@@ -186,23 +221,84 @@
      * @return
     */
     public UpgradeRtu upgradeInfos(String rtuAddr){
-        return upgradeState.get(rtuAddr) ;
+        return this.upgradeRtus.get(rtuAddr) ;
     }
 
     /**
-     * Rtu鍗囩骇淇℃伅
+     * 涓�浜汻tu鍗囩骇淇℃伅
      * @param rtuAddrList
      * @return
      */
     public List<UpgradeRtu> upgradeInfos(List<String> rtuAddrList){
         List<UpgradeRtu> list = new ArrayList<>() ;
         for(String rtuAddr : rtuAddrList){
-            UpgradeRtu info = upgradeState.get(rtuAddr) ;
+            UpgradeRtu info = this.upgradeRtus.get(rtuAddr) ;
             if(info != null){
                 list.add(info) ;
             }
         }
         return list ;
     }
+    /**
+     * 鍏ㄩ儴Rtu鍗囩骇淇℃伅
+     * @return
+     */
+    public List<UpgradeRtu> upgradeInfoAll(){
+        if(this.upgradeRtus != null && this.upgradeRtus.size() > 0){
+            return this.upgradeRtus.values().stream().toList();
+        }else{
+            return null ;
+        }
+    }
 
+    ///////////////////////////////////////////////////////////
+    //浠ヤ笅鏂规硶涓哄唴閮ㄦ湇鍔★紝涓嶅澶栨彁渚涙湇鍔�
+    ///////////////////////////////////////////////////////////
+    /**
+     * 缁熻褰撳墠姝e湪鍗囩骇鐨凴TU鏁伴噺锛屼负鍙楅檺鍚屾椂鍗囩骇鏁伴噺鍋氬噯澶�
+     */
+    protected void statisticsRunningRtuCount(){
+        int runningTotal = 0 ;
+        Collection<UpgradeRtu> col = this.upgradeRtus.values() ;
+        for(UpgradeRtu info : col){
+            if(info.state == UpgradeRtu.STATE_RUNNING){
+                runningTotal ++ ;
+            }
+        }
+        this.curUgRunningRtuTotal = runningTotal ;
+    }
+
+    /**
+     * 缁熻闇�瑕佸崌绾т絾褰撳墠绂荤嚎RTU鐨勬儏鍐�
+     */
+    protected void statisticsOffRtuCountAndSet() {
+        Long now = System.currentTimeMillis() ;
+        if(now - this.setupDtLong > UpgradeUnit.confVo.rtuOffLineWaitDuration){
+            //rtu绂荤嚎锛岀瓑寰呭叾鍗囩骇鐨勬椂闀�(姣)锛岃秴杩囬厤缃殑鏈�澶ф椂闀匡紝璁剧疆鍏跺崌绾уけ璐ワ紝涓旇缃崌绾т换鍔″畬鎴�
+            if (this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0) {
+                Collection<UpgradeRtu> col = this.upgradeRtus.values() ;
+                for(UpgradeRtu info : col){
+                    if(info.state == UpgradeRtu.STATE_OFFLINE){
+                        info.isOver = true ;
+                        info.state = UpgradeRtu.STATE_FAILOFFLINE ;
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 缁熻鏄惁鍗囩骇鍏ㄩ儴缁撴潫
+     */
+    protected boolean statisticsIsAllOver() {
+        if (this.taskVo.rtuAddrList != null && this.taskVo.rtuAddrList.size() > 0) {
+            Collection<UpgradeRtu> col = this.upgradeRtus.values() ;
+            for(UpgradeRtu info : col){
+                if(info.isOver == false){
+                    return false ;
+                }
+            }
+        }
+        return true ;
+    }
 }

--
Gitblit v1.8.0