2018-04-03 11:35:25 +08:00
|
|
|
<?php
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
namespace App\Contracts;
|
2018-04-03 11:35:25 +08:00
|
|
|
|
2019-08-09 00:50:32 +08:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-04-03 11:44:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for implementing retrieving METARs
|
|
|
|
*/
|
2018-04-03 11:35:25 +08:00
|
|
|
abstract class Metar
|
|
|
|
{
|
|
|
|
/**
|
2019-08-09 02:52:34 +08:00
|
|
|
* Implement retrieving the METAR - return the METAR string. Needs to be protected,
|
2021-02-20 01:45:39 +08:00
|
|
|
* since this shouldn't be directly called. Call `metar($icao)`. If not implemented,
|
|
|
|
* return a blank string
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:35:25 +08:00
|
|
|
* @param $icao
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:35:25 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-02-20 01:45:39 +08:00
|
|
|
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;
|
2018-04-03 11:44:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the METAR, wrap in caching
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @param $icao
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2021-02-20 01:45:39 +08:00
|
|
|
public function metar($icao): string
|
2018-04-03 11:44:31 +08:00
|
|
|
{
|
2021-02-20 01:45:39 +08:00
|
|
|
$cache = config('cache.keys.METAR_WEATHER_LOOKUP');
|
2018-04-03 11:44:31 +08:00
|
|
|
$key = $cache['key'].$icao;
|
|
|
|
|
2019-08-09 02:52:34 +08:00
|
|
|
if (Cache::has($key)) {
|
|
|
|
$raw_metar = Cache::get($key);
|
|
|
|
if ($raw_metar !== '') {
|
|
|
|
return $raw_metar;
|
2018-04-03 11:44:31 +08:00
|
|
|
}
|
2019-08-09 02:52:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-02-20 01:45:39 +08:00
|
|
|
$raw_metar = $this->get_metar($icao);
|
2019-08-09 02:52:34 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error('Error getting METAR: '.$e->getMessage(), $e->getTrace());
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($raw_metar !== '') {
|
|
|
|
Cache::put($key, $raw_metar, $cache['time']);
|
|
|
|
}
|
2018-04-03 11:44:31 +08:00
|
|
|
|
|
|
|
return $raw_metar;
|
|
|
|
}
|
2021-02-20 01:45:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2018-04-03 11:35:25 +08:00
|
|
|
}
|