InkjetPrintersConnect.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package com.warewms.hailiang.connect;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import com.github.rholder.retry.*;
  4. import com.warewms.hailiang.connect.base.TCPConnectBase;
  5. import com.warewms.hailiang.domain.Device;
  6. import com.warewms.hailiang.enums.InkjetPrintersDirectivesEnum;
  7. import com.warewms.hailiang.util.ParseMsgTools;
  8. import io.netty.bootstrap.Bootstrap;
  9. import io.netty.buffer.ByteBuf;
  10. import io.netty.buffer.Unpooled;
  11. import io.netty.channel.*;
  12. import io.netty.channel.nio.NioEventLoopGroup;
  13. import io.netty.channel.socket.SocketChannel;
  14. import io.netty.channel.socket.nio.NioSocketChannel;
  15. import io.netty.util.CharsetUtil;
  16. import lombok.extern.slf4j.Slf4j;
  17. import java.util.concurrent.Callable;
  18. import java.util.concurrent.ExecutionException;
  19. import java.util.concurrent.TimeUnit;
  20. import java.util.concurrent.atomic.AtomicBoolean;
  21. /**
  22. * Created with IntelliJ IDEA.
  23. *
  24. * @author: liuzhifei
  25. * Date: 2023/8/9
  26. * Time: 15:45
  27. * To change this template use File | Settings | File Templates.
  28. * Description:喷码机tcp连接类
  29. **/
  30. @Slf4j
  31. public class InkjetPrintersConnect implements TCPConnectBase {
  32. private final String IP_ADDR = "172.20.27.4";
  33. private final int PORT = 51236;
  34. private final String deviceName = "InkjetPrinters";
  35. private boolean enable = true;
  36. private ChannelFuture future;
  37. private Bootstrap bootstrap;
  38. private EventLoopGroup group;
  39. private ChannelPipeline pipeline;
  40. private boolean isInitRoutine = false;
  41. private String movement;
  42. @Override
  43. public void init() throws InterruptedException {
  44. if (enable) {
  45. log.info("喷码机正在进行连接");
  46. group = new NioEventLoopGroup();
  47. try {
  48. bootstrap = new Bootstrap();
  49. bootstrap.group(group).channel(NioSocketChannel.class)
  50. .option(ChannelOption.TCP_NODELAY, true)
  51. .handler(new ChannelInitializer<SocketChannel>() {
  52. @Override
  53. protected void initChannel(SocketChannel socketChannel) throws Exception {
  54. pipeline = socketChannel.pipeline();
  55. pipeline.addLast(new ChannelInboundHandlerAdapter() {
  56. @Override
  57. public void channelInactive(ChannelHandlerContext ctx) {
  58. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "2"));
  59. log.error("设备:{}连接已断开!", deviceName);
  60. retry();
  61. }
  62. @Override
  63. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  64. ByteBuf byteBuf = (ByteBuf) msg;
  65. processMessages(ParseMsgTools.StringTohexString(byteBuf.toString(CharsetUtil.US_ASCII)));
  66. }
  67. });
  68. }
  69. });
  70. future = bootstrap.connect(IP_ADDR, PORT).sync();
  71. future.addListener((channelFuture) -> {
  72. if (channelFuture.isSuccess()) {
  73. initRoutine();
  74. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
  75. log.info("喷码机连接成功");
  76. } else {
  77. log.info("连接失败等待重试!");
  78. retry();
  79. }
  80. });
  81. } catch (Exception e) {
  82. log.error("喷码设备连接异常,{},准备重试", e.getMessage());
  83. retry();
  84. }
  85. }
  86. }
  87. @Override
  88. public void retry() {
  89. Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
  90. .retryIfResult(Boolean.FALSE::equals)
  91. .retryIfExceptionOfType(Exception.class)
  92. .withStopStrategy(StopStrategies.neverStop())
  93. .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
  94. .build();
  95. try {
  96. retryer.call(new Callable<Boolean>() {
  97. AtomicBoolean isSuccess = new AtomicBoolean(false);
  98. @Override
  99. public Boolean call() throws Exception {
  100. log.info("喷码机连接重试");
  101. future = bootstrap.connect(IP_ADDR, PORT).sync();
  102. future.addListener((channelFuture) -> {
  103. isSuccess.set(channelFuture.isSuccess());
  104. if (channelFuture.isSuccess()) {
  105. initRoutine();
  106. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
  107. log.info("喷码机连接成功");
  108. } else {
  109. log.info("连接失败等待重试!");
  110. }
  111. });
  112. Thread.sleep(3000);
  113. return isSuccess.get();
  114. }
  115. });
  116. } catch (RetryException | ExecutionException e) {
  117. log.error("喷码机连接故障,msg:",e);
  118. }
  119. }
  120. @Override
  121. public void close() {
  122. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "2"));
  123. group.shutdownGracefully();
  124. }
  125. @Override
  126. public String getDeviceName() {
  127. return deviceName;
  128. }
  129. @Override
  130. public ChannelPipeline getChannel() {
  131. return pipeline;
  132. }
  133. @Override
  134. public void processMessages(String message) {
  135. log.info("喷码器消息:{}", message);
  136. if ("f7".equalsIgnoreCase(message)) {
  137. log.info("喷码机打印成功!");
  138. }
  139. if ("6".equals(message)) {
  140. log.info(movement + "完成");
  141. }
  142. }
  143. /**
  144. * 初始化打印设置
  145. */
  146. private void initRoutine() {
  147. if (!isInitRoutine) {
  148. log.info("初始化打印设置");
  149. movement = "更新喷码机喷码内容";
  150. pipeline.writeAndFlush(Unpooled.copiedBuffer(ParseMsgTools.hexString2Bytes(InkjetPrintersDirectivesEnum.CODE41.getCode())));
  151. isInitRoutine = true;
  152. }
  153. }
  154. }