Skip to content

Commit

Permalink
Merge pull request #144 from flybywiresim/mrc-aviationweather
Browse files Browse the repository at this point in the history
feat: aviationweather.gov metar + fix taf
  • Loading branch information
tracernz authored Oct 25, 2023
2 parents 88eed29 + 2391b95 commit d89ff95
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
16 changes: 1 addition & 15 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"class-validator": "^0.12.2",
"elastic-apm-node": "^3.9.0",
"express-rate-limit": "^5.1.3",
"fast-xml-parser": "^3.17.4",
"helmet": "^4.1.1",
"iconv-lite": "^0.6.2",
"mysql": "^2.18.1",
Expand Down
2 changes: 1 addition & 1 deletion src/metar/metar.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MetarController {
description: 'The source for the METAR',
example: 'vatsim',
required: false,
enum: ['vatsim', 'ms', 'ivao', 'pilotedge'],
enum: ['vatsim', 'ms', 'ivao', 'pilotedge', 'aviationweather'],
})
@ApiOkResponse({ description: 'METAR notice was found', type: Metar })
@ApiNotFoundResponse({ description: 'METAR not available for ICAO' })
Expand Down
24 changes: 24 additions & 0 deletions src/metar/metar.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class MetarService {
return this.handleMs(icaoCode);
case 'pilotedge':
return this.handlePilotEdge(icaoCode).toPromise();
case 'aviationweather':
return this.handleAviationWeather(icaoCode).toPromise();
}
}

Expand Down Expand Up @@ -117,6 +119,28 @@ export class MetarService {
);
}

// AviationWeather
private handleAviationWeather(icao: string): Observable<Metar> {
return this.http.get<string>(`https://aviationweather.gov/api/data/metar?hours=0&ids=${icao}`, { responseType: 'text' })
.pipe(
tap((response) => this.logger.debug(`Response status ${response.status} for AviationWeather METAR request`)),
map((response) => {
const metars = response.data.replace(/[\s$]+$/g, '').split('\n');

return ({
source: 'AviationWeather',
icao,
metar: metars.find((x) => x.startsWith(icao)).toUpperCase()
});
}),
catchError(
(err) => {
throw this.generateNotAvailableException(err, icao);
},
),
);
}

private generateNotAvailableException(err: any, icao: string): HttpException {
const exception = new HttpException(`METAR not available for ICAO: ${icao}`, 404);
this.logger.error(err);
Expand Down
11 changes: 5 additions & 6 deletions src/taf/taf.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpException, HttpService, Injectable, Logger } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import * as parser from 'fast-xml-parser';
import { Taf } from './taf.class';
import { CacheService } from '../cache/cache.service';

Expand Down Expand Up @@ -33,19 +32,19 @@ export class TafService {

// AviationWeather
private handleAviationWeather(icao: string): Observable<Taf> {
return this.http.get<any>(
'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs'
+ `&requestType=retrieve&format=xml&stationString=${icao}&hoursBeforeNow=0`,
return this.http.get<string>(
`https://aviationweather.gov/api/data/taf?ids=${icao}`,
{ responseType: 'text' },
)
.pipe(
tap((response) => this.logger.debug(`Response status ${response.status} for AviationWeather TAF request`)),
map((response) => {
const obj = parser.parse(response.data);
const tafs = response.data.replace(/\s\s+/g, ' ').trimEnd().split('\n');

return {
source: 'AviationWeather',
icao,
taf: obj.response.data.TAF[0].raw_text.toUpperCase(),
taf: tafs.find((x) => x.startsWith(icao)).toUpperCase(),
};
}),
catchError(
Expand Down

0 comments on commit d89ff95

Please sign in to comment.