only create valid wifiScans
This commit is contained in:
parent
fc8e4ca486
commit
3f3c47d629
@ -58,29 +58,36 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
|
|||||||
event.wifis.map(async (wifi) => {
|
event.wifis.map(async (wifi) => {
|
||||||
// Create new WiFi Scan entry if wigle.net reported location
|
// Create new WiFi Scan entry if wigle.net reported location
|
||||||
const apiResponse = await getLocationForWifiMemoized(wifi.mac);
|
const apiResponse = await getLocationForWifiMemoized(wifi.mac);
|
||||||
return {
|
console.log(apiResponse);
|
||||||
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
|
|
||||||
mac: wifi.mac,
|
// Only return valid data wifiScans (location for MAC was found)
|
||||||
rssi: wifi.rssi,
|
if ((apiResponse != undefined) && (apiResponse.success == true) && (apiResponse?.totalResults > 0)) {
|
||||||
latitude: apiResponse?.results[0]?.trilat,
|
console.log("Create new wifiScan: " + wifi.mac)
|
||||||
longitude: apiResponse?.results[0]?.trilong,
|
return {
|
||||||
};
|
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
|
||||||
|
mac: wifi.mac,
|
||||||
|
rssi: wifi.rssi,
|
||||||
|
latitude: apiResponse.results[0].trilat,
|
||||||
|
longitude: apiResponse.results[0].trilong,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
// Return null for invalid cases
|
||||||
|
console.log("Don't create new wifiScan: " + wifi.mac)
|
||||||
|
return null;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Store valid wifiScans into DB
|
||||||
await wifiScanService.createWifiScans(wifiScans);
|
await wifiScanService.createWifiScans(wifiScans);
|
||||||
|
|
||||||
const { totalWeight, weightedLatitude, weightedLongitude } =
|
const { totalWeight, weightedLatitude, weightedLongitude } =
|
||||||
wifiScans.reduce(
|
wifiScans.reduce(
|
||||||
(acc, { latitude, longitude, rssi }) => {
|
(acc, { latitude, longitude, rssi }) => {
|
||||||
if (latitude && longitude && rssi !== 0) {
|
console.log("Current WifiScan: " + latitude + " " + longitude + " " + rssi)
|
||||||
const weight = 1 / Math.abs(rssi);
|
const weight = 1 / Math.abs(rssi);
|
||||||
|
acc.totalWeight += weight;
|
||||||
acc.totalWeight += weight;
|
acc.weightedLatitude += latitude * weight;
|
||||||
acc.weightedLatitude += latitude * weight;
|
acc.weightedLongitude += longitude * weight;
|
||||||
acc.weightedLongitude += longitude * weight;
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user