phpvms/app/Contracts/Metar.php
B.Fatih KOZ b4d5c0fbcd
Weather METAR/TAF enhancements (#964)
* Update AviationWeather.php

Added the ability to fetch latest TAF report of given icao from ADDS/NOAA

* Update Weather.php

Used updated Metar\AviationWeather service to improve the widget ability and provide raw TAF data for the view

* Style Fix 1

* Update weather.blade.php

Updated blade to match updated Metar\AviationWeather service and the Weather widget controller.

Also fixed the order of temp - dewpoint - humidity - visibility display according to aviation usage.

Widget now displays raw TAF data just below raw METAR data.

* Update Metar inferface and wrap TAF retrieval in cache

* Styles fix

* Add call to getTaf. Don't call AviationWeather directly

* Fix cache lookup strings

* Fix recursion error

* Update weather.blade.php

Used latest weather.blade , added $taf['raw'] after raw metar.

* Compatibility Update

Updated the widget controller to match latest dev (added raw_only to config)

* Update Weather Widget Blade

Made the widget blade compatible with the TAF reports, widget will display raw metar and taf after the decoder, if no metar or taf recieved it will be displayed in its own row. Also added the new option of raw_only to blade, if it is true, metar decoding will be skipped and only raw values will be displayed. ( Useful when displaying multiple wx widgets at the same page for departure, destination and alternate etc )

Co-authored-by: Nabeel Shahzad <nshahzad@live.com>
Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
2021-02-19 12:45:39 -05:00

100 lines
2.2 KiB
PHP

<?php
namespace App\Contracts;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
/**
* Base class for implementing retrieving METARs
*/
abstract class Metar
{
/**
* Implement retrieving the METAR - return the METAR string. Needs to be protected,
* since this shouldn't be directly called. Call `metar($icao)`. If not implemented,
* return a blank string
*
* @param $icao
*
* @return mixed
*/
abstract protected function get_metar($icao): string;
/**
* Implement retrieving the TAF - return the string. Call `taf($icao)`. If not implemented,
* return a blank string
*
* @param $icao
*
* @return mixed
*/
abstract protected function get_taf($icao): string;
/**
* Download the METAR, wrap in caching
*
* @param $icao
*
* @return string
*/
public function metar($icao): string
{
$cache = config('cache.keys.METAR_WEATHER_LOOKUP');
$key = $cache['key'].$icao;
if (Cache::has($key)) {
$raw_metar = Cache::get($key);
if ($raw_metar !== '') {
return $raw_metar;
}
}
try {
$raw_metar = $this->get_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;
}
/**
* Download the TAF, wrap in caching
*
* @param $icao
*
* @return string
*/
public function taf($icao): string
{
$cache = config('cache.keys.TAF_WEATHER_LOOKUP');
$key = $cache['key'].$icao;
if (Cache::has($key)) {
$taf = Cache::get($key);
if ($taf !== '') {
return $taf;
}
}
try {
$taf = $this->get_taf($icao);
} catch (\Exception $e) {
Log::error('Error getting TAF: '.$e->getMessage(), $e->getTrace());
return '';
}
if ($taf !== '') {
Cache::put($key, $taf, $cache['time']);
}
return $taf;
}
}