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;
|
2019-08-09 02:52:34 +08:00
|
|
|
use App\Support\HttpClient;
|
2020-02-29 11:50:41 +08:00
|
|
|
use function count;
|
|
|
|
use Exception;
|
2019-08-08 01:25:40 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-04-03 11:35:25 +08:00
|
|
|
|
|
|
|
/**
|
2021-02-20 01:45:39 +08:00
|
|
|
* Return the raw METAR/TAF string from the NOAA Aviation Weather Service
|
2018-04-03 11:35:25 +08:00
|
|
|
*/
|
|
|
|
class AviationWeather extends Metar
|
|
|
|
{
|
2018-04-03 11:44:31 +08:00
|
|
|
private const METAR_URL =
|
2020-06-04 18:30:35 +08:00
|
|
|
'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=';
|
2018-04-03 11:35:25 +08:00
|
|
|
|
2021-02-20 01:45:39 +08:00
|
|
|
private const TAF_URL =
|
|
|
|
'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=tafs&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=';
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
/**
|
|
|
|
* @var HttpClient
|
|
|
|
*/
|
|
|
|
private HttpClient $httpClient;
|
2019-08-09 02:52:34 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
/**
|
|
|
|
* @param HttpClient $httpClient
|
|
|
|
*/
|
2019-08-09 02:52:34 +08:00
|
|
|
public function __construct(HttpClient $httpClient)
|
|
|
|
{
|
|
|
|
$this->httpClient = $httpClient;
|
|
|
|
}
|
|
|
|
|
2018-04-03 11:35:25 +08:00
|
|
|
/**
|
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
|
|
|
*
|
2019-08-09 02:52:34 +08:00
|
|
|
* @throws \Exception
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
2018-04-03 11:44:31 +08:00
|
|
|
* @return string
|
2018-04-03 11:35:25 +08:00
|
|
|
*/
|
2021-02-20 01:45:39 +08:00
|
|
|
protected function get_metar($icao): string
|
2018-04-03 11:35:25 +08:00
|
|
|
{
|
2019-08-09 05:41:53 +08:00
|
|
|
if ($icao === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2019-08-09 02:52:34 +08:00
|
|
|
$url = static::METAR_URL.$icao;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$res = $this->httpClient->get($url, []);
|
|
|
|
$xml = simplexml_load_string($res);
|
2020-02-29 11:50:41 +08:00
|
|
|
|
2021-05-03 22:00:10 +08:00
|
|
|
if ($xml->errors && count($xml->errors->children()) > 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-02-29 11:50:41 +08:00
|
|
|
$attrs = $xml->data->attributes();
|
|
|
|
if (!isset($attrs['num_results'])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_results = $attrs['num_results'];
|
|
|
|
if (empty($num_results)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$num_results = (int) $num_results;
|
|
|
|
if ($num_results === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($xml->data->METAR->raw_text) === 0) {
|
2019-08-09 02:52:34 +08:00
|
|
|
return '';
|
2018-05-15 00:20:38 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 02:52:34 +08:00
|
|
|
return $xml->data->METAR->raw_text->__toString();
|
2020-02-29 11:50:41 +08:00
|
|
|
} catch (Exception $e) {
|
2019-08-09 02:52:34 +08:00
|
|
|
Log::error('Error reading METAR: '.$e->getMessage());
|
2021-02-20 01:45:39 +08:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the actual retrieval of the TAF
|
|
|
|
*
|
|
|
|
* @param $icao
|
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_taf($icao): string
|
|
|
|
{
|
|
|
|
if ($icao === '') {
|
|
|
|
return '';
|
|
|
|
}
|
2019-08-09 02:52:34 +08:00
|
|
|
|
2021-02-20 01:45:39 +08:00
|
|
|
$tafurl = static::TAF_URL.$icao;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tafres = $this->httpClient->get($tafurl, []);
|
|
|
|
$tafxml = simplexml_load_string($tafres);
|
|
|
|
|
|
|
|
$tafattrs = $tafxml->data->attributes();
|
|
|
|
if (!isset($tafattrs['num_results'])) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$tafnum_results = $tafattrs['num_results'];
|
|
|
|
if (empty($tafnum_results)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$tafnum_results = (int) $tafnum_results;
|
|
|
|
if ($tafnum_results === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($tafxml->data->TAF->raw_text) === 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tafxml->data->TAF->raw_text->__toString();
|
|
|
|
} catch (Exception $e) {
|
|
|
|
Log::error('Error reading TAF: '.$e->getMessage());
|
2020-02-29 11:50:41 +08:00
|
|
|
return '';
|
2019-08-09 02:52:34 +08:00
|
|
|
}
|
2018-04-03 11:35:25 +08:00
|
|
|
}
|
|
|
|
}
|