1、接入中间件代码完善;
2、继续实现通信协议5个功能码。
| | |
| | | public class CodeV1_0_1 { |
| | | //功能码为字符串,十六进制数据 |
| | | public static final String cd_02 = "02" ;//遥测站链路检测 |
| | | public static final String cd_10 = "10" ;//设置遥测站终端地址 |
| | | public static final String cd_21 = "21" ;//设置服务端IP和端口 |
| | | public static final String cd_50 = "50" ;//查询遥测站终端地址 |
| | | public static final String cd_66 = "66" ;//查询阀门状态(靳总把该命令设计成召测83功能码数据的命令了,因为这个命令回执没有阀门状态数据域,所以这个命令只能作为召测命令用了) |
| | | public static final String cd_71 = "71" ;//设置工作模式(李天赐制定的协议,当前未实现) |
| | | public static final String cd_83 = "83" ;//遥测站开关阀自报 |
| | | public static final String cd_84 = "84" ;//开阀工作报 |
| | | public static final String cd_92 = "92" ;//遥控启动阀门 |
| | | public static final String cd_93 = "93" ;//遥控关闭阀门 |
| | | public static final String cd_C0 = "C0" ;//遥测站自报实时数据 |
| | | |
| | | public static String getCodeName(String code) { |
| | | String name = (code.equals(cd_02) ? "链路检测" : |
| | | (code.equals(cd_71) ? "查询阀门状态" : |
| | | (code.equals(cd_10) ? "设置遥测站终端地址" : |
| | | (code.equals(cd_21) ? "设置IP和端口" : |
| | | (code.equals(cd_50) ? "查询遥测站终端地址" : |
| | | (code.equals(cd_66) ? "查询阀门状态" : |
| | | (code.equals(cd_71) ? "设置工作模式" : |
| | | (code.equals(cd_83) ? "开关阀自报" : |
| | | (code.equals(cd_84) ? "开阀工作报" : |
| | | (code.equals(cd_92) ? "遥控启动阀门" : |
| | | (code.equals(cd_93) ? "遥控关闭阀门" : |
| | | (code.equals(cd_C0) ? "自报实时数据" : |
| | | ""))))) ; |
| | | ""))))))))))) ; |
| | | return name ; |
| | | } |
| | | |
| | |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分析Rtu地址 |
| | | * @param bs 上行字节数组 |
| | | * @param index 启始位 |
| | | * @return 控制器地址 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public String parseRtuAddr(byte[] bs, int index)throws Exception{ |
| | | String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + 2) ; |
| | | String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_BE(bs, index + 3) ; |
| | | while(rtuAddrStr.length() < 4){ |
| | | rtuAddrStr = "0" + rtuAddrStr ; |
| | | } |
| | | return rtuAddrBCD + rtuAddrStr ; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 分析功能码 |
| | | * @param bs 上行字节数组 |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 11:28 |
| | | * @LastEditTime 2023/12/25 11:28 |
| | | * @Description |
| | | */ |
| | | @Data |
| | | public class DataCd10Vo { |
| | | public String newRtuAddr ; |
| | | |
| | | public String toString(){ |
| | | StringBuilder sb = new StringBuilder() ; |
| | | sb.append(" 设备RTU地址应答:\n"); |
| | | sb.append(" 新地址:"); |
| | | sb.append(newRtuAddr); |
| | | sb.append("\n"); |
| | | |
| | | return sb.toString() ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 21:14 |
| | | * @LastEditTime 2023/12/25 21:14 |
| | | * @Description |
| | | */ |
| | | @Data |
| | | public class DataCd21Vo { |
| | | public String ip ; |
| | | public Integer port ; |
| | | |
| | | public String toString(){ |
| | | StringBuilder sb = new StringBuilder() ; |
| | | sb.append(" 设置IP应答:\n"); |
| | | sb.append(" IP:"); |
| | | sb.append(ip); |
| | | sb.append(" 端口:"); |
| | | sb.append(port); |
| | | sb.append("\n"); |
| | | |
| | | return sb.toString() ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 21:14 |
| | | * @LastEditTime 2023/12/25 21:14 |
| | | * @Description |
| | | */ |
| | | @Data |
| | | public class DataCd92Vo { |
| | | public boolean success; |
| | | |
| | | public String toString(){ |
| | | StringBuilder sb = new StringBuilder() ; |
| | | sb.append(" 开阀应答:\n"); |
| | | sb.append(" 结果:"); |
| | | sb.append(success?"执行":"失败"); |
| | | sb.append("\n"); |
| | | |
| | | return sb.toString() ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0; |
| | | |
| | | import lombok.Data; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 21:14 |
| | | * @LastEditTime 2023/12/25 21:14 |
| | | * @Description |
| | | */ |
| | | @Data |
| | | public class DataCd93Vo { |
| | | public boolean success; |
| | | |
| | | public String toString(){ |
| | | StringBuilder sb = new StringBuilder() ; |
| | | sb.append(" 关阀应答:\n"); |
| | | sb.append(" 结果:"); |
| | | sb.append(success?"执行":"失败"); |
| | | sb.append("\n"); |
| | | |
| | | return sb.toString() ; |
| | | } |
| | | } |
| | |
| | | public String rtuAddr ;//RtuAddr |
| | | |
| | | public DataCd02Vo dataCd02Vo;//链路检测 |
| | | public DataCdC0Vo dataCdC0Vo;//终端自报实时数据 |
| | | public DataCd10Vo dataCd10Vo;//设置RTU地址 |
| | | public DataCd21Vo dataCd21Vo;//设置IP地址 |
| | | public DataCd71Vo dataCd71Vo;//查询阀门状态 |
| | | public DataCd83OpenVo dataCd83OpenVo;//开阀自报 |
| | | public DataCd83CloseVo dataCd83CloseVo;//关阀自报 |
| | | public DataCd84Vo dataCd84Vo;//开阀工作报 |
| | | public DataCd71Vo dataCd71Vo;//查询阀门状态 |
| | | public DataCd92Vo dataCd92Vo;//开阀应答 |
| | | public DataCd93Vo dataCd93Vo;//关阀应答 |
| | | public DataCdC0Vo dataCdC0Vo;//终端自报实时数据 |
| | | |
| | | public String dt ;//通信中间件产生发报时间(yyyy-MM-dd hh:mm:ss) |
| | | |
| | |
| | | if(dataCd02Vo != null){ |
| | | sb.append(dataCd02Vo.toString()) ; |
| | | } |
| | | if(dataCdC0Vo != null){ |
| | | sb.append(dataCdC0Vo.toString()) ; |
| | | if(dataCd10Vo != null){ |
| | | sb.append(dataCd10Vo.toString()) ; |
| | | } |
| | | if(dataCd21Vo != null){ |
| | | sb.append(dataCd21Vo.toString()) ; |
| | | } |
| | | if(dataCd71Vo != null){ |
| | | sb.append(dataCd71Vo.toString()) ; |
| | | } |
| | | if(dataCd83OpenVo != null){ |
| | | sb.append(dataCd83OpenVo.toString()) ; |
| | |
| | | if(dataCd84Vo != null){ |
| | | sb.append(dataCd84Vo.toString()) ; |
| | | } |
| | | if(dataCd71Vo != null){ |
| | | sb.append(dataCd71Vo.toString()) ; |
| | | if(dataCd92Vo != null){ |
| | | sb.append(dataCd92Vo.toString()) ; |
| | | } |
| | | if(dataCd93Vo != null){ |
| | | sb.append(dataCd93Vo.toString()) ; |
| | | } |
| | | if(dataCdC0Vo != null){ |
| | | sb.append(dataCdC0Vo.toString()) ; |
| | | } |
| | | return sb.toString() ; |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CodeV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ParseParamsForDownV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 11:11 |
| | | * @LastEditTime 2023/12/25 11:11 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_10 |
| | | }) |
| | | public class Cd_10_Down implements CodeParse { |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | | ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ; |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | | byte[] bytes ; |
| | | byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; |
| | | byte index = 0 ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = 0 ;//帧长度 |
| | | |
| | | index++ ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | if(para.param == null){ |
| | | throw new Exception("未提供命令参数数据,不能构造功能码为" + CodeV1_0_1.cd_10 + "的下行命令") ; |
| | | }else{ |
| | | index = 0 ; |
| | | byte[] bs = new byte[12] ; |
| | | String newRtuAddr = (String)para.param ; |
| | | GlCreate.createRtuAddr(newRtuAddr, bs, index); |
| | | index += 5 ; |
| | | GlCreate.createPw(bs, index); |
| | | index += 2 ; |
| | | GlCreate.createTp(bs, index); |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | } |
| | | |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 11:27 |
| | | * @LastEditTime 2023/12/25 11:27 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeUp(ifAny={ |
| | | CodeV1_0_1.cd_10 |
| | | }) |
| | | public class Cd_10_Up implements CodeParse { |
| | | |
| | | private static final Logger log = LogManager.getLogger(Cd_10_Up.class); |
| | | |
| | | /** |
| | | * 分析上行数据 |
| | | */ |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback)throws Exception { |
| | | ParseParamsForUpV1_0_1 para = (ParseParamsForUpV1_0_1)params ; |
| | | int bsLen = new CommonV1_0_1().parseDataLen(para.upBuffer) ; |
| | | if(bsLen > 0){ |
| | | this.doParse(para.upBuffer, |
| | | bsLen, |
| | | para.upCode, |
| | | para.data) ; |
| | | } |
| | | log.info("分析命令应答数据<" + CodeV1_0_1.getCodeName(para.upCode) + " RTU地址=" + para.rtuAddr + ">:\n" + para.data.toString()); |
| | | |
| | | MidResultFromRtu midRs = new MidResultFromRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.upCode = para.upCode ;//上行数据中的功能码 |
| | | midRs.upHex = para.upHex ;//上行数据十六进制形式 |
| | | midRs.upBuffer = para.upBuffer ;//上行数据字节数组 |
| | | midRs.data = para.data ;//解析后的数据 |
| | | |
| | | midRs.reportOrResponse_trueOrFalse = false ;//命令应答 |
| | | |
| | | callback.callback(midRs.reportOrResponse_trueOrFalse); |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | /** |
| | | * 执行分析 |
| | | * @param bs 字节数组 |
| | | * @param bsLen 字节长度(总包长,包括包头和包尾) |
| | | * @param dataCode 功能码 |
| | | * @param data 数据 |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | DataV1_0_1 dV1 = (DataV1_0_1)data.getSubData() ; |
| | | DataCd10Vo cdData = new DataCd10Vo() ; |
| | | dV1.dataCd10Vo = cdData ; |
| | | cdData.newRtuAddr = new CommonV1_0_1().parseRtuAddr(bs, ProtocolConstantV206V1_0_0.dataIndex) ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CodeV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ParseParamsForDownV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | import com.dy.common.util.ByteUtilUnsigned; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 11:37 |
| | | * @LastEditTime 2023/12/25 11:37 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_21 |
| | | }) |
| | | public class Cd_21_Down implements CodeParse { |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | | ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ; |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | | byte[] bytes ; |
| | | byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; |
| | | byte index = 0 ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = 0 ;//帧长度 |
| | | |
| | | index++ ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | if(para.param == null){ |
| | | throw new Exception("未提供命令参数数据,不能构造功能码为" + CodeV1_0_1.cd_21 + "的下行命令") ; |
| | | }else{ |
| | | index = 0 ; |
| | | byte[] bs = new byte[14] ; |
| | | String ipPort = (String)para.param ; |
| | | String[] ipPorts = ipPort.split(",") ; |
| | | ByteUtilUnsigned.short2Bytes_LE(bs, (byte) Integer.parseInt(ipPorts[0]), index++); |
| | | ByteUtilUnsigned.short2Bytes_LE(bs, (byte) Integer.parseInt(ipPorts[1]), index++); |
| | | ByteUtilUnsigned.short2Bytes_LE(bs, (byte) Integer.parseInt(ipPorts[2]), index++); |
| | | ByteUtilUnsigned.short2Bytes_LE(bs, (byte) Integer.parseInt(ipPorts[3]), index++); |
| | | ByteUtilUnsigned.short2Bytes_LE(bs, Integer.parseInt(ipPorts[4]), index); |
| | | index += 2 ; |
| | | GlCreate.createPw(bs, index); |
| | | index += 2 ; |
| | | GlCreate.createTp(bs, index); |
| | | |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | } |
| | | |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import com.dy.common.util.ByteUtilUnsigned; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/25 13:40 |
| | | * @LastEditTime 2023/12/25 13:40 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeUp(ifAny={ |
| | | CodeV1_0_1.cd_21 |
| | | }) |
| | | @SuppressWarnings("unused") |
| | | public class Cd_21_Up implements CodeParse { |
| | | |
| | | private static final Logger log = LogManager.getLogger(Cd_21_Up.class); |
| | | |
| | | /** |
| | | * 分析上行数据 |
| | | */ |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback)throws Exception { |
| | | ParseParamsForUpV1_0_1 para = (ParseParamsForUpV1_0_1)params ; |
| | | int bsLen = new CommonV1_0_1().parseDataLen(para.upBuffer) ; |
| | | if(bsLen > 0){ |
| | | this.doParse(para.upBuffer, |
| | | bsLen, |
| | | para.upCode, |
| | | para.data) ; |
| | | } |
| | | log.info("分析命令应答数据<" + CodeV1_0_1.getCodeName(para.upCode) + " RTU地址=" + para.rtuAddr + ">:\n" + para.data.toString()); |
| | | |
| | | MidResultFromRtu midRs = new MidResultFromRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.upCode = para.upCode ;//上行数据中的功能码 |
| | | midRs.upHex = para.upHex ;//上行数据十六进制形式 |
| | | midRs.upBuffer = para.upBuffer ;//上行数据字节数组 |
| | | midRs.data = para.data ;//解析后的数据 |
| | | |
| | | midRs.reportOrResponse_trueOrFalse = false ;//命令应答 |
| | | |
| | | callback.callback(midRs.reportOrResponse_trueOrFalse); |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | /** |
| | | * 执行分析 |
| | | * @param bs 字节数组 |
| | | * @param bsLen 字节长度(总包长,包括包头和包尾) |
| | | * @param dataCode 功能码 |
| | | * @param data 数据 |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | DataV1_0_1 dV1 = (DataV1_0_1)data.getSubData() ; |
| | | DataCd21Vo cdData = new DataCd21Vo() ; |
| | | dV1.dataCd21Vo = cdData ; |
| | | short index = ProtocolConstantV206V1_0_0.dataIndex ; |
| | | String ip1 = "" +ByteUtilUnsigned.byte2Byte(bs, index++) ; |
| | | String ip2 = "" +ByteUtilUnsigned.byte2Byte(bs, index++) ; |
| | | String ip3 = "" +ByteUtilUnsigned.byte2Byte(bs, index++) ; |
| | | String ip4 = "" +ByteUtilUnsigned.byte2Byte(bs, index++) ; |
| | | |
| | | cdData.ip = ip1 + "." + ip2 + "." + ip3 + "." + ip4 ; |
| | | |
| | | String port = "" +ByteUtilUnsigned.bytes2Short_LE(bs, index++) ; |
| | | cdData.port = Integer.parseInt(port) ; |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CodeV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ParseParamsForDownV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/23 9:35 |
| | | * @LastEditTime 2023/12/23 9:35 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_66 |
| | | }) |
| | | public class Cd_66_Down implements CodeParse { |
| | | |
| | | //private static Logger log = LogManager.getLogger(Cd_66_Down.class); |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | | ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ; |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | | byte[] bytes ; |
| | | byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; |
| | | byte index = 0 ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = 0 ;//帧长度 |
| | | |
| | | index++ ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | if(para.param == null){ |
| | | throw new Exception("未提供命令参数数据,不能构造功能码为" + CodeV1_0_1.cd_66 + "的下行命令") ; |
| | | }else{ |
| | | byte flag = ((Integer)para.param).byteValue() ; |
| | | byte[] bs = new byte[]{flag} ; |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | } |
| | | |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/23 9:48 |
| | | * @LastEditTime 2023/12/23 9:48 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeUp(ifAny={ |
| | | CodeV1_0_1.cd_66 |
| | | }) |
| | | @SuppressWarnings("unused") |
| | | public class Cd_66_Up implements CodeParse { |
| | | |
| | | private static final Logger log = LogManager.getLogger(Cd_66_Up.class); |
| | | |
| | | /** |
| | | * 分析上行数据 |
| | | */ |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback)throws Exception { |
| | | ParseParamsForUpV1_0_1 para = (ParseParamsForUpV1_0_1)params ; |
| | | int bsLen = new CommonV1_0_1().parseDataLen(para.upBuffer) ; |
| | | if(bsLen > 0){ |
| | | this.doParse(para.upBuffer, |
| | | bsLen, |
| | | para.upCode, |
| | | para.data) ; |
| | | } |
| | | log.info("分析命令应答数据<" + CodeV1_0_1.getCodeName(para.upCode) + " RTU地址=" + para.rtuAddr + ">:\n" + para.data.toString()); |
| | | |
| | | MidResultFromRtu midRs = new MidResultFromRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.upCode = para.upCode ;//上行数据中的功能码 |
| | | midRs.upHex = para.upHex ;//上行数据十六进制形式 |
| | | midRs.upBuffer = para.upBuffer ;//上行数据字节数组 |
| | | midRs.data = para.data ;//解析后的数据 |
| | | |
| | | midRs.reportOrResponse_trueOrFalse = false ;//命令应答 |
| | | |
| | | callback.callback(midRs.reportOrResponse_trueOrFalse); |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | /** |
| | | * 执行分析 |
| | | * @param bs 字节数组 |
| | | * @param bsLen 字节长度(总包长,包括包头和包尾) |
| | | * @param dataCode 功能码 |
| | | * @param data 数据 |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | } |
| | | |
| | | } |
| | |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;////rtu返回命令结果 发向目的地web URL |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;////rtu返回命令结果 发向目的地web URL |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | short index = ProtocolConstantV206V1_0_0.dataIndex ; |
| | | byte opType = bs[ProtocolConstantV206V1_0_0.dataIndex]; |
| | | if(opType == 1 || opType == 3 || opType == 8){ |
| | | this.doParseOpen(opType, bs, bsLen, dataCode, data); |
| | |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;////rtu返回命令结果 发向目的地web URL |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CodeV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ParseParamsForDownV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/26 8:49 |
| | | * @LastEditTime 2023/12/26 8:49 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_92 |
| | | }) |
| | | public class Cd_92_Down implements CodeParse { |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | | ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ; |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | | byte[] bytes ; |
| | | byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; |
| | | byte index = 0 ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = 0 ;//帧长度 |
| | | |
| | | index++ ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | if(para.param == null){ |
| | | throw new Exception("未提供命令参数数据,不能构造功能码为" + CodeV1_0_1.cd_92 + "的下行命令") ; |
| | | }else{ |
| | | index = 0 ; |
| | | byte[] bs = new byte[9] ; |
| | | bs[index++] = (byte)0xF0 ; |
| | | GlCreate.createPw(bs, index); |
| | | index += 2 ; |
| | | GlCreate.createTp(bs, index); |
| | | |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | } |
| | | |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import com.dy.common.util.ByteUtil; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/26 8:49 |
| | | * @LastEditTime 2023/12/26 8:49 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeUp(ifAny={ |
| | | CodeV1_0_1.cd_92 |
| | | }) |
| | | @SuppressWarnings("unused") |
| | | public class Cd_92_Up implements CodeParse { |
| | | |
| | | private static final Logger log = LogManager.getLogger(Cd_92_Up.class); |
| | | |
| | | /** |
| | | * 分析上行数据 |
| | | */ |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback)throws Exception { |
| | | ParseParamsForUpV1_0_1 para = (ParseParamsForUpV1_0_1)params ; |
| | | int bsLen = new CommonV1_0_1().parseDataLen(para.upBuffer) ; |
| | | if(bsLen > 0){ |
| | | this.doParse(para.upBuffer, |
| | | bsLen, |
| | | para.upCode, |
| | | para.data) ; |
| | | } |
| | | log.info("分析上行数据<" + CodeV1_0_1.getCodeName(para.upCode) + " RTU地址=" + para.rtuAddr + ">:\n" + para.data.toString()); |
| | | |
| | | MidResultFromRtu midRs = new MidResultFromRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.upCode = para.upCode ;//上行数据中的功能码 |
| | | midRs.upHex = para.upHex ;//上行数据十六进制形式 |
| | | midRs.upBuffer = para.upBuffer ;//上行数据字节数组 |
| | | midRs.data = para.data ;//解析后的数据 |
| | | |
| | | midRs.reportOrResponse_trueOrFalse = false ;//主动上报 |
| | | |
| | | callback.callback(midRs.reportOrResponse_trueOrFalse); |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | /** |
| | | * 执行分析 |
| | | * @param bs 字节数组 |
| | | * @param bsLen 字节长度(总包长,包括包头和包尾) |
| | | * @param dataCode 功能码 |
| | | * @param data 数据 |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | DataV1_0_1 dV1 = (DataV1_0_1)data.getSubData() ; |
| | | DataCd92Vo cdData = new DataCd92Vo() ; |
| | | dV1.dataCd92Vo = cdData ; |
| | | String bin = ByteUtil.byte2Binary(bs[ProtocolConstantV206V1_0_0.dataIndex]); |
| | | if(bin.startsWith("1010")){ |
| | | cdData.success = true ; |
| | | }else{ |
| | | cdData.success = false ; |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CodeV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ParseParamsForDownV1_0_1; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate; |
| | | import com.dy.common.util.ByteUtil; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/26 8:50 |
| | | * @LastEditTime 2023/12/26 8:50 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeDown(ifAny={ |
| | | CodeV1_0_1.cd_93 |
| | | }) |
| | | public class Cd_93_Down implements CodeParse { |
| | | |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { |
| | | ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ; |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | | midRs.downCode = para.commandCode ;//下行命令功能码; |
| | | midRs.downBuffer = bs ;//下行命令数据 |
| | | midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 |
| | | midRs.hasResponse = true ;//是否有应答 |
| | | midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 |
| | | midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true |
| | | |
| | | if(isLowPower != null && isLowPower.booleanValue()){ |
| | | //低功耗时,尽快发送 |
| | | midRs.isQuickSend = true ; |
| | | } |
| | | |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | |
| | | /** |
| | | * 构造下行数据 |
| | | * @param para 参数 |
| | | * @return 字节数组 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception { |
| | | CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ; |
| | | byte[] bytes ; |
| | | byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ; |
| | | byte index = 0 ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = 0 ;//帧长度 |
| | | |
| | | index++ ; |
| | | bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ; |
| | | |
| | | index++ ; |
| | | bsHead[index] = commonV1_0_1.createCtrl((byte)0, (byte)0) ; |
| | | |
| | | index++ ; |
| | | GlCreate.createRtuAddr(para.rtuAddr, bsHead, index); |
| | | index += 5 ; |
| | | |
| | | ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ; |
| | | |
| | | if(para.param == null){ |
| | | throw new Exception("未提供命令参数数据,不能构造功能码为" + CodeV1_0_1.cd_93 + "的下行命令") ; |
| | | }else{ |
| | | index = 0 ; |
| | | byte[] bs = new byte[9] ; |
| | | bs[index++] = (byte)0xF0 ; |
| | | GlCreate.createPw(bs, index); |
| | | index += 2 ; |
| | | GlCreate.createTp(bs, index); |
| | | |
| | | bytes = ByteUtil.bytesMerge(bsHead, bs) ; |
| | | } |
| | | |
| | | GlCreate.createLen(bytes);//长度放字节数组中 |
| | | |
| | | byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中 |
| | | |
| | | bytes = ByteUtil.bytesMerge(bytes, bsTail) ; |
| | | |
| | | return bytes ; |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.dy.common.mw.protocol.p206V1_0_0.parse; |
| | | |
| | | import com.dy.common.mw.protocol.*; |
| | | import com.dy.common.mw.protocol.p206V1_0_0.*; |
| | | import com.dy.common.util.ByteUtil; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | |
| | | /** |
| | | * @Author liurunyu |
| | | * @Date 2023/12/26 8:49 |
| | | * @LastEditTime 2023/12/26 8:49 |
| | | * @Description |
| | | */ |
| | | @AnnotationCodeUp(ifAny={ |
| | | CodeV1_0_1.cd_93 |
| | | }) |
| | | @SuppressWarnings("unused") |
| | | public class Cd_93_Up implements CodeParse { |
| | | |
| | | private static final Logger log = LogManager.getLogger(Cd_93_Up.class); |
| | | |
| | | /** |
| | | * 分析上行数据 |
| | | */ |
| | | @Override |
| | | public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback)throws Exception { |
| | | ParseParamsForUpV1_0_1 para = (ParseParamsForUpV1_0_1)params ; |
| | | int bsLen = new CommonV1_0_1().parseDataLen(para.upBuffer) ; |
| | | if(bsLen > 0){ |
| | | this.doParse(para.upBuffer, |
| | | bsLen, |
| | | para.upCode, |
| | | para.data) ; |
| | | } |
| | | log.info("分析上行数据<" + CodeV1_0_1.getCodeName(para.upCode) + " RTU地址=" + para.rtuAddr + ">:\n" + para.data.toString()); |
| | | |
| | | MidResultFromRtu midRs = new MidResultFromRtu() ; |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址 |
| | | midRs.upCode = para.upCode ;//上行数据中的功能码 |
| | | midRs.upHex = para.upHex ;//上行数据十六进制形式 |
| | | midRs.upBuffer = para.upBuffer ;//上行数据字节数组 |
| | | midRs.data = para.data ;//解析后的数据 |
| | | |
| | | midRs.reportOrResponse_trueOrFalse = false ;//主动上报 |
| | | |
| | | callback.callback(midRs.reportOrResponse_trueOrFalse); |
| | | return new MidResult[]{midRs} ; |
| | | } |
| | | /** |
| | | * 执行分析 |
| | | * @param bs 字节数组 |
| | | * @param bsLen 字节长度(总包长,包括包头和包尾) |
| | | * @param dataCode 功能码 |
| | | * @param data 数据 |
| | | * @throws Exception 异常 |
| | | */ |
| | | protected void doParse(byte[] bs, int bsLen, String dataCode, Data data) throws Exception { |
| | | DataV1_0_1 dV1 = (DataV1_0_1)data.getSubData() ; |
| | | DataCd93Vo cdData = new DataCd93Vo() ; |
| | | dV1.dataCd93Vo = cdData ; |
| | | String bin = ByteUtil.byte2Binary(bs[ProtocolConstantV206V1_0_0.dataIndex]); |
| | | if(bin.startsWith("1010")){ |
| | | cdData.success = true ; |
| | | }else{ |
| | | cdData.success = false ; |
| | | } |
| | | } |
| | | } |
| | |
| | | byte[] bs = this.doParse(para) ; |
| | | |
| | | MidResultToRtu midRs = new MidResultToRtu() ; |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;////rtu返回命令结果 发向目的地web URL |
| | | midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ;//rtu返回命令结果 发向目的地web URL |
| | | midRs.protocolName = para.protocolName ;//协议名称 |
| | | midRs.rtuAddr = para.rtuAddr ;//Rtu地址(电信平台设备IMEI) |
| | | midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 |
| | |
| | | ByteUtilUnsigned.short2Bytes_BE(bs, rtuAddr2Int.shortValue(), index); |
| | | } |
| | | |
| | | public static void createPw(byte[] bs, int index) throws Exception { |
| | | bs[index] = 0; |
| | | bs[index + 1] = 0; |
| | | } |
| | | |
| | | public static void createTp(byte[] bs, int index) throws Exception { |
| | | ByteUtil.string2BCD_LE(bs, DateTime.yyMMddhhmmss(), index) ; |
| | | } |
| | |
| | | * @param index 下标位 |
| | | * @param len 长度 |
| | | * @return 返回 返回 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static boolean bytesIsAll0xFF(byte[] bs, int index, int len)throws Exception { |
| | | public static boolean bytesIsAll0xFF(byte[] bs, int index, int len){ |
| | | int count = 0 ; |
| | | for(int i = index; i < index + len; i++){ |
| | | if(bs[i] == (byte)0xFF){ |
| | |
| | | |
| | | /** |
| | | * 二进制转十进制数 |
| | | * @param str |
| | | * @param str 二进制字符串 |
| | | * @return 返回 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static int binary2Int(String str) throws Exception { |
| | | int cnt=0; |
| | | int sum=0; |
| | | str=new StringBuilder(str).reverse().toString();//反转字符串 |
| | | for(int i=0;i<str.length();i++){ |
| | | cnt++; |
| | | if (str.charAt(i)=='1'){ |
| | | int mul=1; |
| | | for (int j=1;j<cnt;j++){ |
| | | mul*=2; |
| | | } |
| | | sum+=mul; |
| | | } |
| | | else continue; |
| | | } |
| | | return sum; |
| | | public static int binary2Int(String str) { |
| | | return Integer.parseInt(str, 2); |
| | | } |
| | | |
| | | |
| | |
| | | * 字节转存二进制 |
| | | * |
| | | * @param b byte |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | | */ |
| | | public static String byte2Binary(byte b) throws Exception { |
| | | int n = (b + 256) % 256 + 256; |
| | | try { |
| | | return Integer.toBinaryString(n).substring(1); |
| | | } catch (Exception e) { |
| | | throw new Exception("字节转换成二进制的字符串出错!", null); |
| | | } |
| | | public static String byte2Binary(byte b) { |
| | | return Integer.toBinaryString(b & 0xFF) ; |
| | | } |
| | | /** |
| | | * 字节转存8位二进制 |
| | | * |
| | | * @param b |
| | | * byte |
| | | * @throws Exception 异常 |
| | | * @param b byte |
| | | * @return 返回 String |
| | | */ |
| | | public static String byte2bit8Binary(byte b) throws Exception { |
| | | public static String byte2bit8Binary(byte b) { |
| | | String s = byte2Binary(b); |
| | | int len = s.length(); |
| | | for (int i = 0; i < 8 - len; i++) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * @param b |
| | | * @throws Exception 异常 |
| | | * 字节数 取出8个bit |
| | | * @param b 字节数 |
| | | * @return 返回 String |
| | | */ |
| | | public static byte[] getBit(byte b) throws Exception { |
| | | public static byte[] getBit(byte b) { |
| | | byte[] bs = new byte[8] ; |
| | | bs[0] = (byte)(b & 1) ; |
| | | bs[1] = (byte)((b & 2) >> 1) ; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 字节取bit |
| | | * @param b |
| | | * 字节数 取出bit |
| | | * @param b 字节数 |
| | | * @param index 下标位 |
| | | * @throws Exception 异常 |
| | | * @return 返回 String |
| | |
| | | public static void double2Bytes_BE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | Long l = Double.doubleToLongBits(value); |
| | | long2Bytes_BE(bs, l, from); |
| | | long2Bytes_BE(bs, Double.doubleToLongBits(value), from); |
| | | } else { |
| | | throw new Exception("double2Bytes时数组越界"); |
| | | } |
| | |
| | | public static void double2Bytes_LE(byte[] bs, double value, int from)throws Exception { |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | Long l = Double.doubleToLongBits(value); |
| | | long2Bytes_LE(bs, l, from); |
| | | long2Bytes_LE(bs, Double.doubleToLongBits(value), from); |
| | | } else { |
| | | throw new Exception("double2Bytes时数组越界"); |
| | | } |
| | |
| | | boolean b = isOutOfArrLength(bs.length, (from - 1) + 8); |
| | | if (b) { |
| | | long s = 0; |
| | | long s0 = bs[from + 0] ;// 最低位 |
| | | long s0 = bs[from] ;// 最低位 |
| | | long s1 = bs[from + 1] ; |
| | | long s2 = bs[from + 2] ; |
| | | long s3 = bs[from + 3] ; |
| | |
| | | package com.dy.aceMw.web.com; |
| | | |
| | | import com.dy.aceMw.server.ServerProperties; |
| | | import com.dy.aceMw.server.forTcp.TcpSessionCache; |
| | | import com.dy.aceMw.server.local.CommandInnerDeaLer; |
| | | import com.dy.aceMw.server.local.ReturnCommand; |
| | |
| | | if(onLine == null){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("出错,RTU(地址=" + rtuAddr + ")未上线!", command.getId(), command.getCode())) ; |
| | | }else if(!onLine.booleanValue()){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("出错,RTU(地址=" + rtuAddr + ")离线!", command.getId(), command.getCode())) ; |
| | | if(!ServerProperties.isLowPower){ |
| | | return BaseResponseUtils.buildError(ReturnCommand.errored("出错,RTU(地址=" + rtuAddr + ")离线!", command.getId(), command.getCode())) ; |
| | | } |
| | | } |
| | | |
| | | //生成异步任务 |
| | |
| | | return BaseResponseUtils.buildSuccess(ReturnCommand.successed("命令已接受,即将构造并下发命令。", command.getId(), command.getCode())); |
| | | } |
| | | |
| | | /** |
| | | * 收到命令结果 |
| | | * @param command |
| | | private void dealCommandResult(String jgSenderName, Command command){ |
| | | Object obj = command.getParam() ; |
| | | if(obj != null){ |
| | | CommandBackParam p = (CommandBackParam)obj ; |
| | | if(p.getSuccess()){ |
| | | log.info("命令" + (command.getId().equals(Command.defaultId)?"":("(id=" + command.getId() + ")")) + "执行成功" |
| | | + (p.getMessage() == null?"":(p.getMessage().equals("")?"":("," + p.getMessage())))); |
| | | }else{ |
| | | log.error("命令" + (command.getId().equals(Command.defaultId)?"":("(id=" + command.getId() + ")")) + "执行失败" |
| | | + (p.getMessage() == null?"":(p.getMessage().equals("")?"":("," + p.getMessage())))); |
| | | } |
| | | } |
| | | } |
| | | */ |
| | | } |
| | |
| | | this.restTemplate = restTemplate ; |
| | | } |
| | | |
| | | |
| | | public void deal(Data data) { |
| | | if (data.rtuResultSendWebUrl != null && data.rtuResultSendWebUrl.trim().equals("")) { |
| | | String url = UriComponentsBuilder.fromUriString(data.rtuResultSendWebUrl) |