-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackendController.ts
More file actions
93 lines (81 loc) · 3.2 KB
/
Copy pathBackendController.ts
File metadata and controls
93 lines (81 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { DatabaseService } from "db";
import type { IncomingMessage, Server, ServerResponse } from "http";
import type { BackendControllerTypes } from "@/controllers/BackendController/BackendController.types";
import { LapController } from "@/controllers/LapController/LapController";
import { SocketIO } from "@/datasources/SocketIO/SocketIO";
import { SolarMQTTClient } from "@/datasources/SolarMQTTClient/SolarMQTTClient";
import { options } from "@/datasources/SolarMQTTClient/SolarMQTTClient.types";
import { logger } from "@/index";
import { type ITelemetryData } from "@shared/helios-types";
//getDriverInfo
export class BackendController implements BackendControllerTypes {
public socketIO: SocketIO;
public lapController: LapController;
public mqtt: SolarMQTTClient;
public databaseService: DatabaseService;
public carLatency: number;
constructor(
httpsServer: Server<typeof IncomingMessage, typeof ServerResponse>,
) {
this.socketIO = new SocketIO(httpsServer, this);
this.mqtt = new SolarMQTTClient(options, this);
this.lapController = new LapController(this);
this.databaseService = DatabaseService.getInstance();
this.establishCarPinging();
this.carLatency = 0;
this.initializeDatabase();
// this.handleCarLatency();
}
private async initializeDatabase() {
try {
await this.databaseService.initialize();
logger.info("Database connection established successfully!");
} catch (error) {
logger.error("Failed to initialize database:", error);
// Optionally throw or handle gracefully based on your needs
// For non-critical features, you might continue without database
// throw error;
}
}
public establishCarPinging() {
// Ping the car every 5 seconds
this.mqtt.pingTimer(5000);
// send data to car every 5 seconds
this.mqtt.telemetryToCar(5000);
}
public handleTelemetryToCar(carLatency: number) {
// Broadcast the car latency to the frontend
this.socketIO.broadcastCarLatency(carLatency);
logger.info("Car Latency - receiving: ", carLatency.toString());
}
public async handlePacketReceive(message: ITelemetryData) {
// Insert the packet into the database
try {
await this.databaseService.upsertPacketData(message);
} catch (error) {
logger.error("Failed to insert packet data:", error);
}
// Broadcast the packet to the frontend
this.socketIO.broadcastPacket(message);
// Handle the packet in the lap controller
await this.lapController.handlePacket(message);
}
public handleCarDisconnect() {
// Broadcast the car disconnect event to the frontend
this.socketIO.broadcastCarDisconnect({ message: "Car has disconnected" });
logger.info("Car disconnect event broadcasted to frontend");
}
public handleCarConnect() {
// Broadcast the car disconnect event to the frontend
this.socketIO.broadcastCarConnect({ message: "Car has connected" });
logger.info("Car connect event broadcasted to frontend");
}
public async cleanup() {
try {
await this.databaseService.close();
logger.info("Database connection closed successfully");
} catch (error) {
logger.error("Error closing database connection:", error);
}
}
}