CodeReader14Handler.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 CodeReader14Handler extends ChannelInboundHandlerAdapter {
  23. private final String deviceName ="CodeReader14";
  24. @Override
  25. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  26. ctx.writeAndFlush(Unpooled.copiedBuffer("T", CharsetUtil.UTF_8));
  27. }
  28. @Override
  29. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  30. ByteBuf byteBuf = (ByteBuf) msg;
  31. log.info(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8)));
  32. }
  33. @Override
  34. public void channelInactive(ChannelHandlerContext ctx) throws Exception {
  35. log.error("设备:{}连接已断开!",deviceName);
  36. InkjetPrintersConnect inkjetPrintersConnect = (InkjetPrintersConnect) TcpServiceRunner.getTCPInstanceList((deviceName));
  37. inkjetPrintersConnect.retry();
  38. super.channelInactive(ctx);
  39. }
  40. }