forked from sigfox/npm-sensit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
355 lines (319 loc) · 9.35 KB
/
index.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Sens'it uplink frame formats:
* Ref. https://github.com/sigfox/BCX17-resources/blob/master/Sensit-uplink-frames.md
*
* Common
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 0 | Battery MSB | Type | Timeframe | Mode |
* 1 | Temperature MSB | Battery LSB |
*
* Mode
* 0: Button
* 1: Temperature + Humidity
* 2: Light
* 3: Door
* 4: Move
* 5: Reed switch
*
* Timeframe
* 0: 10 mins
* 1: 1 hour
* 2: 6 hours
* 3: 24 hours
*
* Type
* 0: Regular, no alert
* 1: Button call
* 2: Alert
* 3: New mode
*
* Battery
* Use the following formula to get the value in V (between 2.7 & 4.25V)
* ((({battery MSB} * 16) + {battery LSB}) + 54 / 20)
*
* Temperature (MSB only - low precision)
* Sent along each frame (not only in temp mode)
* Value in °C : (({temperature MSB} * 64) - 200) / 8.0
* Value in °F : (((({temperature MSB} * 64) - 200) / 8.0) * 1.8) + 32
*
* Temperature (MSB + LSB)
* Value in °C : (({temperature MSB} * 64) + {temperature LSB} - 200) / 8.0
* Value in °F : (((({temperature MSB} * 64) + {temperature LSB} - 200) / 8.0) * 1.8) + 32
*
*
* Button mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Unused | Reed switch state | Temperature LSB |
* 3 | Major version | Minor version |
*
*
* Temperature + Humidity mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Unused | Reed switch state | Temperature LSB |
* 3 | Humidity (%) = value * 0.5 |
*
*
* Light mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Multiplier | Value |
* 3 | Number of alerts |
*
*
* Door mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Unused (reserved for configuration values) |
* 3 | Number of alerts |
*
*
* Move mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Unused | Reed switch state | Temperature LSB |
* 3 | Number of alerts |
*
*
* Reed switch mode:
*
* Byte Offset \ bit 7 | bit 6 | bit 5 | bit 4 | bit 3 | bit 2 | bit 1 | bit 0 |
* 2 | Unused | Reed switch state | Temperature LSB |
* 3 | Number of alerts |
*/
(function() {
function hexToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function getMode(bytes) {
if (bytes.length < 1) {
return null;
}
return bytes[0] & 0x07;
}
function getModeText(modeValue) {
switch (modeValue) {
case 0:
return 'Button';
case 1:
return 'Temperature + Humidity';
case 2:
return 'Light';
case 3:
return 'Door';
case 4:
return 'Move';
case 5:
return 'Reed switch';
default:
return 'Unknown';
}
}
function getTimeframe(bytes) {
if (bytes.length < 1) {
return null;
}
return (bytes[0] & 0x18) >> 3;
}
function getTimeframeText(timeframeValue) {
switch (timeframeValue) {
case 0:
return '10 mins';
case 1:
return '1 hour';
case 2:
return '6 hours';
case 3:
return '24 hours';
default:
return 'Unknown';
}
}
function getType(bytes) {
if (bytes.length < 1) {
return null;
}
return (bytes[0] & 0x60) >> 5;
}
function getTypeText(typeValue) {
switch (typeValue) {
case 0:
return 'Regular, no alert';
case 1:
return 'Button call';
case 2:
return 'Alert';
case 3:
return 'New mode';
default:
return 'Unknown';
}
}
function getBattery(bytes) {
if (bytes.length < 2) {
return null;
}
var bMSB = (bytes[0] & 0x80) >> 7;
var bLSB = bytes[1] & 0x0f;
return (((bMSB * 16) + bLSB) + 54) / 20.0;
}
function getTempMSB(bytes) {
if (bytes.length < 2) {
return null;
}
return (bytes[1] & 0xf0) >> 4;
}
function getTempLSB(bytes) {
if (bytes.length < 3) {
return null;
}
return bytes[2] & 0x3f;
}
function getTempC(msb, lsb) {
if (msb == null || lsb == null) {
return null;
}
return (((msb * 64) + lsb) - 200) / 8.0;
}
function getTempF(msb, lsb) {
return (getTempC(msb, lsb) * 1.8) + 32;
}
function getHighPrecisionTemperatures(bytes, obj) {
var tempMSB = getTempMSB(bytes);
var tempLSB = getTempLSB(bytes);
obj.tempC = getTempC(tempMSB, tempLSB);
obj.tempF = getTempF(tempMSB, tempLSB);
}
function getHumidity(bytes) {
if (bytes.length < 4) {
return null;
}
return bytes[3] * 0.5;
}
function getReedSwitchState(bytes) {
if (bytes.length < 3) {
return null;
}
return (bytes[2] & 0x40) >> 6;
}
function getLightMultiplier(bytes) {
if (bytes.length < 3) {
return null;
}
switch ((bytes[2] & 0xc0) >> 6) {
case 0:
return 1;
case 1:
return 8;
case 2:
return 64;
case 3:
return 1024;
}
}
function getLightValue(bytes) {
if (bytes.length < 3) {
return null;
}
return bytes[2] & 0x3f;
}
function getMajorVersion(bytes) {
if (bytes.length < 3) {
return null;
}
return (bytes[3] & 0xf0) >> 4;
}
function getMinorVersion(bytes) {
if (bytes.length < 3) {
return null;
}
return bytes[3] & 0x0f;
}
function getNumberOfAlerts(bytes) {
if (bytes.length < 4) {
return null;
}
return bytes[3];
}
function parseButtonMode(bytes, obj) {
getHighPrecisionTemperatures(bytes, obj);
obj.reedSwitchState = getReedSwitchState(bytes)
obj.majorVersion = getMajorVersion(bytes);
obj.minorVersion = getMinorVersion(bytes);
return obj;
}
function parseTemperatureMode(bytes, obj) {
getHighPrecisionTemperatures(bytes, obj);
obj.reedSwitchState = getReedSwitchState(bytes)
if (obj.type != 1) { // do not contain humidity if double clicked (type == 1), because it's always the fixed value '18'
obj.humidity = getHumidity(bytes);
}
return obj;
}
function parseLightMode(bytes, obj) {
var mul = getLightMultiplier(bytes);
var val = getLightValue(bytes);
obj.lux = (mul * val * 0.01);
obj.numAlerts = getNumberOfAlerts(bytes);
return obj;
}
function parseDoorMode(bytes, obj) {
obj.numAlerts = getNumberOfAlerts(bytes);
return obj;
}
function parseMoveMode(bytes, obj) {
getHighPrecisionTemperatures(bytes, obj);
obj.reedSwitchState = getReedSwitchState(bytes)
obj.numAlerts = getNumberOfAlerts(bytes);
return obj;
}
function parseReedSwitchMode(bytes, obj) {
getHighPrecisionTemperatures(bytes, obj);
obj.reedSwitchState = getReedSwitchState(bytes)
obj.numAlerts = getNumberOfAlerts(bytes);
return obj;
}
function parse(sigfoxFrame) {
var bytes = hexToBytes(sigfoxFrame);
// common values
var mode = getMode(bytes);
var timeframe = getTimeframe(bytes);
var type = getType(bytes);
var battery = getBattery(bytes);
var tempMSB = getTempMSB(bytes);
var obj = {
mode: mode,
modeText: getModeText(mode),
timeframe: timeframe,
timeframeText: getTimeframeText(timeframe),
type: type,
typeText: getTypeText(type),
battery: battery,
tempCLowPrecision: getTempC(tempMSB, 0),
tempFLowPrecision: getTempF(tempMSB, 0),
};
switch (mode) {
case 0: // Button mode
return parseButtonMode(bytes, obj);
case 1: // Temperature + Humidity mode
return parseTemperatureMode(bytes, obj);
case 2: // Light mode
return parseLightMode(bytes, obj);
case 3: // Door mode
return parseDoorMode(bytes, obj);
case 4: // Move mode
return parseMoveMode(bytes, obj);
case 5: // Reed switch mode
return parseReedSwitchMode(bytes, obj);
default:
return obj;
}
}
module.exports = {
parse: parse
}
})();