calculate virtual location based on TTN GW

This commit is contained in:
Hendrik Schutter 2024-12-31 13:19:13 +01:00
parent 097cb44649
commit 393eab2b45
3 changed files with 30 additions and 45 deletions

View File

@ -10,7 +10,7 @@ TODO
### Database ### Database
**Change name of database and credentials as you like!** **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';` - 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` - Import tables: `/usr/bin/mariadb -u dbuser -p1234 dev_locationhub < server/sql/tables.sql`

View File

@ -8,6 +8,34 @@ domainEventEmitter.on(
TtnMessageReceivedEventName, TtnMessageReceivedEventName,
async (event: TtnMessageReceivedEvent) => { async (event: TtnMessageReceivedEvent) => {
console.log(event); 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);
}
} }
); );

View File

@ -10,38 +10,6 @@ import ttnRoutes from "./controller/ttnController";
import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController"; import ttnGatewayReceptionRoutes from "./controller/ttnGatewayReceptionController";
import wifiScanRoutes from "./controller/wifiScanController"; 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(); dotenv.config();
const app = express(); const app = express();
@ -56,17 +24,6 @@ app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes);
app.use("/api/locations", locationRoutes); app.use("/api/locations", locationRoutes);
app.use("/api/ttn", ttnRoutes); 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, () => { app.listen(PORT, () => {
console.log(`🚀 Server runs here: http://localhost:${PORT}`); console.log(`🚀 Server runs here: http://localhost:${PORT}`);
}); });