2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-08-01 d5dab0a85de9573ff61e20794af04129dd4b2f87
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.sandu.ximon.admin.manager.iot.frame.inner.report;
 
import cn.hutool.core.util.StrUtil;
import com.sandu.ximon.admin.manager.iot.frame.inner.BaseResponseInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.IResponseInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.util.CRC32Utils;
import lombok.Data;
import lombok.ToString;
 
/**
 * @author chenjiantian
 * @date 2021/12/6 18:20
 * A5-84-01
 * 大气心跳包 应答
 */
@Data
@ToString(callSuper = true)
public class A5AtmosphereHeartbeatReportInnerFrame extends BaseResponseInnerFrame<A5AtmosphereHeartbeatReportInnerFrame> {
 
    //  目标地址    2
    private String destinationAddress;
    //  心跳包数据   58
    private HeartBeatDataPackage heartBeatDataPackage;
 
    private String originFrame;
 
    @Override
    public A5AtmosphereHeartbeatReportInnerFrame transformFrame(String hex) {
        //  长度不一致时,返回null
        if (StrUtil.isBlank(hex)) {
            return null;
        }
        // MQTT通信方式(1)
        setConnectType(hex.substring(0, 2));
        //  功能码(1)
        setFunctionCode(hex.substring(2, 4));
        //  负荷长度(2)
        setPayloadLength(hex.substring(4, 8));
 
        setDestinationAddress(hex.substring(8, 12));
 
        String heartBeatData = hex.substring(12, 76);
        HeartBeatDataPackage heartBeatDataPackage = new HeartBeatDataPackage();
        heartBeatDataPackage.transformFrame(heartBeatData);
        setHeartBeatDataPackage(heartBeatDataPackage);
 
        setCrc32(hex.substring(hex.length() - 8));
        //  校验CRC32
        String frame = hex.substring(2, hex.length() - 8);
        this.setValidate(CRC32Utils.validateFrame(frame, getCrc32()));
        return this;
    }
 
    @Data
    public static class HeartBeatDataPackage implements IResponseInnerFrame<HeartBeatDataPackage> {
        //  设备型号    2
        private String deviceType;
        //  模块预热状态标志    1
        private String moduleWarmUpStatusFlag;
        //  温度值
        private Double temperature;
        //  湿度值
        private Double humidity;
        //  风速
        private Double windSpeed;
        //  风向
        private Double windDirection;
        //  大气压
        private Integer pressure;
        //  光强
        private Integer lightIntensity;
        //  噪音
        private Double noise;
        //  PM2.5
        private Integer pm25;
        //  PM10
        private Integer pm10;
        //  雨量
        private Double rainfall;
        //  SO2二氧化硫相对值
        private Integer so2;
        //  甲醛相对值
        private Integer ech2o;
        //  NO2二氧化氮
        private Integer no2;
        //  TVOC
        private Integer tvoc;
        //  CO一氧化碳
        private Integer co;
        //  二氧化碳
        private Integer co2;
        //  O3臭氧
        private Integer o3;
        //  F氟化物
        private Integer fluoride;
 
        @Override
        public HeartBeatDataPackage transformFrame(String hex) {
            if (hex.length() != 64) {
                return null;
            }
            Integer type = 0;
            this.deviceType = hex.substring(0, 4);
            if (this.deviceType.equals(DeviceType.SENSOR_V1.getType())) {
                type = 1;
            }
            this.moduleWarmUpStatusFlag = hex.substring(4, 6);
            this.temperature = Double.parseDouble(
                    parseVal(hex, 6, 8)
                            + "."
                            + parseVal(hex, 8, 10)
            );
            this.humidity = Double.parseDouble(
                    parseVal(hex, 10, 12)
                            + "."
                            + parseVal(hex, 12, 14)
            );
            this.windSpeed = parseVal(hex, 14, 18) * .1;
            this.windDirection = parseVal(hex, 18, 22) * .1;
            this.pressure = parseVal(hex, 22, 26);
            this.lightIntensity = parseVal(hex, 26, 32);
            this.noise = parseVal(hex, 32, 36) * .1;
            this.pm25 = parseVal(hex, 36, 40);
            this.pm10 = parseVal(hex, 40, 44);
            this.rainfall = parseVal(hex, 44, 48) * .1;
 
            /**
             * 以下区分版本
             */
            if (type.equals(1)) {
                this.ech2o = parseVal(hex, 48, 52);
                this.tvoc = parseVal(hex, 52, 56);
                this.co2 = parseVal(hex, 56, 60);
            } else {
                this.so2 = parseVal(hex, 48, 52);
                this.ech2o = parseVal(hex, 48, 52);
                this.tvoc = parseVal(hex, 52, 56);
                this.no2 = parseVal(hex, 52, 56);
                this.co = parseVal(hex, 56, 60);
                this.co2 = parseVal(hex, 56, 60);
            }
 
            this.o3 = parseVal(hex, 60, 62);
            this.fluoride = parseVal(hex, 62, 64);
 
            return this;
        }
 
        private Integer parseVal(String frame, int start, int end) {
            return Integer.parseInt(frame.substring(start, end), 16);
        }
    }
 
    public enum DeviceType {
 
        SENSOR_V1("0101");
 
        private String type;
 
        DeviceType(String type) {
            this.type = type;
        }
 
        public String getType() {
            return type;
        }
    }
}