wuzeyu
2023-12-02 80495f181ecb3bd7e5658513671609efe854c818
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
135
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.mina.core.future.CloseFuture;
import org.apache.mina.core.session.IoSession;
 
public class MyThreadJob  implements ThreadPool.Job {
 
    public String rtuAddr;
    public String serverIp;
    public Integer serverPort;
 
    public IoSession session ;
 
    public static final int connectTimeout = 3000 ;
 
    public int sendTimes = 0 ;//发送数据次数
    public int heartbeatTimes = 0 ;//上报心跳次数
 
    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(session == null){
            IoSession se = new TcpConnect().createSession(this.rtuAddr, this, this.serverIp, this.serverPort, connectTimeout, new TcpHandler()) ;
            if(se != null){
                this.session = se ;
            }
        }else{
            if(sendTimes <= ServerProperties.sendTimes){
                sendDataOfP206V1_0_0() ;
            }else{
                this.jobOver() ;
            }
        }
    }
 
    private void sendDataOfP206V1_0_0(){
        try{
            if(heartbeatTimes >= ServerProperties.heartbeatTimes){
                heartbeatTimes = 0 ;
                this.sendReportData() ;
                sendTimes++ ;
            }else{
                this.sendHeartbeat() ;
                heartbeatTimes++ ;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    private void sendHeartbeat(){
        try{
            byte[] bs = this.createHeartbeat() ;
            this.session.write(bs) ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
    private void sendReportData(){
        TcpClUnit.clientSendData();
    }
 
    private void jobOver(){
        CloseFuture closeFuture = session.closeOnFlush() ;
        this.isOver = true ;
        TcpClUnit.clientOver() ;
    }
 
    /**
     * 构造下行数据
     * @return 字节数组
     * @throws Exception 异常
     */
    public byte[] createHeartbeat( ) throws Exception {
        CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ;
        byte[] bytes ;
        byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = 0 ;//帧长度
 
        index++ ;
        bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = commonV1_0_1.createCtrl((byte)0) ;
 
        index++ ;
        GlCreate.createRtuAddr(this.rtuAddr, bsHead, index);
        index += 5 ;
 
        ByteUtil.hex2Bytes("02", bsHead, index) ;
 
        byte[] bs = new byte[1] ;
        bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。
 
        bytes = ByteUtil.bytesMerge(bsHead, bs) ;
 
        GlCreate.createLen(bytes);//长度放字节数组中
 
        bytes = GlCreate.createCrcTail(bytes) ;//CRC和尾叠加字节数组中
 
        return bytes ;
    }
 
 
 
    @Override
    public void destroy() {
    }
 
    @Override
    public boolean isDestroy() {
        return false;
    }
 
 
}