From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 07 四月 2025 15:18:51 +0800 Subject: [PATCH] 整理版本 --- pipIrr-platform/pipIrr-web/pipIrr-mwTest-rtu/src/main/java/com/dy/pipIrrMwTestRtu/tcpClient/TcpClUnit.java | 130 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 130 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-mwTest-rtu/src/main/java/com/dy/pipIrrMwTestRtu/tcpClient/TcpClUnit.java b/pipIrr-platform/pipIrr-web/pipIrr-mwTest-rtu/src/main/java/com/dy/pipIrrMwTestRtu/tcpClient/TcpClUnit.java new file mode 100644 index 0000000..e711393 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-mwTest-rtu/src/main/java/com/dy/pipIrrMwTestRtu/tcpClient/TcpClUnit.java @@ -0,0 +1,130 @@ +package com.dy.pipIrrMwTestRtu.tcpClient; + +import com.dy.common.mw.UnitAdapterInterface; +import com.dy.common.mw.UnitInterface; +import com.dy.common.mw.UnitCallbackInterface; +import com.dy.common.util.Callback; +import com.dy.pipIrrMwTestRtu.ServerProperties; +import com.dy.pipIrrMwTestRtu.tcpClient.upData.UpData; +import com.dy.pipIrrMwTestRtu.tcpClient.upData.UpHeartBeat; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.mina.core.session.IoSession; + + +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 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.mwServerIp, + confVo.mwServerPort, + confVo.connectTimeout, + new TcpHandler(), + new Callback() { + @Override + public void call(Object obj) { + if (obj == null) { + log.error("鍒涘缓缃戠粶浼氳瘽杩斿洖涓簄ull"); + } 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() { + if(ServerProperties.onlyOneHeartBeat){ + try { + UpHeartBeat.upCd02Data(ServerProperties.rtuAddr); + Thread.sleep(30000L); + //Thread.sleep(30000000L); + }catch (Exception e){ + } + }else{ + while(true){ + try { + UpHeartBeat.upCd02Data(ServerProperties.rtuAddr); + Thread.sleep(30000L); + //Thread.sleep(30000000L); + }catch (Exception e){ + continue; + } + } + } + } + }).start(); + } + +} -- Gitblit v1.8.0