diff --git a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts index e682c01..9d47ce1 100644 --- a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts +++ b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts @@ -58,29 +58,36 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => { event.wifis.map(async (wifi) => { // Create new WiFi Scan entry if wigle.net reported location const apiResponse = await getLocationForWifiMemoized(wifi.mac); - 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, - }; + console.log(apiResponse); + + // Only return valid data wifiScans (location for MAC was found) + if ((apiResponse != undefined) && (apiResponse.success == true) && (apiResponse?.totalResults > 0)) { + console.log("Create new wifiScan: " + wifi.mac) + 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); const { totalWeight, weightedLatitude, weightedLongitude } = wifiScans.reduce( (acc, { latitude, longitude, rssi }) => { - if (latitude && longitude && rssi !== 0) { - const weight = 1 / Math.abs(rssi); - - acc.totalWeight += weight; - acc.weightedLatitude += latitude * weight; - acc.weightedLongitude += longitude * weight; - } - + console.log("Current WifiScan: " + latitude + " " + longitude + " " + rssi) + const weight = 1 / Math.abs(rssi); + acc.totalWeight += weight; + acc.weightedLatitude += latitude * weight; + acc.weightedLongitude += longitude * weight; return acc; }, {