Compare commits

...

3 Commits

4 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,5 @@
import { EventEmitter } from "events";
class DomainEventEmitter extends EventEmitter {}
export const domainEventEmitter = new DomainEventEmitter();

View File

@ -0,0 +1,14 @@
export const TtnMessageReceivedEventName = "TtnMessageReceived";
export type TtnMessageReceivedEvent = {
lp_ttn_end_device_uplinks_id: string;
wifis: {
mac: string;
rssi: number;
}[];
ttnGateways: {
rssi: number;
latitude: number;
longitude: number;
altitude: number;
}[];
};

View File

@ -0,0 +1,13 @@
import { domainEventEmitter } from "../config/eventEmitter";
import {
TtnMessageReceivedEvent,
TtnMessageReceivedEventName,
} from "../event/ttnMessageReceivedEvent";
domainEventEmitter.on(
TtnMessageReceivedEventName,
async (event: TtnMessageReceivedEvent) => {
console.log(event);
// TODO Hendrik 🚀
}
);

View File

@ -1,6 +1,7 @@
import dotenv from "dotenv"; import dotenv from "dotenv";
import express from "express"; import express from "express";
import "reflect-metadata"; import "reflect-metadata";
import "./eventHandler/ttnMessageReceivedEventHandler";
const cors = require("cors"); const cors = require("cors");
import locationRoutes from "./controller/locationController"; import locationRoutes from "./controller/locationController";
@ -8,6 +9,38 @@ import lpTtnEndDeviceUplinksRoutes from "./controller/lpTtnEndDeviceUplinksContr
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();
@ -21,6 +54,17 @@ app.use("/api/wifi-scans", wifiScanRoutes);
app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes); app.use("/api/ttn-gateway-receptions", ttnGatewayReceptionRoutes);
app.use("/api/locations", locationRoutes); 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, () => { app.listen(PORT, () => {
console.log(`🚀 Server läuft auf http://localhost:${PORT}`); console.log(`🚀 Server runs here: http://localhost:${PORT}`);
}); });