New file |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |