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

Datalink input data #135

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from
Draft
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
6 changes: 4 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import { AirportModule } from './airport/airport.module';
import { GitVersionsModule } from './git-versions/git-versions.module';
import { ChartsModule } from './charts/charts.module';
import { AtcController } from './atc/atc.controller';
import { PilotsController } from './pilots/pilots.controller';
import { VatsimService } from './utilities/vatsim.service';
import { AtcService } from './atc/atc.service';
import { IvaoService } from './utilities/ivao.service';
import { GnssModule } from './gnss/gnss.module';
import { SatellitesModule } from './satellites/satellites.module';
import { CpdlcController } from './cpdlc/cpdlc.controller';
import { CpdlcService } from './cpdlc/cpdlc.service';
import { NotFoundExceptionFilter } from './utilities/not-found.filter';
Expand Down Expand Up @@ -116,14 +117,15 @@ import { NotFoundExceptionFilter } from './utilities/not-found.filter';
AirportModule,
GitVersionsModule,
ChartsModule,
GnssModule,
SatellitesModule,
],
controllers: [
AppController,
MetarController,
AtisController,
TafController,
AtcController,
PilotsController,
CpdlcController,
],
providers: [
Expand Down
10 changes: 0 additions & 10 deletions src/gnss/gnss.module.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/pilots/pilots-info.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';

export class PilotsInfo {
@ApiProperty({ description: 'The atc callsign', example: 'EBBR_TWR' })
callsign: string;

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

@ApiProperty({ description: 'The atc latitude', example: 32.08420727935125 })
latitude: number;

@ApiProperty({ description: 'The atc longitude', example: -81.14929543157402 })
longitude: number;
}
42 changes: 42 additions & 0 deletions src/pilots/pilots.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
CacheInterceptor,
CacheTTL,
Controller,
Get,
Query,
UseInterceptors,
} from '@nestjs/common';
import {
ApiOkResponse,
ApiQuery,
ApiTags,
} from '@nestjs/swagger';
import { PilotsInfo } from './pilots-info.class';
import { PilotsService } from './pilots.service';

@ApiTags('Pilots')
@Controller('api/v1/pilots')
@UseInterceptors(CacheInterceptor)
export class PilotsController {
constructor(private pilotsService: PilotsService) {}

@Get('')
@CacheTTL(120)
@ApiQuery({
name: 'source',
description: 'The source for the atcs',
example: 'vatsim',
required: true,
enum: ['vatsim', 'ivao'],
})
@ApiOkResponse({ description: 'list of connected pilots', type: [PilotsInfo] })
async getPilots(@Query('source') source?: string): Promise<PilotsInfo[]> {
if (source === 'vatsim') {
return this.pilotsService.getVatsimPilots();
}
if (source === 'ivao') {
return this.pilotsService.getIvaoPilots();
}
return null;
}
}
110 changes: 110 additions & 0 deletions src/pilots/pilots.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { Injectable } from '@nestjs/common';
import { PilotsInfo } from './pilots-info.class';
import { VatsimService } from '../utilities/vatsim.service';
import { IvaoService } from '../utilities/ivao.service';

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

public async getVatsimPilots(): Promise<PilotsInfo[]> {
const data = await this.vatsimService.fetchVatsimData();
const transceivers = await this.vatsimService.fetchVatsimTransceivers();

const arr: PilotsInfo[] = [];
for (const c of [...data.controllers, ...data.atis]) {
if (this.callsignIsPilot(c.callsign)) {
const trans = transceivers.find((t) => t.callsign === c.callsign);
const position = this.getCenterOfCoordinates(trans?.transceivers);
const altitude = this.getAltitude(trans?.transceivers);

if (altitude && position) {
arr.push({
callsign: c.callsign,
altitude: this.getAltitude(trans?.transceivers),
latitude: position[0],
longitude: position[1],
});
}
}
}

return arr;
}

public async getIvaoPilots(): Promise<PilotsInfo[]> {
const { clients: { pilots } } = await this.ivaoService.fetchIvaoData();

return pilots.map((pilot) => ({
callsign: pilot.callsign,
altitude: pilot.lastTrack.altitude,
latitude: pilot.lastTrack.latitude,
longitude: pilot.lastTrack.longitude,
}));
}

