-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (85 loc) · 3.45 KB
/
server.js
File metadata and controls
102 lines (85 loc) · 3.45 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
94
95
96
97
98
99
100
101
102
import dotenv from "dotenv";
dotenv.config();
console.log("Dotenv loaded");
import { createClient } from "redis";
import { fetchFlights } from "./vamsys/getFlights.js";
import { sendDiscordWebhook } from "./utils/discord/sendWebhook.js";
import { handleIncoming } from "./hoppie/handleIncoming.js";
import { inboundMsg } from "./functions/inboundMsg.js";
import { sendDept } from "./hoppie/sendDept.js";
import { getBooking } from "./vamsys/getBooking.js";
import { getPilot } from "./vamsys/getPilot.js";
import { sendImageWebhook } from "./utils/discord/sendImageWebhook.js";
import log from "./utils/logging.js";
//console.log("Imported dependencies");
log("Imported dependencies", true);
// Connect to Redis
const redis = createClient({
url: process.env.REDIS_URL || "redis://localhost:6379",
});
// Handle Redis Connetion Errors
redis.on("error", (err) => log(`Redis Client Error: ${err}`, true));
await redis.connect();
//console.log("Connected to Redis");
log("Connected to Redis", true);
// Wipe Redis database on startup
// await redis.flushDb();
// console.log("Redis database wiped on startup");
async function fetchAndSend() {
try {
const response = await fetchFlights();
// Extract flights array from response.data
const flights = response?.data || [];
for (const flight of flights) {
const distanceRemaining = flight.progress?.distanceRemaining;
const callsign = flight.booking?.callsign;
const flightPhase = flight.progress?.currentPhase;
const booking = await getBooking(flight.bookingId);
const pilot = await getPilot(booking.data.pilot_id);
const discordPilotId = pilot.data.discord_id;
//console.log(callsign);
log(`Callsign: ${callsign}`, false);
//console.log(distanceRemaining);
log(`Distance Remaining: ${distanceRemaining}`, false);
// Skip aircraft if there is no callsign or no distance remaining
if (!callsign || distanceRemaining === undefined) {
continue;
}
// Check to see if the aircraft is within 100NM of the destination
if (distanceRemaining < 150) {
// Check to see if we've already sent a message to this aircraft in the last hour
// const gate = getGate(flight.arrivalAirport.identifier);
// const ground = getGround(flight.arrivalAirport.identifier);
// const atis = await getAtis(flight.arrivalAirport.identifier);
// if (!gate) {
// continue;
// }
const redisKey = `acars_sent_inbound:${callsign}`;
const alreadySent = await redis.exists(redisKey);
if (!alreadySent) {
await inboundMsg(flight, callsign, discordPilotId);
// Store the fact that we've sent a message to this aircraft in the last hour
await redis.setEx(redisKey, 3600, "1");
} else {
// Log the skip
// console.log(
// `Skipping ${callsign} - message already sent in the last hour`
// );
log(
`Skipping ${callsign} - message already sent in the last hour`,
false
);
}
}
const preflightKey = `acars_sent_preflight:${callsign}`;
const alreadySentPreflight = await redis.exists(preflightKey);
}
} catch (error) {
// console.error("Error fetching flights:", error);
log(`Error fetching flights: ${error}`, true);
}
}
// Run every 1 minute (60000 milliseconds)
setInterval(fetchAndSend, 60000);
setInterval(handleIncoming, 20000);
fetchAndSend();