| New file | 
 |  |  | 
 |  |  | package com.dy.common.mw.protocol.p206V2; | 
 |  |  |  | 
 |  |  | import com.dy.common.mw.protocol.*; | 
 |  |  | import com.dy.common.mw.protocol.p206V2.parse.TransparentDown; | 
 |  |  | import com.dy.common.util.DateTime; | 
 |  |  |  | 
 |  |  | @AnnotationDriver(enable = true, | 
 |  |  |       name= ProtocolConstantV206V2.protocolName, | 
 |  |  |       version= ProtocolConstantV206V2.protocolVer) | 
 |  |  | public class DriverV2 extends Driver { | 
 |  |  | 	 | 
 |  |  |    private static String scanRootPackage = "com.dy.common.mw.protocol.p206V2" ; | 
 |  |  | 	 | 
 |  |  |    //在Driver中是单例,随Driver的单例对象在单线程中运行 | 
 |  |  |    private ParseParamsForUpV2 upCpParams ; | 
 |  |  |    private ParseParamsForDownV2 downCpParams ; | 
 |  |  | 	 | 
 |  |  |    private Boolean reportOrResponse_trueOrFalse = null ; | 
 |  |  |    private String rtuAddrInData = null ; | 
 |  |  |  | 
 |  |  |    public DriverV2(){ | 
 |  |  |       upCpParams = new ParseParamsForUpV2() ; | 
 |  |  |       downCpParams = new ParseParamsForDownV2() ; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 清空自己 | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public void clear() { | 
 |  |  |       if(this.upCpParams != null){ | 
 |  |  |          this.upCpParams.clear(); | 
 |  |  |       } | 
 |  |  |       if(this.downCpParams != null){ | 
 |  |  |          this.downCpParams.clear(); | 
 |  |  |       } | 
 |  |  |       this.reportOrResponse_trueOrFalse = null ; | 
 |  |  |       this.rtuAddrInData = null ; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 扫描识别功能码注解类 | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public void scanAnnotationCode() throws Exception{ | 
 |  |  |       super.doScanAnnotationCode(this, ProtocolConstantV206V2.protocolName, scanRootPackage) ; | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 解析上行数据 | 
 |  |  |     * @param isLowPower 是否在低功耗环境下运行 | 
 |  |  |     * @param rtuAddr | 
 |  |  |     * @param upBuffer | 
 |  |  |     * @param callback | 
 |  |  |     * @param upHex | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public MidResult[] parseData(Boolean isLowPower, String rtuAddr, byte[] upBuffer, String upHex, DriverParserDataCallback callback, Object... params) { | 
 |  |  |       this.clear(); | 
 |  |  |       boolean isFail = false ; | 
 |  |  |       String upCode = null ; | 
 |  |  |       Short protocolVersion = null ; | 
 |  |  |       Object[] codeDataGrp = {null}; | 
 |  |  |       try{ | 
 |  |  |          CommonV2 cp = new CommonV2() ; | 
 |  |  |          Boolean p206TrueUgFalse = cp.protocolType_p206TrueUgFalse(upBuffer) ; | 
 |  |  |          //到此处了,p206TrueUgFalse不可能为空了 | 
 |  |  |          cp.checkTail(upBuffer) ; | 
 |  |  |          String crcStr = cp.checkCrc_str(upBuffer, p206TrueUgFalse) ; | 
 |  |  |          if(crcStr != null){ | 
 |  |  |             return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr, "严重错误,上行数据CRC检查失败," + crcStr, null))} ; | 
 |  |  |          } | 
 |  |  |          protocolVersion = cp.parseVersion(upBuffer) ; | 
 |  |  |          upCode = cp.parseCode(upBuffer, p206TrueUgFalse) ; | 
 |  |  |          if(upCode == null){ | 
 |  |  |             return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr, "严重错误,未能从上行数据中解析出功能码!", null))} ; | 
 |  |  |          } | 
 |  |  |          Data upData = new Data() ; | 
 |  |  |          upData.setRtuAddr(rtuAddr) ; | 
 |  |  |          upData.setProtocol(ProtocolConstantV206V2.protocolName); | 
 |  |  |          upData.setProtocolVer(protocolVersion); | 
 |  |  |          upData.setCode(upCode) ; | 
 |  |  |          upData.setHex(upHex) ; | 
 |  |  | 			 | 
 |  |  | 			 | 
 |  |  |          DataV2 dV2 = new DataV2() ; | 
 |  |  |          dV2.rtuAddr = rtuAddr ; | 
 |  |  |          dV2.dt = DateTime.yyyy_MM_dd_HH_mm_ss() ; | 
 |  |  |  | 
 |  |  |          upData.setSubData(dV2) ; | 
 |  |  | 			 | 
 |  |  |          this.upCpParams.setValue(ProtocolConstantV206V2.protocolName, | 
 |  |  |                protocolVersion, | 
 |  |  |                rtuAddr,  | 
 |  |  |                upCode, | 
 |  |  |                upHex,  | 
 |  |  |                upBuffer,  | 
 |  |  |                upData); | 
 |  |  |          CodeParse codeParse = this.getUpCodeParser(upCode) ; | 
 |  |  |          if(codeParse == null){ | 
 |  |  |             return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr, "严重错误,未能得到上行数据功能码" + upCode + "处理类!", null))} ; | 
 |  |  |          }else{ | 
 |  |  |             return codeParse.parse(isLowPower, this.upCpParams, new CodeParseCallback(){ | 
 |  |  |                @Override | 
 |  |  |                public void callback(Boolean flag, Object codeData) { | 
 |  |  |                   reportOrResponse_trueOrFalse = flag ; | 
 |  |  |                   rtuAddrInData = dV2.getRtuAddr() ; | 
 |  |  |                   codeDataGrp[0] = codeData ; | 
 |  |  |                } | 
 |  |  |                @Override | 
 |  |  |                public void notify(NotifyInfo ...infos) { | 
 |  |  |                   notify.notify(rtuAddr, ProtocolConstantV206V2.protocolName, ProtocolConstantV206V2.protocolVer, infos); | 
 |  |  |                } | 
 |  |  |             }); | 
 |  |  |          } | 
 |  |  | 				 | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          isFail = true ; | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr, "严重错误,分析Rtu数据出错!数据为:" + upHex + (e.getMessage() == null?"":(",出错信息:" + e.getMessage())), e))} ; | 
 |  |  |       } finally{ | 
 |  |  |          callback.callback(rtuAddr, upCode, CodeV2.getCodeName(upCode), upHex, reportOrResponse_trueOrFalse, isFail, rtuAddr, codeDataGrp[0]); | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 构造下行数据(命令) | 
 |  |  |     * @param isLowPower 是否在低功耗环境下运行 | 
 |  |  |     * @param command 命令 | 
 |  |  |     * @return | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public MidResult[] createCommand(Boolean isLowPower, Command command, Object... params) { | 
 |  |  |       this.clear(); | 
 |  |  |       String RtuResultSendWebUrl = command.getRtuResultSendWebUrl() ; | 
 |  |  |       if(RtuResultSendWebUrl == null || RtuResultSendWebUrl.trim().equals("")){ | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, null, "严重错误,命令中rtu返回命令结果发向目的地web URL为空,不能构建任何命令!", null))} ; | 
 |  |  |       } | 
 |  |  |       String rtuAddr = command.getRtuAddr() ; | 
 |  |  |       if(rtuAddr == null || rtuAddr.trim().equals("")){ | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, null, "严重错误,命令中Rtu地址为空,不能构建任何命令!", null))} ; | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          String commandCode = command.getCode() ; | 
 |  |  | 			 | 
 |  |  |          this.downCpParams.setValue( | 
 |  |  |                RtuResultSendWebUrl, | 
 |  |  |                ProtocolConstantV206V2.protocolName, | 
 |  |  |                command.protocolVersion==null?ProtocolConstantV206V2.protocolVer:command.protocolVersion , | 
 |  |  |                rtuAddr,  | 
 |  |  |                command.getId(), | 
 |  |  |                commandCode,  | 
 |  |  |                command.getParam(),  | 
 |  |  |                command.getAttachment()); | 
 |  |  | 			 | 
 |  |  |          if(commandCode == null){ | 
 |  |  |             return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr,  "严重错误,命令中功能码为空,不能构建任何命令!", null))} ; | 
 |  |  |          }else{ | 
 |  |  |             CodeParse codeParse = this.getDownCodeParser(commandCode) ; | 
 |  |  |             if(codeParse == null){ | 
 |  |  |                return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr,  "严重错误,未能得到下行数据功能码" + commandCode + "处理类!", null))} ; | 
 |  |  |             }else{ | 
 |  |  |                return codeParse.parse(isLowPower, this.downCpParams, null); | 
 |  |  |             } | 
 |  |  |          } | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr,  "严重错误,构造下行数据出错!命令为:" + command.toString(), e))} ; | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  |    /** | 
 |  |  |     * 构造下行透传数据(命令) | 
 |  |  |     * @param isLowPower 是否在低功耗环境下运行 | 
 |  |  |     * @param command 命令 | 
 |  |  |     * @return MidResult[] | 
 |  |  |     */ | 
 |  |  |    @Override | 
 |  |  |    public MidResult[] transparentCommand(Boolean isLowPower, Command command, Object... params){ | 
 |  |  |       this.clear(); | 
 |  |  |       String RtuResultSendWebUrl = command.getRtuResultSendWebUrl() ; | 
 |  |  |       if(RtuResultSendWebUrl == null || RtuResultSendWebUrl.trim().equals("")){ | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, null, "严重错误,命令中rtu返回命令结果发向目的地web URL为空,不能构建任何命令!", null))} ; | 
 |  |  |       } | 
 |  |  |       String rtuAddr = command.getRtuAddr() ; | 
 |  |  |       if(rtuAddr == null || rtuAddr.trim().equals("")){ | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, null, "严重错误,命令中Rtu地址为空,不能构建任何命令!", null))} ; | 
 |  |  |       } | 
 |  |  |       try { | 
 |  |  |          String commandCode = command.getCode() ; | 
 |  |  |  | 
 |  |  |          this.downCpParams.setValue( | 
 |  |  |                RtuResultSendWebUrl, | 
 |  |  |                ProtocolConstantV206V2.protocolName, | 
 |  |  |                command.protocolVersion==null?ProtocolConstantV206V2.protocolVer:command.protocolVersion, | 
 |  |  |                rtuAddr, | 
 |  |  |                command.getId(), | 
 |  |  |                commandCode, | 
 |  |  |                command.getParam(), | 
 |  |  |                command.getAttachment()); | 
 |  |  |  | 
 |  |  |          if(commandCode == null){ | 
 |  |  |             return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr,  "严重错误,命令中功能码为空,不能构建任何命令!", null))} ; | 
 |  |  |          }else{ | 
 |  |  |             return new TransparentDown().parse(isLowPower, this.downCpParams, null); | 
 |  |  |          } | 
 |  |  |       } catch (Exception e) { | 
 |  |  |          return new MidResult[]{(new MidResultError(ProtocolConstantV206V2.protocolName, rtuAddr,  "严重错误,构造下行数据出错!命令为:" + command.toString(), e))} ; | 
 |  |  |       } | 
 |  |  |    } | 
 |  |  |  | 
 |  |  | } |