package com.ard.utils.tcp;
|
|
import com.ard.utils.SpringTool;
|
import io.netty.buffer.ByteBuf;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.SimpleChannelInboundHandler;
|
import lombok.extern.slf4j.Slf4j;
|
import sun.nio.cs.ext.GBK;
|
|
import javax.xml.bind.DatatypeConverter;
|
import java.nio.charset.Charset;
|
import java.nio.charset.StandardCharsets;
|
import java.text.SimpleDateFormat;
|
import java.util.*;
|
|
/**
|
* @Description: tcp客户端处理
|
* @ClassName: NettyTcpClientHandler
|
* @Author: 刘苏义
|
* @Date: 2023年06月25日17:02
|
* @Version: 1.0
|
**/
|
@Slf4j(topic = "radar")
|
public class NettyTcpClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
|
|
@Override
|
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
|
// 处理接收到的消息
|
byte[] byteArray = new byte[msg.readableBytes()];
|
msg.getBytes(msg.readerIndex(), byteArray);
|
byte[] bytes = receiveCompletePacket(byteArray);
|
if (bytes != null) {
|
String hexString = DatatypeConverter.printHexBinary(bytes);
|
log.info(hexString);
|
processData(bytes);
|
}
|
}
|
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) {
|
// 当客户端连接成功后,发送消息给服务器
|
TimerTask timerTask = new TimerTask() {
|
@Override
|
public void run() {
|
ByteBuf message = ctx.alloc().buffer();
|
byte[] heart = {0x01, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00, (byte) 0x83, (byte) 0x88, 0x5d, 0x71, 0x01, 0x02, 0x00};
|
String hexString = DatatypeConverter.printHexBinary(heart);
|
log.info("发送心跳:" + hexString);
|
message.writeBytes(heart);
|
ctx.writeAndFlush(message);
|
}
|
};
|
Timer timer = new Timer();
|
// timer.schedule(timerTask, 0, 6000);
|
}
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
log.error("连接异常");
|
// 发生异常时的处理
|
cause.printStackTrace();
|
ctx.close();
|
}
|
|
public static void processData(byte[] data) {
|
try {
|
data = transferData(data);//去掉包头和包尾及转义
|
String s = DatatypeConverter.printHexBinary(data);
|
log.info(s);
|
byte[] type = Arrays.copyOfRange(data, index.type[0], index.type[1]);//命令类型
|
log.info("命令类型:" + DatatypeConverter.printHexBinary(type));
|
|
byte[] cmdId = Arrays.copyOfRange(data, index.funcc[0], index.funcc[1]);//命令ID
|
log.info("命令ID:" + DatatypeConverter.printHexBinary(cmdId));
|
|
byte[] payloadSize = Arrays.copyOfRange(data, index.payloadSize[0], index.payloadSize[1]);//有效负载大小
|
// log.info("有效负载大小:" + DatatypeConverter.printHexBinary(payloadSize));
|
payloadSize = toLittleEndian(payloadSize);
|
// log.info("有效负载大小(转小端):" + DatatypeConverter.printHexBinary(payloadSize));
|
int payloadSizeToDecimal = byteArrayToDecimal(payloadSize);
|
log.info("有效负载大小(转整型):" + payloadSizeToDecimal);
|
|
if (Arrays.equals(cmdId, new byte[]{0x01})) {
|
byte[] dwTim = Arrays.copyOfRange(data, index.dwTim[0], index.dwTim[1]);
|
// log.info("周视图像的出现时间:" + DatatypeConverter.printHexBinary(dwTim));
|
dwTim = toLittleEndian(dwTim);
|
// log.info("周视图像的出现时间(转小端):" + DatatypeConverter.printHexBinary(dwTim));
|
// log.info("周视图像的出现时间(转整型):" + byteArrayToDecimal(dwTim));
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
long l = byteArrayToDecimal(dwTim);
|
String format = sdf.format(l * 1000);
|
log.info("周视图像的出现时间(转date):" + format);
|
|
byte[] wTargetNum = Arrays.copyOfRange(data, index.wTargetNum[0], index.wTargetNum[1]);
|
wTargetNum = toLittleEndian(wTargetNum);
|
// log.info("目标总点数(转小端):" + DatatypeConverter.printHexBinary(wTargetNum));
|
int targetNum = byteArrayToDecimal(wTargetNum);
|
log.info("目标总点数(转整型):" + targetNum);
|
|
//解析NET_TARGET_UNIT(64是NET_TARGET_HEAD的字节数)
|
int unitIndex = index.acRes[1];
|
log.info("有效负载无头起始位:" + unitIndex);
|
|
// byte datum = data[unitIndex];
|
// log.info("无头起始字节:" + SpringTool.byteToHex(datum));
|
int UNITNum = (payloadSizeToDecimal - 64) / targetNum;
|
log.info("单个Unit大小:" + UNITNum);
|
for (int i = 0; i < targetNum; i++) {
|
Integer index = unitIndex + UNITNum * i;
|
byte[] dwID = Arrays.copyOfRange(data, index, index + 4);
|
dwID = toLittleEndian(dwID);
|
// log.info("dwID:" + DatatypeConverter.printHexBinary(dwID));
|
int id = byteArrayToDecimal(dwID);
|
log.info("目标ID号:" + id);
|
|
byte[] dwGSum = Arrays.copyOfRange(data, index + 4, index + 8);
|
dwGSum = toLittleEndian(dwGSum);
|
int GSum = byteArrayToDecimal(dwGSum);
|
log.info("目标当前像素灰度和:" + GSum);
|
|
byte[] iDistance = Arrays.copyOfRange(data, index + 8, index + 12);
|
iDistance = toLittleEndian(iDistance);
|
int Distance = byteArrayToDecimal(iDistance);
|
log.info("目标当前距离(m):" + Distance);
|
|
byte[] iTw = Arrays.copyOfRange(data, index + 12, index + 16);
|
iTw = toLittleEndian(iTw);
|
int Tw = byteArrayToDecimal(iTw);
|
log.info("目标当前的像素宽度:" + Tw);
|
|
byte[] iTh = Arrays.copyOfRange(data, index + 16, index + 20);
|
iTh = toLittleEndian(iTh);
|
int Th = byteArrayToDecimal(iTh);
|
log.info("目标当前的像素高度:" + Th);
|
|
byte[] wPxlArea = Arrays.copyOfRange(data, index + 20, index + 22);
|
wPxlArea = toLittleEndian(wPxlArea);
|
int PxlArea = byteArrayToDecimal(wPxlArea);
|
log.info("目标当前像素面积:" + PxlArea);
|
|
byte[] cTrkNum = Arrays.copyOfRange(data, index + 22, index + 23);
|
cTrkNum = toLittleEndian(cTrkNum);
|
int TrkNum = byteArrayToDecimal(cTrkNum);
|
log.info("轨迹点数:" + TrkNum);
|
|
byte[] cStat = Arrays.copyOfRange(data, index + 23, index + 24);
|
cStat = toLittleEndian(cStat);
|
int Stat = byteArrayToDecimal(cStat);
|
log.info("目标当前状态:" + Stat);
|
|
byte[] sVx = Arrays.copyOfRange(data, index + 24, index + 26);
|
sVx = toLittleEndian(sVx);
|
int Vx = byteArrayToDecimal(sVx);
|
log.info("目标当前速度矢量(像素距离)X:" + Vx);
|
|
byte[] sVy = Arrays.copyOfRange(data, index + 26, index + 28);
|
sVy = toLittleEndian(sVy);
|
int Vy = byteArrayToDecimal(sVy);
|
log.info("目标当前速度矢量(像素距离)Y:" + Vy);
|
|
byte[] sAreaNo = Arrays.copyOfRange(data, index + 28, index + 30);
|
sAreaNo = toLittleEndian(sAreaNo);
|
int AreaNo = byteArrayToDecimal(sAreaNo);
|
log.info("目标归属的告警区域号:" + AreaNo);
|
|
byte[] cGrp = Arrays.copyOfRange(data, index + 30, index + 31);
|
cGrp = toLittleEndian(cGrp);
|
int Grp = byteArrayToDecimal(cGrp);
|
log.info("所属组:" + Grp);
|
|
byte[] szName = Arrays.copyOfRange(data, index + 64, index + 96);
|
String str = new String(szName, "GBK");
|
log.info("所属告警区域名称:" + str);
|
}
|
}
|
} catch (Exception ex) {
|
log.error(ex.getMessage());
|
}
|
}
|
|
public static int byteArrayToDecimal(byte[] byteArray) {
|
int decimalValue = 0;
|
|
for (int i = 0; i < byteArray.length; i++) {
|
decimalValue = (decimalValue << 8) | (byteArray[i] & 0xFF);
|
}
|
|
return decimalValue;
|
}
|
|
public static byte[] toLittleEndian(byte[] bigEndianBytes) {
|
byte[] littleEndianBytes = new byte[bigEndianBytes.length];
|
|
for (int i = 0; i < bigEndianBytes.length; i++) {
|
int j = bigEndianBytes.length - i - 1;
|
littleEndianBytes[i] = bigEndianBytes[j];
|
}
|
|
return littleEndianBytes;
|
}
|
|
|
public static Integer processPayloadSize(String payloadSize) {//解析有效负载大小
|
Integer payloadSizeInfo = 0;
|
String[] payloadSizeArr = payloadSize.split("");
|
for (int i = 0; i <= payloadSizeArr.length - 1; i++) {
|
Integer num = null;
|
switch (payloadSizeArr[i]) {
|
case "0":
|
num = 0;
|
break;
|
case "1":
|
num = 1;
|
break;
|
case "2":
|
num = 2;
|
break;
|
case "3":
|
num = 3;
|
break;
|
case "4":
|
num = 4;
|
break;
|
case "5":
|
num = 5;
|
break;
|
case "6":
|
num = 6;
|
break;
|
case "7":
|
num = 7;
|
break;
|
case "8":
|
num = 8;
|
break;
|
case "9":
|
num = 9;
|
break;
|
case "a":
|
num = 10;
|
break;
|
case "b":
|
num = 11;
|
break;
|
case "c":
|
num = 12;
|
break;
|
case "d":
|
num = 13;
|
break;
|
case "e":
|
num = 14;
|
break;
|
case "f":
|
num = 15;
|
break;
|
default:
|
break;
|
}
|
if (i % 2 == 0) {
|
payloadSizeInfo = payloadSizeInfo + num * 16;
|
} else {
|
payloadSizeInfo = payloadSizeInfo + num * 256;
|
}
|
}
|
return payloadSizeInfo;
|
}
|
|
public static Boolean checkPayloadSize(Integer payloadSizeInfo, String data) {//校验有效负载大小
|
Integer payloadSize = (data.length() - 8 - 8) / 2;//去除包头8位和校验8位
|
if (payloadSize.equals(payloadSizeInfo)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
// 创建缓冲区列表
|
List<Byte> buffer = new ArrayList<>();
|
|
public byte[] receiveCompletePacket(byte[] receivedData) {
|
// 定义包尾字节序列
|
byte[] packetEnd = {0x01, 0x02, 0x00};
|
// 添加已接收的数据到缓冲区
|
for (byte data : receivedData) {
|
buffer.add(data);
|
}
|
// 检查缓冲区中的数据是否包含完整的包
|
while (buffer.size() >= packetEnd.length) {
|
int endIndex = findPacketEndIndex(buffer, packetEnd);
|
if (endIndex != -1) {
|
// 找到完整的包
|
byte[] packet = extractPacketFromBuffer(buffer, endIndex + packetEnd.length);
|
return packet;
|
} else {
|
// 未找到包尾,继续接收数据
|
break;
|
}
|
}
|
|
// 未找到完整的包
|
return null;
|
}
|
|
public static int findPacketEndIndex(List<Byte> buffer, byte[] packetEnd) {
|
for (int i = 0; i <= buffer.size() - packetEnd.length; i++) {
|
boolean isMatch = true;
|
for (int j = 0; j < packetEnd.length; j++) {
|
if (buffer.get(i + j) != packetEnd[j]) {
|
isMatch = false;
|
break;
|
}
|
}
|
if (isMatch) {
|
return i;
|
}
|
}
|
return -1;
|
}
|
|
public static byte[] extractPacketFromBuffer(List<Byte> buffer, int endIndex) {
|
byte[] packet = new byte[endIndex];
|
for (int i = 0; i < endIndex; i++) {
|
packet[i] = buffer.get(i);
|
}
|
buffer.subList(0, endIndex).clear();
|
return packet;
|
}
|
|
//去掉包头和包尾校验及转义
|
public static byte[] transferData(byte[] data) {
|
data = Arrays.copyOfRange(data, 3, data.length);
|
data = Arrays.copyOfRange(data, 0, data.length - 7);
|
String dataStr = DatatypeConverter.printHexBinary(data);
|
if (dataStr.contains("01020201")) {//转义01020201
|
dataStr = dataStr.replaceAll("01020201", "010201");
|
}
|
if (dataStr.contains("01020200")) {//转义01020200
|
dataStr = dataStr.replaceAll("01020200", "010200");
|
}
|
if (dataStr.contains("01020202")) {//转义01020202
|
dataStr = dataStr.replaceAll("01020202", "010202");
|
}
|
data = DatatypeConverter.parseHexBinary(dataStr);
|
return data;
|
}
|
}
|