From 6cf345bc8fe98d393320b89182f9293171aa19ff Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期二, 22 四月 2025 16:39:55 +0800 Subject: [PATCH] 1、根据江海协议中开阀类型变化,修改相应代码; 2、江海协议RTU模拟器启动参数注释。 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/Driver.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 177 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/Driver.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/Driver.java new file mode 100644 index 0000000..3e7a3e3 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/Driver.java @@ -0,0 +1,177 @@ +package com.dy.common.mw.protocol; + +import java.util.HashMap; +import java.util.Set; + +import org.reflections.Reflections; + + +public abstract class Driver { + + //鍗曠嚎绋嬪崟渚婦river妯″紡涓嬭繍琛� + protected HashMap<String, Class<CodeParse>> upCode2Classes = new HashMap<>() ; + protected HashMap<String, CodeParse> upCode2ClassInstance = new HashMap<>() ; + protected HashMap<String, Class<CodeParse>> downCode2Class = new HashMap<>() ; + protected HashMap<String, CodeParse> downCode2ClassInstance = new HashMap<>() ; + + protected Notify notify ; + + /** + * 娓呯┖鑷繁 + */ + public abstract void clear() ; + + /** + * 璁剧疆RTU淇℃伅閫氱煡鍣� + * @param notify 閫氱煡 + */ + public void setNotify(Notify notify){ + this.notify = notify ; + } + + /** + * 鍗曠嚎绋嬪崟渚婦river妯″紡涓嬭繍琛� + * 鎵弿璇嗗埆鍔熻兘鐮佹敞瑙g被 + * @throws Exception 寮傚父 + */ + public abstract void scanAnnotationCode() throws Exception ; + /** + * 鎵弿璇嗗埆鍔熻兘鐮佹敞瑙g被 + * @param driver 椹卞姩 + * @param protocolName 鍗忚鍚嶇О + * @param scanRootPackage 鎵弿鍖� + * @throws Exception 寮傚父 + */ + @SuppressWarnings("unchecked") + protected void doScanAnnotationCode(Driver driver, String protocolName, String scanRootPackage) throws Exception{ + Reflections reflections = new Reflections(scanRootPackage); // 鎸囧畾鍖呭悕 + String code ; + String[] codes ; + Class<CodeParse> codeParseClazz ; + + Set<Class<?>> codeUpClasses = reflections.getTypesAnnotatedWith(AnnotationCodeUp.class); + if (codeUpClasses != null && codeUpClasses.size() > 0) { + for (Class<?> clazz : codeUpClasses) { + AnnotationCodeUp ann = clazz.getAnnotation(AnnotationCodeUp.class) ; + if(ann != null){ + codeParseClazz = (Class<CodeParse>)clazz ; + codes = ann.ifAny() ; + if(codes.length > 0){ + for(String cd: codes){ + cd = cd.trim() ; + if(!(cd.trim().equals(""))){ + if(driver.upCode2Classes.containsKey(cd)){ + throw new Exception("涓ラ噸閿欒锛屽崗璁�" + protocolName + "鐨勪笂琛屾暟鎹姛鑳界爜" + cd + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ; + } + driver.upCode2Classes.put(cd, codeParseClazz) ; + } + } + }else{ + code = ann.value().trim() ; + if(!(code.equals(""))){ + if(driver.upCode2Classes.containsKey(code)){ + throw new Exception("涓ラ噸閿欒锛屽崗璁�" + protocolName + "鐨勪笂琛屾暟鎹姛鑳界爜" + code + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ; + } + driver.upCode2Classes.put(code, codeParseClazz) ; + } + } + } + } + } + + Set<Class<?>> codeDownClasses = reflections.getTypesAnnotatedWith(AnnotationCodeDown.class); + if (codeDownClasses != null && codeDownClasses.size() > 0) { + for (Class<?> clazz : codeDownClasses) { + AnnotationCodeDown ann = clazz.getAnnotation(AnnotationCodeDown.class) ; + if(ann != null){ + codeParseClazz = (Class<CodeParse>)clazz ; + codes = ann.ifAny() ; + if(codes.length > 0){ + for(String cd: codes){ + cd = cd.trim() ; + if(!(cd.trim().equals(""))){ + if(driver.downCode2Class.containsKey(cd)){ + throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔�" + protocolName + "鐨勪笅琛屾暟鎹姛鑳界爜" + cd + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ; + } + driver.downCode2Class.put(cd, codeParseClazz) ; + } + } + }else{ + code = ann.value() ; + if(!(code.trim().equals(""))){ + if(driver.downCode2Class.containsKey(code)){ + throw new Exception("涓ラ噸閿欒锛屽崗璁┍鍔�" + protocolName + "鐨勪笅琛屾暟鎹姛鑳界爜" + code + "鍑虹幇浜嗛噸澶嶆敞瑙o紒") ; + } + driver.downCode2Class.put(code, codeParseClazz) ; + } + } + } + } + } + } + + /** + * 鍗曠嚎绋嬪崟渚婦river妯″紡涓嬭繍琛� + * 寰楀埌涓婅鏁版嵁鍔熻兘鐮佸鐞嗙被CodeParse瀵硅薄 + * @param code 鍔熻兘鐮� + * @return CodeParse + * @throws Exception 寮傚父 + */ + public CodeParse getUpCodeParser(String code) throws Exception{ + CodeParse codeParse = upCode2ClassInstance.get(code) ; + if(codeParse == null){ + Class<CodeParse> clazz = upCode2Classes.get(code) ; + if(clazz == null){ + clazz = upCode2Classes.get(ProtocolConstant.anyCode) ; + } + if(clazz != null){ + //codeParse = clazz.newInstance() ; + codeParse = clazz.getDeclaredConstructor().newInstance(); + upCode2ClassInstance.put(code, codeParse) ; + } + } + return codeParse ; + } + /** + * 鍗曠嚎绋嬪崟渚婦river妯″紡涓嬭繍琛� + * 寰楀埌涓嬭鏁版嵁鍔熻兘鐮佸鐞嗙被CodeParse瀵硅薄 + * @param code 鍔熻兘鐮� + * @return CodeParse + * @throws Exception 寮傚父 + */ + public CodeParse getDownCodeParser(String code) throws Exception{ + CodeParse codeParse = downCode2ClassInstance.get(code) ; + if(codeParse == null){ + Class<CodeParse> clazz = downCode2Class.get(code) ; + if(clazz == null){ + clazz = downCode2Class.get(ProtocolConstant.anyCode) ; + } + if(clazz != null){ + //codeParse = clazz.newInstance() ; + codeParse = clazz.getDeclaredConstructor().newInstance(); + downCode2ClassInstance.put(code, codeParse) ; + } + } + return codeParse ; + } + /** + * 瑙f瀽RTU涓婅鏁版嵁 + * @param isLowPower 鏄惁鍦ㄤ綆鍔熻�楃幆澧冧笅杩愯 + * @param rtuAddr 鎺у埗鍣ㄥ湴鍧� + * @param upBuffer 瀛楄妭鏁扮粍 + * @param upHex 瀛楄妭鏁扮粍16杩涘埗褰㈠紡 + * @param callback 鍥炶皟 + * @return MidResult[] + */ + public abstract MidResult[] parseData(Boolean isLowPower, String rtuAddr, byte[] upBuffer, String upHex, DriverParserDataCallback callback, Object... params) ; + + /** + * 鏋勯�犱笅琛屾暟鎹紙鍛戒护锛� + * @param isLowPower 鏄惁鍦ㄤ綆鍔熻�楃幆澧冧笅杩愯 + * @param command 鍛戒护 + * @return MidResult[] + */ + public abstract MidResult[] createCommand(Boolean isLowPower, Command command, Object... params); + + +} -- Gitblit v1.8.0