Newer
Older
rain_receiver / src / main / java / com / casic / server / ReceiverServerHandler.java
chaizhuang on 7 Dec 2023 4 KB 增加语音发送功能
package com.casic.server;


import com.alibaba.fastjson.JSON;
import com.casic.enums.RelaySwitchEnums;
import com.casic.model.RelayStatusDTO;
import com.casic.resolver.DatagramResolver;
import com.casic.util.SendVoice;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @description: 消息处理handler
 * @author: Stone
 * @create: 2019-01-10 20:01
 **/
@Slf4j
@Component
@ChannelHandler.Sharable
public class ReceiverServerHandler extends ChannelInboundHandlerAdapter implements RelaySwitchEnums {

    volatile ChannelGroup channels =
            new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Autowired
    private List<DatagramResolver> datagramResolverList;

    public ReceiverServerHandler() {
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        super.channelRegistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        channels.add(ctx.channel());
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        channels.forEach(channel -> {
                    if (channel == ctx.channel()) {
                        channels.remove(channel);
                    }
                }
        );
        super.channelInactive(ctx);
    }

    /**
     * 雨量计
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof String) {
            if (!String.valueOf(msg).contains("iccid")) {
                datagramResolverList.forEach(
                        rainFallDataResolver -> {
                            RelayStatusDTO relayStatusDTO = rainFallDataResolver.datagram(String.valueOf(msg));
                            sendMsg(ctx, relayStatusDTO);
                        }
                );
            }
        }
//        if (msg instanceof ByteBuf) {
//            ByteBuf
//            log.info(ByteBufUtil.hexDump((ByteBuf) msg));
//        }
        super.channelRead(ctx, msg);
    }

    private void frozenInvalidByteBuf(ByteBuf byteBuf) {
        byteBuf.resetReaderIndex();
        byteBuf.readBytes(byteBuf.writerIndex());
        byteBuf.markReaderIndex();
    }

    private void sendMsg(ChannelHandlerContext ctx, RelayStatusDTO relayStatusDTO) {
        if (!ObjectUtils.isEmpty(relayStatusDTO)) {
            ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer();
            String switchContent = preFix + relayStatusDTO.getChannelName() + "," + relayStatusDTO.getLampSwitch() + postFix;
//          for (int i = 1; i < 6; i++) {
//          switchConent = preFix + relayStatusDTO.getChannelName() + "," + 1 + postFix;
            log.info(JSON.toJSONString(relayStatusDTO) + "-------" + switchContent);
            if (relayStatusDTO.getLampSwitch() == 1) {
                //只推送报警设备
                SendVoice.send(relayStatusDTO.getDeviceType(), relayStatusDTO.getChannelName());
            }
            out.writeBytes(switchContent.getBytes());
            channels.forEach(channel -> {
                        if (channel.isActive()) {
                            if (channel != ctx.channel()) {
                                channel.writeAndFlush(out);
                            }
                        }
                    }
            );
        }
//        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        cause.printStackTrace();
        ctx.close();
    }

}