Skip to content

Commit

Permalink
implement nool vdgs interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dotFionn committed Apr 6, 2024
1 parent e0fb7e0 commit fbed7a6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/backend/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { PluginTokenModule } from './plugin-token/plugin-token.module';
import { agendaProviders } from './schedule.module';
import { UserModule } from './user/user.module';
import { UtilsModule } from './utils/utils.module';
import { VdgsModule } from './vdgs/vdgs.module';

const { frontendProxy } = getAppConfig();

Expand Down Expand Up @@ -52,6 +53,7 @@ const { frontendProxy } = getAppConfig();
AuthModule,
ConfigModule,
PluginTokenModule,
VdgsModule,
],
providers: [...databaseProviders, ...agendaProviders],
exports: [...databaseProviders],
Expand Down
5 changes: 5 additions & 0 deletions src/backend/utils/utils.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from 'node:crypto';
import fs from 'node:fs';

import { Injectable } from '@nestjs/common';
import dayjs from 'dayjs';
import peggy, { Parser } from 'peggy';

import logger from '../logger';
Expand Down Expand Up @@ -161,4 +162,8 @@ export class UtilsService {

return peggy.generate(newFilter);
}

formatDateForHeader(date: Date | void): string {
return dayjs(date ?? new Date()).toString();
}
}
56 changes: 56 additions & 0 deletions src/backend/vdgs/vdgs.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Controller, Get, Param, Res } from '@nestjs/common';
import dayjs from 'dayjs';
import { Response } from 'express';

import { AirportService } from '../airport/airport.service';
import getAppConfig from '../config';
import { PilotService } from '../pilot/pilot.service';
import { UtilsService } from '../utils/utils.service';

const { publicUrl } = getAppConfig();

@Controller('/api/vdgs')
export class VdgsController {
constructor(
private airportService: AirportService,
private pilotService: PilotService,
private utilsService: UtilsService,
) {}

private setDateHeaders(res: Response, date: Date | void) {
res.setHeader('Date', this.utilsService.formatDateForHeader());
res.setHeader('Last-Modified', this.utilsService.formatDateForHeader(date));
}

@Get('/nool')
async getNoolAirportList(@Res({ passthrough: true }) res: Response) {
const airports = await this.airportService.getAllAirports();

this.setDateHeaders(res);

return {
version: 1,
airports: Object.fromEntries(airports.map(a => ([a.icao, [`${publicUrl}/api/vdgs/nool/${a.icao}`]]))),
};
}

@Get('/nool/:icao')
async getNoolAirport(@Res({ passthrough: true }) res: Response, @Param('icao') icao: string) {
const flights = await this.pilotService.getPilots({ 'flightplan.adep': icao });

this.setDateHeaders(res);

return {
version: 1,
flights: flights.map(flight => ({
lat: flight.position.lat,
lon: flight.position.lon,
callsign: flight.callsign,
tobt: dayjs(flight.vacdm.tobt).format('HHmm'),
tsat: dayjs(flight.vacdm.tsat).format('HHmm'),
runway: flight.clearance.dep_rwy,
sid: flight.clearance.sid,
})),
};
}
}
17 changes: 17 additions & 0 deletions src/backend/vdgs/vdgs.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Module } from '@nestjs/common';

import { AirportModule } from '../airport/airport.module';
import { PilotModule } from '../pilot/pilot.module';
import { UtilsModule } from '../utils/utils.module';

import { VdgsController } from './vdgs.controller';

@Module({
controllers: [VdgsController],
imports: [
AirportModule,
PilotModule,
UtilsModule,
],
})
export class VdgsModule {}

0 comments on commit fbed7a6

Please sign in to comment.