Compare commits

...

2 Commits

Author SHA1 Message Date
49be089aed
Merge branch 'main' of https://git.0xem.ma/0xEmma/CZQM-Ops-Demo 2024-11-17 16:26:54 -05:00
c3cbd624f4
Add Controllers 2024-11-17 16:26:52 -05:00
5 changed files with 185 additions and 6 deletions

View File

@ -0,0 +1,104 @@
import { NextResponse } from 'next/server';
interface Controller {
callsign: string;
frequency: string;
facility: number;
}
const facilityTypes = {
0: "OBS",
1: "FSS",
2: "DEL",
3: "GND",
4: "TWR",
5: "APP",
6: "CTR",
} as const;
const facilityLongNames = {
OBS: "Observer",
FSS: "Flight Service Station",
DEL: "Clearance Delivery",
GND: "Ground",
TWR: "Tower",
APP: "Approach/Departure",
CTR: "Centre",
} as const;
// Define FIR coverage for airports
const firCoverage: Record<string, string[]> = {
CZQM: ["CYHZ", "CYFC", "CYQM", "CYSJ", "CYZX", "CYYG", "CYYT", "CYQX", "CYYR", "LFVP", "CYQI", "CYAY", "CYDF", "CYJT"],
CZUL: ["CYZV"],
};
export async function GET() {
try {
const response = await fetch('https://data.vatsim.net/v3/vatsim-data.json', {
next: { revalidate: 60 }
});
if (!response.ok) {
throw new Error('Failed to fetch VATSIM data');
}
const data = await response.json();
// First, get all controllers including CTR
const allControllers = data.controllers.map((controller: Controller) => {
const shortFacility = facilityTypes[controller.facility as keyof typeof facilityTypes] || "Unknown";
return {
callsign: controller.callsign,
frequency: controller.frequency,
facility: shortFacility,
facilityLong: facilityLongNames[shortFacility as keyof typeof facilityLongNames] || "Unknown",
facilityType: controller.facility,
airport: controller.callsign.split('_')[0]
};
});
// Separate CTR controllers
const ctrControllers = allControllers.filter(c => c.facilityType === 6);
// Get non-CTR controllers
const localControllers = allControllers.filter(c => c.facilityType < 6 && c.facilityType > 0 && !c.callsign.includes("ATIS"));
// Group local controllers by airport
const controllersByAirport = localControllers.reduce((acc: any, controller) => {
if (!acc[controller.airport]) {
acc[controller.airport] = [];
}
acc[controller.airport].push(controller);
return acc;
}, {});
// Sort controllers at each airport by facility type (higher number = higher priority)
Object.values(controllersByAirport).forEach((controllers: any) => {
controllers.sort((a: any, b: any) => b.facilityType - a.facilityType);
});
// Add CTR coverage for airports without local controllers
ctrControllers.forEach(ctr => {
const fir = ctr.callsign.split('_')[0];
const coveredAirports = firCoverage[fir] || [];
coveredAirports.forEach(airport => {
// Only add CTR if no local controllers are available
if (!controllersByAirport[airport] || controllersByAirport[airport].length === 0) {
controllersByAirport[airport] = [{
callsign: ctr.callsign,
frequency: ctr.frequency,
facility: ctr.facility,
facilityLong: "Centre (Top-down)",
airport: airport
}];
}
});
});
return NextResponse.json(controllersByAirport);
} catch (error) {
console.error(error);
return NextResponse.json({ error: 'Failed to fetch controller data' }, { status: 500 });
}
}

View File

@ -10,7 +10,7 @@ export async function GET() {
headers: {
'Authorization': 'Bearer vmtkb1D8Tuva2Jw2tXihWcKE3m2sfDJkySBZygVx82I'
},
next: { revalidate: 60 } // Cache for 1 minutes
next: { revalidate: 300 } // Cache for 5 minutes
});
if (!response.ok) return null;

View File

