Fix creation of WifiScan #8
@ -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)
|
||||||
|
@ -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,47 +55,52 @@ 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);;
|
||||||
return {
|
// Only return valid data wifiScans (location for MAC was found)
|
||||||
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
|
if ((apiResponse?.success == true) && (apiResponse.totalResults > 0)) {
|
||||||
mac: wifi.mac,
|
return {
|
||||||
rssi: wifi.rssi,
|
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
|
||||||
latitude: apiResponse?.results[0]?.trilat,
|
mac: wifi.mac,
|
||||||
longitude: apiResponse?.results[0]?.trilong,
|
rssi: wifi.rssi,
|
||||||
};
|
latitude: apiResponse?.results[0].trilat,
|
||||||
|
longitude: apiResponse?.results[0].trilong,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
await wifiScanService.createWifiScans(wifiScans);
|
const wifiScansFiltered = wifiScans.filter(w => (w !== undefined));
|
||||||
|
|
||||||
const { totalWeight, weightedLatitude, weightedLongitude } =
|
// Store valid wifiScans into DB
|
||||||
wifiScans.reduce(
|
const locatedWifiScans = await wifiScanService.createWifiScans(wifiScansFiltered);
|
||||||
(acc, { latitude, longitude, rssi }) => {
|
|
||||||
if (latitude && longitude && rssi !== 0) {
|
if (locatedWifiScans.length !== 0) {
|
||||||
|
const { totalWeight, weightedLatitude, weightedLongitude } =
|
||||||
|
locatedWifiScans.reduce(
|
||||||
|
(acc, { 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;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
totalWeight: 0,
|
||||||
|
weightedLatitude: 0,
|
||||||
|
weightedLongitude: 0,
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
return acc;
|
// Calculate the weighted average to get the virtual location
|
||||||
},
|
virtualLocation.latitude = weightedLatitude / totalWeight;
|
||||||
{
|
virtualLocation.longitude = weightedLongitude / totalWeight;
|
||||||
totalWeight: 0,
|
|
||||||
weightedLatitude: 0,
|
|
||||||
weightedLongitude: 0,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
// Calculate the weighted average to get the virtual location
|
console.log("Tracker location based on WiFi Scan location:", virtualLocation);
|
||||||
virtualLocation.latitude = weightedLatitude / totalWeight;
|
}
|
||||||
virtualLocation.longitude = weightedLongitude / totalWeight;
|
|
||||||
|
|
||||||
console.log("Tracker location based on WiFi Scan location:", virtualLocation);
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
wifi_latitude: virtualLocation.latitude,
|
wifi_latitude: virtualLocation.latitude,
|
||||||
|
@ -54,9 +54,14 @@ export const getLocationForWifi = async (
|
|||||||
Authorization: `Basic ${process.env.WIGLE_TOKEN}`,
|
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) {
|
} catch (error) {
|
||||||
console.error("Fehler beim Aufruf des Services:", error);
|
console.error("Error during call of API wigle.net:", error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user