added Mission Closed Toeast, enhanced logic
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { MissionLog, prisma } from "@repo/db";
|
||||
import { MissionLog, NotificationPayload, prisma } from "@repo/db";
|
||||
import { io } from "index";
|
||||
import cron from "node-cron";
|
||||
|
||||
const removeClosedMissions = async () => {
|
||||
@@ -11,6 +12,7 @@ const removeClosedMissions = async () => {
|
||||
const lastAlert = (mission.missionLog as unknown as MissionLog[]).find((l) => {
|
||||
return l.type === "alert-log";
|
||||
});
|
||||
|
||||
const lastAlertTime = lastAlert ? new Date(lastAlert.timeStamp) : null;
|
||||
|
||||
const aircraftsInMission = await prisma.connectedAircraft.findMany({
|
||||
@@ -21,17 +23,65 @@ const removeClosedMissions = async () => {
|
||||
},
|
||||
});
|
||||
|
||||
const allConnectedAircraftsInIdleStatus = aircraftsInMission.every((a) =>
|
||||
["1", "2", "6"].includes(a.fmsStatus),
|
||||
);
|
||||
|
||||
const allStationsInMissionChangedFromStatus4to1Or8to1 = mission.missionStationIds.every(
|
||||
(stationId) => {
|
||||
const status4Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "4"
|
||||
);
|
||||
});
|
||||
const status8Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "8"
|
||||
);
|
||||
});
|
||||
|
||||
const status1Log = (mission.missionLog as unknown as MissionLog[]).findIndex((l) => {
|
||||
return (
|
||||
l.type === "station-log" &&
|
||||
l.data?.stationId === stationId &&
|
||||
l.data?.newFMSstatus === "1"
|
||||
);
|
||||
});
|
||||
return (
|
||||
status4Log !== -1 &&
|
||||
status1Log !== -1 &&
|
||||
(status4Log < status1Log || status8Log < status1Log)
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
const missionHastManualReactivation = (mission.missionLog as unknown as MissionLog[]).some(
|
||||
(l) => l.type === "reopened-log",
|
||||
);
|
||||
|
||||
if (
|
||||
aircraftsInMission.length > 0 && // Check if any aircraft is still active
|
||||
!aircraftsInMission.some((a) => ["1", "2", "6"].includes(a.fmsStatus)) // Check if any aircraft is in a status that indicates it's not inactive
|
||||
!allConnectedAircraftsInIdleStatus // If some aircrafts are still active, do not close the mission
|
||||
)
|
||||
return;
|
||||
|
||||
const now = new Date();
|
||||
if (!lastAlertTime) return;
|
||||
|
||||
// change State to closed if last alert was more than 180 minutes ago
|
||||
if (now.getTime() - lastAlertTime.getTime() < 30 * 60 * 1000) return;
|
||||
// Case 1: Forgotten Mission, last alert more than 3 Hours ago
|
||||
// Case 2: All stations in mission changed from status 4 to 1 or from status 8 to 1
|
||||
if (
|
||||
!(
|
||||
now.getTime() - lastAlertTime.getTime() > 1000 * 60 * 180 ||
|
||||
allStationsInMissionChangedFromStatus4to1Or8to1
|
||||
) ||
|
||||
missionHastManualReactivation
|
||||
)
|
||||
return;
|
||||
|
||||
const log: MissionLog = {
|
||||
type: "completed-log",
|
||||
auto: true,
|
||||
@@ -39,7 +89,7 @@ const removeClosedMissions = async () => {
|
||||
data: {},
|
||||
};
|
||||
|
||||
await prisma.mission.update({
|
||||
const updatedMission = await prisma.mission.update({
|
||||
where: {
|
||||
id: mission.id,
|
||||
},
|
||||
@@ -50,6 +100,16 @@ const removeClosedMissions = async () => {
|
||||
},
|
||||
},
|
||||
});
|
||||
io.to("dispatchers").emit("new-mission", { updatedMission });
|
||||
io.to("dispatchers").emit("notification", {
|
||||
type: "mission-auto-close",
|
||||
status: "chron",
|
||||
message: `Einsatz ${updatedMission.publicId} wurde aufgrund von Inaktivität geschlossen.`,
|
||||
data: {
|
||||
missionId: updatedMission.id,
|
||||
publicMissionId: updatedMission.publicId,
|
||||
},
|
||||
} as NotificationPayload);
|
||||
console.log(`Mission ${mission.id} closed due to inactivity.`);
|
||||
});
|
||||
};
|
||||
@@ -74,11 +134,11 @@ const removeConnectedAircrafts = async () => {
|
||||
});
|
||||
};
|
||||
|
||||
cron.schedule("*/5 * * * *", async () => {
|
||||
cron.schedule("*/1 * * * *", async () => {
|
||||
try {
|
||||
await removeClosedMissions();
|
||||
await removeConnectedAircrafts();
|
||||
} catch (error) {
|
||||
console.error("Error removing closed missions:", error);
|
||||
console.error("Error on cron job:", error);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user