@ -34,7 +34,7 @@ export async function GET(
.map((pilot) => ({
callsign: pilot.callsign,
type: pilot.flight_plan.departure === icao ? 'Departure' : 'Arrival',
time: pilot.flight_plan.departure === icao ? pilot.flight_plan.deptime : pilot.flight_plan.arrival,
time: pilot.flight_plan.departure === icao ? pilot.flight_plan.deptime : pilot.flight_plan.enroute_time,
route: pilot.flight_plan.route,
origin: pilot.flight_plan.departure,
destination: pilot.flight_plan.arrival

View File

@ -1,6 +1,6 @@
"use client";
import { useState, Fragment } from "react";
import { useState, Fragment, useEffect } from "react";
import {
Table,
TableBody,
@ -10,7 +10,14 @@ import {
TableRow,
} from "@/components/ui/table";
import { Skeleton } from "@/components/ui/skeleton";
import { ChevronDown, ChevronUp, Plane, PlaneLanding, PlaneTakeoff } from "lucide-react";
import { ChevronDown, ChevronUp, Plane, PlaneLanding, PlaneTakeoff, Radio } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
interface Flight {
callsign: string;
@ -21,6 +28,13 @@ interface Flight {
destination: string;
}
interface Controller {
callsign: string;
frequency: string;
facility: string;
facilityLong: string;
}
interface MetarTableProps {
data: any[];
loading: boolean;
@ -30,6 +44,25 @@ export function MetarTable({ data, loading }: MetarTableProps) {
const [expandedAirport, setExpandedAirport] = useState<string | null>(null);
const [flights, setFlights] = useState<Flight[]>([]);
const [loadingFlights, setLoadingFlights] = useState(false);
const [controllers, setControllers] = useState<Record<string, Controller[]>>({});
// Fetch controller data
useEffect(() => {
async function fetchControllers() {
try {
const response = await fetch('/api/controllers');
const data = await response.json();
setControllers(data);
} catch (error) {
console.error('Failed to fetch controller data:', error);
}
}
fetchControllers();
// Refresh controller data every minute
const interval = setInterval(fetchControllers, 60000);
return () => clearInterval(interval);
}, []);
const handleAirportClick = async (airport: string) => {
if (expandedAirport === airport) {
@ -50,6 +83,19 @@ export function MetarTable({ data, loading }: MetarTableProps) {
}
};
const getBadgeVariant = (facility: string) => {
switch (facility) {
case 'CTR':
return "secondary";
case 'APP':
return "default";
case 'TWR':
return "default";
default:
return "outline";
}
};
if (loading) {
return <LoadingSkeleton />;
}
@ -65,6 +111,7 @@ export function MetarTable({ data, loading }: MetarTableProps) {
<TableHead>Headwind (kts)</TableHead>
<TableHead>Xwind (kts)</TableHead>
<TableHead>Altimeter</TableHead>
<TableHead>ATC</TableHead>
<TableHead>METAR</TableHead>
</TableRow>
</TableHeader>
@ -95,11 +142,38 @@ export function MetarTable({ data, loading }: MetarTableProps) {
{row.crosswind}
</TableCell>
<TableCell>{row.altimeter}</TableCell>
<TableCell>
{controllers[row.airport] ? (
<div className="flex flex-col gap-1">
<TooltipProvider>
{controllers[row.airport].map((controller, i) => (
<Tooltip key={i}>
<TooltipTrigger>
<Badge
variant={getBadgeVariant(controller.facility)}
className="flex items-center gap-1 w-fit"
>
<Radio className="h-3 w-3" />
{controller.facility} {controller.frequency}
</Badge>
</TooltipTrigger>
<TooltipContent>
<p>{controller.facilityLong}</p>
<p className="text-xs text-muted-foreground">{controller.callsign}</p>
</TooltipContent>
</Tooltip>
))}
</TooltipProvider>
</div>
) : (
<span className="text-muted-foreground text-sm">Uncontrolled</span>
)}
</TableCell>
<TableCell className="font-mono text-sm">{row.metar}</TableCell>
</TableRow>
{expandedAirport === row.airport && (
<TableRow>
<TableCell colSpan={8} className="p-0 bg-muted/50">
<TableCell colSpan={9} className="p-0 bg-muted/50">
{loadingFlights ? (
<div className="p-4 space-y-2">
<Skeleton className="h-8 w-full" />
@ -170,6 +244,7 @@ function LoadingSkeleton() {
<Skeleton className="h-4 w-[80px]" />
<Skeleton className="h-4 w-[80px]" />
<Skeleton className="h-4 w-[80px]" />
<Skeleton className="h-4 w-[120px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
))}

View File

@ -36,7 +36,7 @@ export function getUTCtime() {
const now = new Date();
return `${String(now.getUTCHours()).padStart(2, "0")}:${String(
now.getUTCMinutes()
).padStart(2, "0")} UTC`;
).padStart(2, "0")}Z`;
}
export function calculateAirportData(airportCode: string, data: any) {