liurunyu
2024-07-11 b6833ab41416dd68a30f7cd368a802aa3a0a2e21
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
128
129
130
131
132
133
134
package com.dy.testClient.tcpClient;
 
import com.dy.common.mw.protocol.p206V1_0_0.CommonV1_0_1;
import com.dy.common.mw.protocol.p206V1_0_0.ProtocolConstantV206V1_0_0;
import com.dy.common.mw.protocol.p206V1_0_0.parse.global.GlCreate;
import com.dy.common.threadPool.ThreadPool;
import com.dy.common.util.ByteUtil;
import com.dy.testClient.ServerProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.mina.core.session.IoSession;
 
public class MyThreadJob  implements ThreadPool.Job {
 
    private static final Logger log = LogManager.getLogger(MyThreadJob.class) ;
 
    public static String icCardAddr = "3B7D1E1A" ;
    public static String icCardNo = "61181622830147821" ;
    public static String openValveDt = "240711163700" ;
    public static String cdC0 = "C0" ;
    public static String cd83Open = "83Open" ;
    public static String cd83Close = "83Close" ;
 
 
    public static String curCd = cdC0 ;//通过修改这个值,来改变上报数据类型
 
    public String rtuAddr;
    public String serverIp;
    public Integer serverPort;
 
    public IoSession session ;
    public boolean exceptionOnConnect = false ;
 
    public static final int connectTimeout = 3000 ;
 
    public int sendTimes = 1 ;//发送数据次数
    public int heartbeatTimes = 1 ;//上报心跳次数
 
    public long overStart = 0L;
    public boolean isOver = false ;
 
    public MyThreadJob(){
    }
    public MyThreadJob(String rtuAddr, String serverIp, Integer serverPort){
        this.rtuAddr = rtuAddr ;
        this.serverIp = serverIp ;
        this.serverPort = serverPort ;
    }
 
    @Override
    public void execute() throws Exception {
        if(!this.isOver){
            if(session != null){
                if(sendTimes <= ServerProperties.sendTimes){
                    log.info("RTU" + rtuAddr + "开始任务");
                    log.info("RTU" + rtuAddr + "将要执行" + ServerProperties.sendTimes + "轮次任务,当前轮次是" + sendTimes);
                    sendDataOfP206V1_0_0(curCd) ;
                }else{
                    if(ServerProperties.sendOverThenCloseConnect == 1){
                        log.info("RTU" + rtuAddr + "等待一会,以接收通信中间件下行数据");
                        if(overStart == 0){
                            overStart = System.currentTimeMillis() ;
                        }else{
                            long now = System.currentTimeMillis() ;
                            if(now - overStart >= 30 * 1000){
                                this.jobOver() ;
                            }
                        }
                    }else{
                        this.jobOver() ;
                    }
                }
            }
        }
    }
 
    private void sendDataOfP206V1_0_0(String cd){
        try{
            if(heartbeatTimes > ServerProperties.heartbeatTimes){
                heartbeatTimes = 1 ;
                byte[] bs = null ;
                if(cd.equals(cdC0)){
                    bs = RtuUpData.createCdC0(this.rtuAddr) ;
                }else if(cd.equals(cd83Open)){
                    bs = RtuUpData.createCd83Open(this.rtuAddr, icCardAddr, icCardNo, openValveDt) ;
                }else if(cd.equals(cd83Close)){
                    bs = RtuUpData.createCd83Close(this.rtuAddr, icCardAddr, icCardNo, openValveDt) ;
                }
                this.session.write(bs) ;
                log.info("RTU" + rtuAddr + "发送了上行数据:" + ByteUtil.bytes2Hex(bs, false));
                TcpClUnit.clientSendData();
                sendTimes++ ;
            }else{
                this.sendHeartbeat() ;
                TcpClUnit.clientSendData();
                heartbeatTimes++ ;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void sendHeartbeat(){
        try{
            byte[] bs = RtuUpData.createHeartbeat(this.rtuAddr) ;
            this.session.write(bs) ;
            log.info("RTU" + rtuAddr + "发送了心跳数据:" + ByteUtil.bytes2Hex(bs, false));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
 
    private void jobOver(){
        if(ServerProperties.sendOverThenCloseConnect == 1){
            session.closeOnFlush() ;
        }
        this.isOver = true ;
        TcpClUnit.clientOver() ;
    }
 
 
 
 
    @Override
    public void destroy() {
    }
 
    @Override
    public boolean isDestroy() {
        return false;
    }
 
 
}