From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 07 四月 2025 15:18:51 +0800 Subject: [PATCH] 整理版本 --- pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu3rd/src/main/java/com/dy/rtuMw3rd/tcp4Bjnl/Worker.java | 127 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 127 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu3rd/src/main/java/com/dy/rtuMw3rd/tcp4Bjnl/Worker.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu3rd/src/main/java/com/dy/rtuMw3rd/tcp4Bjnl/Worker.java new file mode 100644 index 0000000..9c71c77 --- /dev/null +++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu3rd/src/main/java/com/dy/rtuMw3rd/tcp4Bjnl/Worker.java @@ -0,0 +1,127 @@ +package com.dy.rtuMw3rd.tcp4Bjnl; + +import com.alibaba.fastjson2.JSON; +import com.dy.rtuMw3rd.tcp4Bjnl.protocol.*; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.Timer; +import java.util.TimerTask; + +/** + * @Author: liurunyu + * @Date: 2025/2/26 14:39 + * @Description + */ +public class Worker extends TimerTask { + + private static final Logger log = LogManager.getLogger(Worker.class) ; + + private static Worker instance = new Worker() ; + + private String apikey ; + private String secretkey ; + private Long workInterval ;//寰幆浠诲姟闂撮殧 + + private Timer timer; + + private boolean stop; + + private boolean first = true ; + private static boolean isLogined = false ; + + private Worker(){ + this.timer = new Timer(); + this.stop = false ; + } + + public static Worker getInstance(){ + return instance ; + } + + + public void setApikey(String apikey){ + this.apikey = apikey ; + } + public void setSecretkey(String secretkey){ + this.secretkey = secretkey ; + } + public void setWorkInterval(Long workInterval){ + this.workInterval = workInterval ; + } + + public void setLogined(){ + log.info("鍖椾含鍐滄灄--璇锋眰鐧诲綍鎴愬姛"); + isLogined = true ; + } + + public void stop(){ + this.stop = true ; + if(this.timer != null){ + this.timer.cancel(); + } + } + public boolean isStop(){ + return this.stop ; + } + + public void start(){ + new Thread(() -> { + while(true){ + try { + login() ; + }catch (Exception e){ + log.error("鍖椾含鍐滄灄--璇锋眰鐧诲綍寮傚父" + (e.getMessage() == null ? "" : (":" + e.getMessage())), e); + } + try{ + Thread.sleep(10000); + }catch (Exception e){ + } + if(isLogined){ + timer.schedule(this, 0, this.workInterval); + break ; + } + } + }).start(); + } + + @Override + public void run() { + if(first){ + first = false ; + }else{ + if(TcpConnect.isConnected() && isLogined) { + try{ + heartbeat() ; + }catch(Exception e){ + log.error("鍖椾含鍐滄灄--鍙戦�佸績璺冲紓甯�" + (e.getMessage() == null ? "" : (":" + e.getMessage())), e); + } + } + } + } + + private void login(){ + ParamLogin pl = new ParamLogin() ; + pl.protocol = new ParamProtocol() ; + pl.auth = new ParamAuth() ; + pl.protocol.version = BjnlProtocol.ProtocolVersion ; + pl.protocol.protocolcode = BjnlProtocol.Protocolcode ; + pl.auth.apikey = apikey ; + pl.auth.secretkey = secretkey ; + + String json = JSON.toJSONString(pl) ; + byte[] bs = BjnlCommon.wrap(json) ; + try { + TcpConnect.output(bs); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + private void heartbeat() throws Exception{ + try { + TcpConnect.output(BjnlProtocol.HeartBeat); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } +} -- Gitblit v1.8.0