vince
2024-05-20 75bdadc0c4e468217b93142d965cd92ee52838ec
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
package com.fzzy.protocol.zldz.analysis;
 
 
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.gateway.entity.GatewayDevice;
import com.fzzy.protocol.ProtocolUtils;
import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.zldz.data.ReMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
/**
 * 解析温湿度信息
 *
 * @author Andy
 */
@Slf4j
@Component(AnalysisTH.BEAN_ID)
public class AnalysisTH {
 
    public static final String BEAN_ID = "zldz.analysisTH";
 
 
    public static int ERROR_TH_TAG = -50;
 
    /**
     * 解析仓库温湿度信息,将信息放在缓存中,以便粮情使用
     *
     * @param msg
     * @param device
     */
    public void analysis8828(ReMessage msg, GatewayDevice device) {
 
        log.debug("-----------analysis8828------------{}.{}", device.getIp(), device.getPort());
 
        String content = msg.getBody().getContent();
 
        //温湿度地址,截取4位
        String temp = content.substring(0, 0 + 2 * 2);
        //高低位转换
        temp = BytesUtil.tran_LH(temp);
        int thAddr = BytesUtil.hexToInt(temp);
 
        int t, h;
 
        THDto th = new THDto();
        th.setTempIn(ProtocolUtils.ERROR_TEMP);
        th.setHumidityIn(ProtocolUtils.ERROR_TEMP);
 
        //温度
        int start = 2, len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        t = BytesUtil.hexToInt(temp);
        if (t > 127) {//说明是负数
            t = BytesUtil.hexToInt("FF" + temp);
        }
        if (t == ERROR_TH_TAG) {
            log.error("{}温湿度解析异常,原因:没有检测到传感器", device.getDeviceName());
            th.setRemark(device.getDeviceName() + "温湿度解析异常,原因:没有检测到传感器");
        } else {
            th.setTempIn(Double.valueOf(t));
        }
 
        //湿度
        start = 3;
        len = 1;
        temp = content.substring(start * 2, start * 2 + len * 2);
        h = BytesUtil.hexToInt(temp);
        if (h > 127) {//说明是负数
            h = BytesUtil.hexToInt("FF" + temp);
        }
        if (h == ERROR_TH_TAG) {
            log.error("{}温湿度解析异常,原因:没有检测到传感器", device.getDeviceName());
            th.setRemark(device.getDeviceName() + "温湿度解析异常,原因:没有检测到传感器");
        } else {
            th.setHumidityIn(Double.valueOf(h));
        }
 
        th.setSerId(device.getDeviceId());
        th.setThAddr(thAddr + "");
 
        //存放缓存
        ProtocolUtils.addTh2Map(device.getDeviceId(), th);
 
        log.info("仓温仓湿解析完成={}", th);
    }
}