vince
2023-07-01 7d84b6cbf8268a6993f661e22a842ffa12c13732
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.ld.igds.protocol.bhzn.verb.server;
 
 
import com.ld.igds.util.BytesUtil;
import com.ld.igds.util.ContextUtil;
import com.ld.io.api.IoSession;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 工具类
 *
 * @author vince
 */
public class BhznVerbServerUtils {
 
 
    /**
     * 设备状态返回的结果 KEY= serId + "_STATUS_" + deviceId value = 结果状态
     */
    private static Map<String, String> contextStatusMap = new HashMap<>();
 
 
    /**
     * 设备解析后的装法存放
     *
     * @param serId
     * @param passcode 设备通道ID
     * @param status
     */
    public static void add2StatusMap(String companyId, String serId, String passcode, String status) {
        contextStatusMap.put(ContextUtil.buildDeviceStatusKey(companyId, serId, passcode), status);
    }
    public static void add2StatusMap(String companyId, String serId, int passcode, String status) {
        contextStatusMap.put(ContextUtil.buildDeviceStatusKey(companyId, serId, passcode), status);
    }
    public static Map<String, String> getStatusMap() {
        return contextStatusMap;
    }
    /**
     * 记录每个连接的最后一次信息时间 key = 连接的KYE,data =当前时间戳
     */
    public static Map<String, Long> contextMapHeart = new HashMap<>();
 
    public static Map<String, IoSession> contextIoSession = new HashMap<>();
 
 
    public static int HEART_BEAT_TIME = 30;//心跳间隔时间
 
 
    public static String MSG_START = "3C42485A4E3E";//<BHZN>
    public static String MSG_START1 = "<BHZN>";//<BHZN>
    public static String MSG_START2 = "AA";
    public static String MSG_END = "<END>";//<END>
    public static String MSG_END_16 = "3C454E443E";//<END>
    public static final String CHARSET = "UTF-8";
 
 
    /**
     * 针对无线粮情主机的默认ID配置
     */
    public static String DEFAULT_MAC_ID = "53681";
 
    public static String FUNCTION_ID_00 = "00";
    public static String FUNCTION_ID_F1 = "F1";
    public static String FUNCTION_ID_F2 = "F2";
    public static String FUNCTION_ID_83 = "83";
    public static String FUNCTION_ID_93 = "93";
    public static String FUNCTION_ID_92 = "92";
 
 
    /**
     * 生成TCP连接的KEY
     *
     * @param ip
     * @param port
     * @return
     */
    public static String getServerKey(String ip, Integer port) {
        return ip + ":" + port;
    }
 
 
    /**
     * 添加最新心跳时间戳
     *
     * @param session
     */
    public static void addHeartBeat(IoSession session) {
        contextMapHeart.put(getServerKey(session.getAddress(), session.getPort()), System.currentTimeMillis());
    }
 
    public static Long getHearBeat(IoSession session) {
        return contextMapHeart.get(getServerKey(session.getAddress(), session.getPort()));
    }
 
 
    /**
     * 计算校验
     *
     * @param content
     * @return
     */
    public static String getCheck(String content) {
        int start = BhznVerbServerUtils.MSG_START.length() + BhznVerbServerUtils.MSG_START2.length();
        content = content.substring(start);
        int sum = 0;
        String hex;
        for (int i = 0; i < content.length() / 2; i++) {
            hex = content.substring(i * 2, i * 2 + 2);
            sum += BytesUtil.hexToInt(hex);
        }
        String hexSum = BytesUtil.intToHexStr(sum);
        int check = BytesUtil.hexToInt(hexSum.substring(hexSum.length() - 2));
 
        return BytesUtil.intToHexStr(256 - check).substring(2);
    }
 
//    public static void addSession(IoSession session) {
//        contextIoSession.put(DEFAULT_MAC_ID, session);
//    }
//
//    public static IoSession getSession() {
//        return contextIoSession.get(DEFAULT_MAC_ID);
//    }
}