package com.casic.server; import com.casic.enums.RelaySwitchEnums; import com.casic.model.RelayStatusDTO; import com.casic.resolver.DatagramResolver; 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.scheduling.concurrent.ThreadPoolTaskExecutor; 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 { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 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) { log.info(ByteBufUtil.hexDump((ByteBuf) msg)); } super.channelRead(ctx, msg); } private void sendMsg(ChannelHandlerContext ctx, RelayStatusDTO relayStatusDTO) { if (!ObjectUtils.isEmpty(relayStatusDTO)) { ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); String switchConent = preFix + relayStatusDTO.getChannelName() + "," + relayStatusDTO.getLampSwitch() + postFix; // for (int i = 1; i < 6; i++) { // switchConent = preFix + relayStatusDTO.getChannelName() + "," + 1 + postFix; System.out.println(switchConent); out.writeBytes(switchConent.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(); } }