Created
April 12, 2021 19:57
-
-
Save descartes/2c9f7cb1e7095a502b96c90ae7b91c38 to your computer and use it in GitHub Desktop.
Testing a TTN v3 JS decoder for a forumite
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Javascript Payload Tester</title> | |
</head> | |
<body> | |
<script> | |
var payloadBase64 = "02D4D6C000807E9C0F4164"; | |
var port = 1; | |
var payload = hexStringToBinaryArray(payloadBase64); | |
var input = []; | |
input.bytes = payload; | |
input.port = port; | |
var v2 = Decoder(payload, port); | |
var v2_format = testReturn_v2(); | |
debugger; | |
/* Expected result: | |
{ | |
"ALARM_status": "FALSE", | |
"Altitude": 0, | |
"BatV": 3.905, | |
"FW": 164, | |
"LON": "ON", | |
"Latitude": 47.50304, | |
"Longitude": 8.42102, | |
"MD": "Move", | |
"Pitch": 0, | |
"Roll": 0 | |
} | |
*/ | |
var v3 = decodeUplink(input); | |
var v3_format = testReturn_v3(); | |
debugger; | |
/* Current result | |
decoded_payload": { | |
"ALARM_status": "FALSE", | |
"BatV": 3.905, | |
"FW": 164, | |
"LON": "ON", | |
"MD": "Disable", | |
"Pitch": 0, | |
"Roll": 0, | |
"altitude": 0, | |
"hdop": null, | |
"latitude": 47.50304, | |
"longitude": 0 | |
}, | |
*/ | |
/* Tested via simulate uplink | |
"decoded_payload": { | |
"ALARM_status": "FALSE", | |
"Altitude": 0, | |
"BatV": 3.905, | |
"FW": 164, | |
"HDOP": null, | |
"LON": "ON", | |
"Latitude": 47.50304, | |
"Longitude": 8.42102, | |
"MD": "Move", | |
"Pitch": 0, | |
"Roll": 0 | |
}, | |
*/ | |
/* Reported differences | |
MD: v2 = “Move”, v3 =“Disable” | |
Longitude: v2 = “8.42102”, v3 = “0” | |
Altitude, Longitude, Latitude: v2 = uppercase first letter, v3 = lowercase first letter (this is less of a problem, though) | |
*/ | |
function Decoder(bytes, port) { | |
// Decode an uplink message from a buffer | |
// (array) of bytes to an object of fields. | |
var latitude; //gps latitude,units: ° | |
latitude = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) / 1000000; //gps latitude,units: ° | |
var longitude; | |
longitude = (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]) / 1000000; //gps longitude,units: ° | |
var alarm = (bytes[8] & 0x40) ? "TRUE" : "FALSE"; //Alarm status | |
var batV = (((bytes[8] & 0x3f) << 8) | bytes[9]) / 1000; //Battery,units:V | |
if ((bytes[10] & 0xC0) == 0x40) { | |
var motion_mode = "Move"; | |
} else if ((bytes[10] & 0xC0) == 0x80) { | |
motion_mode = "Collide"; | |
} else if ((bytes[10] & 0xC0) == 0xC0) { | |
motion_mode = "User"; | |
} else { | |
motion_mode = "Disable"; | |
} //mode of motion | |
var led_updown = (bytes[10] & 0x20) ? "ON" : "OFF"; //LED status for position,uplink and downlink | |
var Firmware = 160 + (bytes[10] & 0x1f); // Firmware version; 5 bits | |
var roll = (bytes[11] << 8 | bytes[12]) / 100; //roll,units: ° | |
var pitch = (bytes[13] << 8 | bytes[14]) / 100; //pitch,units: ° | |
var hdop = 0; | |
if (bytes[15] > 0) { | |
hdop = bytes[15] / 100; //hdop,units: ° | |
} else { | |
hdop = bytes[15]; | |
} | |
var altitude = (bytes[16] << 8 | bytes[17]) / 100; //Altitude,units: ° | |
return { | |
Latitude: latitude, | |
Longitude: longitude, | |
Roll: roll, | |
Pitch: pitch, | |
BatV: batV, | |
ALARM_status: alarm, | |
MD: motion_mode, | |
LON: led_updown, | |
FW: Firmware, | |
HDOP: hdop, | |
Altitude: altitude, | |
}; | |
} | |
function decodeUplink(input) { | |
var bytes; | |
bytes = input.bytes; | |
var port; | |
port = input.fport; | |
var latitude; //gps latitude,units: ° | |
latitude = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) / 1000000; //gps latitude,units: ° | |
var longitude; | |
longitude = (bytes[4] << 24 | bytes[5] << 16 | bytes[6] << 8 | bytes[7]) / 1000000; //gps longitude,units: ° | |
var alarm = (bytes[8] & 0x40) ? "TRUE" : "FALSE"; //Alarm status | |
var batV = (((bytes[8] & 0x3f) << 8) | bytes[9]) / 1000; //Battery,units:V | |
if ((bytes[10] & 0xC0) == 0x40) { | |
var motion_mode = "Move"; | |
} else if ((bytes[10] & 0xC0) == 0x80) { | |
motion_mode = "Collide"; | |
} else if ((bytes[10] & 0xC0) == 0xC0) { | |
motion_mode = "User"; | |
} else { | |
motion_mode = "Disable"; | |
} //mode of motion | |
var led_updown = (bytes[10] & 0x20) ? "ON" : "OFF"; //LED status for position,uplink and downlink | |
var Firmware = 160 + (bytes[10] & 0x1f); // Firmware version; 5 bits | |
var roll = (bytes[11] << 8 | bytes[12]) / 100; //roll,units: ° | |
var pitch = (bytes[13] << 8 | bytes[14]) / 100; //pitch,units: ° | |
var hdop = 0; | |
if (bytes[15] > 0) { | |
hdop = bytes[15] / 100; //hdop,units: ° | |
} else { | |
hdop = bytes[15]; | |
} | |
var altitude = (bytes[16] << 8 | bytes[17]) / 100; //Altitude,units: ° | |
return { | |
data: { | |
Latitude: latitude, | |
Longitude: longitude, | |
Roll: roll, | |
Pitch: pitch, | |
BatV: batV, | |
ALARM_status: alarm, | |
MD: motion_mode, | |
LON: led_updown, | |
FW: Firmware, | |
HDOP: hdop, | |
Altitude: altitude | |
}, | |
warnings: [], | |
errors: [] | |
}; | |
} | |
function testReturn_v2() { | |
var x = 1; | |
var y = 2; | |
var z = 3; | |
return { | |
X: x, | |
Y: y, | |
Z: z | |
}; | |
} | |
function testReturn_v3() { | |
var x = 1; | |
var y = 2; | |
var z = 3; | |
return { | |
data: { | |
X: x, | |
Y: y, | |
Z: z | |
}, | |
warnings: [], | |
errors: [] | |
}; | |
} | |
// Support function | |
function hexStringToBinaryArray(str) { | |
var result = []; | |
var l = str.length; | |
for (c = 0; c < l; c += 2) { | |
result.push(parseInt(str.substring(c, c + 2), 16)); | |
} | |
return result; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment