zhubaomin
2025-04-07 1a2b07f01ba4616fd9e894dddf474b56d020158c
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
package com.dy.simRtu202404.tcpClient;
 
import com.dy.simRtu202404.ServerProperties;
import com.dy.simRtu202404.tcpClient.upData.UpData;
import com.dy.simRtu202404.tcpClient.upData.UpHeartBeat;
import com.dy.simRtu202404.tcpClient.upData.UpOpenCloseValve;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.core.session.IoSession;
 
/**
 * @Author: liurunyu
 * @Date: 2025/2/26 14:39
 * @Description
 */
public class Worker {
 
    private static final Logger log = LogManager.getLogger(TcpClUnit.class) ;
 
    protected static IoSession session ;
 
    private static Worker instance = new Worker() ;
 
    private static Integer UpHeartBeatTimes = 0 ;
    private static boolean isOpenValve = false ;
    private static boolean isCloseValve = false ;
    private static Integer OpenedCount = 0 ;
    private static Integer ClosedCount = 0 ;
 
    private Worker(){
    }
 
    public static Worker getInstance(IoSession session){
        Worker.session = session ;
        return instance ;
    }
 
    public void doWork(){
        UpData.setSession(Starter.session);
        new Thread(new Runnable(){
            @Override
            public void run() {
                while(true){
                    try {
                        reportData() ;
                        Thread.sleep(1000L);
                    }catch (Exception e){
                        continue;
                    }
                }
            }
        }).start();
    }
    private void reportData() throws Exception{
        UpHeartBeat.upCd02Data(ServerProperties.rtuAddr);
        UpHeartBeatTimes += 1 ;
 
        Thread.sleep(10000L);
 
        if(UpHeartBeatTimes == 2){
            UpHeartBeatTimes = 0 ;
            if(ServerProperties.type == 1){
                if(!isOpenValve){
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                }
            }
 
            if(ServerProperties.type == 2){
                if(!isCloseValve){
                    UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                    isCloseValve = true ;
                }
            }
 
            if(ServerProperties.type == 3){
                if(!isOpenValve){
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                }
                OpenedCount += 1 ;
                if(OpenedCount == 10){
                    OpenedCount = 0 ;
                    if(!isCloseValve){
                        UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                        isCloseValve = true ;
                    }
                }
            }
 
            if(ServerProperties.type == 4){
                if(!isOpenValve && !isCloseValve){
                    //初次
                    UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                    isOpenValve = true ;
                    isCloseValve = false ;
                }else{
                    if(isOpenValve){
                        OpenedCount += 1 ;
                    }
                    if(OpenedCount == 10){
                        OpenedCount = 0 ;
                        UpOpenCloseValve.upCd85Data(ServerProperties.rtuAddr);
                        isOpenValve = false ;
                        isCloseValve = true ;
                    }
 
                    if(isCloseValve){
                        ClosedCount += 1 ;
                    }
                    if(ClosedCount == 10){
                        ClosedCount = 0 ;
                        UpOpenCloseValve.upCd84Data(ServerProperties.rtuAddr);
                        isOpenValve = true ;
                        isCloseValve = false ;
                    }
                }
            }
        }
    }
}