Compare commits

..

No commits in common. "283482b361df96114451b17e0c9c29cf004e07b92fdc62dd790fde5b9fa2e2e3" and "fc8e4ca486c53d4819234d45722a69f7f108de0b91882d40d723f08383e6cc90" have entirely different histories.

3 changed files with 32 additions and 43 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 NOT NULL, latitude DOUBLE,
longitude DOUBLE NOT NULL, longitude DOUBLE,
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,7 +7,6 @@ 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);
@ -55,37 +54,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
let wifiScans = await Promise.all( const 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);
// Only return valid data wifiScans (location for MAC was found)
if ((apiResponse?.success == true) && (apiResponse.totalResults > 0)) {
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 undefined;
}) })
); );
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 } =
locatedWifiScans.reduce( wifiScans.reduce(
(acc, { latitude, longitude, rssi }) => { (acc, { latitude, longitude, rssi }) => {
if (latitude && longitude && rssi !== 0) {
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;
}, },
{ {
@ -101,7 +96,6 @@ 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,14 +54,9 @@ 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("Error during call of API wigle.net:", error); console.error("Fehler beim Aufruf des Services:", error);
} }
}; };