package com.warewms.hailiang.connect.handler; import com.warewms.common.annotation.Log; import com.warewms.hailiang.connect.InkjetPrintersConnect; import com.warewms.hailiang.init.TcpServiceRunner; import com.warewms.hailiang.util.ParseMsgTools; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * Created with IntelliJ IDEA. * * @author: liuzhifei * Date: 2023/8/9 * Time: 17:07 * To change this template use File | Settings | File Templates. * Description: 喷码机连接消息处理类 **/ @Slf4j @Component public class InkjetPrintersHandler extends ChannelInboundHandlerAdapter { private final String deviceName ="InkjetPrinters"; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(Unpooled.copiedBuffer(ParseMsgTools.StringTohexString(""), CharsetUtil.UTF_8)); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; log.info(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8))); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { log.error("设备:{}连接已断开!",deviceName); InkjetPrintersConnect inkjetPrintersConnect = (InkjetPrintersConnect) TcpServiceRunner.getTCPInstanceList((deviceName)); inkjetPrintersConnect.retry(); super.channelInactive(ctx); } }