jiazx0107@163.com
2023-12-24 66b091963fb0f3356f27ec094c013369bf91db89
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
package com.fzzy.protocol.weightyh;
 
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayRemoteManager;
import com.fzzy.gateway.data.BaseReqData;
import com.fzzy.gateway.entity.GatewayDevice;
import com.ld.io.api.IoMsgConsumer;
import com.ld.io.api.IoSession;
import lombok.extern.slf4j.Slf4j;
 
/**
 * 地磅信息推送
 *
 * @author: andy.jia
 * @description:
 * @version:
 * @data:2020年3月19日
 */
@Slf4j
public class MessageConsumer implements IoMsgConsumer {
 
    private long timeTag = 0;
 
    private GatewayRemoteManager gatewayRemoteManager;
 
    @Override
    public void consume(IoSession session, byte[] bytes) {
        analysisInfo(session.getAddress(), session.getPort(), bytes);
    }
 
    /**
     * 02 2B 30 30 31 31 38 30 30 31 33 03 02开始 03 结束
     */
    public void analysisInfo(String ip, Integer port, byte[] bytes) {
        String strMsg = BytesUtil.bytesToString(bytes);
 
        if (strMsg.length() < 22) {
            return;
        }
 
        // 每间隔2.5执行一次
        if (System.currentTimeMillis() - timeTag <= 2500) {
            return;
        }
        timeTag = System.currentTimeMillis();
 
        String temp = strMsg.substring(2, 4);
        //String symbol = HexASCIIToConvert(temp);
        temp = strMsg.substring(4, 16);
        String w = HexASCIIToConvert(temp);
        temp = strMsg.substring(16, 18);
        int d = getProduct(Integer.parseInt(HexASCIIToConvert(temp)));
 
        //最终重量结果
        double weigh = Double.valueOf(w) / d;
 
        log.debug("WEIGHT-MESSAGE=" + strMsg);
        log.debug("----------地磅称重数值解析------{}", weigh);
 
        //当前地磅协议不支持传递SN,使用局域网IP作为SN
        String sn = ip;
 
        //根据信息获取设备
        GatewayDevice device = GatewayUtils.getCacheByDeviceSn(sn);
 
        if (null == device) {
            log.error("----------没有获取到设备信息--------{}", sn);
            return;
        }
 
        //直接调用实现类,更多实现类单独调用
        if (null == gatewayRemoteManager) {
            gatewayRemoteManager = SpringUtil.getBean(GatewayRemoteManager.class);
        }
 
        //数据封装推送
        BaseReqData reqData = new BaseReqData();
        reqData.setDevice(device);
        reqData.setDeviceId(device.getDeviceId());
        reqData.setProductId(device.getProductId());
        reqData.setDeviceName(device.getDeviceName());
        reqData.setWeight(weigh);
 
        if (weigh > 0) {
            gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData);
        }
    }
 
    /**
     * 根据小数点位数计算原数字需要除以多少
     *
     * @param i
     */
    private int getProduct(int i) {
        if (i == 0)
            return 1;
        int num = 1;
        for (int a = 0; a < i; a++) {
            num = num * 10;
        }
        return num;
    }
 
    private String HexASCIIToConvert(String value) {
        // String value = "30 30 31 31 38 30 30";
        StringBuffer sbu = new StringBuffer();
        char t = 0;
        int size = value.length() / 2;
        for (int i = 0; i < size; i++) {
            String tmp = value.substring(i * 2, (i + 1) * 2);
            t = (char) Integer.parseInt(""
                    + BytesUtil.bytesToInt(BytesUtil.hexStrToBytes(tmp)));
            sbu.append(t);
        }
 
        return sbu.toString();
    }
 
 
}