From d9765e031e100e1d8eea34e086cb9c81766e1a00 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期五, 21 二月 2025 09:52:14 +0800
Subject: [PATCH] 针对王江海制定的协议: 1、优化相关注释; 2、优化代码; 3、协议中报警信息有变化:外门报警改为电池电压报警、增加阀门开关状态、增加阀门堵转报警等。

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java |   93 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 79 insertions(+), 14 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
index 9918155..dd0eedd 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V202404/CommonV202404.java
@@ -1,11 +1,25 @@
 package com.dy.common.mw.protocol.p206V202404;
 
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1;
+import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2;
 import com.dy.common.util.ByteUtil;
 import com.dy.common.util.ByteUtilUnsigned;
+import com.dy.common.util.CRC16;
 import com.dy.common.util.CRC8_for_2_0;
+import org.apache.mina.core.session.IoSession;
 
 
 public class CommonV202404 {
+
+    /**
+     * 鍦↖o浼氳瘽涓缃崗璁悕绉板強鐗堟湰鍙�
+     * @param ioSession 浼氳瘽
+     */
+    public void setThisProtocolArr2IoSession(IoSession ioSession){
+        ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolName) ;
+        ioSession.setAttribute(TcpIoSessionAttrIdIsRtuAddr.sessionArrProtocolName, ProtocolConstantV206V1.protocolVer) ;
+    }
     /**
      * 妫�鏌ュご
      * @param bs 涓婅瀛楄妭鏁扮粍
@@ -40,23 +54,24 @@
     }
 
     /**
-     * 妫�鏌ュご
+     * 妫�鏌ュ崗璁被鍨�
      * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鍗忚绫诲瀷
      * @throws Exception 寮傚父
      */
-    public void checkHead(byte[] bs) throws Exception{
-        if(bs.length < ProtocolConstantV206V202404.lenHead2Code
-                || bs[0] != ProtocolConstantV206V202404.P_Head_Byte){
-            throw new Exception("涓婅鏁版嵁甯уご涓嶆纭紒") ;
+    public Boolean protocolType_p206TrueUgFalse(byte[] bs){
+        if(bs == null){
+            return null ;
+        }else if(bs.length >= (ProtocolConstantV206V202404.ctrlIndex)
+                && bs[0] == ProtocolConstantV206V202404.P_Head_Byte
+                && bs[2] == ProtocolConstantV206V202404.P_Head_Byte){
+            return true ;
+        }else if(bs.length >= (ProtocolConstantV206V202404.UG_codeIndex)
+                && bs[0] == ProtocolConstantV206V202404.UG_P_Head_Byte
+                && bs[3] == ProtocolConstantV206V202404.UG_P_Head_Byte){
+            return false ;
         }else{
-            if(bs.length >= ProtocolConstantV206V202404.lenHead2Code){
-                if(bs[2] != ProtocolConstantV206V202404.P_Head_Byte
-                    && (byte)(bs[2] & 0xF8) != ProtocolConstantV206V202404.P_Head_Byte){
-                    throw new Exception("涓婅鏁版嵁甯уご涓嶆纭紒") ;
-                }
-            }else{
-                throw new Exception("涓婅鏁版嵁甯уご涓嶆纭紒") ;
-            }
+            return null ;
         }
     }
 
@@ -97,8 +112,19 @@
      * @return 鏁版嵁闀垮害
      * @throws Exception 寮傚父
      */
-    public int parseDataLen(byte[] bs)throws Exception{
+    public int parseDataLen4P202404(byte[] bs)throws Exception{
         return parseFrameLen(bs) ;
+    }
+
+    /**
+     * 鍒嗘瀽鐢ㄦ埛鏁版嵁鍩熷瓧鑺傛暟(鍗囩骇鍗忚)
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鏁版嵁闀垮害
+     * @throws Exception 寮傚父
+     */
+    public int parseDataLen4Ug(byte[] bs)throws Exception{
+        int len = ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V2.UG_dataLenIndex_start) ;
+        return len - ProtocolConstantV206V2.UG_lenCmd - ProtocolConstantV206V2.UG_lenRtuAddr ;
     }
 
 
@@ -145,6 +171,18 @@
     }
 
     /**
+     * 鍒嗘瀽鍔熻兘鐮�
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鍔熻兘鐮�
+     */
+    public String parseCode(byte[] bs, boolean p202404TrueUgFalse){
+        if(p202404TrueUgFalse) {
+            return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V202404.codeIndex, 1);
+        }else{
+            return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V202404.UG_codeIndex, 2);
+        }
+    }
+    /**
      * 鏍¢獙鍜屾鏌�
      * @param bs  涓婅瀛楄妭鏁扮粍
      * @return 閫氳繃null锛屾湭閫氳繃杩斿洖鍘熷洜
@@ -160,6 +198,33 @@
         }
     }
 
+    /**
+     * 鏍¢獙鍜屾鏌�
+     * @param bs  涓婅瀛楄妭鏁扮粍
+     * @param p202404TrueUgFalse 202404鍗忚涓簍rue锛屽崌绾у崗璁负false
+     * @return 閫氳繃null锛屾湭閫氳繃杩斿洖鍘熷洜
+     * @throws Exception 寮傚父
+     */
+    public String checkCrc_str(byte[] bs, boolean p202404TrueUgFalse) throws Exception {
+        if(p202404TrueUgFalse){
+            byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V202404.ctrlIndex, bs.length - 3) ;
+            byte crcInBs = bs[bs.length - 2] ;
+            if(crcCompute == crcInBs){
+                return null ;
+            }else{
+                return "璁$畻CRC鏄�:" + crcCompute + "锛屼笂浼燙RC鏄�" + crcInBs ;
+            }
+        }else{
+            short crcCompute = new CRC16().CRC(bs, 0, bs.length - 4) ;
+            short crcInBs = ByteUtil.bytes2Short_BE(bs,bs.length - 3) ;
+            //int crcInBs = ByteUtilUnsigned.bytes2Short_BE(bs, bs.length - 3) ;
+            if(crcCompute == crcInBs){
+                return null ;
+            }else{
+                return "璁$畻CRC鏄�:" + crcCompute + "锛屼笂浼燙RC鏄�" + crcInBs ;
+            }
+        }
+    }
 
     /*
     鏋勯�犳帶鍒跺煙

--
Gitblit v1.8.0