This commit is contained in:
Philipp Schweizer 2025-01-02 16:35:28 +01:00
parent 283482b361
commit bc0695626f

View File

@ -1,13 +1,12 @@
import { container } from "tsyringe";
import { domainEventEmitter } from "../config/eventEmitter"; import { domainEventEmitter } from "../config/eventEmitter";
import { import {
TtnMessageReceivedEvent, TtnMessageReceivedEvent,
TtnMessageReceivedEventName, TtnMessageReceivedEventName,
} from "../event/ttnMessageReceivedEvent"; } from "../event/ttnMessageReceivedEvent";
import { container } from "tsyringe"; import { getLocationForWifiMemoized } from "../proxy/wigle";
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 { WifiScan } from "../models/wifiScan";
const locationService = container.resolve(LocationService); const locationService = container.resolve(LocationService);
const wifiScanService = container.resolve(WifiScanService); const wifiScanService = container.resolve(WifiScanService);
@ -20,13 +19,13 @@ const CalculateTtnGatewayLocation = (event: TtnMessageReceivedEvent) => {
}; };
if (!event.ttnGateways || event.ttnGateways.length === 0) { if (!event.ttnGateways || event.ttnGateways.length === 0) {
console.log("No TTN Gateway location received!") console.log("No TTN Gateway location received!");
} else { } else {
let totalWeight = 0; let totalWeight = 0;
let weightedLatitude = 0; let weightedLatitude = 0;
let weightedLongitude = 0; let weightedLongitude = 0;
event.ttnGateways.forEach(gw => { event.ttnGateways.forEach((gw) => {
const weight = 1 / Math.abs(gw.rssi); // Higher RSSI (closer to 0) gives more weight const weight = 1 / Math.abs(gw.rssi); // Higher RSSI (closer to 0) gives more weight
totalWeight += weight; totalWeight += weight;
weightedLatitude += gw.latitude * weight; weightedLatitude += gw.latitude * weight;
@ -34,9 +33,15 @@ const CalculateTtnGatewayLocation = (event: TtnMessageReceivedEvent) => {
}); });
// Calculate the weighted average to get the virtual location // Calculate the weighted average to get the virtual location
virtualLocation.latitude = weightedLatitude / totalWeight; const virtualLocation = {
virtualLocation.longitude = weightedLongitude / totalWeight; latitude: weightedLatitude / totalWeight,
console.log("Tracker location based on TTN Gateway location:", virtualLocation); longitude: weightedLongitude / totalWeight,
};
console.log(
"Tracker location based on TTN Gateway location:",
virtualLocation
);
} }
return { return {
ttn_latitude: virtualLocation.latitude, ttn_latitude: virtualLocation.latitude,
@ -52,7 +57,7 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
}; };
if (!event.wifis || event.wifis.length === 0) { if (!event.wifis || event.wifis.length === 0) {
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( let wifiScans = await Promise.all(
@ -99,8 +104,10 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
virtualLocation.latitude = weightedLatitude / totalWeight; virtualLocation.latitude = weightedLatitude / totalWeight;
virtualLocation.longitude = weightedLongitude / 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 { return {
wifi_latitude: virtualLocation.latitude, wifi_latitude: virtualLocation.latitude,
@ -110,7 +117,10 @@ const CalculateWifiLocation = async (event: TtnMessageReceivedEvent) => {
const CalculateGnssLocation = (event: TtnMessageReceivedEvent) => { const CalculateGnssLocation = (event: TtnMessageReceivedEvent) => {
// Get location based on reported GNSS // Get location based on reported GNSS
if (event.gnssLocation.latitude === undefined || event.gnssLocation.longitude === undefined) { if (
event.gnssLocation.latitude === undefined ||
event.gnssLocation.longitude === undefined
) {
console.log("No valid GNSS location received!"); console.log("No valid GNSS location received!");
} }
@ -134,6 +144,7 @@ domainEventEmitter.on(
if (event.ttnGateways && event.ttnGateways.length > 0) { if (event.ttnGateways && event.ttnGateways.length > 0) {
const virtualLocation = CalculateTtnGatewayLocation(event); const virtualLocation = CalculateTtnGatewayLocation(event);
console.log(virtualLocation);
ttn_gw_based_latitude = virtualLocation.ttn_latitude; ttn_gw_based_latitude = virtualLocation.ttn_latitude;
ttn_gw_based_longitude = virtualLocation.ttn_longitude; ttn_gw_based_longitude = virtualLocation.ttn_longitude;
} }
@ -148,6 +159,16 @@ domainEventEmitter.on(
gnss_based_latitude = virtualLocation.gnss_latitude; gnss_based_latitude = virtualLocation.gnss_latitude;
gnss_based_longitude = virtualLocation.gnss_longitude; gnss_based_longitude = virtualLocation.gnss_longitude;
console.log({
lp_ttn_end_device_uplinks_id: event.lp_ttn_end_device_uplinks_id,
ttn_gw_latitude: ttn_gw_based_latitude,
ttn_gw_longitude: ttn_gw_based_longitude,
gnss_latitude: gnss_based_latitude,
gnss_longitude: gnss_based_longitude,
wifi_latitude: wifi_based_latitude,
wifi_longitude: wifi_based_longitude,
});
const newLocation = await locationService.createLocation({ const newLocation = await locationService.createLocation({
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,
ttn_gw_latitude: ttn_gw_based_latitude, ttn_gw_latitude: ttn_gw_based_latitude,
@ -158,7 +179,6 @@ domainEventEmitter.on(
wifi_longitude: wifi_based_longitude, wifi_longitude: wifi_based_longitude,
}); });
console.log(newLocation) console.log(newLocation);
} }
); );