2018-04-03 11:35:25 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services\Metar;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Metar;
|
2018-04-03 11:35:25 +08:00
|
|
|
use App\Support\Http;
|
2018-05-15 00:20:38 +08:00
|
|
|
use Cache;
|
2018-04-03 11:35:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the raw METAR string from the NOAA Aviation Weather Service
|
|
|
|
*/
|
|
|
|
class AviationWeather extends Metar
|
|
|
|
{
|
2018-04-03 11:44:31 +08:00
|
|
|
private const METAR_URL =
|
2018-04-03 11:35:25 +08:00
|
|
|
'https://www.aviationweather.gov/adds/dataserver_current/httpparam?'
|
|
|
|
.'dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3'
|
|
|
|
.'&mostRecent=true&fields=raw_text&stationString=';
|
|
|
|
|
|
|
|
/**
|
2018-04-03 11:44:31 +08:00
|
|
|
* Implement the METAR - Return the 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:44:31 +08:00
|
|
|
* @return string
|
2018-04-03 11:35:25 +08:00
|
|
|
*/
|
2018-04-03 11:44:31 +08:00
|
|
|
protected function metar($icao): string
|
2018-04-03 11:35:25 +08:00
|
|
|
{
|
2018-05-15 00:20:38 +08:00
|
|
|
$metar = Cache::remember(
|
|
|
|
config('cache.keys.WEATHER_LOOKUP.key').$icao,
|
|
|
|
config('cache.keys.WEATHER_LOOKUP.time'),
|
|
|
|
function () use ($icao) {
|
|
|
|
$url = static::METAR_URL.$icao;
|
2018-08-27 00:40:04 +08:00
|
|
|
|
2018-05-15 00:20:38 +08:00
|
|
|
try {
|
|
|
|
$res = Http::get($url, []);
|
|
|
|
$xml = simplexml_load_string($res);
|
2018-08-27 02:50:08 +08:00
|
|
|
if (\count($xml->data->METAR->raw_text) === 0) {
|
2018-08-27 00:40:04 +08:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return $xml->data->METAR->raw_text->__toString();
|
2018-05-15 00:20:38 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $metar;
|
2018-04-03 11:35:25 +08:00
|
|
|
}
|
|
|
|
}
|