-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDL_SHT21.java
More file actions
137 lines (117 loc) · 4.18 KB
/
Copy pathDL_SHT21.java
File metadata and controls
137 lines (117 loc) · 4.18 KB
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
/* https://www.decentlab.com/support */
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
interface Conversion {
double execute(double x[]);
}
class Sensor {
public int length;
public SensorValue values[];
Sensor(int length, SensorValue values[]) {
this.length = length;
this.values = values;
}
}
class SensorValue {
public String name;
public String unit;
public Conversion convert;
SensorValue(String name, String unit, Conversion convert) {
this.name = name;
this.unit = unit;
this.convert = convert;
}
}
class DecodedValue {
public double value;
public String unit;
DecodedValue(double value, String unit) {
this.value = value;
this.unit = unit;
}
public String toString() {
return this.value + (this.unit != null ? " [" + this.unit + "]" : "") ;
}
}
class DecentlabDecoder {
public static final int PROTOCOL_VERSION = 2;
private static int readInt(InputStream is) throws IOException {
return ((is.read() & 0xff) << 8) + (is.read() & 0xff);
}
public static Map<String, DecodedValue> decode(Sensor[] SENSORS, byte[] msg) throws IOException {
return decode(SENSORS, new ByteArrayInputStream(msg));
}
public static Map<String, DecodedValue> decode(Sensor[] SENSORS, String msg) throws IOException {
byte[] buf = new byte[msg.length() / 2];
for (int i = 0, j = 0; i < msg.length(); i += 2, j++) {
buf[j] = (byte) Integer.parseInt(msg.substring(i, i + 2), 16);
}
return decode(SENSORS, new ByteArrayInputStream(buf));
}
public static Map<String, DecodedValue> decode(Sensor[] SENSORS, InputStream msg) throws IOException {
int version = msg.read();
if (version != PROTOCOL_VERSION) {
throw new IOException("protocol version " + version + " doesn't match v2");
}
Map<String, DecodedValue> result = new HashMap<String, DecodedValue>();
result.put("Protocol version", new DecodedValue(version, null));
int deviceId = readInt(msg);
result.put("Device ID", new DecodedValue(deviceId, null));
int flags = readInt(msg);
for (Sensor sensor : SENSORS) {
if ((flags & 1) == 1) {
double[] x = new double[sensor.length];
for (int i = 0; i < sensor.length; i++) {
x[i] = readInt(msg);
}
for (SensorValue val : sensor.values) {
if (val.convert != null) {
result.put(val.name, new DecodedValue(val.convert.execute(x), val.unit));
}
}
}
flags >>= 1;
}
return result;
}
}
class DL_SHT21_Definition {
public static final Sensor SENSORS[] = new Sensor[] {
new Sensor(2, new SensorValue[] {
new SensorValue("Air temperature", "°C", new Conversion() {
public double execute(double x[]) { return 175.72 * x[0] / 65536 - 46.85; }
}),
new SensorValue("Air humidity", "%", new Conversion() {
public double execute(double x[]) { return 125 * x[1] / 65536 - 6; }
})
}),
new Sensor(1, new SensorValue[] {
new SensorValue("Battery voltage", "V", new Conversion() {
public double execute(double x[]) { return x[0] / 1000; }
})
})
};
}
public class DL_SHT21 {
public static void main(String[] args) {
String[] payloads = new String[] {
"02030e000364a079b10c60",
"02030e00020c60"
};
for (String pl : payloads) {
try {
Map<String, DecodedValue> decoded = DecentlabDecoder.decode(DL_SHT21_Definition.SENSORS, pl);
for (Map.Entry<String, DecodedValue> v : decoded.entrySet()) {
System.out.println(v.getKey() + ": " + v.getValue());
}
}
catch(IOException e) {
e.printStackTrace();
}
System.out.println();
}
}
}