From 3f3c47d6299020efafe3f70873fdbe60e81fd530b00c01d84af3e5f8a950f2fa Mon Sep 17 00:00:00 2001 From: localhorst Date: Thu, 2 Jan 2025 15:42:52 +0100 Subject: [PATCH 1/2] only create valid wifiScans --- .../ttnMessageReceivedEventHandler.ts | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) 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; }, { From ad32baa844164f081fbd93be9645d4f762e1461bd56bb0db509e9638a06e859c Mon Sep 17 00:00:00 2001 From: localhorst Date: Thu, 2 Jan 2025 16:31:55 +0100 Subject: [PATCH 2/2] refactor wifi location parsing --- server/sql/tables.sql | 4 +- .../ttnMessageReceivedEventHandler.ts | 65 +++++++++---------- server/src/proxy/wigle.ts | 9 ++- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/server/sql/tables.sql b/server/sql/tables.sql index 5788884..96e9328 100644 --- a/server/sql/tables.sql +++ b/server/sql/tables.sql @@ -18,8 +18,8 @@ CREATE TABLE IF NOT EXISTS wifi_scan ( lp_ttn_end_device_uplinks_id UUID, mac VARCHAR(255), rssi NUMERIC, - latitude DOUBLE, - longitude DOUBLE, + latitude DOUBLE NOT NULL, + longitude DOUBLE NOT NULL, created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (lp_ttn_end_device_uplinks_id) REFERENCES lp_ttn_end_device_uplinks(lp_ttn_end_device_uplinks_id) diff --git a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts index 9d47ce1..e59893d 100644 --- a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts +++ b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts @@ -7,6 +7,7 @@ import { container } from "tsyringe"; import { LocationService } from "../services/locationService"; import { WifiScanService } from "../services/wifiScanService"; import { getLocationForWifiMemoized } from "../proxy/wigle"; +import { WifiScan } from "../models/wifiScan"; const locationService = container.resolve(LocationService); const wifiScanService = container.resolve(WifiScanService); @@ -54,54 +55,52 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => { console.log("No WiFi scans received!") } else { // Process Wi-Fi data to compute weighted location - const wifiScans = await Promise.all( + let wifiScans = await Promise.all( event.wifis.map(async (wifi) => { // Create new WiFi Scan entry if wigle.net reported location - const apiResponse = await getLocationForWifiMemoized(wifi.mac); - console.log(apiResponse); - + const apiResponse = await getLocationForWifiMemoized(wifi.mac);; // 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) + if ((apiResponse?.success == true) && (apiResponse.totalResults > 0)) { 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, - }; + 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; + return undefined; }) ); + const wifiScansFiltered = wifiScans.filter(w => (w !== undefined)); + // Store valid wifiScans into DB - await wifiScanService.createWifiScans(wifiScans); + const locatedWifiScans = await wifiScanService.createWifiScans(wifiScansFiltered); - const { totalWeight, weightedLatitude, weightedLongitude } = - wifiScans.reduce( - (acc, { latitude, longitude, rssi }) => { - 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; - }, - { - totalWeight: 0, - weightedLatitude: 0, - weightedLongitude: 0, - } - ); + if (locatedWifiScans.length !== 0) { + const { totalWeight, weightedLatitude, weightedLongitude } = + locatedWifiScans.reduce( + (acc, { latitude, longitude, rssi }) => { + const weight = 1 / Math.abs(rssi); + acc.totalWeight += weight; + acc.weightedLatitude += latitude * weight; + acc.weightedLongitude += longitude * weight; + return acc; + }, + { + totalWeight: 0, + weightedLatitude: 0, + weightedLongitude: 0, + } + ); - // Calculate the weighted average to get the virtual location - virtualLocation.latitude = weightedLatitude / totalWeight; - virtualLocation.longitude = weightedLongitude / totalWeight; + // Calculate the weighted average to get the virtual location + virtualLocation.latitude = weightedLatitude / totalWeight; + virtualLocation.longitude = weightedLongitude / totalWeight; - console.log("Tracker location based on WiFi Scan location:", virtualLocation); + console.log("Tracker location based on WiFi Scan location:", virtualLocation); + } } return { wifi_latitude: virtualLocation.latitude, diff --git a/server/src/proxy/wigle.ts b/server/src/proxy/wigle.ts index d2139cd..705c180 100644 --- a/server/src/proxy/wigle.ts +++ b/server/src/proxy/wigle.ts @@ -54,9 +54,14 @@ export const getLocationForWifi = async ( Authorization: `Basic ${process.env.WIGLE_TOKEN}`, }, }); - return await response.json(); + if (response.ok) { + return await response.json(); + } + console.log(response.status); + return undefined; + } catch (error) { - console.error("Fehler beim Aufruf des Services:", error); + console.error("Error during call of API wigle.net:", error); } };