CodeReader5Handler.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.warewms.hailiang.connect.handler;
  2. import com.warewms.hailiang.connect.InkjetPrintersConnect;
  3. import com.warewms.hailiang.init.TcpServiceRunner;
  4. import io.netty.buffer.ByteBuf;
  5. import io.netty.buffer.Unpooled;
  6. import io.netty.channel.ChannelHandlerContext;
  7. import io.netty.channel.ChannelInboundHandlerAdapter;
  8. import io.netty.util.CharsetUtil;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Component;
  11. /**
  12. * Created with IntelliJ IDEA.
  13. *
  14. * @author: liuzhifei
  15. * Date: 2023/8/9
  16. * Time: 17:07
  17. * To change this template use File | Settings | File Templates.
  18. * Description: 喷码机连接消息处理类
  19. **/
  20. @Slf4j
  21. @Component
  22. public class CodeReader5Handler extends ChannelInboundHandlerAdapter {
  23. @Override
  24. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  25. ctx.writeAndFlush(Unpooled.copiedBuffer("T", CharsetUtil.UTF_8));
  26. }
  27. @Override
  28. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  29. ByteBuf byteBuf = (ByteBuf) msg;
  30. log.info(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8)));
  31. }
  32. @Override
  33. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  34. log.error("倒角读码器连接已断开!");
  35. InkjetPrintersConnect inkjetPrintersConnect = (InkjetPrintersConnect) TcpServiceRunner.getTCPInstanceList().get("InkjetPrintersConnect");
  36. inkjetPrintersConnect.retry();
  37. super.channelInactive(ctx);
  38. }
  39. }