Compare commits
3 Commits
feat/web-c
...
95adba8e9a
Author | SHA256 | Date | |
---|---|---|---|
95adba8e9a | |||
a4a8b6c3c1 | |||
aa3c250c2e |
5
server/src/config/eventEmitter.ts
Normal file
5
server/src/config/eventEmitter.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
class DomainEventEmitter extends EventEmitter {}
|
||||
|
||||
export const domainEventEmitter = new DomainEventEmitter();
|
14
server/src/event/ttnMessageReceivedEvent.ts
Normal file
14
server/src/event/ttnMessageReceivedEvent.ts
Normal 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;
|
||||
}[];
|
||||
};
|
13
server/src/eventHandler/ttnMessageReceivedEventHandler.ts
Normal file
13
server/src/eventHandler/ttnMessageReceivedEventHandler.ts
Normal 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 🚀
|
||||
}
|
||||
);
|
@ -1,6 +1,7 @@
|
||||
import dotenv from "dotenv";
|
||||
import express from "express";
|
||||
import "reflect-metadata";
|
||||
import "./eventHandler/ttnMessageReceivedEventHandler";
|
||||
const cors = require("cors");
|
||||
|
||||
import locationRoutes from "./controller/locationController";
|
||||
@ -8,6 +9,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 +54,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