1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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);
        }
    }
}