Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement poscon atc endpoint #136

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { GnssModule } from './gnss/gnss.module';
import { CpdlcController } from './cpdlc/cpdlc.controller';
import { CpdlcService } from './cpdlc/cpdlc.service';
import { NotFoundExceptionFilter } from './utilities/not-found.filter';
import { PosconService } from './utilities/poscon.service';

@Module({
imports: [
Expand Down Expand Up @@ -131,7 +132,7 @@ import { NotFoundExceptionFilter } from './utilities/not-found.filter';
provide: APP_FILTER,
useClass: NotFoundExceptionFilter,
},
MetarService, AtisService, TafService, VatsimService, IvaoService, AtcService, CpdlcService],
MetarService, AtisService, TafService, VatsimService, IvaoService, PosconService, AtcService, CpdlcService],
})
export class AppModule {
}
8 changes: 6 additions & 2 deletions src/atc/atc-info.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ export class ATCInfo {
@ApiProperty({ description: 'The atc callsign', example: 'EBBR_TWR' })
callsign: string;

telephony?: string;

@ApiProperty({ description: 'The atc frequency', example: '128.800' })
frequency: string;

vhfFreq?: string;

@ApiProperty({ description: 'The atc visual range', example: 150 })
visualRange: number;
visualRange?: number;

@ApiProperty({ description: 'The atc current ATIS', example: ['line 1', 'line2', 'line3'] })
textAtis: string[];
textAtis?: string[];

@ApiProperty({ description: 'The atc type', example: 'GND' })
type: AtcType;
Expand Down
6 changes: 5 additions & 1 deletion src/atc/atc.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class AtcController {
description: 'The source for the atcs',
example: 'vatsim',
required: true,
enum: ['vatsim', 'ivao'],
enum: ['vatsim', 'ivao', 'poscon'],
})
@ApiOkResponse({ description: 'list of connected atc', type: [ATCInfo] })
async getControllers(@Query('source') source?: string): Promise<ATCInfo[]> {
Expand All @@ -37,6 +37,10 @@ export class AtcController {
if (source === 'ivao') {
return this.atcService.getIvaoControllers();
}

if (source === 'poscon') {
return this.atcService.getPosconControllers();
}
return null;
}
}
24 changes: 22 additions & 2 deletions src/atc/atc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Injectable } from '@nestjs/common';
import { ATCInfo, AtcType } from './atc-info.class';
import { VatsimService } from '../utilities/vatsim.service';
import { IvaoService } from '../utilities/ivao.service';
import { PosconService } from '../utilities/poscon.service';

@Injectable()
export class AtcService {
constructor(private readonly vatsimService: VatsimService,
private readonly ivaoService: IvaoService) { }
constructor(
private readonly vatsimService: VatsimService,
private readonly ivaoService: IvaoService,
private readonly posconService: PosconService,
) {}

public async getVatsimControllers(): Promise<ATCInfo[]> {
const data = await this.vatsimService.fetchVatsimData();
Expand Down Expand Up @@ -60,6 +64,22 @@ export class AtcService {
}));
}

public async getPosconControllers(): Promise<ATCInfo[]> {
const data = await this.posconService.fetchPosconData();
const atc: ATCInfo[] = [];
data.atc.map((x): AtcType => {
const [latitude, longitude] = x.centerPoint;
return atc.push({
callsign: x.telephony,
frequency: x.vhfFreq,
type: this.callSignToAtcType(x.type),
latitude,
longitude,
});
});
return atc;
}

public callSignToAtcType(callsign: string): AtcType {
switch (callsign.split('_').reverse()[0]) {
case 'CTR': return AtcType.RADAR;
Expand Down
33 changes: 33 additions & 0 deletions src/utilities/poscon.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { HttpService, Injectable, Logger } from '@nestjs/common';
import { map, tap } from 'rxjs/operators';
import { CacheService } from '../cache/cache.service';

@Injectable()
export class PosconService {
private readonly logger = new Logger(PosconService.name);

constructor(
private http: HttpService,
private readonly cache: CacheService,
) {
}

public async fetchPosconData(): Promise<any> {
const cacheHit = await this.cache.get('/poscon/data');

if (cacheHit) {
this.logger.debug('Returning from cache');
return cacheHit;
}
const data = await this.http.get<any>('https://hqapi.poscon.net/online.json')
.pipe(
tap((response) => this.logger.debug(`Response status ${response.status} for POSCON request`)),
map((response) => response.data),
)
.toPromise();

this.cache.set('/poscon/data', data, 120)
.then();
return data;
}
}