zhubaomin
2025-04-07 1a2b07f01ba4616fd9e894dddf474b56d020158c
pipIrr-platform/pipIrr-mw/pipIrr-mw-simulate-rtu/src/main/java/com/dy/simRtu/tcpClient/TcpClUnit.java
@@ -14,14 +14,12 @@
public class TcpClUnit implements UnitInterface {
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
    private static TcpClUnit instance = new TcpClUnit() ;
    public static TcpClUnitAdapter adapter ;
    public static TcpClUnitConfigVo confVo ;
    public static IoSession session ;
    private static Worker worker ;
    private TcpClUnit(){} ;
@@ -39,12 +37,13 @@
        if(TcpClUnit.confVo == null){
            throw new Exception("Tcp Client模块配置对象不能为空!") ;
        }
        TcpClUnit.worker = Worker.getInstance(TcpClUnit.confVo) ;
    }
    @Override
    public void start(UnitCallbackInterface callback) throws Exception {
        System.out.println("Tcp Client模块成功启动");
        this.doStart();
        TcpClUnit.worker.doStart();
        callback.call(null) ;
    }
@@ -53,67 +52,5 @@
        callback.call(null);
    }
    private void doStart(){
        new Thread(() -> {
            Exception ex ;
            while(true){
                ex = null ;
                try {
                    new TcpConnect().createSession(
                            confVo.mwServerIp,
                            confVo.mwServerPort,
                            confVo.connectTimeout,
                            new TcpHandler(),
                            new Callback() {
                                @Override
                                public void call(Object obj) {
                                    if (obj == null) {
                                        log.error("创建网络会话返回为null");
                                    } else {
                                        TcpClUnit.session = (IoSession) obj;
                                        log.info("成功创建与通信中间件的网络连接");
                                        UpData.setSession(TcpClUnit.session);
                                        heartBeat() ;
                                    }
                                }
                                @Override
                                public void call(Object... objs) {
                                }
                                @Override
                                public void exception(Exception e) {
                                }
                            });
                }catch (Exception e){
                   ex = e ;
                }
                if(ex == null){
                    break ;
                }else{
                    try{
                        Thread.sleep(100);
                    }catch (Exception e){
                    }
                }
            }
        }).start();
    }
    private void heartBeat(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
                        Thread.sleep(30000L);
                        //Thread.sleep(30000000L);
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }).start();
    }
}