Fixes in how METARs are parsed

This commit is contained in:
Nabeel Shahzad 2018-04-04 09:04:14 -05:00
parent 7a3a2f3e9a
commit 052813ba4a
4 changed files with 29 additions and 9 deletions

View File

@ -39,6 +39,9 @@ abstract class Metar
$raw_metar = Cache::remember($key, $cache['time'], function () use ($icao) { $raw_metar = Cache::remember($key, $cache['time'], function () use ($icao) {
try { try {
return $this->metar($icao); return $this->metar($icao);
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace());
return '';
} catch (\Exception $e) { } catch (\Exception $e) {
Log::error('Error getting METAR: '. $e->getMessage(), $e->getTrace()); Log::error('Error getting METAR: '. $e->getMessage(), $e->getTrace());
return ''; return '';

View File

@ -20,12 +20,13 @@ class AviationWeather extends Metar
* Implement the METAR - Return the string * Implement the METAR - Return the string
* @param $icao * @param $icao
* @return string * @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/ */
protected function metar($icao): string protected function metar($icao): string
{ {
$url = static::METAR_URL.$icao; $url = static::METAR_URL.$icao;
$res = Http::get($url, []); $res = Http::get($url, []);
$xml = simplexml_load_string($res); $xml = simplexml_load_string($res);
return $xml->data->METAR->raw_text; return $xml->data->METAR->raw_text->__toString();
} }
} }

View File

@ -16,6 +16,7 @@ class Http
* @param $uri * @param $uri
* @param array $opts * @param array $opts
* @return string * @return string
* @throws \GuzzleHttp\Exception\GuzzleException
*/ */
public static function get($uri, array $opts) public static function get($uri, array $opts)
{ {

View File

@ -31,9 +31,18 @@ class Weather extends Widget
protected function determineCategory($metar): string protected function determineCategory($metar): string
{ {
$category = 'VFR'; $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(); $ceiling = $metar->getClouds();
if ($ceiling) { if ($ceiling && count($ceiling) > 0) {
$ceiling = $ceiling[0]->getBaseHeight()->getValue(); $ceiling = $ceiling[0]->getBaseHeight()->getValue();
} else { } else {
$ceiling = 1000; $ceiling = 1000;
@ -58,13 +67,19 @@ class Weather extends Widget
$metar_class = new $klass; $metar_class = new $klass;
$raw_metar = $metar_class->get_metar($this->config['icao']); $raw_metar = $metar_class->get_metar($this->config['icao']);
if(!$raw_metar || $raw_metar === '') {
$category = null;
$metar = null;
} else {
// Run through this parser // Run through this parser
$decoder = new MetarDecoder(); $decoder = new MetarDecoder();
$metar = $decoder->parse($raw_metar); $metar = $decoder->parse($raw_metar);
// Determine the flight category that's allowed // Determine the flight category that's allowed
// Just check if we need to be under IFR conditions // Just check if we need to be under IFR conditions
$category = $this->determineCategory($metar); $category = $this->determineCategory($metar);
}
return view('widgets.weather', [ return view('widgets.weather', [
'config' => $this->config, 'config' => $this->config,