From 5cafebe4d6cda280749281a1092507fa40c36124 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Thu, 8 Aug 2019 14:52:34 -0400 Subject: [PATCH] Fix/metar reading (#353) * Fix reading of the METAR information for AviationWeather. Fix the DI * StyleCI fixes --- app/Console/Commands/DevCommands.php | 15 +++++++++ app/Contracts/Metar.php | 39 ++++++++++----------- app/Providers/WeatherServiceProvider.php | 17 ++++++++++ app/Services/AirportService.php | 41 ++++++++++++++++++++++ app/Services/Metar/AviationWeather.php | 43 +++++++++++++----------- app/Support/Http.php | 39 --------------------- app/Support/HttpClient.php | 4 +-- app/Support/Metar.php | 2 +- app/Widgets/Weather.php | 19 ++--------- config/app.php | 1 + tests/VersionTest.php | 1 - 11 files changed, 121 insertions(+), 100 deletions(-) create mode 100755 app/Providers/WeatherServiceProvider.php create mode 100644 app/Services/AirportService.php delete mode 100644 app/Support/Http.php diff --git a/app/Console/Commands/DevCommands.php b/app/Console/Commands/DevCommands.php index 8167dae9..201a108e 100644 --- a/app/Console/Commands/DevCommands.php +++ b/app/Console/Commands/DevCommands.php @@ -7,6 +7,7 @@ use App\Models\Acars; use App\Models\Airline; use App\Models\Pirep; use App\Models\User; +use App\Services\AirportService; use App\Services\AwardService; use App\Services\DatabaseService; use Illuminate\Database\QueryException; @@ -55,6 +56,7 @@ class DevCommands extends Command 'db-attrs' => 'dbAttrs', 'list-awards' => 'listAwardClasses', 'manual-insert' => 'manualInsert', + 'metar' => 'getMetar', 'reset-install' => 'resetInstall', 'xml-to-yaml' => 'xmlToYaml', ]; @@ -187,6 +189,19 @@ class DevCommands extends Command $this->info('Writing yaml to storage: '.$file_name); } + protected function getMetar(): void + { + $icao = $this->argument('param'); + if (!$icao) { + $this->error('Enter an ICAO!'); + exit(); + } + + $airportSvc = app(AirportService::class); + $metar = $airportSvc->getMetar($icao); + $this->info($metar->raw); + } + /** * Insert the rows from the file, manually advancing each row */ diff --git a/app/Contracts/Metar.php b/app/Contracts/Metar.php index d5a205cb..861aaae9 100644 --- a/app/Contracts/Metar.php +++ b/app/Contracts/Metar.php @@ -2,7 +2,6 @@ namespace App\Contracts; -use GuzzleHttp\Exception\GuzzleException; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Log; @@ -12,9 +11,8 @@ use Illuminate\Support\Facades\Log; abstract class Metar { /** - * Implement retrieving the METAR- Return the string - * Needs to be protected, since this shouldn't be - * directly called. Call `get_metar($icao)` instead + * Implement retrieving the METAR - return the METAR string. Needs to be protected, + * since this shouldn't be directly called. Call `get_metar($icao)` instead * * @param $icao * @@ -22,13 +20,6 @@ abstract class Metar */ abstract protected function metar($icao): string; - /** - * @param $icao - * - * @return string - */ - //abstract protected function taf($icao): string; - /** * Download the METAR, wrap in caching * @@ -41,17 +32,23 @@ abstract class Metar $cache = config('cache.keys.WEATHER_LOOKUP'); $key = $cache['key'].$icao; - $raw_metar = Cache::remember($key, $cache['time'], function () use ($icao) { - try { - return $this->metar($icao); - } catch (GuzzleException $e) { - Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace()); - return ''; - } catch (\Exception $e) { - Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace()); - return ''; + if (Cache::has($key)) { + $raw_metar = Cache::get($key); + if ($raw_metar !== '') { + return $raw_metar; } - }); + } + + try { + $raw_metar = $this->metar($icao); + } catch (\Exception $e) { + Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace()); + return ''; + } + + if ($raw_metar !== '') { + Cache::put($key, $raw_metar, $cache['time']); + } return $raw_metar; } diff --git a/app/Providers/WeatherServiceProvider.php b/app/Providers/WeatherServiceProvider.php new file mode 100755 index 00000000..3e84c531 --- /dev/null +++ b/app/Providers/WeatherServiceProvider.php @@ -0,0 +1,17 @@ +app->bind( + Metar::class, + config('phpvms.metar') + ); + } +} diff --git a/app/Services/AirportService.php b/app/Services/AirportService.php new file mode 100644 index 00000000..f8f092c2 --- /dev/null +++ b/app/Services/AirportService.php @@ -0,0 +1,41 @@ +metarProvider = $metarProvider; + } + + /** + * Return the METAR for a given airport + * + * @param $icao + * + * @return Metar|null + */ + public function getMetar($icao): Metar + { + $metar = null; + $wind = null; + $raw_metar = $this->metarProvider->get_metar($icao); + + if ($raw_metar && $raw_metar !== '') { + return new Metar($raw_metar); + } + + return null; + } +} diff --git a/app/Services/Metar/AviationWeather.php b/app/Services/Metar/AviationWeather.php index 31246098..19332750 100644 --- a/app/Services/Metar/AviationWeather.php +++ b/app/Services/Metar/AviationWeather.php @@ -3,8 +3,7 @@ namespace App\Services\Metar; use App\Contracts\Metar; -use App\Support\Http; -use Illuminate\Support\Facades\Cache; +use App\Support\HttpClient; use Illuminate\Support\Facades\Log; /** @@ -17,35 +16,39 @@ class AviationWeather extends Metar .'dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3' .'&mostRecent=true&fields=raw_text&stationString='; + private $httpClient; + + public function __construct(HttpClient $httpClient) + { + $this->httpClient = $httpClient; + } + /** * Implement the METAR - Return the string * * @param $icao * + * @throws \Exception + * @throws \GuzzleHttp\Exception\GuzzleException + * * @return string */ protected function metar($icao): string { - $metar = Cache::remember( - config('cache.keys.WEATHER_LOOKUP.key').$icao, - config('cache.keys.WEATHER_LOOKUP.time'), - function () use ($icao) { - $url = static::METAR_URL.$icao; + $url = static::METAR_URL.$icao; - try { - $res = Http::get($url, []); - $xml = simplexml_load_string($res); - if (\count($xml->data->METAR->raw_text) === 0) { - return ''; - } - return $xml->data->METAR->raw_text->__toString(); - } catch (\Exception $e) { - Log::error('Error reading METAR: '.$e->getMessage()); - return ''; - } + try { + $res = $this->httpClient->get($url, []); + $xml = simplexml_load_string($res); + if (\count($xml->data->METAR->raw_text) === 0) { + return ''; } - ); - return $metar; + return $xml->data->METAR->raw_text->__toString(); + } catch (\Exception $e) { + Log::error('Error reading METAR: '.$e->getMessage()); + + throw $e; + } } } diff --git a/app/Support/Http.php b/app/Support/Http.php deleted file mode 100644 index 0cfb6482..00000000 --- a/app/Support/Http.php +++ /dev/null @@ -1,39 +0,0 @@ - 2, // wait two seconds by default - ], $opts); - - $client = new Client(); - $responseSeederService = $client->request('GET', $uri, $opts); - - $body = $response->getBody()->getContents(); - if ($response->getHeader('content-type') === 'application/json') { - $body = \GuzzleHttp\json_decode($body); - } - - return $body; - } -} diff --git a/app/Support/HttpClient.php b/app/Support/HttpClient.php index cdd94686..cef76136 100644 --- a/app/Support/HttpClient.php +++ b/app/Support/HttpClient.php @@ -2,7 +2,7 @@ namespace App\Support; -use GuzzleHttp\Client; +use GuzzleHttp\Client as GuzzleClient; /** * Helper for HTTP stuff @@ -12,7 +12,7 @@ class HttpClient private $httpClient; public function __construct( - Client $httpClient + GuzzleClient $httpClient ) { $this->httpClient = $httpClient; } diff --git a/app/Support/Metar.php b/app/Support/Metar.php index 6fdb9087..c995eec0 100644 --- a/app/Support/Metar.php +++ b/app/Support/Metar.php @@ -310,7 +310,7 @@ class Metar implements \ArrayAccess /* * Other variables. */ - private $raw; + public $raw; private $raw_parts = []; diff --git a/app/Widgets/Weather.php b/app/Widgets/Weather.php index 45f34dac..1cf6491d 100644 --- a/app/Widgets/Weather.php +++ b/app/Widgets/Weather.php @@ -3,7 +3,7 @@ namespace App\Widgets; use App\Contracts\Widget; -use App\Support\Metar; +use App\Services\AirportService; /** * This is a widget for the 3rd party CheckWX service @@ -14,26 +14,13 @@ class Weather extends Widget 'icao' => null, ]; - public const URL = 'https://avwx.rest/api/metar/'; - /** * Attempt to get the data from the CheckWX API */ public function run() { - /** - * @var \App\Contracts\Metar - */ - $klass = config('phpvms.metar'); - $metar_class = new $klass(); - - $metar = null; - $wind = null; - $raw_metar = $metar_class->get_metar($this->config['icao']); - - if ($raw_metar && $raw_metar !== '') { - $metar = new Metar($raw_metar); - } + $airportSvc = app(AirportService::class); + $metar = $airportSvc->getMetar($this->config['icao']); return view('widgets.weather', [ 'config' => $this->config, diff --git a/config/app.php b/config/app.php index 7b469582..09574c41 100755 --- a/config/app.php +++ b/config/app.php @@ -87,6 +87,7 @@ return [ App\Providers\vaCentralServiceProvider::class, App\Providers\ExtendedTimezonelistProvider::class, App\Providers\MeasurementsProvider::class, + App\Providers\WeatherServiceProvider::class, ], 'aliases' => [ diff --git a/tests/VersionTest.php b/tests/VersionTest.php index 5b2d3715..95ed7d7c 100644 --- a/tests/VersionTest.php +++ b/tests/VersionTest.php @@ -24,7 +24,6 @@ class VersionTest extends TestCase '7.0.0-beta' => '7.0.0-alpha', '7.0.0-beta.1' => '7.0.0-beta', '7.0.0-beta.2' => '7.0.0-beta.1', - '7.0.0-beta.1' => '7.0.0-alpha', ]; $versionSvc = app(VersionService::class);