public callsignIsPilot(callsign: string): boolean {
switch (callsign.split('_').reverse()[0]) {
case 'CTR':
case 'DEL':
case 'GND':
case 'DEP':
case 'TWR':
case 'APP':
case 'ATIS':
case 'OBS':
return false;
default:
return true;
}
}

public getAltitude(array: any[]) {
if (array && array.length > 0 && array[0].heightAglM) {
let altInString: string = array[0].heightAglM.toString();
return Math.round(parseFloat(altInString));
}
return null;
}

public getCenterOfCoordinates(array: any[]) {
if (!array) {
return null;
}

const numCoords = array.length;
if (numCoords === 1) {
return [array[0].latDeg, array[0].lonDeg];
}

let X = 0.0;
let Y = 0.0;
let Z = 0.0;

for (let i = 0; i < numCoords; i++) {
const lat = (array[i].latDeg * Math.PI) / 180;
const lon = (array[i].lonDeg * Math.PI) / 180;
const a = Math.cos(lat) * Math.cos(lon);
const b = Math.cos(lat) * Math.sin(lon);
const c = Math.sin(lat);

X += a;
Y += b;
Z += c;
}

X /= numCoords;
Y /= numCoords;
Z /= numCoords;

const lon = Math.atan2(Y, X);
const hyp = Math.sqrt(X * X + Y * Y);
const lat = Math.atan2(Z, hyp);

const finalLat = (lat * 180) / Math.PI;
const finalLng = (lon * 180) / Math.PI;

return [finalLat, finalLng];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { CacheInterceptor, CacheTTL, Controller, Get, Logger, UseInterceptors } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Cron } from '@nestjs/schedule';
import { GnssService } from './gnss.service';
import { SatellitesService } from './satellites.service';
import { SatelliteInfo } from './dto/satellite-info.dto';
import { CacheService } from '../cache/cache.service';

@ApiTags('GNSS')
@Controller('api/v1/gnss')
@ApiTags('Satellites')
@Controller('api/v1/satellites')
@UseInterceptors(CacheInterceptor)
export class GnssController {
private readonly logger = new Logger(GnssController.name);
export class SatellitesController {
private readonly logger = new Logger(SatellitesController.name);

constructor(
private service: GnssService,
private service: SatellitesService,
private cache: CacheService,
) {}

@Get()
@Get('gnss')
@CacheTTL(3600) // 1h
@ApiOkResponse({ description: 'Satellite data for the GNSS constellation', type: [SatelliteInfo] })
getGnssInfo(): Observable<SatelliteInfo[]> {
return this.service.getGnssInfo();
}

@Get('iridium')
@CacheTTL(3600) // 1h
@ApiOkResponse({ description: 'Satellite data for the IRIDIUM constellation', type: [SatelliteInfo] })
getIridiumInfo(): Observable<SatelliteInfo[]> {
return this.service.getIridiumInfo();
}

@Cron('0 1 * * *')
async clearCache() {
try {
Expand Down
10 changes: 10 additions & 0 deletions src/satellites/satellites.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HttpModule, Module } from '@nestjs/common';
import { SatellitesService } from './satellites.service';
import { SatellitesController } from './satellites.controller';

@Module({
imports: [HttpModule],
providers: [SatellitesService],
controllers: [SatellitesController],
})
export class SatellitesModule {}
16 changes: 12 additions & 4 deletions src/gnss/gnss.service.ts → src/satellites/satellites.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { Observable } from 'rxjs';
import { SatelliteInfo } from './dto/satellite-info.dto';

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

constructor(private readonly http: HttpService) {
}

public getGnssInfo(): Observable<SatelliteInfo[]> {
return this.http.get<any>('https://celestrak.com/NORAD/elements/gp.php?GROUP=gnss&FORMAT=json')
private getSatellitesInfo(group: string): Observable<SatelliteInfo[]> {
return this.http.get<any>(`https://celestrak.com/NORAD/elements/gp.php?GROUP=${group}&FORMAT=json`)
.pipe(
tap((response) => this.logger.debug(`Response status ${response.status} for Celestrak request`)),
map((response) => response.data),
Expand All @@ -36,4 +36,12 @@ export class GnssService {
}))),
);
}

public getGnssInfo(): Observable<SatelliteInfo[]> {
return this.getSatellitesInfo('gnss');
}

public getIridiumInfo(): Observable<SatelliteInfo[]> {
return this.getSatellitesInfo('iridium');
}
}