/
tektelic-kona-home-decoder.js
209 lines (185 loc) · 6.43 KB
/
tektelic-kona-home-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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Decoder function for The Things Network to unpack the payload of TEKTELIC's All-in-One Home sensors
* More info on the sensors/buy online:
* https://connectedthings.store/products/tektelic-kona-home-sensor-pir
* https://connectedthings.store/products/tektelic-kona-home-sensor-base
* This function was created by Al Bennett at Sensational Systems - al@sensational.systems
*/
function bin2HexStr(arr)
{
var str = "";
for(var i=0; i<arr.length; i++)
{
var tmp = arr[i].toString(16);
if(tmp.length == 1)
{
tmp = "0" + tmp;
}
tmp = "0x" + tmp;
if (i != arr.length - 1) {
tmp += ",";
}
str += tmp;
}
return str;
}
// Wrapper for ChirpStack
function Decode(port, bytes) {
//Simply call the TTN function with the parameters switched
return Decoder(bytes, port);
}
//TTN Handler
function Decoder(bytes, port) {
var params = {
"battery_voltage": null,
"reed_state": null,
"light_detected": null,
"temperature": null,
"humidity": null,
"impact_magnitude": null,
"break_in": null,
"acceleration_x": null,
"acceleration_y": null,
"acceleration_z": null,
"reed_count": null,
"moisture": null,
"activity": null,
"mcu_temperature": null,
"impact_alarm": null,
"activity_count": null,
"external_input": null,
"external_input_count": null,
"decode_data_hex": bin2HexStr(bytes),
"bytes": bytes
}
for (var i = 0; i < bytes.length; i++) {
// Handle battery voltage
if(0x00 === bytes[i] && 0xFF === bytes[i+1]) {
params.battery_voltage = 0.01 * ((bytes[i+2] << 8) | bytes[i+3]);
i = i+3;
}
// Handle reed switch state
if(0x01 === bytes[i] && 0x00 === bytes[i+1]) {
if(0x00 === bytes[i+2]) {
params.reed_state = true;
} else if(0xFF === bytes[i+2]) {
params.reed_state = false;
}
i = i+2;
}
// Handle light detection
if(0x02 === bytes[i] && 0x00 === bytes[i+1]) {
if(0x00 === bytes[i+2]) {
params.light_detected = false;
} else if(0xFF === bytes[i+2]) {
params.light_detected = true;
}
i = i+2;
}
// Handle temperature
if(0x03 === bytes[i] && 0x67 === bytes[i+1]) {
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
params.temperature = (bytes[i+2]<<24>>16 | bytes[i+3]) / 10;
i = i+3;
}
// Handle humidity
if(0x04 === bytes[i] && 0x68 === bytes[i+1]) {
params.humidity = 0.5 * bytes[i+2];
i = i+2;
}
// Handle impact magnitude
if(0x05 === bytes[i] && 0x02 === bytes[i+1]) {
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
params.impact_magnitude = (bytes[i+2]<<24>>16 | bytes[i+3])/1000;
i = i+3;
}
// Handle break-in
if(0x06 === bytes[i] && 0x00 === bytes[i+1]) {
if(0x00 === bytes[i+2]) {
params.break_in = false;
} else if(0xFF === bytes[i+2]) {
params.break_in = true;
}
i = i+2;
}
// Handle accelerometer data
if(0x07 === bytes[i] && 0x71 === bytes[i+1]) {
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
params.acceleration_x = (bytes[i+2]<<24>>16 | bytes[i+3])/1000;
params.acceleration_y = (bytes[i+4]<<24>>16 | bytes[i+5])/1000;
params.acceleration_z = (bytes[i+6]<<24>>16 | bytes[i+7])/1000;
i = i+7;
}
// Handle reed switch count
if(0x08 === bytes[i] && 0x04 === bytes[i+1]) {
params.reed_count = (bytes[i+2] << 8) | bytes[i+3];
i = i+3;
}
// Handle moisture
if(0x09 === bytes[i] && 0x00 === bytes[i+1]) {
i = i+1;
//check data
if (0x00 === bytes[i+1]) {
params.moisture = false;
i = i+1;
}
else if( 0xFF === bytes[i+1]) {
params.moisture = true;
i = i+1;
}
}
// Handle PIR activity
//check the channel and type
if(0x0A === bytes[i] && 0x00 === bytes[i+1]) {
i = i+1;
//check data
if (0x00 === bytes[i+1]) {
params.activity = false;
i = i+1;
}
else if( 0xFF === bytes[i+1]) {
params.activity = true;
i = i+1;
}
}
// Handle temperature
if(0x0B === bytes[i] && 0x67 === bytes[i+1]) {
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
params.mcu_temperature = (bytes[i+2]<<24>>16 | bytes[i+3]) / 10;
i = i+3;
}
// Handle impact alarm
if(0x0C === bytes[i] && 0x00 === bytes[i+1]) {
if(0x00 === bytes[i+2]) {
params.impact_alarm = false;
} else if(0xFF === bytes[i+2]) {
params.impact_alarm = true;
}
i = i+2;
}
// Handle motion (PIR activity) event count
if(0x0D === bytes[i] && 0x04 === bytes[i+1]) {
params.activity_count = (bytes[i+2] << 8) | bytes[i+3];
i = i+3;
}
// Handle external input state
if(0x0E === bytes[i] && 0x00 === bytes[i+1]) {
if(0x00 === bytes[i+2]) {
params.external_input = true;
} else if(0xFF === bytes[i+2]) {
params.external_input = false;
}
i = i+2;
}
// Handle external input count
if(0x0F === bytes[i] && 0x04 === bytes[i+1]) {
params.external_input_count = (bytes[i+2] << 8) | bytes[i+3];
i = i+3;
}
}
return params
}