diff --git a/src/main/java/com/casic/handler/ReceiverServerHandler.java b/src/main/java/com/casic/handler/ReceiverServerHandler.java index b33964d..5868fd5 100644 --- a/src/main/java/com/casic/handler/ReceiverServerHandler.java +++ b/src/main/java/com/casic/handler/ReceiverServerHandler.java @@ -1,10 +1,14 @@ package com.casic.handler; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.javassist.bytecode.ByteArray; import org.springframework.stereotype.Component; /** @@ -39,13 +43,38 @@ String msgStr = msg.toString(); // 以固定行分隔符结尾分割,头部可能有不符合接口协议的字段, 去掉 int idx = msgStr.indexOf("##"); - if(idx >= 0) { + if (idx >= 0) { msgStr = msgStr.substring(idx); } // msgProducerService.sendMsg(Common.ACTIVEMQ_QUEUE_NAME, msgStr); - } + //ByteBuf byteBuf + public static void main(String[] main) { + byte[] hexBytes = {(byte) 0x86, 0x53, 0x28, 0x06, (byte) 0x84, 0x19, (byte) 0x94, + 0x30, (byte) 0xAA, (byte) 0xAA, 0x01, 0x03, (byte) 0x02, 0x02, 0x00, (byte) 0xB8, 0x44}; + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); + out.writeBytes(hexBytes); + pareDeviceHex(out); + } + + private static void pareDeviceHex(ByteBuf byteBuf) { + //去掉补位 + String devcode = ByteBufUtil.hexDump(byteBuf.slice(0, 8)); + devcode = devcode.substring(0, devcode.length() - 1); + //数值读取 + byteBuf.readBytes(12); + byteBuf.markReaderIndex(); + Integer defaultValueLength = byteBuf.readByte() & 0xff; + long defaultValue = 0; + for (int i = 0; i < defaultValueLength; i++) { + long tempLong = byteBuf.readByte() & 0xff; + defaultValue = defaultValue * 256 + tempLong; + } + System.out.println("设备编号为" + devcode + ",设备数据为" + defaultValue); + } + + @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); diff --git a/src/main/java/com/casic/handler/ReceiverServerHandler.java b/src/main/java/com/casic/handler/ReceiverServerHandler.java index b33964d..5868fd5 100644 --- a/src/main/java/com/casic/handler/ReceiverServerHandler.java +++ b/src/main/java/com/casic/handler/ReceiverServerHandler.java @@ -1,10 +1,14 @@ package com.casic.handler; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import lombok.extern.slf4j.Slf4j; +import org.apache.ibatis.javassist.bytecode.ByteArray; import org.springframework.stereotype.Component; /** @@ -39,13 +43,38 @@ String msgStr = msg.toString(); // 以固定行分隔符结尾分割,头部可能有不符合接口协议的字段, 去掉 int idx = msgStr.indexOf("##"); - if(idx >= 0) { + if (idx >= 0) { msgStr = msgStr.substring(idx); } // msgProducerService.sendMsg(Common.ACTIVEMQ_QUEUE_NAME, msgStr); - } + //ByteBuf byteBuf + public static void main(String[] main) { + byte[] hexBytes = {(byte) 0x86, 0x53, 0x28, 0x06, (byte) 0x84, 0x19, (byte) 0x94, + 0x30, (byte) 0xAA, (byte) 0xAA, 0x01, 0x03, (byte) 0x02, 0x02, 0x00, (byte) 0xB8, 0x44}; + ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); + out.writeBytes(hexBytes); + pareDeviceHex(out); + } + + private static void pareDeviceHex(ByteBuf byteBuf) { + //去掉补位 + String devcode = ByteBufUtil.hexDump(byteBuf.slice(0, 8)); + devcode = devcode.substring(0, devcode.length() - 1); + //数值读取 + byteBuf.readBytes(12); + byteBuf.markReaderIndex(); + Integer defaultValueLength = byteBuf.readByte() & 0xff; + long defaultValue = 0; + for (int i = 0; i < defaultValueLength; i++) { + long tempLong = byteBuf.readByte() & 0xff; + defaultValue = defaultValue * 256 + tempLong; + } + System.out.println("设备编号为" + devcode + ",设备数据为" + defaultValue); + } + + @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4f76abd..d27f7a5 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,12 @@ +server: + port: 11315 +################### spring配置 ################### spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://111.198.10.15:11102/smartwell_demos?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&&allowMultiQueries=true&&useSSL=true + username: root + password: Casic203! jms: pub-sub-domain: false activemq: @@ -11,6 +19,8 @@ topic: 66 + + # customer config c-activemq: queue-name: "hjt.msg.queue"