Last active
August 19, 2022 11:06
-
-
Save prohazko2/bef9c524becde2d0c1dd788265e69831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EVENT_PACKET = 1; | |
const EVENT_ALARM = 2; | |
/** | |
* Check if n-th bit set for number | |
* @param value {number} number value | |
* @param bit {number} bit position | |
*/ | |
function bit(value, bit) { | |
return (value & (1 << bit)) !== 0; | |
} | |
/** | |
* Parse Vega SI-11 Payload | |
* https://en.iotvega.com/product/si11 | |
* | |
* ``` | |
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8-11 | 12-15 | 16-19 | 20-23 | |
* 1 - EVENT_PACKET | type | bat | cfg | timestamp | t | in[0] | in[1] | in[2] | in[3] | |
* 2 - EVENT_ALARM | type | bat | cfg | i | timestamp | in[0] | in[1] | in[2] | in[3] | |
* | |
* ``` | |
* @param payload {string} base64 encoded payload | |
*/ | |
export function process(payload) { | |
const data = ric.base64.decode(payload); | |
let offset = 0; | |
/* - option 1 - parse binary payload from raw byte array */ | |
const bytes = new Uint8Array(data.buffer); | |
const evtype = bytes[offset++]; | |
const battery = bytes[offset++]; | |
const configByte = bytes[offset++]; | |
const config = { | |
otaa: !bit(configByte, 0), | |
abp: bit(configByte, 0), | |
inputs: [ | |
+bit(configByte, 4), | |
+bit(configByte, 5), | |
// etc ... | |
], | |
}; | |
const evin = evtype === EVENT_ALARM | |
? bytes[offset++] | |
: undefined; | |
/* - option 2 - use JS `DataView` interface (like Node's `Buffer`) */ | |
const evtime = data.getUint32(offset, true) * 1000; | |
offset += 4; | |
const temperature = evtype === EVENT_PACKET | |
? data.getUint8(offset++) | |
: undefined; | |
const inputs = []; | |
for (let i = 0; i < 4; i++) { | |
inputs[i] = data.getUint32(offset, true); | |
offset += 4; | |
} | |
return { evtype, battery, config, evin, evtime, temperature, inputs }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment