InkjetPrintersHandler.java 1.7 KB

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