This commit is contained in:
2026-05-27 08:45:18 +08:00
parent 2547decd29
commit 2a99b0bd5e
24 changed files with 869 additions and 133 deletions

View File

@@ -43,11 +43,12 @@ public class NettyServer {
pipeline.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS));
pipeline.addLast(new HeaderFooterDecoder());
pipeline.addLast(new HexDecoder());
pipeline.addLast(new HexEncoder());
// pipeline.addLast(new HexEncoder());
pipeline.addLast(deviceConnectHandler);
pipeline.addLast(heartbeatHandler);
pipeline.addLast(dataToDataBaseHandler);
pipeline.addLast(commandForwardHandler);
pipeline.addLast(new HexEncoder());
}
});
System.out.println("聊天室服务器启动了...");

View File

@@ -423,6 +423,7 @@ public class DeviceConnectHandler extends SimpleChannelInboundHandler<String> {
sysUserClientService.updateAliveStatus(masterDevice.getConnectorId(), slaveId, 1);
if (masterDevice.getChannel() != null && masterDevice.getChannel().isActive()) {
DeviceRequestDTO reDto = new DeviceRequestDTO();
reDto.setPlatform(requestDTO.getPlatform());
DeviceRespondDTO respond = new DeviceRespondDTO();
respond.setSwitchResult(true);
respond.setHolder("you");

View File

@@ -4,6 +4,8 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@@ -14,8 +16,16 @@ public class HeaderFooterDecoder extends ByteToMessageDecoder {
private static final byte[] FOOTER = {(byte) 0xAA, (byte) 0xAB};
private static final int MAX_FRAME_LENGTH = 4096;
private static final Logger logger = LoggerFactory.getLogger(HeaderFooterDecoder.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
// 获取处理前的原始数据
if (in.readableBytes() > 0) {
byte[] rawData = new byte[in.readableBytes()];
in.getBytes(in.readerIndex(), rawData);
logger.debug("【处理前原始数据】长度:{},内容:{}",rawData.length,Hex.encodeHexString(rawData).toUpperCase());
}
while (true) {
int readerIndex = in.readerIndex();
int writerIndex = in.writerIndex();
@@ -48,9 +58,9 @@ public class HeaderFooterDecoder extends ByteToMessageDecoder {
byte[] frameBytes = new byte[frame.readableBytes()];
frame.getBytes(frame.readerIndex(), frameBytes);
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
+ " 收到原始数据:" + Hex.encodeHexString(frameBytes).toUpperCase());
// System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
// + " 收到原始数据:" + Hex.encodeHexString(frameBytes).toUpperCase());
logger.debug("【处理后原始数据】长度:{},内容:{}",frameBytes.length,Hex.encodeHexString(frameBytes).toUpperCase());
out.add(frame);
}
}

View File

@@ -8,11 +8,27 @@ import org.apache.commons.codec.binary.Hex;
import java.util.Arrays;
// 编码器:16进制字符串 → ByteBuf
public class HexEncoder extends MessageToByteEncoder<String> {
public class HexEncoder extends MessageToByteEncoder<Object> {
// @Override
// protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
// byte[] bytes = Hex.decodeHex(msg.toCharArray()); // 16进制字符串转字节数组
// out.writeBytes(bytes);
// }
@Override
protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
byte[] bytes = Hex.decodeHex(msg.toCharArray()); // 16进制字符串转字节数组
out.writeBytes(bytes);
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
if (msg instanceof String) {
// 16进制字符串转字节数组
byte[] bytes = Hex.decodeHex(((String) msg).toCharArray());
out.writeBytes(bytes);
} else if (msg instanceof ByteBuf) {
// 直接写入 ByteBuf
ByteBuf buf = (ByteBuf) msg;
out.writeBytes(buf);
} else if (msg instanceof byte[]) {
// 直接写入字节数组
out.writeBytes((byte[]) msg);
}
}
}