‘liusuyi’
2023-12-28 1c490ff27a0eeb4327923a9367f3a7c2aaa929b2
src/main/java/com/ard/utils/netty/tcp/BootNettyChannelInboundHandlerAdapter.java
@@ -9,10 +9,7 @@
import com.ard.utils.util.GisUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.EmptyByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@@ -21,59 +18,44 @@
import java.net.InetSocketAddress;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static com.ard.utils.util.ByteUtils.byteToBitString;
import static com.ard.utils.util.ByteUtils.toLittleEndian;
@Slf4j(topic = "netty")
@ChannelHandler.Sharable
public class BootNettyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter {
public class BootNettyChannelInboundHandlerAdapter extends SimpleChannelInboundHandler<ByteBuf> {
    /**
     * 从服务端收到新的数据时,这个方法会在收到消息时被调用
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception, IOException {
        if(msg == null){
            return;
        }
        //System.out.println("channelRead:read msg:"+msg1.toString());
        //BootNettyClientChannel bootNettyClientChannel = BootNettyClientChannelCache.get("clientId:"+ctx.channel().id().toString());
        //if(bootNettyClientChannel != null){
        //    System.out.println("to do");
        //    bootNettyClientChannel.setLast_data(msg1.toString());
        //}
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String host = inSocket.getAddress().getHostAddress();
        int port = inSocket.getPort();
        ArdEquipRadar ardEquipRadar = ClientInitialize.tureConnectMap.get(host+":"+port);
        // msg转Buf
        ByteBuf buf = (ByteBuf) msg;
        // 创建缓冲中字节数的字节数组
        byte[] byteArray = new byte[buf.readableBytes()];
        // 写入数组
        buf.readBytes(byteArray);
        // 处理接收到的消息
        byte[] bytes = MessageParsing.receiveCompletePacket(byteArray);
        if (bytes != null) {
            processData(ardEquipRadar, bytes);
        ArdEquipRadar ardEquipRadar = BootNettyClientChannelCache.getRadar(host + ":" + port);
        if (ardEquipRadar != null) {
            // 创建缓冲中字节数的字节数组
            byte[] byteArray = new byte[msg.readableBytes()];
            // 写入数组
            msg.readBytes(byteArray);
            // 处理接收到的消息
            byte[] bytes = MessageParsing.receiveCompletePacket(byteArray);
            if (bytes != null) {
                processData(ardEquipRadar, bytes);
            }
        }
        //回应服务端
        //ctx.write("I got server message thanks server!");
    }
    /**
     * 从服务端收到新的数据、读取完成时调用
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws IOException {
        System.out.println("channelReadComplete");
        //System.out.println("channelReadComplete");
        ctx.flush();
    }
    /**
     * 当出现 Throwable 对象才会被调用,即当 Netty 由于 IO 错误或者处理器在处理事件时抛出的异常时
     */
@@ -83,7 +65,7 @@
        cause.printStackTrace();
        ctx.close();//抛出异常,断开与客户端的连接
    }
    /**
     * 客户端与服务端第一次建立连接时 执行
     */
@@ -92,25 +74,29 @@
        super.channelActive(ctx);
        // 客户端与服务端 建立连接
        InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = inSocket.getAddress().getHostAddress();
        String host = inSocket.getAddress().getHostAddress();
        int port = inSocket.getPort();
        log.debug("连接成功:【"+clientIp+":"+port+"】");
        log.debug("连接成功:【" + host + ":" + port + "】");
    }
    /**
     * 客户端与服务端 断连时 执行
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception{
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        int port = ipSocket.getPort();
        String host = ipSocket.getHostString();
        log.error("与设备" + host + ":" + port + "连接断开!");
        // 重连
        BootNettyClientThread thread = new BootNettyClientThread(host,port);
        thread.start();
        ArdEquipRadar ardEquipRadar = BootNettyClientChannelCache.getRadar(host + ":" + port);
        if (ardEquipRadar != null) {
            BootNettyClientThread thread = new BootNettyClientThread(ardEquipRadar);
            thread.start();
        }
    }
    /**
     * 解析报警数据
     */
@@ -150,7 +136,7 @@
            List<ArdAlarmRadar> well = new ArrayList<>();
            String alarmTime = "";
            Integer targetNum = 0;
            log.debug("Processing radar data 【" + radarName + "】数据-->命令ID:"  + cmdIdStr + "二进制:" + byteToBitString(cmdId[0]));
            log.debug("Processing radar data 【" + radarName + "】数据-->命令ID:" + cmdIdStr + "二进制:" + byteToBitString(cmdId[0]));
            //雷达移动防火报警
            if (Arrays.equals(cmdId, new byte[]{0x01})) {
                //region 告警信息反馈