diff --git a/app/Interfaces/Metar.php b/app/Interfaces/Metar.php index 29d57d72..dc2ec109 100644 --- a/app/Interfaces/Metar.php +++ b/app/Interfaces/Metar.php @@ -39,6 +39,9 @@ abstract class Metar $raw_metar = Cache::remember($key, $cache['time'], function () use ($icao) { try { return $this->metar($icao); + } catch (\GuzzleHttp\Exception\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 ''; diff --git a/app/Services/Metar/AviationWeather.php b/app/Services/Metar/AviationWeather.php index fb7d5f7f..f3442cca 100644 --- a/app/Services/Metar/AviationWeather.php +++ b/app/Services/Metar/AviationWeather.php @@ -20,12 +20,13 @@ class AviationWeather extends Metar * Implement the METAR - Return the string * @param $icao * @return string + * @throws \GuzzleHttp\Exception\GuzzleException */ protected function metar($icao): string { $url = static::METAR_URL.$icao; $res = Http::get($url, []); $xml = simplexml_load_string($res); - return $xml->data->METAR->raw_text; + return $xml->data->METAR->raw_text->__toString(); } } diff --git a/app/Support/Http.php b/app/Support/Http.php index 5f07ab45..79d6fe05 100644 --- a/app/Support/Http.php +++ b/app/Support/Http.php @@ -16,6 +16,7 @@ class Http * @param $uri * @param array $opts * @return string + * @throws \GuzzleHttp\Exception\GuzzleException */ public static function get($uri, array $opts) { diff --git a/app/Widgets/Weather.php b/app/Widgets/Weather.php index 104bdb3d..e9e6e252 100644 --- a/app/Widgets/Weather.php +++ b/app/Widgets/Weather.php @@ -31,9 +31,18 @@ class Weather extends Widget protected function determineCategory($metar): string { $category = 'VFR'; - $visibility = $metar->getVisibility()->getVisibility()->getValue(); + + $visibility = 10; // assume it's ok and VFR + $vis = $metar->getVisibility(); + if($vis) { + $vis = $vis->getVisibility(); + if($vis) { + $visibility = $vis->getValue(); + } + } + $ceiling = $metar->getClouds(); - if ($ceiling) { + if ($ceiling && count($ceiling) > 0) { $ceiling = $ceiling[0]->getBaseHeight()->getValue(); } else { $ceiling = 1000; @@ -58,13 +67,19 @@ class Weather extends Widget $metar_class = new $klass; $raw_metar = $metar_class->get_metar($this->config['icao']); - // Run through this parser - $decoder = new MetarDecoder(); - $metar = $decoder->parse($raw_metar); + if(!$raw_metar || $raw_metar === '') { + $category = null; + $metar = null; + } else { + // Run through this parser + $decoder = new MetarDecoder(); + $metar = $decoder->parse($raw_metar); - // Determine the flight category that's allowed - // Just check if we need to be under IFR conditions - $category = $this->determineCategory($metar); + + // Determine the flight category that's allowed + // Just check if we need to be under IFR conditions + $category = $this->determineCategory($metar); + } return view('widgets.weather', [ 'config' => $this->config,