Merge branch 'feature/ttn-location-algo' of git.mosad.xyz:localhorst/LocationHub into feature/ttn-location-algo

This commit is contained in:
Philipp Schweizer 2024-12-30 23:21:43 +01:00
commit 95adba8e9a

View File

@ -9,6 +9,38 @@ import lpTtnEndDeviceUplinksRoutes from "./controller/lpTtnEndDeviceUplinksContr
import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController";
import wifiScanRoutes from "./controller/wifiScanController";
interface LocationData {
rssi: number;
latitude: number;
longitude: number;
}
function calculateTtnGatewayLocation(dataList: LocationData[]): { latitude: number, longitude: number } {
if (!dataList || dataList.length === 0) {
throw new Error("The data list is empty or undefined.");
}
let totalWeight = 0;
let weightedLatitude = 0;
let weightedLongitude = 0;
// Loop through the dataList and calculate weighted sums based on RSSI
dataList.forEach(point => {
const weight = 1 / Math.abs(point.rssi); // Higher RSSI (closer to 0) gives more weight
totalWeight += weight;
weightedLatitude += point.latitude * weight;
weightedLongitude += point.longitude * weight;
});
// Calculate the weighted average to get the virtual location
const virtualLocation = {
latitude: weightedLatitude / totalWeight,
longitude: weightedLongitude / totalWeight
};
return virtualLocation;
}
dotenv.config();
const app = express();
@ -22,6 +54,17 @@ app.use("/api/wifi-scans", wifiScanRoutes);
app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes);
app.use("/api/locations", locationRoutes);
const dataList: LocationData[] = [
//sensecap_exporter_2024-10-18_12-52-30.json
{ rssi: -108, latitude: 51.875759063444335, longitude: 4.448537528514863 },
{ rssi: -114, latitude: 51.8875851, longitude: 4.4919652 },
{ rssi: -106, latitude: 51.9102795122954, longitude: 4.48074765239573 },
{ rssi: -103, latitude: 51.9111671447754, longitude: 4.46313190460205 }
];
const virtualLocation = calculateTtnGatewayLocation(dataList);
console.log("TTN Gateway location:", virtualLocation);
app.listen(PORT, () => {
console.log(`🚀 Server läuft auf http://localhost:${PORT}`);
console.log(`🚀 Server runs here: http://localhost:${PORT}`);
});