CodeReader14Connect.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 io.netty.bootstrap.Bootstrap;
  7. import io.netty.buffer.ByteBuf;
  8. import io.netty.channel.*;
  9. import io.netty.channel.nio.NioEventLoopGroup;
  10. import io.netty.channel.socket.SocketChannel;
  11. import io.netty.channel.socket.nio.NioSocketChannel;
  12. import io.netty.util.CharsetUtil;
  13. import lombok.extern.slf4j.Slf4j;
  14. import java.util.concurrent.Callable;
  15. import java.util.concurrent.ExecutionException;
  16. import java.util.concurrent.TimeUnit;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. /**
  19. * Created with IntelliJ IDEA.
  20. *
  21. * @author: liuzhifei
  22. * Date: 2023/8/9
  23. * Time: 15:45
  24. * To change this template use File | Settings | File Templates.
  25. * Description:倒角读码器连接
  26. **/
  27. @Slf4j
  28. public class CodeReader14Connect implements TCPConnectBase {
  29. private final String IP_ADDR = "172.20.27.14";
  30. private final int PORT = 51236;
  31. private final String deviceName ="CodeReader14";
  32. private boolean enable = false;
  33. private ChannelFuture future;
  34. private Bootstrap bootstrap;
  35. private EventLoopGroup group;
  36. private ChannelPipeline pipeline;
  37. @Override
  38. public void init() throws InterruptedException {
  39. if (enable) {
  40. log.info("ip:{},deviceName:{}正在进行连接", IP_ADDR, deviceName);
  41. group = new NioEventLoopGroup();
  42. try {
  43. bootstrap = new Bootstrap();
  44. bootstrap.group(group).channel(NioSocketChannel.class)
  45. .option(ChannelOption.TCP_NODELAY, true)
  46. .handler(new ChannelInitializer<SocketChannel>() {
  47. @Override
  48. protected void initChannel(SocketChannel socketChannel) throws Exception {
  49. pipeline = socketChannel.pipeline();
  50. pipeline.addLast(new ChannelInboundHandlerAdapter(){
  51. @Override
  52. public void channelInactive(ChannelHandlerContext ctx) {
  53. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "2"));
  54. log.error("设备:{}连接已断开!",deviceName);
  55. retry();
  56. }
  57. @Override
  58. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  59. ByteBuf byteBuf = (ByteBuf) msg;
  60. processMessages(String.valueOf(byteBuf.toString(CharsetUtil.UTF_8)));
  61. }
  62. });
  63. }
  64. });
  65. future = bootstrap.connect(IP_ADDR, PORT).sync();
  66. future.addListener((channelFuture) -> {
  67. if (channelFuture.isSuccess()) {
  68. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName, "1"));
  69. log.info("ip:{},deviceName:{}连接成功", IP_ADDR, deviceName);
  70. } else {
  71. log.info("ip:{},deviceName:{}连接失败等待重试!", IP_ADDR, deviceName);
  72. retry();
  73. }
  74. });
  75. } catch (Exception e) {
  76. log.error("ip:{},deviceName:{}连接异常,{},准备重试", IP_ADDR, deviceName, e.getMessage());
  77. retry();
  78. }
  79. }
  80. }
  81. @Override
  82. public void retry() {
  83. Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
  84. .retryIfResult(Boolean.FALSE::equals)
  85. .retryIfExceptionOfType(Exception.class)
  86. .withStopStrategy(StopStrategies.neverStop())
  87. .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.SECONDS))
  88. .build();
  89. try {
  90. retryer.call(new Callable<Boolean>() {
  91. AtomicBoolean isSuccess = new AtomicBoolean(false);
  92. @Override
  93. public Boolean call() throws Exception {
  94. log.info("ip:{},deviceName:{}连接重试",IP_ADDR,deviceName);
  95. future = bootstrap.connect(IP_ADDR, PORT).sync();
  96. future.addListener((channelFuture) -> {
  97. isSuccess.set(channelFuture.isSuccess());
  98. if (channelFuture.isSuccess()) {
  99. SpringUtil.getApplicationContext().publishEvent(new Device(deviceName,"1"));
  100. log.info("ip:{},deviceName:{}连接成功",IP_ADDR,deviceName);
  101. }else {
  102. log.info("ip:{},deviceName:{}连接失败等待重试!",IP_ADDR,deviceName);
  103. }
  104. });
  105. Thread.sleep(3000);
  106. return isSuccess.get();
  107. }
  108. });
  109. } catch (RetryException | ExecutionException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. @Override
  114. public void close() {
  115. group.shutdownGracefully();
  116. }
  117. @Override
  118. public String getDeviceName() {
  119. return deviceName;
  120. }
  121. @Override
  122. public ChannelPipeline getChannel() {
  123. return null;
  124. }
  125. @Override
  126. public void processMessages(String message) {
  127. }
  128. }