diff --git a/src/main/java/com/telpo/dipperposition/common/HexConvert.java b/src/main/java/com/telpo/dipperposition/common/HexConvert.java index 8f79122..20a8708 100644 --- a/src/main/java/com/telpo/dipperposition/common/HexConvert.java +++ b/src/main/java/com/telpo/dipperposition/common/HexConvert.java @@ -118,6 +118,40 @@ public class HexConvert { } return hex; } + + + /** + * 生成校验码的int值 + * */ + public static String makeChecksumForBytes(byte[] byteDatas) { + if (byteDatas == null || byteDatas.length == 0) { + return ""; + } + int total = 0; + int len = byteDatas.length; + + final String HEX = "0123456789abcdef"; + StringBuilder sb = null; + for (byte b : byteDatas) { + sb = new StringBuilder(2); + // 取出这个字节的高4位,然后与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数 + sb.append(HEX.charAt((b >> 4) & 0x0f)); + // 取出这个字节的低位,与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数 + sb.append(HEX.charAt(b & 0x0f)); + total += Integer.parseInt(sb.toString(), 16); + } + /** + * 用256求余最大是255,即16进制的FF + */ + int mod = total % 256; + String hex = Integer.toHexString(mod); + len = hex.length(); + // 如果不够校验位的长度,补0,这里用的是两位校验 + if (len < 2) { + hex = "0" + hex; + } + return hex; + } // // public static void main(String[] args) { // diff --git a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java index 3d062e5..c9cae7e 100644 --- a/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java +++ b/src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java @@ -5,10 +5,12 @@ import com.telpo.dipperposition.service.IDipperAstPosAsyncTaskService; import com.telpo.dipperposition.service.IDipperAstTimeAsyncTaskService; import com.telpo.dipperposition.service.IDipperDataAsyncTaskService; import io.netty.buffer.ByteBuf; +import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.socket.SocketChannel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -87,7 +89,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { //String str = "通知客户端链接建立成功" + " " + LocalDateTime.now() + " " + channel.localAddress().getHostString() + // "\r\n"; ByteBuf buf = Unpooled.buffer(ackAckCheckRef.getBytes().length); - buf.writeBytes(ackAckCheckRef.getBytes("GBK")); + buf.writeBytes(ackAckCheckRef.getBytes(CharsetUtil.UTF_8)); ctx.writeAndFlush(buf); } @@ -117,46 +119,67 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter { String channelAns = ""; // 返回时间指令 - //if (Integer.parseInt(timeAsycServerPort) == channel.localAddress().getPort()) { - //ByteBuf recvmg = (ByteBuf) msg; ByteBuf buf = null; - //String recvmgStr = recvmg.toString(); - //if (AST_TIME_CMD.equals(recvmg)) { if (AST_TIME_CMD.equals(msg)) { // 初始时间辅助输入; channelAns = nettyServerHandler.dipperTimeAsyncTaskService.pushAstTime(); -// log.info("返回时间:" + channelAns); + //log.debug(channelAns); + if (channelAns != null) { + buf = Unpooled.buffer(channelAns.getBytes().length); + buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + ctx.writeAndFlush(buf); + } } // 发送SDBP-AST-POS获取辅助位置信息 - // if (Integer.parseInt(posAsycServerPort) == channel.localAddress().getPort()) { - - //if (AST_POS_CMD.equals(recvmg)) { if (AST_POS_CMD.equals(msg)) { channelAns = nettyServerHandler.dipperAstPosAsyncTaskService.pushAstPos(ipAddress); + //log.debug(channelAns); + if (channelAns != null) { + buf = Unpooled.buffer(channelAns.getBytes().length); + buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + ctx.writeAndFlush(buf); + } } // 从缓存获取SDBP-AST-EPH星历数 - //if (Integer.parseInt(starsAsycServerPort) == channel.localAddress().getPort()) { - //if (AST_POS_CMD.equals(recvmg)) { if (AST_EPH_CMD.equals(msg)) { channelAns = nettyServerHandler.dipperDataAsyncTaskService.getAstEPH(); + //log.debug(channelAns); + if (channelAns != null) { + buf = Unpooled.buffer(channelAns.getBytes().length); + buf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + ctx.writeAndFlush(buf); + } } // 最后把SDBP-AST-TIME、SDBP-AST-POS、SDBP-AST-EPH并包一起发给设备。 // 设备采用16进制获取数据,则代理服务器也是采用16进制返回数据。 // 通知客户端链消息发送成功 if (AST_ALL_CMD.equals(msg)) { + CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer(); channelAns = nettyServerHandler.dipperTimeAsyncTaskService.pushAstTime(); - channelAns += nettyServerHandler.dipperAstPosAsyncTaskService.pushAstPos(ipAddress); - channelAns += nettyServerHandler.dipperDataAsyncTaskService.getAstEPH(); - } - log.info(channelAns); - if (channelAns != null) { - buf = Unpooled.buffer(channelAns.getBytes().length); - buf.writeBytes(channelAns.getBytes("UTF-8")); - ctx.writeAndFlush(buf); + if (channelAns != null) { + ByteBuf channelTimeAnsBuf = Unpooled.buffer(channelAns.getBytes().length); + channelTimeAnsBuf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + compositeByteBuf.addComponent(channelTimeAnsBuf); + } + channelAns = nettyServerHandler.dipperAstPosAsyncTaskService.pushAstPos(ipAddress); + + if (channelAns != null) { + ByteBuf channelPosAnsBuf = Unpooled.buffer(channelAns.getBytes().length); + channelPosAnsBuf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + compositeByteBuf.addComponent(channelPosAnsBuf); + } + channelAns = nettyServerHandler.dipperDataAsyncTaskService.getAstEPH(); + if (channelAns != null) { + ByteBuf channelPehAnsBuf = Unpooled.buffer(channelAns.getBytes().length); + channelPehAnsBuf.writeBytes(channelAns.getBytes(CharsetUtil.UTF_8)); + compositeByteBuf.addComponent(channelPehAnsBuf); + } + ctx.writeAndFlush(compositeByteBuf); } + } /** diff --git a/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java b/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java index 6aa2edc..e9d63e4 100644 --- a/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java +++ b/src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java @@ -1,12 +1,5 @@ package com.telpo.dipperposition.server; -import com.alibaba.nacos.api.NacosFactory; -import com.alibaba.nacos.api.PropertyKeyConst; -import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.config.annotation.NacosValue; -import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; -import com.telpo.dipperposition.co.PositionConfigInfo; import com.telpo.dipperposition.handler.ServerChannelInitializer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; @@ -15,7 +8,6 @@ import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.stereotype.Component; @@ -29,15 +21,12 @@ import java.net.InetSocketAddress; */ @Slf4j @Component -//@NacosPropertySource(dataId="dipperposition-service", autoRefreshed=true) public class DipperPositionServer { - //@NacosValue("position-server.serverAddr") - //private String myServerAddr; private String serverAddr; private Integer starsAsycPort; - public DipperPositionServer(ConfigurableEnvironment environment) throws NacosException { + public DipperPositionServer(ConfigurableEnvironment environment) { this.serverAddr = environment.getProperty("position-server.serverAddr"); @@ -68,17 +57,17 @@ public class DipperPositionServer { //绑定端口,开始接收进来的连接 try { - ChannelFuture channelFuture3 = bootstrap.bind(socketAddress).sync(); + ChannelFuture channelFuture = bootstrap.bind(socketAddress).sync(); log.info("星历服务器启动开始监听端口: {}", starsAsycPort); //log.info("服务器: {}", myServerAddr); - channelFuture3.addListener(future -> { + channelFuture.addListener(future -> { if (future.isSuccess()){ System.out.println("start success"); }else{ System.out.println("start failed"); } }); - channelFuture3.channel().closeFuture().sync(); + channelFuture.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { 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 13e37a6..a8f0260 100644 --- a/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java +++ b/src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java @@ -210,15 +210,16 @@ public class DipperAstPosAsyncTaskServiceImpl implements IDipperAstPosAsyncTaskS // 00 2F 为校验和 // astPosCmd 组装 - String astPosCmd = "233E0401"; - astPosCmd += "1000"; - astPosCmd += HexConvert.encodeHEX(lanLongValue).toUpperCase(); - astPosCmd += HexConvert.encodeHEX(altLongValue).toUpperCase(); - astPosCmd += "70170000"; - astPosCmd += "A0860100"; - - log.info(astPosCmd); - String checkSum = HexConvert.makeChecksum(astPosCmd).toUpperCase(); + StringBuilder astPosCmdBuf = new StringBuilder(); + astPosCmdBuf.append("233E0401"); + astPosCmdBuf.append("1000"); + astPosCmdBuf.append(HexConvert.encodeHEX(lanLongValue).toUpperCase()); + astPosCmdBuf.append(HexConvert.encodeHEX(altLongValue).toUpperCase()); + 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) { @@ -227,17 +228,17 @@ public class DipperAstPosAsyncTaskServiceImpl implements IDipperAstPosAsyncTaskS checkSum = astCheckSumBuf.toString(); log.info(checkSum); - byte[] astPosCmdBytes = HexConvert.hexStringToBytes(astPosCmd); - StringBuffer astPosCmdBuf = new StringBuffer(); - for(int i=0; i