calculate virtual location based on TTN GW
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
|
Reference in New Issue
Block a user