-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecoder.js
75 lines (65 loc) · 2.66 KB
/
decoder.js
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
function get_num(x, min, max, precision, round) {
var range = max - min;
var new_range = (Math.pow(2, precision) - 1) / range;
var back_x = x / new_range;
if (back_x===0) {
back_x = min;
}
else if (back_x === (max - min)) {
back_x = max;
}
else {
back_x += min;
}
return Math.round(back_x*Math.pow(10,round))/Math.pow(10,round);
}
function Decoder(bytes) {
var decoded = {};
var resetCause_dict = {
0:"POWERON",
1:"EXTERNAL",
2:"SOFTWARE",
3:"WATCHDOG",
4:"FIREWALL",
5:"OTHER",
6:"STANDBY"
};
// settings
if (port === 3){
decoded.system_status_interval = (bytes[1] << 8) | bytes[0];
decoded.system_functions = {};//bytes[2];
decoded.system_functions.gps_periodic = ((bytes[2] >> 0)&0x01)? 1 : 0;
decoded.system_functions.gps_triggered = ((bytes[2] >> 1)&0x01)? 1 : 0;
decoded.system_functions.gps_hot_fix = ((bytes[2] >> 2)&0x01)? 1 : 0;
decoded.system_functions.accelerometer_enabled = ((bytes[2] >> 3)&0x01)? 1 : 0;
decoded.system_functions.light_enabled = ((bytes[2] >> 4)&0x01)? 1 : 0;
decoded.system_functions.temperature_enabled = ((bytes[2] >> 5)&0x01)? 1 : 0;
decoded.system_functions.humidity_enabled = ((bytes[2] >> 6)&0x01)? 1 : 0;
decoded.system_functions.pressure_enabled = ((bytes[2] >> 7)&0x01)? 1 : 0;
decoded.lorawan_datarate_adr = {};//bytes[3];
decoded.lorawan_datarate_adr.datarate = bytes[3]&0x0f;
decoded.lorawan_datarate_adr.confirmed_uplink = ((bytes[3] >> 6)&0x01)? 1 : 0;
decoded.lorawan_datarate_adr.adr = ((bytes[3] >> 7)&0x01)? 1 : 0;
decoded.sensor_interval = (bytes[5] << 8) | bytes[4];
decoded.gps_cold_fix_timeout = (bytes[7] << 8) | bytes[6];
decoded.gps_hot_fix_timeout = (bytes[9] << 8) | bytes[8];
decoded.gps_minimal_ehpe = bytes[10];
decoded.mode_slow_voltage_threshold = bytes[11];
}
else if (port === 2){
decoded.resetCause = resetCause_dict[bytes[0]];
decoded.mode = bytes[1];
decoded.battery = get_num(bytes[2],0,100,8,1);
decoded.temperature = get_num(bytes[3],-20,80,8,1);
decoded.vbus = get_num(bytes[4],0,3.6,8,2);
decoded.system_functions_errors = bytes[5].toString(2);
}
else if (port === 1){
decoded.temperature = get_num(((bytes[1] << 8) | bytes[0]),-20,80,16,2);
decoded.pressure = get_num(((bytes[3] << 8) | bytes[2]),8000,12000,16,2);
decoded.status = bytes[4];
decoded.humidity = get_num(bytes[5],0,100,8,1);
decoded.accelerometer = get_num(bytes[6],-100,100,8,2);
}
return decoded;
}