implemented connectedDispatch record for dispatcher
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { prisma } from "@repo/db";
|
||||
import { getPublicUser, prisma } from "@repo/db";
|
||||
import { pubClient } from "modules/redis";
|
||||
import { Server, Socket } from "socket.io";
|
||||
|
||||
@@ -11,47 +11,76 @@ export const handleConnectDispatch =
|
||||
logoffTime: string;
|
||||
selectedZone: string;
|
||||
}) => {
|
||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||
console.log("User connected to dispatch server");
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
const connectedDispatcherEntry = await prisma.connectedDispatcher.create({
|
||||
data: {
|
||||
publicUser: {},
|
||||
esimatedLogoutTime: logoffTime,
|
||||
lastHeartbeat: new Date(),
|
||||
userId: userId,
|
||||
loginTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
||||
socket.join(`user:${userId}`); // Dem User-Raum beitreten
|
||||
|
||||
/* const keys = await pubClient.keys("Dispatcher:*");
|
||||
await Promise.all(
|
||||
keys.map(async (key) => {
|
||||
return await pubClient.json.get(key);
|
||||
}),
|
||||
); */
|
||||
io.to("dispatchers").emit("dispatcher-update");
|
||||
io.to("pilots").emit("dispatcher-update");
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
try {
|
||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||
console.log("User connected to dispatch server");
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: connectedDispatcherEntry.id,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
});
|
||||
socket.on("reconnect", async () => {
|
||||
console.log("Reconnected to dispatch server");
|
||||
});
|
||||
|
||||
if (!user) return Error("User not found");
|
||||
|
||||
let parsedLogoffDate = null;
|
||||
if (logoffTime.length > 0) {
|
||||
const now = new Date();
|
||||
const [hours, minutes] = logoffTime.split(":").map(Number);
|
||||
if (!hours || !minutes) {
|
||||
throw new Error("Invalid logoffTime format");
|
||||
}
|
||||
parsedLogoffDate = new Date(now);
|
||||
parsedLogoffDate.setHours(hours, minutes, 0, 0);
|
||||
|
||||
// If the calculated time is earlier than now, add one day to make it tomorrow
|
||||
if (parsedLogoffDate <= now) {
|
||||
parsedLogoffDate.setDate(parsedLogoffDate.getDate() + 1);
|
||||
}
|
||||
|
||||
// If the calculated time is in the past, add one day to make it in the future
|
||||
if (parsedLogoffDate <= now) {
|
||||
parsedLogoffDate.setDate(parsedLogoffDate.getDate() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const connectedDispatcherEntry = await prisma.connectedDispatcher.create({
|
||||
data: {
|
||||
publicUser: getPublicUser(user) as any,
|
||||
esimatedLogoutTime: parsedLogoffDate?.toISOString() || null,
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
userId: userId,
|
||||
loginTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
|
||||
socket.join("dispatchers"); // Dem Dispatcher-Raum beitreten
|
||||
socket.join(`user:${userId}`); // Dem User-Raum beitreten
|
||||
|
||||
/* const keys = await pubClient.keys("Dispatcher:*");
|
||||
await Promise.all(
|
||||
keys.map(async (key) => {
|
||||
return await pubClient.json.get(key);
|
||||
}),
|
||||
); */
|
||||
io.to("dispatchers").emit("dispatcher-update");
|
||||
io.to("pilots").emit("dispatcher-update");
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
where: {
|
||||
id: connectedDispatcherEntry.id,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
});
|
||||
socket.on("reconnect", async () => {
|
||||
console.log("Reconnected to dispatch server");
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error connecting to dispatch server:", error);
|
||||
console.log("Error connecting to dispatch server:", error);
|
||||
}
|
||||
};
|
||||
|
||||
94
apps/dispatch-server/socket-events/connect-pilot.ts
Normal file
94
apps/dispatch-server/socket-events/connect-pilot.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { getPublicUser, prisma } from "@repo/db";
|
||||
import { Server, Socket } from "socket.io";
|
||||
|
||||
export const handleConnectDispatch =
|
||||
(socket: Socket, io: Server) =>
|
||||
async ({
|
||||
logoffTime,
|
||||
stationId,
|
||||
}: {
|
||||
logoffTime: string;
|
||||
stationId: number;
|
||||
}) => {
|
||||
try {
|
||||
const userId = socket.data.user.id; // User ID aus dem JWT-Token
|
||||
console.log("User connected to dispatch server");
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) return Error("User not found");
|
||||
|
||||
let parsedLogoffDate = null;
|
||||
if (logoffTime.length > 0) {
|
||||
const now = new Date();
|
||||
const [hours, minutes] = logoffTime.split(":").map(Number);
|
||||
if (!hours || !minutes) {
|
||||
throw new Error("Invalid logoffTime format");
|
||||
}
|
||||
parsedLogoffDate = new Date(now);
|
||||
parsedLogoffDate.setHours(hours, minutes, 0, 0);
|
||||
|
||||
// If the calculated time is earlier than now, add one day to make it tomorrow
|
||||
if (parsedLogoffDate <= now) {
|
||||
parsedLogoffDate.setDate(parsedLogoffDate.getDate() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const connectedAircraftEntry = await prisma.connectedAircraft.create({
|
||||
data: {
|
||||
publicUser: getPublicUser(user) as any,
|
||||
esimatedLogoutTime: parsedLogoffDate?.toISOString() || null,
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
userId: userId,
|
||||
loginTime: new Date().toISOString(),
|
||||
stationId: stationId,
|
||||
/* user: { connect: { id: userId } }, // Ensure the user relationship is set
|
||||
station: { connect: { id: stationId } }, // Ensure the station relationship is set */
|
||||
},
|
||||
});
|
||||
|
||||
socket.join("dispatchers"); // Join the dispatchers room
|
||||
socket.join(`user:${userId}`); // Join the user-specific room
|
||||
socket.join(`station:${stationId}`); // Join the station-specific room
|
||||
|
||||
io.to("dispatchers").emit("dispatcher-update");
|
||||
io.to("pilots").emit("dispatcher-update");
|
||||
|
||||
// Add a listener for station-specific events
|
||||
socket.on(`station:${stationId}:event`, async (data) => {
|
||||
console.log(`Received event for station ${stationId}:`, data);
|
||||
// Handle station-specific logic here
|
||||
io.to(`station:${stationId}`).emit("station-event-update", data);
|
||||
});
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
console.log("Disconnected from dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
data: {
|
||||
logoutTime: new Date().toISOString(),
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
socket.on("reconnect", async () => {
|
||||
console.log("Reconnected to dispatch server");
|
||||
await prisma.connectedDispatcher.update({
|
||||
where: {
|
||||
id: connectedAircraftEntry.id,
|
||||
},
|
||||
data: {
|
||||
lastHeartbeat: new Date().toISOString(),
|
||||
logoutTime: null,
|
||||
},
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error connecting to dispatch server:", error);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user