Hi,
Im trying to connect a Rak10701 field tester to my Chirpstack network. ive set up the field tester and i have it connecting to my network through my gateway and then sending packets to Data cake but the ball seams to stop there my debug is saying :
Payload decoder did not return list of measurements Returned instead: null
Log: null
and if i run the payload decoder i get: json: unsupported value: NaN
Ive sifted through the code and cant find anything obvious ive also run through the setup again but still returning the same. any help would be greatly appreciated!
payload decoder code below:
function distance(lat1, lon1, lat2, lon2) {
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
var radlat1 = Math.PI * lat1 / 180;
var radlat2 = Math.PI * lat2 / 180;
var theta = lon1 - lon2;
var radtheta = Math.PI * theta / 180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180 / Math.PI;
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
return dist;
}
}
function Decoder(bytes, fPort) {
var decoded = {};
// avoid sending Downlink ACK to integration (Cargo)
if (fPort === 1) {
var lonSign = (bytes[0] >> 7) & 0x01 ? -1 : 1;
var latSign = (bytes[0] >> 6) & 0x01 ? -1 : 1;
var encLat = ((bytes[0] & 0x3f) << 17) +
(bytes[1] << 9) +
(bytes[2] << 1) +
(bytes[3] >> 7);
var encLon = ((bytes[3] & 0x7f) << 16) +
(bytes[4] << 8) +
bytes[5];
var hdop = bytes[8] / 10;
var sats = bytes[9];
var maxHdop = 2;
var 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;
decoded.location = "(" + decoded.latitude + "," + decoded.longitude + ")";
} else {
decoded.error = "Need more GPS precision (hdop must be <" + maxHdop +
" & sats must be >= " + minSats + ") current hdop: " + hdop + " & sats:" + sats;
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;
decoded.location = "(" + decoded.latitude + "," + decoded.longitude + ")";
}
// decoded.raw = rawPayload.uplink_message.rx_metadata[0].location;
decoded.num_gw = normalizedPayload.gateways.length;
decoded.minRSSI = 0;
decoded.maxRSSI = 0;
decoded.minSNR = 0;
decoded.maxSNR = 0;
decoded.minDistance = 0;
decoded.maxDistance = 0;
var server_type = 0;
// Check if payload comes from TTN
if (typeof (rawPayload.uplink_message) != "undefined") {
console.log("Found TTN format");
server_type = 1;
decoded.is_chirpstack = 1;
}
// Check if payload comes from Helium
else if (typeof (rawPayload.hotspots) != "undefined") {
console.log("Found Helium format");
server_type = 2;
}
// Check if payload comes from Chirpstack
else if (typeof (rawPayload.rxInfo) != "undefined") {
console.log("Found Chirpstack format");
server_type = 3;
decoded.is_chirpstack = 1;
}
//Check if the payload comes from LORIOT
else if (typeof (rawPayload.cmd) != "undefined") {
console.log("Found LORIOT format");
server_type = 4;
decoded.is_chirpstack = 1;
}
else {
console.log("Unknown raw format");
}
var gw_lat = {};
var gw_long = {};
var gw_loc_valid = {};
decoded.num_gw = 0;
for (idx_tst = 0; idx_tst < 10; idx_tst++) {
if (typeof (normalizedPayload.gateways[idx_tst]) != "undefined") {
console.log("Found gateway with IDX " + idx_tst);
decoded.num_gw += 1;
}
}
var has_location = false;
for (idx = 0; idx < decoded.num_gw; idx++) {
has_location = true;
var new_rssi = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[idx] && normalizedPayload.gateways[idx].rssi) || 0;
var new_snr = (!!normalizedPayload.gateways && !!normalizedPayload.gateways[idx] && normalizedPayload.gateways[idx].snr) || 0;
if ((new_rssi < decoded.minRSSI) || (decoded.minRSSI == 0)) {
decoded.minRSSI = new_rssi;
}
if ((new_rssi > decoded.maxRSSI) || (decoded.maxRSSI == 0)) {
decoded.maxRSSI = new_rssi;
}
if ((new_snr < decoded.minSNR) || (decoded.minSNR == 0)) {
decoded.minSNR = new_snr;
}
if ((new_snr > decoded.maxSNR) || (decoded.maxSNR == 0)) {
decoded.maxSNR = new_snr;
}
switch (server_type) {
//TTN
case 1:
if (typeof (rawPayload.uplink_message.rx_metadata[idx].location) != "undefined") {
gw_lat[idx] = rawPayload.uplink_message.rx_metadata[idx].location.latitude;
gw_long[idx] = rawPayload.uplink_message.rx_metadata[idx].location.longitude;
has_location = true;
} else {
has_location = false;
decoded.num_gw -= 1;
idx++;
has_location = false;
}
break;
// Helium
case 2:
if (typeof (rawPayload.hotspots[idx].lat) != "undefined") {
gw_lat[idx] = rawPayload.hotspots[idx].lat;
gw_long[idx] = rawPayload.hotspots[idx].long;
gw_loc_valid[idx] = true;
} else {
gw_loc_valid[idx] = false;
decoded.num_gw -= 1;
idx++;
has_location = false;
}
break;
// Chirpstack
case 3:
if (typeof (rawPayload.rxInfo[idx].location) != "undefined") {
gw_lat[idx] = rawPayload.rxInfo[idx].location.latitude;
gw_long[idx] = rawPayload.rxInfo[idx].location.longitude;
gw_loc_valid[idx] = true;
} else {
gw_loc_valid[idx] = false;
decoded.num_gw -= 1;
idx++;
has_location = false;
}
break;
// LORIOT
case 4:
if (typeof (rawPayload.gws[0].lat) != "undefined") {
gw_lat[idx] = rawPayload.gws[0].lat;
gw_long[idx] = rawPayload.gws[0].lon;
gw_loc_valid[idx] = true;
} else {
gw_loc_valid[idx] = false;
decoded.num_gw -= 1;
idx++;
has_location = false;
}
break;
default:
console.log("Unknown LNS");
gw_loc_valid[idx] = false;
decoded.num_gw -= 1;
idx++;
has_location = false;
break;
}
if (has_location == true) {
console.log("IDX " + idx + " lat " + gw_lat[idx] + " long " + gw_long[idx]);
// Calculate distance
var new_distance = distance(gw_lat[idx], gw_long[idx], decoded.latitude, decoded.longitude);
if ((new_distance * 1000 < decoded.minDistance) || (decoded.minDistance == 0)) {
decoded.minDistance = new_distance * 1000;
}
if ((new_distance * 1000 > decoded.maxDistance) || (decoded.maxDistance == 0)) {
decoded.maxDistance = new_distance * 1000;
}
} else {
console.log("Gateway IDX " + idx-1 + " has no location");
}
}
var hotspot_name = ""
for (idx = 0; idx < decoded.num_gw; idx++) {
var index = idx + 1;
if (index < 9) {
hotspot_name = "hotspot_0" + index.toString();
} else {
hotspot_name = "hotspot_" + index.toString();
}
console.log(hotspot_name);
decoded[hotspot_name] = "(" + gw_lat[idx] + "," + gw_long[idx] + ")";
}
decoded.maxMod = 1 + parseInt((Math.round(decoded.maxDistance / 250.0)), 10);
decoded.minMod = 1 + parseInt((Math.round(decoded.minDistance / 250.0)), 10);
return decoded;
}
return null;
}