From bda423b1aae1d5322116511651ae03a54cdcd231 Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期二, 26 十一月 2024 17:25:39 +0800
Subject: [PATCH] 1、几个测试模块系统的log4j.xml配置中的编码修改; 2、RTU升级模拟器增加命令行设置服务端IP和端口功能; 3、应王江海要求,井电双控协议中也实现远程升级功能,因井电双控协议、阀控器协议不同,而双方协议中的升级子协议相同,所以通信中间件做增量开发、适应性修改,以适应这种混合协议情况,同时也提高通信中间件运行效率。

---
 pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java |  301 +++++++++++++++++++++++++------------------------
 1 files changed, 154 insertions(+), 147 deletions(-)

diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java
index 3eea8c9..f8cfc6e 100644
--- a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java
+++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/AnnotationScan.java
@@ -1,13 +1,13 @@
 package com.dy.common.mw.protocol;
 
-import java.net.URI;
-import java.net.URL;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.List;
+import java.util.Set;
 
-import com.dy.common.util.ClassScan;
 import com.dy.common.util.NumUtil;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.reflections.Reflections;
 
 /**
  * 鎵弿锛�
@@ -19,14 +19,16 @@
  */
 @SuppressWarnings("unused")
 public class AnnotationScan {
-	
-	private static String scanRootPackage = "com.dy.common.mw.protocol." ;
+
+	private static final Logger log = LogManager.getLogger(AnnotationScan.class);
+
+	private static String scanRootPackage = "com.dy.common.mw.protocol" ;
 
 	private static AnnotationScan instance ;
-	
+
 	private AnnotationScan(){
 	}
-	
+
 	/**
 	 * 寰楀埌鍞竴瀹炰緥锛�
 	 * 骞跺湪鐢熸垚鍞竴瀹炰緥鏃讹紝鎵弿娉ㄨВ绫�
@@ -40,148 +42,153 @@
 		}
 		return instance ;
 	}
-	
+
 	/**
 	 * 鎵弿鍑烘敞瑙g殑绫�
 	 * @throws ClassNotFoundException 寮傚父
 	 * @throws Exception 寮傚父
 	 */
-	private void scanEndpointAnnotations() throws Exception{
-		ClassLoader load = this.getClass().getClassLoader() ;
-		
-		if(!scanRootPackage.endsWith(".")){
-			scanRootPackage += "." ;
-		}
-		URL url = AnnotationScan.class.getResource("/" + scanRootPackage.replace('.', '/'));
-		if(url != null){
-			String urlStr = url.toString() ;
-			if(urlStr != null){
-				URI uri = new URI(url.toString());
+	private void scanEndpointAnnotations() throws Exception {
+		Reflections reflections = new Reflections(scanRootPackage); // 鎸囧畾鍖呭悕
+		Set<Class<?>> driverClasses = reflections.getTypesAnnotatedWith(AnnotationDriver.class);
+		if (driverClasses != null && driverClasses.size() > 0) {
+			String protocolName;
+			short protocolVersion;
+			String key ;
+			int priority;
+			int onLineDataMinLength;
+			int headMinLength;
 
-				List<String> classNames = new ClassScan().searchClassFromUrl(new URI[]{uri}, scanRootPackage) ;
-				if(classNames != null && classNames.size() > 0){
-					String protocolName ;
-					int priority;
-					int onLineDataMinLength;
-					int headMinLength ;
+			HashMap<String, AnnotationDriverVo> driverMap = ProtocolCache.getDriverMap();
+			HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = ProtocolCache.getPrefixedDataAvailableMap();
+			HashMap<String, AnnotationOnLineVo> onLineMap = ProtocolCache.getOnLineMap();
 
-					HashMap<String, AnnotationDriverVo> driverMap = ProtocolCache.getDriverMap() ;
-					HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap = ProtocolCache.getPrefixedDataAvailableMap() ;
-					HashMap<String, AnnotationOnLineVo> onLineMap = ProtocolCache.getOnLineMap() ;
-
-					for(String cName : classNames){
-						String className = cName.substring(0,  cName.lastIndexOf('.')) ;
-						Class<?> clazz = load.loadClass(className) ;
-						if(clazz.isAnnotationPresent(AnnotationDriver.class)){
-							AnnotationDriver ann = clazz.getAnnotation(AnnotationDriver.class) ;
-							if(ann != null){
-								if(ann.enable()){
-									protocolName = ann.name() ;
-									if(!(protocolName.trim().equals(""))){
-										if(driverMap.containsKey(protocolName)){
-											throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔ㄥ悕绉�" + protocolName + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ;
-										}
-										driverMap.put(protocolName, new AnnotationDriverVo(clazz)) ;
-									}else{
-										throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔ㄥ悕绉版敞瑙e�间负绌哄瓧绗︿覆") ;
-									}
-								}
-							}
+			for (Class<?> clazz : driverClasses) {
+				AnnotationDriver ann = clazz.getAnnotation(AnnotationDriver.class);
+				if (ann != null) {
+					if (ann.enable()) {
+						protocolName = ann.name();
+						protocolVersion = ann.version() ;
+						if(protocolName == null || protocolName.trim().equals("")){
+							throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔ㄥ悕绉版敞瑙e�间负绌哄瓧绗︿覆");
 						}
-
-
-						if(clazz.isAnnotationPresent(AnnotationPrefixedDataAvailable.class)){
-							AnnotationPrefixedDataAvailable ann = clazz.getAnnotation(AnnotationPrefixedDataAvailable.class) ;
-							if(ann != null){
-								if(ann.enable()){
-									protocolName = ann.protocolName() ;
-									if(!(protocolName.trim().equals(""))){
-										if(prefixedDataAvailableMap.containsKey(protocolName)){
-											throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶅崗璁┍鍔�" + protocolName + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ;
-										}
-									}else{
-										throw new Exception("涓ラ噸閿欒锛屼笂琛屾暟鎹畬鏁存�ф鏌ユ墍閰嶅崗璁┍鍔ㄦ敞瑙e�间负绌哄瓧绗︿覆") ;
-									}
-
-									priority = ann.priority() ;
-									if(priority == 0){
-										throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄紭鍏堢骇娉ㄨВ涓嶈兘涓�0") ;
-									}
-									if(isRepeatPrefixedDataAvailablePriority(priority, prefixedDataAvailableMap)){
-										throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄紭鍏堢骇娉ㄨВ(priority)鏁板��" + priority + "閲嶅") ;
-									}
-
-									onLineDataMinLength = ann.onLineDataMinLength() ;
-									/*姘旇薄鍗忚涓�0
-									if(onLineDataMinLength == 0){
-										throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄笂绾挎暟鎹渶灏忛暱搴︽敞瑙d笉鑳戒负0") ;
-									}
-									*/
-												headMinLength = ann.headMinLength() ;
-									/*姘旇薄鍗忚涓�0
-									if(headMinLength == 0){
-										throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄笂鎶ユ暟鎹殑澶撮儴鏈�灏忛暱搴︽敞瑙d笉鑳戒负0") ;
-									}
-									*/
-									prefixedDataAvailableMap.put(protocolName, new AnnotationPrefixedDataAvailableVo(clazz, protocolName, priority, onLineDataMinLength, headMinLength, ProtocolConstant.errorMaxLength)) ;
-								}
-							}
+						if(protocolVersion == -1){
+							throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔ㄧ増鏈彿鏈厤缃�");
 						}
-
-						if(clazz.isAnnotationPresent(AnnotationOnLine.class)){
-							AnnotationOnLine ann = clazz.getAnnotation(AnnotationOnLine.class) ;
-							if(ann != null){
-								if(ann.enable()){
-									protocolName = ann.protocolName() ;
-									if(!(protocolName.trim().equals(""))){
-										if(onLineMap.containsKey(protocolName)){
-											throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶅崗璁┍鍔�" + protocolName + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ;
-										}
-									}else{
-										throw new Exception("涓ラ噸閿欒锛屼笂绾挎暟鎹垎鏋愭墍閰嶅崗璁┍鍔ㄦ敞瑙e�间负绌哄瓧绗︿覆") ;
-									}
-
-									priority = ann.priority() ;
-									if(priority == 0){
-										throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶄紭鍏堢骇娉ㄨВ涓嶈兘涓�0") ;
-									}
-									if(isRepeatOnLinePriority(priority, onLineMap)){
-										throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶄紭鍏堢骇娉ㄨВ(priority)鏁板��" + priority + "閲嶅") ;
-									}
-
-									onLineMap.put(protocolName, new AnnotationOnLineVo(clazz, protocolName, priority)) ;
-								}
-							}
+						key = protocolName + protocolVersion ;
+						if (driverMap.containsKey(key)) {
+							throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔�(鍚嶇О+鐗堟湰鍙�)" + key + "鍑虹幇浜嗛噸澶嶆敞瑙o紒");
 						}
+						driverMap.put(key, new AnnotationDriverVo(clazz));
 					}
-
-					String error = this.hasErrorInDriver(driverMap) ;
-					if(error != null){
-						throw new Exception(error) ;
-					}else{
-						error = this.hasErrorInPrefixedDataAvailable(driverMap, prefixedDataAvailableMap) ;
-						if(error != null){
-							throw new Exception(error) ;
-						}else{
-							error = this.hasErrorInOnLine(driverMap, onLineMap) ;
-							if(error != null){
-								throw new Exception(error) ;
-							}
-						}
-					}
-				}else{
-					throw new Exception("涓ラ噸閿欒锛� 鎵弿鍗忚鐩稿叧娉ㄨВ鎵�寰楀埌绫诲瀷闆嗗悎涓虹┖锛屾壂鎻忔牴璺緞鏄�" + scanRootPackage) ;
 				}
 			}
+
+			Set<Class<?>> prefixedClass = reflections.getTypesAnnotatedWith(AnnotationPrefixedDataAvailable.class);
+			for (Class<?> clazz : prefixedClass) {
+				AnnotationPrefixedDataAvailable ann = clazz.getAnnotation(AnnotationPrefixedDataAvailable.class);
+				if (ann != null) {
+					if (ann.enable()) {
+						protocolName = ann.protocolName();
+						if (!(protocolName.trim().equals(""))) {
+							if (prefixedDataAvailableMap.containsKey(protocolName)) {
+								throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶅崗璁┍鍔�" + protocolName + "鍑虹幇浜嗛噸澶嶆敞瑙o紒");
+							}
+						} else {
+							throw new Exception("涓ラ噸閿欒锛屼笂琛屾暟鎹畬鏁存�ф鏌ユ墍閰嶅崗璁┍鍔ㄦ敞瑙e�间负绌哄瓧绗︿覆");
+						}
+
+						protocolVersion = ann.protocolVersion();
+						if (protocolVersion == -1) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶅崗璁増鏈彿鏈厤缃�");
+						}
+
+						priority = ann.priority();
+						if (priority == 0) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄紭鍏堢骇娉ㄨВ涓嶈兘涓�0");
+						}
+						if (isRepeatPrefixedDataAvailablePriority(priority, prefixedDataAvailableMap)) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄紭鍏堢骇娉ㄨВ(priority)鏁板��" + priority + "閲嶅");
+						}
+
+						onLineDataMinLength = ann.onLineDataMinLength();
+						/*姘旇薄鍗忚涓�0
+						if(onLineDataMinLength == 0){
+							throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄笂绾挎暟鎹渶灏忛暱搴︽敞瑙d笉鑳戒负0") ;
+						}
+						*/
+						headMinLength = ann.headMinLength();
+						/*姘旇薄鍗忚涓�0
+						if(headMinLength == 0){
+							throw new Exception("涓ラ噸閿欒锛� 涓婅鏁版嵁瀹屾暣鎬ф鏌ユ墍閰嶄笂鎶ユ暟鎹殑澶撮儴鏈�灏忛暱搴︽敞瑙d笉鑳戒负0") ;
+						}
+						*/
+						key = protocolName + protocolVersion ;
+						prefixedDataAvailableMap.put(key, new AnnotationPrefixedDataAvailableVo(clazz, protocolName, protocolVersion, priority, onLineDataMinLength, headMinLength, ProtocolConstant.errorMaxLength));
+					}
+				}
+			}
+
+			Set<Class<?>> onLineClass = reflections.getTypesAnnotatedWith(AnnotationOnLine.class);
+			for (Class<?> clazz : onLineClass) {
+				AnnotationOnLine ann = clazz.getAnnotation(AnnotationOnLine.class);
+				if (ann != null) {
+					if (ann.enable()) {
+						protocolName = ann.protocolName();
+						if (!(protocolName.trim().equals(""))) {
+							if (onLineMap.containsKey(protocolName)) {
+								throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶅崗璁┍鍔�" + protocolName + "鍑虹幇浜嗛噸澶嶆敞瑙o紒");
+							}
+						} else {
+							throw new Exception("涓ラ噸閿欒锛屼笂绾挎暟鎹垎鏋愭墍閰嶅崗璁┍鍔ㄦ敞瑙e�间负绌哄瓧绗︿覆");
+						}
+
+						protocolVersion = ann.protocolVersion();
+						if (protocolVersion == -1) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶅崗璁増鏈彿鏈厤缃�");
+						}
+
+						priority = ann.priority();
+						if (priority == 0) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶄紭鍏堢骇娉ㄨВ涓嶈兘涓�0");
+						}
+						if (isRepeatOnLinePriority(priority, onLineMap)) {
+							throw new Exception("涓ラ噸閿欒锛� 涓婄嚎鏁版嵁鍒嗘瀽鎵�閰嶄紭鍏堢骇娉ㄨВ(priority)鏁板��" + priority + "閲嶅");
+						}
+
+						key = protocolName + protocolVersion ;
+						onLineMap.put(key, new AnnotationOnLineVo(clazz, protocolName, protocolVersion, priority));
+					}
+				}
+			}
+
+			//杩涜楠岃瘉
+			String error = this.hasErrorInDriver(driverMap);
+			if (error != null) {
+				throw new Exception(error);
+			} else {
+				error = this.hasErrorInPrefixedDataAvailable(driverMap, prefixedDataAvailableMap);
+				if (error != null) {
+					throw new Exception(error);
+				} else {
+					error = this.hasErrorInOnLine(driverMap, onLineMap);
+					if (error != null) {
+						throw new Exception(error);
+					}
+				}
+			}
+		} else {
+			throw new Exception("涓ラ噸閿欒锛� 鎵弿鍗忚鐩稿叧娉ㄨВ鎵�寰楀埌绫诲瀷闆嗗悎涓虹┖锛屾壂鎻忔牴璺緞鏄�" + scanRootPackage);
 		}
 	}
+
 	/**
 	 * 妫�鏌ヤ紭鍏堢骇閲嶅
 	 * @param priority 浼樺厛绾�
 	 * @return 缁撴灉
 	 */
 	private boolean isRepeatPrefixedDataAvailablePriority(int priority, HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap){
-		Collection<AnnotationPrefixedDataAvailableVo> col = prefixedDataAvailableMap.values(); 
+		Collection<AnnotationPrefixedDataAvailableVo> col = prefixedDataAvailableMap.values();
 		for(AnnotationPrefixedDataAvailableVo vo : col){
 			if(vo.priority == priority){
 				return true ;
@@ -195,7 +202,7 @@
 	 * @return 缁撴灉
 	 */
 	private boolean isRepeatOnLinePriority(int priority, HashMap<String, AnnotationOnLineVo> onLineMap){
-		Collection<AnnotationOnLineVo> col = onLineMap.values(); 
+		Collection<AnnotationOnLineVo> col = onLineMap.values();
 		for(AnnotationOnLineVo vo : col){
 			if(vo.priority == priority){
 				return true ;
@@ -203,7 +210,7 @@
 		}
 		return false ;
 	}
-	
+
 	private String hasErrorInDriver(HashMap<String, AnnotationDriverVo> driverMap){
 		if(driverMap.size() == 0){
 			return "涓ラ噸閿欒锛屾湭娉ㄨВ浠讳綍鍗忚椹卞姩" ;
@@ -211,23 +218,23 @@
 		return null ;
 	}
 
-	private String hasErrorInPrefixedDataAvailable(HashMap<String, AnnotationDriverVo> driverMap, 
-			HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap){
-		Collection<AnnotationPrefixedDataAvailableVo> col = prefixedDataAvailableMap.values(); 
+	private String hasErrorInPrefixedDataAvailable(HashMap<String, AnnotationDriverVo> driverMap,
+												   HashMap<String, AnnotationPrefixedDataAvailableVo> prefixedDataAvailableMap){
+		Collection<AnnotationPrefixedDataAvailableVo> col = prefixedDataAvailableMap.values();
 		for(AnnotationPrefixedDataAvailableVo vo : col){
-			if(!driverMap.containsKey(vo.protocolName)){
-				return "涓ラ噸閿欒锛屼笂琛屾暟鎹畬鏁存�ф鏌ユ墍閰嶉┍鍔ㄥ悕绉�(" + vo.protocolName + ")鍦ㄥ崗璁┍鍔ㄤ腑鏈厤缃�" ;
+			if(!driverMap.containsKey(vo.protocolName + vo.protocolVersion)){
+				return "涓ラ噸閿欒锛屼笂琛屾暟鎹畬鏁存�ф鏌ユ墍閰嶉┍鍔ㄥ悕绉�(" + (vo.protocolName + vo.protocolVersion) + ")鍦ㄥ崗璁┍鍔ㄤ腑鏈厤缃�" ;
 			}
 		}
-		
+
 		int[] prioritys = new int[prefixedDataAvailableMap.size()] ;
 		int count = 0 ;
-		col = prefixedDataAvailableMap.values(); 
+		col = prefixedDataAvailableMap.values();
 		for(AnnotationPrefixedDataAvailableVo vo : col){
 			prioritys[count++] = vo.priority ;
 		}
 		NumUtil.sort(prioritys, true) ;
-		
+
 		int firstPriority = prioritys[0] ;
 		if(ProtocolConstant.firstPriority != firstPriority){
 			return "涓ラ噸閿欒锛屼笂琛屾暟鎹畬鏁存�ф鏌ユ墍閰嶄紭鍏堢骇蹇呴』浠� " + ProtocolConstant.firstPriority + " 寮�濮�" ;
@@ -246,24 +253,24 @@
 		}
 		return null ;
 	}
-	
+
 	private String hasErrorInOnLine(HashMap<String, AnnotationDriverVo> driverMap,
-			HashMap<String, AnnotationOnLineVo> onLineMap){
-		Collection<AnnotationOnLineVo> col = onLineMap.values(); 
+									HashMap<String, AnnotationOnLineVo> onLineMap){
+		Collection<AnnotationOnLineVo> col = onLineMap.values();
 		for(AnnotationOnLineVo vo : col){
-			if(!driverMap.containsKey(vo.protocolName)){
-				return "涓ラ噸閿欒锛屼笂绾挎暟鎹垎鏋愭墍閰嶉┍鍔ㄥ悕绉�(" + vo.protocolName + ")鍦ㄥ崗璁┍鍔ㄤ腑鏈厤缃�" ;
+			if(!driverMap.containsKey(vo.protocolName + vo.protocolVersion)){
+				return "涓ラ噸閿欒锛屼笂绾挎暟鎹垎鏋愭墍閰嶉┍鍔ㄥ悕绉�(" + (vo.protocolName + vo.protocolVersion) + ")鍦ㄥ崗璁┍鍔ㄤ腑鏈厤缃�" ;
 			}
 		}
-		
+
 		int[] prioritys = new int[onLineMap.size()] ;
 		int count = 0 ;
-		col = onLineMap.values(); 
+		col = onLineMap.values();
 		for(AnnotationOnLineVo vo : col){
 			prioritys[count++] = vo.priority ;
 		}
 		NumUtil.sort(prioritys, true) ;
-		
+
 		int firstPriority = prioritys[0] ;
 		if(ProtocolConstant.firstPriority != firstPriority){
 			return "涓ラ噸閿欒锛屼笂绾挎暟鎹垎鏋愭墍閰嶄紭鍏堢骇蹇呴』浠� " + ProtocolConstant.firstPriority + " 寮�濮�" ;

--
Gitblit v1.8.0