basic algo that computes virtual location based on TTN gateway locations
This commit is contained in:
		| @ -8,6 +8,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(); | ||||
| @ -21,6 +53,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}`); | ||||
| }); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user