From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期一, 07 四月 2025 15:18:51 +0800
Subject: [PATCH] 整理版本

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/CommonV1.java |  280 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 280 insertions(+), 0 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/CommonV1.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/CommonV1.java
new file mode 100644
index 0000000..e4619c3
--- /dev/null
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/CommonV1.java
@@ -0,0 +1,280 @@
+package com.dy.common.mw.protocol.p206V1;
+
+import com.dy.common.mw.channel.tcp.TcpIoSessionAttrIdIsRtuAddr;
+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 CommonV1 {
+
+
+	/**
+	 * 妫�鏌ュご
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @return [銆�0銆�->true:鏄湰鍗忚P206锛宖alse:涓嶆槸鏈崗璁�;  銆�1銆�->true:鏄按璧勬簮鍗忚锛宖alse:鏄崌绾у崗璁甝
+	 * @throws Exception  寮傚父
+	 */
+	public Boolean[] isThisProtocolHead(byte[] bs) throws Exception{
+		if(bs == null){
+			return null ;
+		}else if(bs.length >= (ProtocolConstantV206V1.ctrlIndex)
+				&& bs[0] == ProtocolConstantV206V1.P_Head_Byte
+				&& bs[2] == ProtocolConstantV206V1.P_Head_Byte){
+			return new Boolean[]{true, true} ;
+		}else if(bs.length >= (ProtocolConstantV206V1.UG_codeIndex)
+				&& bs[0] == ProtocolConstantV206V1.UG_P_Head_Byte
+				&& bs[3] == ProtocolConstantV206V1.UG_P_Head_Byte){
+			return new Boolean[]{true, false} ;
+		}else{
+			return new Boolean[]{false, false} ;
+		}
+	}
+
+	/**
+     * 妫�鏌ュ崗璁被鍨�
+     * @param bs 涓婅瀛楄妭鏁扮粍
+     * @return 鍗忚绫诲瀷
+     * @throws Exception 寮傚父
+	 */
+	public Boolean protocolType_p206TrueUgFalse(byte[] bs){
+		if(bs == null){
+			return null ;
+		}else if(bs.length >= (ProtocolConstantV206V1.ctrlIndex)
+				&& bs[0] == ProtocolConstantV206V1.P_Head_Byte
+				&& bs[2] == ProtocolConstantV206V1.P_Head_Byte){
+			return true ;
+		}else if(bs.length >= (ProtocolConstantV206V1.UG_codeIndex)
+				&& bs[0] == ProtocolConstantV206V1.UG_P_Head_Byte
+				&& bs[3] == ProtocolConstantV206V1.UG_P_Head_Byte){
+			return false ;
+		}else{
+			return null ;
+		}
+	}
+
+	/**
+	 * 妫�鏌ュ熬
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @throws Exception 寮傚父
+	 */
+	public void checkTail(byte[] bs) throws Exception{
+		if(bs.length < 1 || bs[bs.length - 1] != ProtocolConstantV206V1.P_Tail_Byte){
+			throw new Exception("涓婅鏁版嵁灏句笉姝g‘锛�") ;
+		}
+	}
+
+
+	/**
+	 * 鍒嗘瀽甯ч暱搴�
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @param p206TrueUgFalse 206鍗忚涓簍rue锛屽崌绾у崗璁负false
+	 * @return 鏁版嵁闀垮害
+	 * @throws Exception 寮傚父
+	 */
+	public int parseFrameLen(byte[] bs, boolean p206TrueUgFalse)throws Exception{
+		if(p206TrueUgFalse) {
+			int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1.dataLenIndex);
+			return len + ProtocolConstantV206V1.lenHead2ctrl + ProtocolConstantV206V1.lenTail;
+		}else{
+			int len = ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1.UG_dataLenIndex_start);
+			return len + ProtocolConstantV206V1.UG_lenHead2Cmd + ProtocolConstantV206V1.UG_lenTail;
+		}
+	}
+
+
+	/**
+	 * 鍒嗘瀽鐢ㄦ埛鏁版嵁鍩熷瓧鑺傛暟(榛樿鏄潪鍗囩骇鍗忚)
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @return 鏁版嵁闀垮害
+	 * @throws Exception 寮傚父
+	 */
+	public int parseDataLen4P206(byte[] bs)throws Exception{
+		int len = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1.dataLenIndex) ;
+		return len - ProtocolConstantV206V1.lenCtrl - ProtocolConstantV206V1.lenRtuAddr ;
+	}
+
+	/**
+	 * 鍒嗘瀽鐢ㄦ埛鏁版嵁鍩熷瓧鑺傛暟(鍗囩骇鍗忚)
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @return 鏁版嵁闀垮害
+	 * @throws Exception 寮傚父
+	 */
+	public int parseDataLen4Ug(byte[] bs)throws Exception{
+		int len = ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V1.UG_dataLenIndex_start) ;
+		return len - ProtocolConstantV206V1.UG_lenCmd - ProtocolConstantV206V1.UG_lenRtuAddr ;
+	}
+
+
+	/**
+	 * 鍒嗘瀽Rtu鍦板潃
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @param p206TrueUgFalse 206鍗忚涓簍rue锛屽崌绾у崗璁负false
+	 * @return 鎺у埗鍣ㄥ湴鍧�
+	 * @throws Exception 寮傚父
+	 */
+	public String parseRtuAddr(byte[] bs, boolean p206TrueUgFalse)throws Exception{
+		if(p206TrueUgFalse){
+			String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1.rtuAddr1Index_start, ProtocolConstantV206V1.rtuAddr1Index_end) ;
+			String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, ProtocolConstantV206V1.rtuAddr2Index_start) ;
+			while(rtuAddrStr.length() <= 5){
+				rtuAddrStr = "0" + rtuAddrStr ;
+			}
+			return rtuAddrBCD + rtuAddrStr ;
+		}else{
+			return ("" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1.UG_rtuAddrIndex_start, ProtocolConstantV206V1.UG_rtuAddrIndex_end)) ;
+		}
+	}
+
+
+	/**
+	 * 鍒嗘瀽Rtu鍦板潃
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @param index 鍚浣�
+	 * @return 鎺у埗鍣ㄥ湴鍧�
+	 * @throws Exception 寮傚父
+	 */
+	public String parseRtuAddr(byte[] bs, int index, boolean p206TrueUgFalse)throws Exception{
+		if(p206TrueUgFalse){
+			String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + 2) ;//鍦板潃鏄ぇ绔ā寮�
+			String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, index + 3) ;
+			while(rtuAddrStr.length() <= 5){
+				rtuAddrStr = "0" + rtuAddrStr ;
+			}
+			return rtuAddrBCD + rtuAddrStr ;
+		}else{
+			throw new Exception("鍗囩骇鍗忚鏃犺缃湴鍧�") ;
+		}
+	}
+
+
+	/**
+	 * 鍒嗘瀽鍔熻兘鐮�
+	 * @param bs 涓婅瀛楄妭鏁扮粍
+	 * @return 鍔熻兘鐮�
+	 */
+	public String parseCode(byte[] bs, boolean p206TrueUgFalse){
+		if(p206TrueUgFalse) {
+			return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1.codeIndex, 1);
+		}else{
+			return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1.UG_codeIndex, 2);
+		}
+	}
+
+	/**
+	 * 鏍¢獙鍜屾鏌�
+	 * @param bs  涓婅瀛楄妭鏁扮粍
+	 * @param p206TrueUgFalse 206鍗忚涓簍rue锛屽崌绾у崗璁负false
+	 * @return 閫氳繃null锛屾湭閫氳繃杩斿洖鍘熷洜
+	 * @throws Exception 寮傚父
+	 */
+	public String checkCrc_str(byte[] bs, boolean p206TrueUgFalse) throws Exception {
+		if(p206TrueUgFalse){
+			byte crcCompute = (byte)new CRC8_for_2_0().CRC8(bs, ProtocolConstantV206V1.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 ;
+			}
+		}
+	}
+
+
+	/*
+	鏋勯�犳帶鍒跺煙
+	D7                  D6                  D5锝濪4            D3锝濪0
+	浼犺緭鏂瑰悜浣� DIR        鎷嗗垎鏍囧織浣� DIV       甯ц鏁颁綅 FCB       鍔熻兘鐮�
+	*/
+	public byte createCtrl(byte dir, byte funcCode){
+		byte b = dir;//(byte)0x80//鎺у埗鍩燂細DIR=1锛岃〃绀烘甯ф姤鏂囨槸鐢辩粓绔彂鍑虹殑涓婅鎶ユ枃锛�
+		b = (byte)(b | funcCode) ;
+		//FCB == 3
+		b = (byte)(b | (byte)0x18) ;
+		//DIV = 0
+		//DIR = 0
+		return b ;
+	}
+	/**
+	 * 寰楀埌IC鍗$被鍨嬪悕绉� 锛� 鍗$被鍨�(1:鐢ㄦ埛鍗★紱2锛氱鐞嗗憳鍗★紱3锛氳皟璇曞崱锛�4锛氬贰妫�鍗★紱5锛氭竻绌哄崱)锛�
+	 * @param type 瀛楄妭
+	 * @return 鍚嶇О
+	 */
+	public static String icCardType(byte type){
+		return switch (type) {
+			case 0 -> "鏃犲崱";
+			case 1 -> "鐢ㄦ埛鍗�";
+			case 2 -> "绠$悊鍛樺崱";
+			case 3 -> "璋冭瘯鍗�";
+			case 4 -> "宸℃鍗�";
+			case 5 -> "娓呯┖鍗�";
+			default -> "鏈煡";
+		};
+	}
+
+	/**
+	 * 寰楀埌鍏冲紑闃�绫诲瀷鍚嶇О
+	 * 寮�鍏抽榾绫诲瀷(
+	 * 1:鍒峰崱寮�闃�锛�
+	 * 2:鍒峰崱鍏抽榾锛�
+	 * 3:涓績绔欏紑闃�锛�
+	 * 4:涓績绔欏叧闃�锛�
+	 * 5:浣欓涓嶈冻鍏抽榾锛�
+	 * 6:娴侀噺璁℃晠闅滃叧闃�锛�
+	 * 7:绱ф�ュ叧闂紱
+	 * 8:鐢ㄦ埛杩滅▼寮�闃�锛�
+	 * 9:鐢ㄦ埛杩滅▼鍏抽榾锛�
+	 * 10:宸℃鍗″叧闃�锛�
+	 * 11:宸℃鍗″埛鍗″崱寮�闃�锛�
+	 * 12:榛戝悕鍗曞懡浠ゅ叧闃�锛�
+	 * 13:杩滅▼瀹氭椂鍏抽榾锛�
+	 * 14:杩滅▼瀹氶噺鍏抽榾锛�
+	 * 16:绠¢亾鏃犳按鑷姩鍏抽榾锛�(鐜嬫睙娴峰崗璁�)
+	 * )
+	 * @param type 瀛楄妭
+	 * @return 鍚嶇О
+	 */
+	public static String openCloseValveType(Byte type){
+		if(type == null){
+			return "" ;
+		}
+		return switch (type) {
+			case 1 -> "鍒峰崱寮�闃�";
+			case 2 -> "鍒峰崱鍏抽榾";
+			case 3 -> "涓績绔欏紑闃�";
+			case 4 -> "涓績绔欏叧闃�";
+			case 5 -> "浣欓涓嶈冻鍏抽榾";
+			case 6 -> "娴侀噺璁℃晠闅滃叧闃�";
+			case 7 -> "绱ф�ュ叧闃�";
+			case 8 -> "鐢ㄦ埛杩滅▼寮�闃�";
+			case 9 -> "鐢ㄦ埛杩滅▼鍏抽榾";
+			case 10 -> "宸℃鍗″叧闃�";
+			case 11 -> "宸℃鍗″紑闃�";
+			case 12 -> "榛戝悕鍗曞懡浠ゅ叧闃�";
+			case 13 -> "杩滅▼瀹氭椂鍏抽榾";
+			case 14 -> "杩滅▼瀹氶噺鍏抽榾";
+			case 16 -> "绠¢亾鏃犳按鑷姩鍏抽榾";
+			default -> "鏈煡";
+		};
+	}
+	public static Boolean isCloseValveType(byte type){
+		return switch (type) {
+			case 1, 3, 8, 11 -> false ;
+			case 2, 4, 5, 6, 7, 9, 10, 12, 13, 14 -> true ;
+			default -> null ;
+		};
+	}
+
+
+}

--
Gitblit v1.8.0