* SimBrief integration #405 * Add briefing as API response; add acars_xml field #405
This commit is contained in:
@@ -239,6 +239,12 @@ class Flight extends Model
|
||||
return $this->hasMany(FlightFieldValue::class, 'flight_id');
|
||||
}
|
||||
|
||||
public function simbrief()
|
||||
{
|
||||
// id = key from table, flight_id = reference key
|
||||
return $this->belongsTo(SimBrief::class, 'id', 'flight_id');
|
||||
}
|
||||
|
||||
public function subfleets()
|
||||
{
|
||||
return $this->belongsToMany(Subfleet::class, 'flight_subfleet');
|
||||
|
||||
@@ -160,7 +160,7 @@ class Pirep extends Model
|
||||
*
|
||||
* @return \App\Models\Pirep
|
||||
*/
|
||||
public static function fromFlight(Flight $flight)
|
||||
public static function fromFlight(Flight $flight): self
|
||||
{
|
||||
return new self([
|
||||
'flight_id' => $flight->id,
|
||||
@@ -175,6 +175,28 @@ class Pirep extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PIREP from a SimBrief instance
|
||||
*
|
||||
* @param \App\Models\SimBrief $simBrief
|
||||
*
|
||||
* @return \App\Models\Pirep
|
||||
*/
|
||||
public static function fromSimBrief(SimBrief $simBrief): self
|
||||
{
|
||||
return new self([
|
||||
'flight_id' => $simBrief->flight->id,
|
||||
'airline_id' => $simBrief->flight->airline_id,
|
||||
'flight_number' => $simBrief->flight->flight_number,
|
||||
'route_code' => $simBrief->flight->route_code,
|
||||
'route_leg' => $simBrief->flight->route_leg,
|
||||
'dpt_airport_id' => $simBrief->flight->dpt_airport_id,
|
||||
'arr_airport_id' => $simBrief->flight->arr_airport_id,
|
||||
'route' => $simBrief->xml->getRouteString(),
|
||||
'level' => $simBrief->xml->getFlightLevel(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the flight ident, e.,g JBU1900
|
||||
*
|
||||
@@ -484,6 +506,11 @@ class Pirep extends Model
|
||||
->latest();
|
||||
}
|
||||
|
||||
public function simbrief()
|
||||
{
|
||||
return $this->belongsTo(SimBrief::class, 'id', 'pirep_id');
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(JournalTransaction::class, 'ref_model_id')
|
||||
|
||||
98
app/Models/SimBrief.php
Normal file
98
app/Models/SimBrief.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Contracts\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* @property string $id The Simbrief OFP ID
|
||||
* @property int $user_id The user that generated this
|
||||
* @property string $flight_id Optional, if attached to a flight, removed if attached to PIREP
|
||||
* @property string $pirep_id Optional, if attached to a PIREP, removed if attached to flight
|
||||
* @property string $acars_xml
|
||||
* @property string $ofp_xml
|
||||
* @property string $ofp_html
|
||||
* @property Collection $images
|
||||
* @property Collection $files
|
||||
* @property Flight $flight
|
||||
* @property User $user
|
||||
* @property SimBriefXML $xml
|
||||
* @property string $acars_flightplan_url
|
||||
*/
|
||||
class SimBrief extends Model
|
||||
{
|
||||
public $table = 'simbrief';
|
||||
public $incrementing = false;
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'user_id',
|
||||
'flight_id',
|
||||
'pirep_id',
|
||||
'acars_xml',
|
||||
'ofp_xml',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
/** @var \App\Models\SimBriefXML Store a cached version of the XML object */
|
||||
private $xml_instance;
|
||||
|
||||
/**
|
||||
* Return a SimpleXML object of the $ofp_xml
|
||||
*
|
||||
* @return \App\Models\SimBriefXML|null
|
||||
*/
|
||||
public function getXmlAttribute(): SimBriefXML
|
||||
{
|
||||
if (empty($this->attributes['ofp_xml'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$this->xml_instance) {
|
||||
$this->xml_instance = simplexml_load_string(
|
||||
$this->attributes['ofp_xml'],
|
||||
SimBriefXML::class
|
||||
);
|
||||
}
|
||||
|
||||
return $this->xml_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of images
|
||||
*/
|
||||
public function getImagesAttribute(): Collection
|
||||
{
|
||||
return $this->xml->getImages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the flight plans
|
||||
*/
|
||||
public function getFilesAttribute(): Collection
|
||||
{
|
||||
return $this->xml->getFlightPlans();
|
||||
}
|
||||
|
||||
/*
|
||||
* Relationships
|
||||
*/
|
||||
|
||||
public function flight()
|
||||
{
|
||||
if (!empty($this->attributes['flight_id'])) {
|
||||
return $this->belongsTo(Flight::class, 'flight_id');
|
||||
}
|
||||
|
||||
if (!empty($this->attributes['pirep_id'])) {
|
||||
return $this->belongsTo(Pirep::class, 'pirep_id');
|
||||
}
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
}
|
||||
121
app/Models/SimBriefXML.php
Normal file
121
app/Models/SimBriefXML.php
Normal file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use SimpleXMLElement;
|
||||
|
||||
/**
|
||||
* Represents the SimBrief XML instance with some helper methods
|
||||
*/
|
||||
class SimBriefXML extends SimpleXMLElement
|
||||
{
|
||||
/**
|
||||
* Return a padded flight level
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFlightLevel(): string
|
||||
{
|
||||
if (empty($this->alternate->cruise_altitude)) {
|
||||
return '0'; // unknown?
|
||||
}
|
||||
|
||||
$fl = (int) ($this->alternate->cruise_altitude) / 100;
|
||||
|
||||
return str_pad($fl, 3, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all of the flightplans
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getFlightPlans(): Collection
|
||||
{
|
||||
$fps = [];
|
||||
$base_url = $this->fms_downloads->directory;
|
||||
|
||||
// TODO: Put vmsACARS on top
|
||||
if (!empty($this->fms_downloads->vma)) {
|
||||
$fps[] = [
|
||||
'name' => $this->fms_downloads->vma->name->__toString(),
|
||||
'url' => $base_url.$this->fms_downloads->vma->link,
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($this->fms_downloads->children() as $child) {
|
||||
if ($child->getName() === 'directory') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fps[] = [
|
||||
'name' => $child->name->__toString(),
|
||||
'url' => $base_url.$child->link,
|
||||
];
|
||||
}
|
||||
|
||||
return collect($fps);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a generator which sends out the fix values. This can be a long list
|
||||
*
|
||||
* @return \Generator
|
||||
*/
|
||||
public function getRoute()
|
||||
{
|
||||
foreach ($this->navlog->children()->fix as $fix) {
|
||||
$type = $fix->type->__toString();
|
||||
if ($type === 'apt') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ident = $fix->ident->__toString();
|
||||
|
||||
if ($ident === 'TOC' || $ident === 'TOD') {
|
||||
continue;
|
||||
}
|
||||
|
||||
yield $fix;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the route as a string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRouteString(): string
|
||||
{
|
||||
if (!empty($this->general->route)) {
|
||||
return $this->general->route->__toString();
|
||||
}
|
||||
|
||||
$route = [];
|
||||
foreach ($this->getRoute() as $fix) {
|
||||
$route[] = $fix->ident->__toString();
|
||||
}
|
||||
|
||||
return implode(' ', $route);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all of the image links
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getImages(): Collection
|
||||
{
|
||||
$images = [];
|
||||
$base_url = $this->images->directory;
|
||||
foreach ($this->images->map as $image) {
|
||||
$images[] = [
|
||||
'name' => $image->name->__toString(),
|
||||
'url' => $base_url.$image->link,
|
||||
];
|
||||
}
|
||||
|
||||
return collect($images);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user