diff --git a/README.md b/README.md index 9727bc4..e77d4fb 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ TODO ### Database **Change name of database and credentials as you like!** -- Create new database: `CREATE DATABASE locationhub;` +- Create new database: `CREATE DATABASE dev_locationhub;` - Create new user for database: `GRANT ALL PRIVILEGES ON dev_locationhub.* TO 'dbuser'@'localhost' IDENTIFIED BY '1234';` - Import tables: `/usr/bin/mariadb -u dbuser -p1234 dev_locationhub < server/sql/tables.sql` diff --git a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts index 90c0007..9192b80 100644 --- a/server/src/eventHandler/ttnMessageReceivedEventHandler.ts +++ b/server/src/eventHandler/ttnMessageReceivedEventHandler.ts @@ -8,6 +8,34 @@ domainEventEmitter.on( TtnMessageReceivedEventName, async (event: TtnMessageReceivedEvent) => { console.log(event); - // TODO Hendrik 🚀 + + if (!event.ttnGateways || event.ttnGateways.length === 0) { + console.log("No TTN Gateway location received!") + } else { + let totalWeight = 0; + let weightedLatitude = 0; + let weightedLongitude = 0; + + event.ttnGateways.forEach(gw => { + const weight = 1 / Math.abs(gw.rssi); // Higher RSSI (closer to 0) gives more weight + totalWeight += weight; + weightedLatitude += gw.latitude * weight; + weightedLongitude += gw.longitude * weight; + }); + + // Calculate the weighted average to get the virtual location + const virtualLocation = { + latitude: weightedLatitude / totalWeight, + longitude: weightedLongitude / totalWeight + }; + + console.log("Tracker location based on TTN Gateway location:", virtualLocation); + } + + + + + } ); + diff --git a/server/src/index.ts b/server/src/index.ts index a7c0494..dcc61d9 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -10,38 +10,6 @@ import ttnRoutes from "./controller/ttnController"; 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(); @@ -56,17 +24,6 @@ app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes); app.use("/api/locations", locationRoutes); app.use("/api/ttn", ttnRoutes); -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 runs here: http://localhost:${PORT}`); });