package com.casic.server; import com.casic.enums.RelaySwitchEnums; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; import lombok.extern.slf4j.Slf4j; import java.nio.charset.Charset; import java.util.List; /** * 自定义解码器 * * @description: 将从接口取到的数据编码 * @author: cz * @create: 2023-5-10 **/ @Slf4j public class ReceiverDecoder extends ByteToMessageDecoder implements RelaySwitchEnums { // final ChannelGroup channels = // new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); @Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { if (msg.toString(Charset.defaultCharset()).toLowerCase().contains("aaaa")) { pareDeviceHex(msg); } // if(msg.readableBytes()>2){ // out.add(msg); // } } // @Override // public void channelActive(ChannelHandlerContext ctx) throws Exception { // channels.add(ctx.channel()); // super.channelActive(ctx); // } // // @Override // public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // super.channelRead(ctx, msg); // ByteBuf out = ByteBufAllocator.DEFAULT.heapBuffer(); // String switchConent = preFix + 1 + "," + 1 + postFix; // out.writeBytes(switchConent.getBytes()); // channels.forEach( // channel ->{ // if(channel!=ctx.channel()) { // channel.writeAndFlush(out); // } // } // ); // } private static void pareDeviceHex(ByteBuf byteBuf) { //去掉补位 log.info(" String : " + byteBuf.toString(Charset.defaultCharset())); String devcode = byteBuf.slice(0, 15).toString(Charset.defaultCharset()); log.info(" devcode : " + devcode); byteBuf.resetReaderIndex();//重置位判断 String valueStr = byteBuf.slice(26, 4).toString(Charset.defaultCharset()); log.info(" valueStr : " + valueStr); long defaultValue = Long.valueOf(valueStr); log.info("设备编号为" + devcode + ",设备数据为" + defaultValue); // System.out.println("设备编号为" + devcode + ",设备数据为" + defaultValue); } }