| New file | 
 |  |  | 
 |  |  | package com.dy.rtuMw3rd.tcp4Bjnl; | 
 |  |  |  | 
 |  |  | import com.dy.common.mw.UnitAdapterInterface; | 
 |  |  | import com.dy.common.mw.UnitCallbackInterface; | 
 |  |  | import com.dy.common.mw.UnitInterface; | 
 |  |  | import com.dy.common.util.Callback; | 
 |  |  | import com.dy.rtuMw3rd.tcp4Bjnl.protocol.BjnlProtocol; | 
 |  |  | import org.apache.logging.log4j.LogManager; | 
 |  |  | import org.apache.logging.log4j.Logger; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @Author: liurunyu | 
 |  |  |  * @Date: 2025/03/18 14:20 | 
 |  |  |  * @Description | 
 |  |  |  */ | 
 |  |  | 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 ; | 
 |  |  |  | 
 |  |  |     private TcpClUnit(){} ; | 
 |  |  |  | 
 |  |  |     public static TcpClUnit getInstance(){ | 
 |  |  |         return instance ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void setAdapter(UnitAdapterInterface adapter) throws Exception { | 
 |  |  |         if(adapter == null){ | 
 |  |  |             throw new Exception("Tcp Client模块适配器对象不能为空!") ; | 
 |  |  |         } | 
 |  |  |         TcpClUnit.adapter = (TcpClUnitAdapter)adapter ; | 
 |  |  |         TcpClUnit.confVo = TcpClUnit.adapter.getConfig() ; | 
 |  |  |         if(TcpClUnit.confVo == null){ | 
 |  |  |             throw new Exception("Tcp Client模块配置对象不能为空!") ; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void start(UnitCallbackInterface callback) throws Exception { | 
 |  |  |         System.out.println("Tcp Client模块成功启动"); | 
 |  |  |         this.doStart(); | 
 |  |  |         callback.call(null) ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void stop(UnitCallbackInterface callback) throws Exception { | 
 |  |  |         callback.call(null); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void doStart(){ | 
 |  |  |         new Thread(() -> { | 
 |  |  |             Exception ex ; | 
 |  |  |             while(true){ | 
 |  |  |                 ex = null ; | 
 |  |  |                 try { | 
 |  |  |                     new TcpConnect().createSession( | 
 |  |  |                             confVo.BjnlServerIp, | 
 |  |  |                             confVo.BjnlServerPort, | 
 |  |  |                             confVo.BjnlConnectTimeout, | 
 |  |  |                             new TcpHandler(), | 
 |  |  |                             new Callback() { | 
 |  |  |                                 @Override | 
 |  |  |                                 public void call(Object obj) { | 
 |  |  |                                     if (obj == null) { | 
 |  |  |                                         log.error("北京农林--创建网络会话返回为null"); | 
 |  |  |                                     } else { | 
 |  |  |                                         log.info("北京农林--成功创建网络连接"); | 
 |  |  |                                         Worker w = Worker.getInstance() ; | 
 |  |  |                                         w.setApikey(confVo.BjnlApikey); | 
 |  |  |                                         w.setSecretkey(confVo.BjnlSecretkey ); | 
 |  |  |                                         w.setWorkInterval(confVo.heartBeatInterval + 0L); | 
 |  |  |                                         w.start(); | 
 |  |  |                                     } | 
 |  |  |                                 } | 
 |  |  |                                 @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(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |