From 8c4a723ff13e34152934b84c52267615c4e0fb8b Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 06 十二月 2024 10:22:34 +0800
Subject: [PATCH] 升级模拟器增加上报81数据,为上报阀门状态,用81数据代替心跳

---
 pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java               |    1 
 pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java   |   50 +++++++++
 pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java |   78 +++++++++------
 pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java            |   69 -------------
 pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java               |   96 +++++++++++++++++++
 5 files changed, 193 insertions(+), 101 deletions(-)

diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java
index 3005508..2e20a49 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/PipIrrMwSimulateRtuApplication.java
@@ -2,7 +2,6 @@
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
-import org.springframework.boot.SpringApplication;
 import org.springframework.boot.WebApplicationType;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -36,25 +35,32 @@
     public static void main(String[] args) {
         if(args != null && args.length > 0){
             if(args.length == 1){
-                getArg0(args) ;
+                getArg0(args, 0) ;
             }else if(args.length == 2){
-                getArg0(args) ;
-                getArg1(args) ;
+                getArg0(args, 0) ;
+                getArg1(args, 1) ;
             }else if(args.length == 3){
-                getArg0(args) ;
-                getArg1(args) ;
-                getArg2(args) ;
+                getArg0(args, 0) ;
+                getArg1(args, 1) ;
+                getArg2(args, 2) ;
             }else if(args.length == 4){
-                getArg0(args) ;
-                getArg1(args) ;
-                getArg2(args) ;
-                getArg3(args) ;
+                getArg0(args, 0) ;
+                getArg1(args, 1) ;
+                getArg2(args, 2) ;
+                getArg3(args, 3) ;
             }else if(args.length == 5){
-                getArg0(args) ;
-                getArg1(args) ;
-                getArg2(args) ;
-                getArg3(args) ;
-                getArg4(args) ;
+                getArg0(args, 0) ;
+                getArg1(args, 1) ;
+                getArg2(args, 2) ;
+                getArg3(args, 3) ;
+                getArg4(args,4) ;
+            }else if(args.length == 6){
+                getArg0(args, 0) ;
+                getArg1(args, 1) ;
+                getArg2(args, 2) ;
+                getArg3(args, 3) ;
+                getArg4(args, 4) ;
+                getArg5(args, 5) ;
             }
 
             if(ServerProperties.argOneDie && ServerProperties.argMultiDie){
@@ -65,37 +71,43 @@
                 .web(WebApplicationType.NONE)//涓嶅惎鍔╳eb鏈嶅姟
                 .run(args);
     }
-    private static void getArg0(String[] args){
-        if(args[0] instanceof String){
-            if(args[0] != null && !args[0].trim().equals("")){
-                String rtuAddr = (String)args[0] ;//鏈ā鎷熷櫒妯℃嫙RTU鍦板潃
+    private static void getArg0(String[] args, int index){
+        if(args[index] instanceof String){
+            if(args[index] != null && !args[index].trim().equals("")){
+                String rtuAddr = (String)args[index] ;//鏈ā鎷熷櫒妯℃嫙RTU鍦板潃
                 ServerProperties.argRtuAddr = rtuAddr ;
             }
         }
     }
-    private static void getArg1(String[] args){
-        if(args[1] != null && !args[1].trim().equals("")){
-            Integer oneDie = Integer.parseInt(args[1]) ;//涓�鍖呮锛�1鏄紝0鍚�
+    private static void getArg1(String[] args, int index){
+        if(args[index] != null && !args[index].trim().equals("")){
+            Integer conf = Integer.parseInt(args[index]) ;//蹇冭烦绫诲瀷锛�1锛氬姛鑳界爜02锛�0:鍔熻兘鐮�81
+            ServerProperties.argHeardBeatBy02True81False = conf == 1?true:false ;
+        }
+    }
+    private static void getArg2(String[] args, int index){
+        if(args[index] != null && !args[index].trim().equals("")){
+            Integer oneDie = Integer.parseInt(args[index]) ;//涓�鍖呮锛�1鏄紝0鍚�
             ServerProperties.argOneDie = oneDie == 1?true:false ;
         }
     }
-    private static void getArg2(String[] args){
-        if(args[2] != null && !args[2].trim().equals("")){
-            Integer multiDie = Integer.parseInt(args[2]) ;//澶氬寘姝伙細1鏄紝0鍚�
+    private static void getArg3(String[] args, int index){
+        if(args[index] != null && !args[index].trim().equals("")){
+            Integer multiDie = Integer.parseInt(args[index]) ;//澶氬寘姝伙細1鏄紝0鍚�
             ServerProperties.argMultiDie = multiDie == 1?true:false ;
         }
     }
-    private static void getArg3(String[] args){
-        if(args[3] instanceof String){
-            if(args[3] != null && !args[3].trim().equals("")){
-                String serverIp = (String)args[3] ;//鏈嶅姟绔疘P
+    private static void getArg4(String[] args, int index){
+        if(args[index] instanceof String){
+            if(args[index] != null && !args[index].trim().equals("")){
+                String serverIp = (String)args[index] ;//鏈嶅姟绔疘P
                 ServerProperties.serverIp = serverIp ;
             }
         }
     }
-    private static void getArg4(String[] args){
-        if(args[4] != null && !args[4].trim().equals("")){
-            Integer serverPort = Integer.parseInt(args[4]) ;//鏈嶅姟绔痯ort
+    private static void getArg5(String[] args, int index){
+        if(args[index] != null && !args[index].trim().equals("")){
+            Integer serverPort = Integer.parseInt(args[index]) ;//鏈嶅姟绔痯ort
             ServerProperties.serverPort = serverPort ;
         }
     }
diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java
index edb849f..36c29b8 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/ServerProperties.java
@@ -12,6 +12,7 @@
 
     //绋嬪簭鍚姩鍙傛暟锛�
     public static String argRtuAddr ;
+    public static Boolean argHeardBeatBy02True81False = true ;
     public static Boolean argOneDie = false ;
     public static Boolean argMultiDie = false ;
 
diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java
index ae2a3a1..41b0fe6 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java
@@ -14,14 +14,12 @@
 
 public class TcpClUnit implements UnitInterface {
 
-    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
-
     private static TcpClUnit instance = new TcpClUnit() ;
 
     public static TcpClUnitAdapter adapter ;
     public static TcpClUnitConfigVo confVo ;
 
-    public static IoSession session ;
+    private static Worker worker ;
 
     private TcpClUnit(){} ;
 
@@ -39,12 +37,13 @@
         if(TcpClUnit.confVo == null){
             throw new Exception("Tcp Client妯″潡閰嶇疆瀵硅薄涓嶈兘涓虹┖锛�") ;
         }
+        TcpClUnit.worker = Worker.getInstance(TcpClUnit.confVo) ;
     }
 
     @Override
     public void start(UnitCallbackInterface callback) throws Exception {
         System.out.println("Tcp Client妯″潡鎴愬姛鍚姩");
-        this.doStart();
+        TcpClUnit.worker.doStart();
         callback.call(null) ;
     }
 
@@ -53,67 +52,5 @@
         callback.call(null);
     }
 
-    private void doStart(){
-        new Thread(() -> {
-            Exception ex ;
-            while(true){
-                ex = null ;
-                try {
-                    new TcpConnect().createSession(
-                            confVo.mwServerIp,
-                            confVo.mwServerPort,
-                            confVo.connectTimeout,
-                            new TcpHandler(),
-                            new Callback() {
-                                @Override
-                                public void call(Object obj) {
-                                    if (obj == null) {
-                                        log.error("鍒涘缓缃戠粶浼氳瘽杩斿洖涓簄ull");
-                                    } else {
-                                        TcpClUnit.session = (IoSession) obj;
-                                        log.info("鎴愬姛鍒涘缓涓庨�氫俊涓棿浠剁殑缃戠粶杩炴帴");
-                                        UpData.setSession(TcpClUnit.session);
-                                        heartBeat() ;
-                                    }
-                                }
-                                @Override
-                                public void call(Object... objs) {
-                                }
-                                @Override
-                                public void exception(Exception e) {
-                                }
-                            });
-                }catch (Exception e){
-                   ex = e ;
-                }
-                if(ex == null){
-                    break ;
-                }else{
-                    try{
-                        Thread.sleep(100);
-                    }catch (Exception e){
-                    }
-                }
-            }
-        }).start();
-    }
-
-
-    private void heartBeat(){
-        new Thread(new Runnable(){
-            @Override
-            public void run() {
-                while(true){
-                    try {
-                        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
-                        Thread.sleep(30000L);
-                        //Thread.sleep(30000000L);
-                    }catch (Exception e){
-                        continue;
-                    }
-                }
-            }
-        }).start();
-    }
 
 }
diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java
new file mode 100644
index 0000000..ba73ff4
--- /dev/null
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/Worker.java
@@ -0,0 +1,96 @@
+package com.dy.simRtu.tcpClient;
+
+import com.dy.common.util.Callback;
+import com.dy.simRtu.ServerProperties;
+import com.dy.simRtu.tcpClient.upData.UpData;
+import com.dy.simRtu.tcpClient.upData.UpHeartBeat;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.mina.core.session.IoSession;
+
+/**
+ * @Author: liurunyu
+ * @Date: 2024/12/6 8:49
+ * @Description
+ */
+public class Worker {
+
+    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
+
+    private static Worker instance = new Worker() ;
+
+    private static TcpClUnitConfigVo confVo ;
+
+    public static IoSession session ;
+
+    private Worker(){
+    }
+
+    public static Worker getInstance(TcpClUnitConfigVo confVo){
+        Worker.confVo = confVo ;
+        return instance ;
+    }
+
+    public void doStart(){
+        new Thread(() -> {
+            Exception ex ;
+            while(true){
+                ex = null ;
+                try {
+                    new TcpConnect().createSession(
+                            confVo.mwServerIp,
+                            confVo.mwServerPort,
+                            confVo.connectTimeout,
+                            new TcpHandler(),
+                            new Callback() {
+                                @Override
+                                public void call(Object obj) {
+                                    if (obj == null) {
+                                        log.error("鍒涘缓缃戠粶浼氳瘽杩斿洖涓簄ull");
+                                    } else {
+                                        Worker.session = (IoSession) obj;
+                                        log.info("鎴愬姛鍒涘缓涓庨�氫俊涓棿浠剁殑缃戠粶杩炴帴");
+                                        UpData.setSession(Worker.session);
+                                        heartBeat() ;
+                                    }
+                                }
+                                @Override
+                                public void call(Object... objs) {
+                                }
+                                @Override
+                                public void exception(Exception e) {
+                                }
+                            });
+                }catch (Exception e){
+                    ex = e ;
+                }
+                if(ex == null){
+                    break ;
+                }else{
+                    try{
+                        Thread.sleep(100);
+                    }catch (Exception e){
+                    }
+                }
+            }
+        }).start();
+    }
+
+
+    private void heartBeat(){
+        new Thread(new Runnable(){
+            @Override
+            public void run() {
+                while(true){
+                    try {
+                        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
+                        Thread.sleep(30000L);
+                        //Thread.sleep(30000000L);
+                    }catch (Exception e){
+                        continue;
+                    }
+                }
+            }
+        }).start();
+    }
+}
diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java
index 8185fd2..6acfbcc 100644
--- a/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java
+++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/upData/UpHeartBeat.java
@@ -2,6 +2,8 @@
 
 import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
 import com.dy.common.util.ByteUtil;
+import com.dy.common.util.DateTime;
+import com.dy.simRtu.ServerProperties;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -22,7 +24,12 @@
         if(upHeartBeat){
             try{
                 if(UpData.session != null && UpData.session.isConnected()){
-                    byte[] bs = createData(rtuAddr) ;
+                    byte[] bs ;
+                    if(ServerProperties.argHeardBeatBy02True81False){
+                        bs = createCd02Data(rtuAddr) ;
+                    }else{
+                        bs = createCd81Data(rtuAddr) ;
+                    }
                     UpData.upSend(bs) ;
                 }else{
                     log.error("鏈繛鎺ラ�氫俊涓棿浠讹紝涓嶈兘鍙戦�佹暟鎹�");
@@ -39,7 +46,7 @@
      * @return 瀛楄妭鏁扮粍
      * @throws Exception 寮傚父
      */
-    private static byte[] createData(String rtuAddr) throws Exception {
+    private static byte[] createCd02Data(String rtuAddr) throws Exception {
         byte[] bytes = creatHead(rtuAddr, "02", (byte)0xB0);
 
         byte[] bs = new byte[1] ;
@@ -60,4 +67,43 @@
 
         return bytes ;
     }
+
+
+
+    /**
+     * 鏋勯�犲績璺虫暟鎹�
+     * @return 瀛楄妭鏁扮粍
+     * @throws Exception 寮傚父
+     */
+    private static byte[] createCd81Data(String rtuAddr) throws Exception {
+        byte[] bytes = creatHead(rtuAddr, "81", (byte)0xB0);
+
+        int index = 0 ;
+        byte[] bs1 = new byte[4] ;
+        bs1[index++] = (byte)0x22 ;
+        bs1[index++] = (byte)0x6 ;
+        bs1[index++] = (byte)0x44 ;
+        bs1[index++] = (byte)0x00 ;
+        bytes = ByteUtil.bytesMerge(bytes, bs1) ;
+
+        int[] ymdhms = DateTime.yyyy_MM_dd_HH_MM_SS_2_ymdhmsGroup(DateTime.yyyy_MM_dd_HH_mm_ss()) ;
+        byte[] bs2 = new byte[6] ;
+        index = 0 ;
+        int index1 = 5 ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
+        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--] - 2000)[0] ;
+        bytes = ByteUtil.bytesMerge(bytes, bs2) ;
+
+        GlCreate.createLen(bytes);//闀垮害鏀惧瓧鑺傛暟缁勪腑
+
+        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC鍜屽熬鍙犲姞瀛楄妭鏁扮粍涓�
+
+        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
+
+        return bytes ;
+    }
 }

--
Gitblit v1.8.0