Add METAR interface and use local library for decoding
This commit is contained in:
13
app/Interfaces/Metar.php
Normal file
13
app/Interfaces/Metar.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
abstract class Metar
|
||||
{
|
||||
/**
|
||||
* Implement the METAR- Return the string
|
||||
* @param $icao
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function get($icao);
|
||||
}
|
||||
31
app/Services/Metar/AviationWeather.php
Normal file
31
app/Services/Metar/AviationWeather.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Metar;
|
||||
|
||||
use App\Interfaces\Metar;
|
||||
use App\Support\Http;
|
||||
|
||||
/**
|
||||
* Return the raw METAR string from the NOAA Aviation Weather Service
|
||||
* @package App\Services\Metar
|
||||
*/
|
||||
class AviationWeather extends Metar
|
||||
{
|
||||
private const URL =
|
||||
'https://www.aviationweather.gov/adds/dataserver_current/httpparam?'
|
||||
.'dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3'
|
||||
.'&mostRecent=true&fields=raw_text&stationString=';
|
||||
|
||||
/**
|
||||
* Implement the METAR- Return the string
|
||||
* @param $icao
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($icao)
|
||||
{
|
||||
$url = static::URL.$icao;
|
||||
$res = Http::get($url, []);
|
||||
$xml = simplexml_load_string($res);
|
||||
return $xml->data->METAR->raw_text;
|
||||
}
|
||||
}
|
||||
51
app/Support/Units/Temperature.php
Normal file
51
app/Support/Units/Temperature.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support\Units;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
|
||||
/**
|
||||
* Wrap the converter class
|
||||
* @package App\Support\Units
|
||||
*/
|
||||
class Temperature extends \PhpUnitsOfMeasure\PhysicalQuantity\Temperature implements Arrayable
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$unit = strtoupper(setting('units.temperature'));
|
||||
$value = $this->toUnit($unit);
|
||||
|
||||
return (string) round($value, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return value in native unit as integer
|
||||
* @return array
|
||||
*/
|
||||
public function toNumber()
|
||||
{
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the HTTP Resource call
|
||||
*/
|
||||
public function toObject()
|
||||
{
|
||||
return [
|
||||
'f' => round($this->toUnit('F'), 2),
|
||||
'c' => round($this->toUnit('C') / 1000, 2),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return $this->__toString();
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Widgets;
|
||||
|
||||
use App\Interfaces\Widget;
|
||||
use App\Support\Http;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
/**
|
||||
* This is a widget for the 3rd party CheckWX service
|
||||
* @package App\Widgets
|
||||
*/
|
||||
class CheckWx extends Widget
|
||||
{
|
||||
protected $config = [
|
||||
'icao' => null,
|
||||
];
|
||||
|
||||
/**
|
||||
* Attempt to get the data from the CheckWX API
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (!config('checkwx.api_key')) {
|
||||
$data = null;
|
||||
} else {
|
||||
// Cache the request so we don't need to repeatedly call out
|
||||
$cache = config('cache.keys.WEATHER_LOOKUP');
|
||||
$key = $cache['key'].$this->config['icao'];
|
||||
|
||||
$data = Cache::remember($key, $cache['time'], function () {
|
||||
$url = config('checkwx.url').'/metar/'.$this->config['icao'].'/decoded';
|
||||
$data = Http::get($url, [
|
||||
'headers' => [
|
||||
'X-API-Key' => config('checkwx.api_key'),
|
||||
'content-type' => 'application/json',
|
||||
]
|
||||
]);
|
||||
|
||||
$data = json_decode($data);
|
||||
return $data;
|
||||
});
|
||||
|
||||
if($data->results === 1) {
|
||||
$data = $data->data[0];
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
}
|
||||
|
||||
return view('widgets.check_wx', [
|
||||
'config' => $this->config,
|
||||
'data' => $data,
|
||||
'unit_alt' => setting('units.altitude'),
|
||||
'unit_dist' => setting('units.distance'),
|
||||
'unit_temp' => setting('units.temperature'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
84
app/Widgets/Weather.php
Normal file
84
app/Widgets/Weather.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace App\Widgets;
|
||||
|
||||
use App\Interfaces\Widget;
|
||||
use App\Support\Http;
|
||||
use App\Support\Units\Distance;
|
||||
use App\Support\Units\Temperature;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use MetarDecoder\MetarDecoder;
|
||||
use SimpleXMLElement;
|
||||
|
||||
/**
|
||||
* This is a widget for the 3rd party CheckWX service
|
||||
* @package App\Widgets
|
||||
*/
|
||||
class Weather extends Widget
|
||||
{
|
||||
protected $config = [
|
||||
'icao' => null,
|
||||
];
|
||||
|
||||
public const URL = 'https://avwx.rest/api/metar/';
|
||||
|
||||
/**
|
||||
* Determine the category depending on the rules for visibility/ceiling
|
||||
* https://www.aviationweather.gov/cva/help
|
||||
* @param $metar
|
||||
* @return string
|
||||
*/
|
||||
protected function determineCategory($metar): string
|
||||
{
|
||||
$category = 'VFR';
|
||||
$visibility = $metar->getVisibility()->getVisibility()->getValue();
|
||||
$ceiling = $metar->getClouds();
|
||||
if ($ceiling) {
|
||||
$ceiling = $ceiling[0]->getBaseHeight()->getValue();
|
||||
} else {
|
||||
$ceiling = 1000;
|
||||
}
|
||||
|
||||
if ($visibility < 3 || $ceiling < 1000) {
|
||||
$category = 'IFR';
|
||||
}
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to get the data from the CheckWX API
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// Cache the request so we don't need to repeatedly call out
|
||||
$cache = config('cache.keys.WEATHER_LOOKUP');
|
||||
$key = $cache['key'].$this->config['icao'];
|
||||
|
||||
$raw_metar = Cache::remember($key, $cache['time'], function () {
|
||||
/**
|
||||
* @var \App\Interfaces\Metar $klass
|
||||
*/
|
||||
$klass = config('phpvms.metar');
|
||||
$metar_class = new $klass;
|
||||
return $metar_class->get($this->config['icao']);
|
||||
});
|
||||
|
||||
// Run through this parser
|
||||
$decoder = new MetarDecoder();
|
||||
$metar = $decoder->parse($raw_metar);
|
||||
|
||||
// Determine the flight category that's allowed
|
||||
// Just check if we need to be under IFR conditions
|
||||
$category = $this->determineCategory($metar);
|
||||
|
||||
return view('widgets.weather', [
|
||||
'config' => $this->config,
|
||||
'category' => $category,
|
||||
'metar' => $metar,
|
||||
'unit_alt' => setting('units.altitude'),
|
||||
'unit_dist' => setting('units.distance'),
|
||||
'unit_temp' => setting('units.temperature'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user