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
package com.dy.simRtu.tcpClient.upData;
 
import com.dy.common.mw.protocol.p206V1.parse.global.GlCreate;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.DateTime;
import com.dy.simRtu.ServerProperties;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
/**
 * @Author: liurunyu
 * @Date: 2024/7/15 14:18
 * @Description
 */
public class UpHeartBeat extends UpData {
 
    private static final Logger log = LogManager.getLogger(UpHeartBeat.class) ;
 
    private static boolean isFirst = true ;
 
    public static boolean upHeartBeat = true ;//是否可以发送心跳
 
    public static void upCd02Data(String rtuAddr){
        if(upHeartBeat){
            try{
                if(UpData.session != null && UpData.session.isConnected()){
                    byte[] bs ;
                    if(ServerProperties.argHeardBeatBy02True81False){
                        bs = createCd02Data(rtuAddr) ;
                    }else{
                        bs = createCd81Data(rtuAddr) ;
                    }
                    UpData.upSend(bs) ;
                }else{
                    log.error("未连接通信中间件,不能发送数据");
                }
            }catch (Exception e){
                log.error("向通信中间件发送数据产生异常", e);
            }
        }
    }
 
 
    /**
     * 构造心跳数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createCd02Data(String rtuAddr) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "02", (byte)0xB0);
 
        byte[] bs = new byte[1] ;
        if(isFirst){
            isFirst = false ;
            bs[0] = (byte)0xF0 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。
        }else{
            bs[0] = (byte)0xF2 ;//数据域: 1 个字节,F0 登录, F1 退出登录,F2 在线保持。
        }
 
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        GlCreate.createLen(bytes);//长度放字节数组中
 
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
 
        return bytes ;
    }
 
 
 
    /**
     * 构造心跳数据
     * @return 字节数组
     * @throws Exception 异常
     */
    private static byte[] createCd81Data(String rtuAddr) throws Exception {
        byte[] bytes = creatHead(rtuAddr, "81", (byte)0xB0);
 
        int index = 0 ;
        byte[] bs1 = new byte[4] ;
        bs1[index++] = (byte)0x22 ;
        bs1[index++] = (byte)0x6 ;
        bs1[index++] = (byte)0x44 ;
        bs1[index++] = (byte)0x00 ;
        bytes = ByteUtil.bytesMerge(bytes, bs1) ;
 
        int[] ymdhms = DateTime.yyyy_MM_dd_HH_MM_SS_2_ymdhmsGroup(DateTime.yyyy_MM_dd_HH_mm_ss()) ;
        byte[] bs2 = new byte[6] ;
        index = 0 ;
        int index1 = 5 ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--])[0] ;
        bs2[index++] = ByteUtil.int2BCD_BE(ymdhms[index1--] - 2000)[0] ;
        bytes = ByteUtil.bytesMerge(bytes, bs2) ;
 
        GlCreate.createLen(bytes);//长度放字节数组中
 
        byte[] bsTail = GlCreate.createCrcTail4P206(bytes) ;//CRC和尾叠加字节数组中
 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ;
 
        return bytes ;
    }
}