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

TLE data of satellites #139

Open
wants to merge 5 commits 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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { AtcController } from './atc/atc.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,7 +116,7 @@ import { NotFoundExceptionFilter } from './utilities/not-found.filter';
AirportModule,
GitVersionsModule,
ChartsModule,
GnssModule,
SatellitesModule,
],
controllers: [
AppController,
Expand Down
36 changes: 0 additions & 36 deletions src/gnss/gnss.controller.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/gnss/gnss.module.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/gnss/gnss.service.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ export class SatelliteInfo {

@ApiProperty()
meanMotionDdot: number;

@ApiProperty()
tleLineOne: string;

@ApiProperty()
tleLineTwo: string;
}
43 changes: 43 additions & 0 deletions src/satellites/satellites.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger';
import { CacheInterceptor, CacheTTL, Controller, Get, Logger, Query, UseInterceptors } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Cron } from '@nestjs/schedule';
import { SatellitesService } from './satellites.service';
import { SatelliteInfo } from './dto/satellite-info.dto';
import { CacheService } from '../cache/cache.service';

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

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

@Get()
@CacheTTL(3600) // 1h
@ApiQuery({
name: 'type',
description: 'The requested satellite type',
example: 'gnss',
required: true,
enum: ['gnss', 'iridium', 'iridium-NEXT', 'starlink', 'galileo', 'glo-ops', 'beidou', 'intelsat'],
})
@ApiOkResponse({ description: 'Satellite data for the requested type constellation', type: [SatelliteInfo] })
getGnssInfo(@Query('type') type: string): Observable<SatelliteInfo[]> {
return this.service.getSatellitesInfo(type);
}

@Cron('0 1 * * *')
async clearCache() {
try {
this.logger.log('Clearing GNSS cache');
await this.cache.del('/api/v1/satellites');
} catch (e) {
this.logger.error(e);
}
}
}
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 {}
62 changes: 62 additions & 0 deletions src/satellites/satellites.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { HttpService, Injectable, Logger } from '@nestjs/common';
import { map, tap } from 'rxjs/operators';
import { forkJoin, Observable } from 'rxjs';
import { SatelliteInfo } from './dto/satellite-info.dto';

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

constructor(private readonly http: HttpService) {
}

public getSatellitesInfo(type: string): Observable<SatelliteInfo[]> {
return forkJoin({
jsonResponse: this.http.get<any>(`https://celestrak.com/NORAD/elements/gp.php?GROUP=${type}&FORMAT=json`),
tleResponse: this.http.get<any>(`https://celestrak.com/NORAD/elements/gp.php?GROUP=${type}&FORMAT=tle`),
}).pipe(
tap((response) => this.logger.debug(`Response status ${response.jsonResponse.status} for Celestrak request`)),
tap((response) => this.logger.debug(`Response status ${response.tleResponse.status} for Celestrak request`)),
map((response) => [response.jsonResponse.data, response.tleResponse.data]),
map((data) => {
const satellites: SatelliteInfo[] = data[0].map((info) => ({
name: info.OBJECT_NAME,
id: info.OBJECT_ID,
epoch: new Date(info.EPOCH),
meanMotion: info.MEAN_MOTION,
eccentricity: info.ECCENTRICITY,
inclination: info.INCLINATION,
raOfAscNode: info.RA_OF_ASC_NODE,
argOfPericenter: info.ARG_OF_PERICENTER,
meanAnomaly: info.MEAN_ANOMALY,
ephemerisType: info.EPHEMERIS_TYPE,
classificationType: info.CLASSIFICATION_TYPE,
noradCatId: info.NORAD_CAT_ID,
elementSetNo: info.ELEMENT_SET_NO,
revAtEpoch: info.REV_AT_EPOCH,
bstar: info.BSTAR,
meanMotionDot: info.MEAN_MOTION_DOT,
meanMotionDdot: info.MEAN_MOTION_DDOT,
tleLineOne: '',
tleLineTwo: '',
}));

const tleData: string[] = data[1].split('\n');
for (let i = 0; i < tleData.length; i += 3) {
const name = tleData[i].trim();
if (name.length === 0) continue;

const idx = satellites.findIndex((satellite) => satellite.name === name);
if (idx > -1) {
satellites[idx].tleLineOne = tleData[i + 1].trim();
satellites[idx].tleLineTwo = tleData[i + 2].trim();
} else {
this.logger.warn(`Unable to find ${name}, ${tleData[i]}`);
}
}

return satellites;
}),
);
}
}