From 4fb557eaf62b2e6382f245d14df052ff98296392 Mon Sep 17 00:00:00 2001 From: linwl <304115325@qq.com> Date: Wed, 3 Feb 2021 10:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=9F=E5=8E=86=E6=95=B0=E6=8D=AE=E7=AE=80?= =?UTF-8?q?=E5=8C=96=E4=B8=BAbyte[]=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/NettyServerHandler.java | 76 ++++++++++++++----- .../DipperAstPosAsyncTaskServiceImpl.java | 66 ++++++++++------ .../DipperAstTimeAsyncTaskServiceImpl.java | 44 ++++++----- .../impl/DipperDataAsyncTaskServiceImpl.java | 10 ++- .../service/impl/IpProvinceServiceImpl.java | 2 +- src/main/resources/application.properties | 4 +- 6 files changed, 131 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java index 9ec061e..c4f8578 100644 --- a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java +++ b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java @@ -1,5 +1,6 @@ package com.telpo.dipperposition.handler; +import com.telpo.dipperposition.common.HexConvert; import com.telpo.dipperposition.config.PositionConfig; import com.telpo.dipperposition.service.IDipperAstPosAsyncTaskService; import com.telpo.dipperposition.service.IDipperAstTimeAsyncTaskService; @@ -73,10 +74,10 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { //通知客户端链接建立成功 // 默认返回取得时间成功 // String ackAckCheckRef = "233E0101020004020A1D"; - String ackAckCheckRef = "23 3E 01 01 02 00 04 21 29 3C"; - ByteBuf buf = Unpooled.buffer(ackAckCheckRef.getBytes().length); - buf.writeBytes(ackAckCheckRef.getBytes(CharsetUtil.UTF_8)); - ctx.writeAndFlush(buf); + //String ackAckCheckRef = "23 3E 01 01 02 00 04 21 29 3C"; + //ByteBuf buf = Unpooled.buffer(ackAckCheckRef.getBytes().length); + //buf.writeBytes(ackAckCheckRef.getBytes(CharsetUtil.UTF_8)); + //ctx.writeAndFlush(buf); } @@ -112,9 +113,14 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { channelAns = nettyServerHandler.dipperTimeAsyncTaskService.pushAstTime(); //log.debug(channelAns); if (channelAns != null) { - buf = bba.buffer(channelAns.getBytes().length); +// buf = bba.buffer(channelAns.getBytes().length); + byte[] returnBytes = getReturnBytes(channelAns); + buf = bba.buffer(returnBytes.length + 4); //Unpooled.buffer(channelAns.getBytes().length); - buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); +// buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + buf.writeBytes(returnBytes); + buf.writeBytes(getCheckSumBytes(channelAns)); + buf.writeBytes(getReturnBytes()); ctx.write(buf); ctx.flush(); } @@ -124,9 +130,15 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { channelAns = nettyServerHandler.dipperAstPosAsyncTaskService.pushAstPos(ipAddress); //log.debug(channelAns); if (channelAns != null) { - buf = bba.buffer(channelAns.getBytes().length); +// buf = bba.buffer(channelAns.getBytes().length); + byte[] returnBytes = getReturnBytes(channelAns); + buf = bba.buffer(returnBytes.length + 4); //Unpooled.buffer(channelAns.getBytes().length); - buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); +// buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + buf.writeBytes(returnBytes); + buf.writeBytes(getCheckSumBytes(channelAns)); + buf.writeBytes(getReturnBytes()); + ctx.write(buf); ctx.flush(); } @@ -154,23 +166,23 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { channelAns = nettyServerHandler.dipperTimeAsyncTaskService.pushAstTime(); if (channelAns != null) { - log.debug("Time Buffer Length is:" + channelAns.getBytes().length); - ByteBuf channelTimeAnsBuf = bba.buffer(channelAns.getBytes().length); - //Unpooled.buffer(channelAns.getBytes().length); - channelTimeAnsBuf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); - // compositeByteBuf.addComponent(channelTimeAnsBuf); - ctx.write(channelTimeAnsBuf); + byte[] returnBytes = getReturnBytes(channelAns); + buf = bba.buffer(returnBytes.length + 4); + buf.writeBytes(returnBytes); + buf.writeBytes(getCheckSumBytes(channelAns)); + buf.writeBytes(getReturnBytes()); + ctx.write(buf); ctx.flush(); } channelAns = nettyServerHandler.dipperAstPosAsyncTaskService.pushAstPos(ipAddress); if (channelAns != null) { - log.debug("Pos Buffer Length is:" + channelAns.getBytes().length); - ByteBuf channelPosAnsBuf = bba.buffer(channelAns.getBytes().length); - //Unpooled.buffer(channelAns.getBytes().length); - channelPosAnsBuf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); - //compositeByteBuf.addComponent(channelPosAnsBuf); - ctx.write(channelPosAnsBuf); + byte[] returnBytes = getReturnBytes(channelAns); + buf = bba.buffer(returnBytes.length + 4); + buf.writeBytes(returnBytes); + buf.writeBytes(getCheckSumBytes(channelAns)); + buf.writeBytes(getReturnBytes()); + ctx.write(buf); ctx.flush(); } byte[] returnBytes = nettyServerHandler.dipperDataAsyncTaskService.getAstEPH(); @@ -203,5 +215,29 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { ctx.close(); } + /* + * 取得byte数组 + */ + private byte[] getReturnBytes(String astCmdBuf) { + return HexConvert.hexStringToBytes(astCmdBuf); + } + + private byte[] getCheckSumBytes(String astCmdBuf) { + String checkSum = HexConvert.makeChecksum(astCmdBuf).toUpperCase(); + StringBuffer astCheckSumBuf = new StringBuffer(); + astCheckSumBuf.append(checkSum); + while (astCheckSumBuf.length()<4) { + astCheckSumBuf.insert(0,"0"); + } + return getReturnBytes(astCheckSumBuf.toString()); + //log.info(checkSum); + } + + + private byte[] getReturnBytes() { + String returnStr = "0D0A"; + return getReturnBytes(returnStr); + } + } diff --git a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java index 3095d86..800d6f7 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java @@ -163,17 +163,15 @@ public class DipperAstPosAsyncTaskServiceImpl implements IDipperAstPosAsyncTaskS double lanValue = Double.parseDouble(lonStr) * 10000000; long lanLongValue = Double.doubleToLongBits(lanValue); if (lanLongValue < 0) { + // FFFFFFFF - 439C3270 + 1= BC63CD90(补码) lanLongValue = lanLongValue + 4294967295L + 1; } double altValue = Double.parseDouble(altStr) * 10000000; long altLongValue = Double.doubleToLongBits(altValue); if (altLongValue < 0) { + // FFFFFFFF - 439C3270 + 1= BC63CD90(补码) altLongValue = altLongValue + 4294967295L + 1; } - // 小端模式补码 - // FFFFFFFF - 439C3270 + 1= BC63CD90(补码) - lanLongValue = 4294967295L - lanLongValue + 1; - altLongValue = 4294967295L - altLongValue + 1; // 数值换算举例(以经度举例。纬度、高度、位置精度换算方法一致): // (1)经度数值为 113.431,则换算方法如下: @@ -202,32 +200,50 @@ public class DipperAstPosAsyncTaskServiceImpl implements IDipperAstPosAsyncTaskS StringBuilder astPosCmdBuf = new StringBuilder(); astPosCmdBuf.append("233E0401"); astPosCmdBuf.append("1000"); - astPosCmdBuf.append(HexConvert.encodeHEX(lanLongValue).toUpperCase()); - astPosCmdBuf.append(HexConvert.encodeHEX(altLongValue).toUpperCase()); + + + // 小端模式 + String lanString = HexConvert.encodeHEX(lanLongValue).toUpperCase(); + String altSting = HexConvert.encodeHEX(altLongValue).toUpperCase(); + //astPosCmdBuf.append(HexConvert.encodeHEX(lanLongValue).toUpperCase()); + astPosCmdBuf.append(lanString.substring(6,8)); + astPosCmdBuf.append(lanString.substring(4,6)); + astPosCmdBuf.append(lanString.substring(2,4)); + astPosCmdBuf.append(lanString.substring(0,2)); + + //astPosCmdBuf.append(HexConvert.encodeHEX(altLongValue).toUpperCase()); + + astPosCmdBuf.append(altSting.substring(6,8)); + astPosCmdBuf.append(altSting.substring(4,6)); + astPosCmdBuf.append(altSting.substring(2,4)); + astPosCmdBuf.append(altSting.substring(0,2)); astPosCmdBuf.append("70170000"); astPosCmdBuf.append("A0860100"); //log.info(astPosCmd); - String checkSum = HexConvert.makeChecksum(astPosCmdBuf.toString()).toUpperCase(); - StringBuffer astCheckSumBuf = new StringBuffer(); - astCheckSumBuf.append(checkSum); - while (astCheckSumBuf.length()<4) { - astCheckSumBuf.insert(0,"0"); - } - checkSum = astCheckSumBuf.toString(); - //log.info(checkSum); - - byte[] astPosCmdBytes = HexConvert.hexStringToBytes(astPosCmdBuf.toString()); - StringBuilder astPosCmdNewBuf = new StringBuilder(); - for (byte astPosCmdByte : astPosCmdBytes) { - String s = Integer.toHexString(astPosCmdByte & 0xff); - if (s.length() < 2) { - astPosCmdNewBuf.append('0'); - } - astPosCmdNewBuf.append(s + " "); - } +// String checkSum = HexConvert.makeChecksum(astPosCmdBuf.toString()).toUpperCase(); +// StringBuffer astCheckSumBuf = new StringBuffer(); +// astCheckSumBuf.append(checkSum); +// while (astCheckSumBuf.length()<4) { +// astCheckSumBuf.insert(0,"0"); +// } +// checkSum = astCheckSumBuf.toString(); +// //log.info(checkSum); +// +// byte[] astPosCmdBytes = HexConvert.hexStringToBytes(astPosCmdBuf.toString()); +// StringBuilder astPosCmdNewBuf = new StringBuilder(); +// for (byte astPosCmdByte : astPosCmdBytes) { +// String s = Integer.toHexString(astPosCmdByte & 0xff); +// if (s.length() < 2) { +// astPosCmdNewBuf.append('0'); +// } +// astPosCmdNewBuf.append(s + " "); +// } +// + return astPosCmdBuf.toString(); + //+ checkSum.substring(0,2) + " " + checkSum.substring(2,4); + - return astPosCmdNewBuf.toString() + checkSum.substring(0,2) + " " + checkSum.substring(2,4); } } diff --git a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java index 65d35c9..f31cf22 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java @@ -77,30 +77,32 @@ public class DipperAstTimeAsyncTaskServiceImpl implements IDipperAstTimeAsyncTas hexSecondString = "0" + hexSecondString; } astTimeCmdBuf.append(hexSecondString.toUpperCase()); - astTimeCmdBuf.append("0000000000"); + astTimeCmdBuf.append("00000000"); astTimeCmdBuf.append("005ED0B2"); - byte[] astTimeCmdBytes = HexConvert.hexStringToBytes(astTimeCmdBuf.toString()); - StringBuilder astTimeCmdNewBuf = new StringBuilder(); - for(int i=0; i example = Example.of(query, matcher); Optional data = ipProvinceMapper.findOne(example); - return data.get(); + return data.orElse(null); } catch (Exception e) { log.error("获取IP省份异常:", e); return null; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0642c24..eee8f0b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -5,9 +5,9 @@ logging.level.com.telpo.dipperposition.service.mapper=DEBUG spring.application.name=dipperposition-service spring.redis.database=1 -spring.redis.host=172.19.42.45 +spring.redis.host=127.0.0.1 #8090 -spring.redis.port=6389 +spring.redis.port=6379 spring.redis.password=telpo#1234 spring.redis.timeout=3000 spring.redis.lettuce.pool.max-active=100