Переглянути джерело

String组装改为StringBuilder

Handler内组装启用CompositionBuffer
tags/v1.0.0^2
林万龙 3 роки тому
джерело
коміт
36e4faf9bd
6 змінених файлів з 127 додано та 71 видалено
  1. +34
    -0
      src/main/java/com/telpo/dipperposition/common/HexConvert.java
  2. +42
    -19
      src/main/java/com/telpo/dipperposition/handler/NettyServerHandler.java
  3. +4
    -15
      src/main/java/com/telpo/dipperposition/server/DipperPositionServer.java
  4. +17
    -16
      src/main/java/com/telpo/dipperposition/service/impl/DipperAstPosAsyncTaskServiceImpl.java
  5. +21
    -20
      src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java
  6. +9
    -1
      src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java

+ 34
- 0
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) {
//


+ 42
- 19
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);
}

}

/**


+ 4
- 15
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 {


+ 17
- 16
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<astPosCmdBytes.length; i++) {
String s = Integer.toHexString(astPosCmdBytes[i] & 0xff);
byte[] astPosCmdBytes = HexConvert.hexStringToBytes(astPosCmdBuf.toString());
StringBuilder astPosCmdNewBuf = new StringBuilder();
for (byte astPosCmdByte : astPosCmdBytes) {
String s = Integer.toHexString(astPosCmdByte & 0xff);
if (s.length() < 2) {
astPosCmdBuf.append('0');
astPosCmdNewBuf.append('0');
}
astPosCmdBuf.append(s + " ");
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);

}
}

+ 21
- 20
src/main/java/com/telpo/dipperposition/service/impl/DipperAstTimeAsyncTaskServiceImpl.java Переглянути файл

@@ -43,25 +43,26 @@ public class DipperAstTimeAsyncTaskServiceImpl implements IDipperAstTimeAsyncTas
// 00 5E D0 B2 表示 3 秒的时间精度(十六进制 B2 D0 5E 00 转为十进制为3000000000,乘以比 例因子 10-9就是 3 秒 小端模式00 5E D0 B2)
// 00 2F 为校验和
// TODO astTimeCmd 组装
String astTimeCmd = "233E0402";
astTimeCmd += "1000";
astTimeCmd += "20E1";
StringBuilder astTimeCmdBuf = new StringBuilder();
astTimeCmdBuf.append("233E0402");
astTimeCmdBuf.append("1000");
astTimeCmdBuf.append("20E1");
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
String hexYearString = Integer.toHexString(year);
hexYearString = "0" + hexYearString;
astTimeCmd += hexYearString.substring(2,3).toUpperCase() + hexYearString.substring(0,1).toUpperCase();
astTimeCmdBuf.append(hexYearString.substring(2,3).toUpperCase() + hexYearString.substring(0,1).toUpperCase());
String hexMonthString = Integer.toHexString(month);
hexMonthString = "0" + hexMonthString;
astTimeCmd += hexMonthString.toUpperCase();
astTimeCmdBuf.append(hexMonthString.toUpperCase());

String hexDayString = Integer.toHexString(day);
if (day < 16) {
hexDayString = "0" + hexDayString;
}
astTimeCmd += hexDayString.toUpperCase();
astTimeCmdBuf.append(hexDayString.toUpperCase());

int hour = now.getHour();
int minitor = now.getMinute();
@@ -70,41 +71,41 @@ public class DipperAstTimeAsyncTaskServiceImpl implements IDipperAstTimeAsyncTas
if (hour < 16) {
hexHourString = "0" + hexHourString;
}
astTimeCmd += hexHourString.toUpperCase();
astTimeCmdBuf.append(hexHourString.toUpperCase());
String hexMinitorString = Integer.toHexString(minitor);
if (minitor < 16) {
hexMinitorString = "0" + hexMinitorString;
}
astTimeCmd += hexMinitorString.toUpperCase();
astTimeCmdBuf.append(hexMinitorString.toUpperCase());
String hexSecondString = Integer.toHexString(second);
if (second < 16) {
hexSecondString = "0" + hexSecondString;
}
astTimeCmd += hexSecondString.toUpperCase();
astTimeCmd += "0000000000";
astTimeCmd += "005ED0B2";
astTimeCmdBuf.append(hexSecondString.toUpperCase());
astTimeCmdBuf.append("0000000000");
astTimeCmdBuf.append("005ED0B2");

byte[] astTimeCmdBytes = HexConvert.hexStringToBytes(astTimeCmd);
StringBuffer astTimeCmdBuf = new StringBuffer();
byte[] astTimeCmdBytes = HexConvert.hexStringToBytes(astTimeCmdBuf.toString());
StringBuilder astTimeCmdNewBuf = new StringBuilder();
for(int i=0; i<astTimeCmdBytes.length; i++) {
String s = Integer.toHexString(astTimeCmdBytes[i] & 0xff);
if (s.length() < 2) {
astTimeCmdBuf.append('0');
astTimeCmdNewBuf.append('0');
}
astTimeCmdBuf.append(s + " ");
astTimeCmdNewBuf.append(s + " ");
}

log.info(astTimeCmd);
String checkSum = HexConvert.makeChecksum(astTimeCmd).toUpperCase();
StringBuffer astCheckSumBuf = new StringBuffer();
//log.info(astTimeCmd);
String checkSum = HexConvert.makeChecksum(astTimeCmdBuf.toString()).toUpperCase();
StringBuilder astCheckSumBuf = new StringBuilder();
astCheckSumBuf.append(checkSum);
while (astCheckSumBuf.length()<4) {
astCheckSumBuf.insert(0,"0");
}
checkSum = astCheckSumBuf.toString();
log.info(checkSum);
//log.info(checkSum);

return astTimeCmdBuf.toString() + checkSum.substring(0,2) + " " + checkSum.substring(2,4);
return astTimeCmdNewBuf.toString() + checkSum.substring(0,2) + " " + checkSum.substring(2,4);
//String hexIn = astTimeCmd + HexConvert.makeChecksum(astTimeCmd);
//log.info("DipperAstTimeAsyncTaskServiceImpl 返回时间:" + hexIn);



+ 9
- 1
src/main/java/com/telpo/dipperposition/service/impl/DipperDataAsyncTaskServiceImpl.java Переглянути файл

@@ -54,7 +54,15 @@ public class DipperDataAsyncTaskServiceImpl implements IDipperDataAsyncTaskServi

// astTimeCmd 组装
String astTimeCmd = DIPPER_ALL_DATA_REQ;
String hexIn = astTimeCmd + HexConvert.makeChecksum(astTimeCmd);
String checkSum = HexConvert.makeChecksum(astTimeCmd.toString()).toUpperCase();
StringBuilder astCheckSumBuf = new StringBuilder();
astCheckSumBuf.append(checkSum);
while (astCheckSumBuf.length()<4) {
astCheckSumBuf.insert(0,"0");
}
checkSum = astCheckSumBuf.toString();

String hexIn = astTimeCmd + checkSum.substring(0,2) + " " + checkSum.substring(2,4);

String ackAckCheckRef = "233E010102000421293C";
String sendResult = client.sendCmd(hexIn, ackAckCheckRef);


Завантаження…
Відмінити
Зберегти