jiazx0107@163.com
2023-12-24 8efb5a8cd2c8a6b40e58f4f3fb851d54cf415af9
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
package com.fzzy.protocol.youxian0.analysis;
 
import com.fzzy.api.Constant;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.youxian0.ServiceUtils;
import org.springframework.stereotype.Component;
 
import java.net.InetAddress;
 
/**
 * 游仙主库,返回报文解析
 */
@Component(AnalysisService.BEAN_ID)
public class AnalysisService {
 
    public static final String BEAN_ID = "youxian0.analysisService";
 
 
    /**
     * 协议解析
     * <p>
     * 7E 00 02 02 00 77 00 00 A0 FF FF 66 FF 04 BE 01 64 6A 6B 6A 68 CE 02 A4 BB BA BA B4 CE 03 E4 32 3D 32 3D CE 04 44 BC B3 BC B3 20 47 7E
     *
     * @param address
     * @param port
     * @param strMsg
     */
    public void analysis(InetAddress address, int port, String strMsg) {
 
        //分机ID
        int start = 2 * 2;
        String deviceSn = strMsg.substring(start, start + 2);
        deviceSn = BytesUtil.hexToInt(deviceSn) + "";
 
 
        //命令ID
        start = 5 * 2;
        String msgIdHex = strMsg.substring(start, start + 2);
        int msgId = BytesUtil.hexToInt(msgIdHex);
 
        //命令类型
        start = 11 * 2;
        String funId = strMsg.substring(start, start + 2);
 
 
        GatewayDevice device = GatewayUtils.getCacheByDeviceSn(deviceSn);
 
        //粮情返回
        if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) {
            this.analysisGrain(device, msgId, strMsg);
        }
 
    }
 
    /**
     * 粮情解析
     * <p>
     * 7E 00 02 02 00 77 00 00 A0 FF FF 66 FF 04 BE 01 64 6A 6B 6A 68 CE
     * 02 A4 BB BA BA B4 CE 03 E4 32 3D 32 3D CE 04 44 BC B3 BC B3 20 47 7E
     *
     * @param device
     * @param msgId  命令ID
     */
    private void analysisGrain(GatewayDevice device, int msgId, String strMsg) {
 
        //只保留粮情信息
        int start = 22 * 2;
        strMsg = strMsg.substring(start);
 
 
        //密钥和点数
        String kyeNumHex = strMsg.substring(2, 4);
        String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex));
 
        String key = kyeNumBin.substring(0, 3);
        int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
        key = kyeNumBin.substring(4);
        int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key));
 
 
        //02 A4 BB BA BA B4
        start = 2 * 2;
        String tempHex;
        for (int i = 0; i < numValue; i++) {
            start = start + i * 2;
            tempHex = strMsg.substring(start, start + 2);
            //实际温度=密钥*密钥*37(溢出为无符号字节)再异或加密后的温度/2。
 
        }
 
 
    }
}