refactor wifi location parsing

This commit is contained in:
Hendrik Schutter 2025-01-02 16:31:55 +01:00
parent 3f3c47d629
commit ad32baa844
3 changed files with 41 additions and 37 deletions

View File

@ -18,8 +18,8 @@ CREATE TABLE IF NOT EXISTS wifi_scan (
lp_ttn_end_device_uplinks_id UUID, lp_ttn_end_device_uplinks_id UUID,
mac VARCHAR(255), mac VARCHAR(255),
rssi NUMERIC, rssi NUMERIC,
latitude DOUBLE, latitude DOUBLE NOT NULL,
longitude DOUBLE, longitude DOUBLE NOT NULL,
created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE 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) FOREIGN KEY (lp_ttn_end_device_uplinks_id) REFERENCES lp_ttn_end_device_uplinks(lp_ttn_end_device_uplinks_id)

View File

@ -7,6 +7,7 @@ import { container } from "tsyringe";
import { LocationService } from "../services/locationService"; import { LocationService } from "../services/locationService";
import { WifiScanService } from "../services/wifiScanService"; import { WifiScanService } from "../services/wifiScanService";
import { getLocationForWifiMemoized } from "../proxy/wigle"; import { getLocationForWifiMemoized } from "../proxy/wigle";
import { WifiScan } from "../models/wifiScan";
const locationService = container.resolve(LocationService); const locationService = container.resolve(LocationService);
const wifiScanService = container.resolve(WifiScanService); const wifiScanService = container.resolve(WifiScanService);
@ -54,36 +55,33 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
console.log("No WiFi scans received!") console.log("No WiFi scans received!")
} else { } else {
// Process Wi-Fi data to compute weighted location // Process Wi-Fi data to compute weighted location
const wifiScans = await Promise.all( let wifiScans = await Promise.all(
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);;
console.log(apiResponse);
// Only return valid data wifiScans (location for MAC was found) // Only return valid data wifiScans (location for MAC was found)
if ((apiResponse != undefined) && (apiResponse.success == true) && (apiResponse?.totalResults > 0)) { if ((apiResponse?.success == true) && (apiResponse.totalResults > 0)) {
console.log("Create new wifiScan: " + wifi.mac)
return { return {
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id, lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
mac: wifi.mac, mac: wifi.mac,
rssi: wifi.rssi, rssi: wifi.rssi,
latitude: apiResponse.results[0].trilat, latitude: apiResponse?.results[0].trilat,
longitude: apiResponse.results[0].trilong, longitude: apiResponse?.results[0].trilong,
};
} }
// Return null for invalid cases }
console.log("Don't create new wifiScan: " + wifi.mac) return undefined;
return null;
}) })
); );
// Store valid wifiScans into DB const wifiScansFiltered = wifiScans.filter(w => (w !== undefined));
await wifiScanService.createWifiScans(wifiScans);
// Store valid wifiScans into DB
const locatedWifiScans = await wifiScanService.createWifiScans(wifiScansFiltered);
if (locatedWifiScans.length !== 0) {
const { totalWeight, weightedLatitude, weightedLongitude } = const { totalWeight, weightedLatitude, weightedLongitude } =
wifiScans.reduce( locatedWifiScans.reduce(
(acc, { latitude, longitude, rssi }) => { (acc, { latitude, longitude, rssi }) => {
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;
@ -103,6 +101,7 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
console.log("Tracker location based on WiFi Scan location:", virtualLocation); console.log("Tracker location based on WiFi Scan location:", virtualLocation);
} }
}
return { return {
wifi_latitude: virtualLocation.latitude, wifi_latitude: virtualLocation.latitude,
wifi_longitude: virtualLocation.longitude, wifi_longitude: virtualLocation.longitude,

View File

@ -54,9 +54,14 @@ export const getLocationForWifi = async (
Authorization: `Basic ${process.env.WIGLE_TOKEN}`, Authorization: `Basic ${process.env.WIGLE_TOKEN}`,
}, },
}); });
if (response.ok) {
return await response.json(); return await response.json();
}
console.log(response.status);
return undefined;
} catch (error) { } catch (error) {
console.error("Fehler beim Aufruf des Services:", error); console.error("Error during call of API wigle.net:", error);
} }
}; };