Created
July 10, 2023 19:44
-
-
Save tbaumann/e1e7b4fc3601ca2eda1ac7af0ff6b22a 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
/* | |
Decoding downlink messages received by https://dev.disk91.com/fieldtester/ttn/v3 webhook | |
See https://github.com/disk91/WioLoRaWANFieldTester/blob/master/doc/DEVELOPMENT.md | |
*/ | |
function encodeDownlink(input) { | |
return { | |
bytes: [], | |
fPort: 1, | |
warnings: [], | |
errors: [] | |
}; | |
} | |
/* | |
Byte Usage | |
0 Sequence ID % 255 | |
1 Min Rssi + 200 (160 = -40dBm) | |
2 Max Rssi + 200 (160 = -40dBm) | |
3 Min Distance step 250m | |
4 Max Distance step 250m | |
5 Seen hotspot | |
*/ | |
function decodeDownlink(input) { | |
errors = []; | |
if(input.fPort == 2){ | |
data = {}; | |
data.sequece = input.bytes[0]; | |
data.min_rssi = input.bytes[1]-200; | |
data.max_rssi = input.bytes[2]-200; | |
data.min_distance = (input.bytes[3] * 250) + "m"; | |
data.max_distance = (input.bytes[4] * 250) + "m"; | |
data.hotspots = input.bytes[5]; | |
}else{ | |
data = input.bytes; | |
errors = "Can only decode fPort 2 messages" | |
} | |
return { | |
data: data, | |
warnings: [], | |
errors: errors | |
} | |
} |
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
// For details see https://github.com/disk91/WioLoRaWANFieldTester/blob/master/doc/DEVELOPMENT.md | |
function Decoder(bytes, port) { | |
var decoded = {}; | |
// avoid sending Downlink ACK to integration (Cargo) | |
var lonSign; | |
var latSign; | |
var encLat; | |
var encLon; | |
if (port === 1) { | |
// Decode normal messages | |
lonSign = (bytes[0] >> 7) & 0x01 ? -1 : 1; | |
latSign = (bytes[0] >> 6) & 0x01 ? -1 : 1; | |
encLat = ((bytes[0] & 0x3f) << 17) + | |
(bytes[1] << 9) + | |
(bytes[2] << 1) + | |
(bytes[3] >> 7); | |
encLon = ((bytes[3] & 0x7f) << 16) + | |
(bytes[4] << 8) + | |
bytes[5]; | |
var hdop = bytes[8] / 10; | |
var sats = bytes[9]; | |
const maxHdop = 2; | |
const minSats = 5; | |
if ((hdop < maxHdop) && (sats >= minSats)) { | |
// Send only acceptable quality of position to mappers | |
decoded.latitude = latSign * (encLat * 108 + 53) / 10000000; | |
decoded.longitude = lonSign * (encLon * 215 + 107) / 10000000; | |
decoded.altitude = ((bytes[6] << 8) + bytes[7]) - 1000; | |
decoded.accuracy = (hdop * 5 + 5) / 10 | |
decoded.hdop = hdop; | |
decoded.sats = sats; | |
} else { | |
decoded.error = "Need more GPS precision (hdop must be <" + maxHdop + | |
" & sats must be >= " + minSats + ") current hdop: " + hdop + " & sats:" + sats; | |
} | |
return decoded; | |
} else if (port === 3) { | |
// Decode discovery messages | |
lonSign = (bytes[0] >> 7) & 0x01 ? -1 : 1; | |
latSign = (bytes[0] >> 6) & 0x01 ? -1 : 1; | |
encLat = ((bytes[0] & 0x3f) << 17) + | |
(bytes[1] << 9) + | |
(bytes[2] << 1) + | |
(bytes[3] >> 7); | |
encLon = ((bytes[3] & 0x7f) << 16) + | |
(bytes[4] << 8) + | |
bytes[5]; | |
decoded.latitude = latSign * (encLat * 108 + 53) / 10000000; | |
decoded.longitude = lonSign * (encLon * 215 + 107) / 10000000; | |
return decoded; | |
} | |
return null; | |
} | |
// TTN v3 workaround from https://gist.github.com/tiefpunkt/9407f4049365c065fdd6905adf949993 | |
function decodeUplink(input) { | |
var data = input.bytes; | |
var valid = true; | |
if (typeof Decoder === "function") { | |
data = Decoder(data, input.fPort); | |
} | |
if (typeof Converter === "function") { | |
data = Converter(data, input.fPort); | |
} | |
if (typeof Validator === "function") { | |
valid = Validator(data, input.fPort); | |
} | |
if (valid) { | |
return { | |
data: data | |
}; | |
} else { | |
return { | |
data: {}, | |
errors: ["Invalid data received"] | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment