| | |
| | | import io.netty.handler.timeout.IdleStateHandler; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.net.InetSocketAddress; |
| | | import java.util.HashMap; |
| | |
| | | |
| | | public void initOneConnection(String host,Integer port){ |
| | | |
| | | log.info("调用一次{}:{}",host,port); |
| | | log.info("初始化连接天宇tcp服务器【{}:{}】", host, port); |
| | | //判断该账号下,是否有盒子被控制 |
| | | //当前正在被控制的盒子 |
| | | String currentBoxSn = ""; |
| | | if(channels.containsKey(host+"/"+port)) |
| | | { |
| | | currentBoxSn = channels.get(host+"/"+port).getSubBoxSn(); |
| | | channels.get(host+"/"+port).getFuture().channel().close(); |
| | | if (channels.containsKey(host + ":" + port)) { |
| | | currentBoxSn = channels.get(host + ":" + port).getSubBoxSn(); |
| | | channels.get(host + ":" + port).getFuture().channel().close(); |
| | | } |
| | | |
| | | //异步连接tcp服务端 |
| | | Bootstrap bootstrap = getBootstrap(null); |
| | | ChannelFuture future = bootstrap.connect(host,port).addListener((ChannelFuture futureListener) -> { |
| | | if (futureListener.isSuccess()) { |
| | | log.info("连接天宇TCP服务端成功:"+host+":"+port); |
| | | log.info("连接天宇TCP服务器成功【{}:{}】", host, port); |
| | | } else { |
| | | final EventLoop eventLoop = futureListener.channel().eventLoop(); |
| | | eventLoop.schedule(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | log.info("连接天宇TCP服务端失败ip:{},port:{},正尝试重连...",host,port); |
| | | eventLoop.schedule(() -> { |
| | | log.info("连接天宇TCP服务器失败【{}:{}】,正尝试重连...", host, port); |
| | | try { |
| | | initOneConnection(host,port); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | }, 5, TimeUnit.SECONDS); |
| | | } |
| | | }); |
| | | |
| | | //判断是否有盒子被订阅 |
| | | channels.put(host+"/"+port, TyBoxSession.builder().subBoxSn(currentBoxSn).future(future) |
| | | channels.put(host + ":" + port, TyBoxSession.builder().subBoxSn(currentBoxSn).future(future) |
| | | .isReConnect(!currentBoxSn.equals("")).build()); |
| | | } |
| | | } |