Option for SB only on bids; add the new file type #642 (#643)

Option for SB only on bids; add the new file type #642
This commit is contained in:
Nabeel S
2020-03-27 11:49:19 -04:00
committed by GitHub
parent f3fe3a56ba
commit 4a3ec38919
15 changed files with 746 additions and 56 deletions

View File

@@ -159,6 +159,13 @@
options: ''
type: string
description: 'Your SimBrief API key'
- key: simbrief.only_bids
name: 'Only allow for bids'
group: simbrief
value: true
options: ''
type: boolean
description: 'Only allow briefs to be created for bidded flights'
- key: simbrief.expire_days
name: 'SimBrief Expire Time'
group: simbrief

View File

@@ -9,11 +9,12 @@ use App\Repositories\AirportRepository;
use App\Repositories\Criteria\WhereCriteria;
use App\Repositories\FlightRepository;
use App\Repositories\SubfleetRepository;
use App\Repositories\UserRepository;
use App\Services\GeoService;
use Flash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laracasts\Flash\Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
@@ -24,6 +25,7 @@ class FlightController extends Controller
private $flightRepo;
private $subfleetRepo;
private $geoSvc;
private $userRepo;
/**
* @param AirlineRepository $airlineRepo
@@ -31,19 +33,22 @@ class FlightController extends Controller
* @param FlightRepository $flightRepo
* @param GeoService $geoSvc
* @param SubfleetRepository $subfleetRepo
* @param UserRepository $userRepo
*/
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo,
GeoService $geoSvc,
SubfleetRepository $subfleetRepo
SubfleetRepository $subfleetRepo,
UserRepository $userRepo
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->flightRepo = $flightRepo;
$this->geoSvc = $geoSvc;
$this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
}
/**
@@ -113,6 +118,7 @@ class FlightController extends Controller
'dep_icao' => $request->input('dep_icao'),
'subfleet_id' => $request->input('subfleet_id'),
'simbrief' => !empty(setting('simbrief.api_key')),
'simbrief_bids' => setting('simbrief.only_bids'),
]);
}
@@ -125,19 +131,25 @@ class FlightController extends Controller
*/
public function bids(Request $request)
{
$user = Auth::user();
$user = $this->userRepo
->with(['bids', 'bids.flight'])
->find(Auth::user()->id);
$flights = $user->flights()->paginate();
$saved_flights = $flights->pluck('id')->toArray();
$flights = collect();
$saved_flights = [];
foreach ($user->bids as $bid) {
$flights->add($bid->flight);
$saved_flights[] = $bid->flight->id;
}
return view('flights.index', [
'title' => trans_choice('flights.mybid', 2),
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'subfleets' => $this->subfleetRepo->selectBoxList(true),
'simbrief' => !empty(setting('simbrief.api_key')),
return view('flights.bids', [
'airlines' => $this->airlineRepo->selectBoxList(true),
'airports' => $this->airportRepo->selectBoxList(true),
'flights' => $flights,
'saved' => $saved_flights,
'subfleets' => $this->subfleetRepo->selectBoxList(true),
'simbrief' => !empty(setting('simbrief.api_key')),
'simbrief_bids' => setting('simbrief.only_bids'),
]);
}

View File

@@ -26,6 +26,21 @@ class SimBriefXML extends SimpleXMLElement
return str_pad($fl, 3, '0', STR_PAD_LEFT);
}
/**
* Return the URL to the vmsACARS flight plan file
*
* @return string|null
*/
public function getAcarsXmlUrl()
{
if (!empty($this->fms_downloads->vms)) {
$base_url = $this->fms_downloads->directory;
return $base_url.$this->fms_downloads->vms->link;
}
return null;
}
/**
* Retrieve all of the flightplans
*

View File

@@ -7,6 +7,7 @@ use App\Models\Acars;
use App\Models\Enums\AcarsType;
use App\Models\Pirep;
use App\Models\SimBrief;
use App\Models\SimBriefXML;
use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
@@ -31,7 +32,7 @@ class SimBriefService extends Service
*
* @return SimBrief|null
*/
public function checkForOfp(string $user_id, string $ofp_id, string $flight_id): SimBrief
public function checkForOfp(string $user_id, string $ofp_id, string $flight_id)
{
$uri = str_replace('{id}', $ofp_id, config('phpvms.simbrief_url'));
@@ -52,20 +53,27 @@ class SimBriefService extends Service
$body = $response->getBody()->getContents();
/** @var SimBriefXML $ofp */
$ofp = simplexml_load_string($body, SimBriefXML::class);
$attrs = [
'user_id' => $user_id,
'flight_id' => $flight_id,
'ofp_xml' => $body,
'ofp_xml' => $ofp->asXML(),
];
// TODO: Retrieve the ACARS XML and store that. For now, replace the doctype
// Try to download the XML file for ACARS. If it doesn't work, try to modify the main OFP
$acars_xml = $this->getAcarsOFP($ofp);
if (empty($acars_xml)) {
$new_doctype = '<VMSAcars Type="FlightPlan" version="1.0" generated="'.time().'">';
$acars_xml = str_replace('<OFP>', $new_doctype, $body);
$acars_xml = str_replace('</OFP>', '</VMSAcars>', $acars_xml);
$acars_xml = str_replace("\n", '', $acars_xml);
$new_doctype = '<VMSAcars Type="FlightPlan" version="1.0" generated="'.time().'">';
$acars_xml = str_replace('<OFP>', $new_doctype, $body);
$acars_xml = str_replace('</OFP>', '</VMSAcars>', $acars_xml);
$acars_xml = str_replace("\n", '', $acars_xml);
$attrs['acars_xml'] = simplexml_load_string($acars_xml)->asXML();
$attrs['acars_xml'] = simplexml_load_string($acars_xml)->asXML();
} else {
$attrs['acars_xml'] = $acars_xml->asXML();
}
// Save this into the Simbrief table, if it doesn't already exist
return SimBrief::updateOrCreate(
@@ -74,6 +82,38 @@ class SimBriefService extends Service
);
}
/**
* @param \App\Models\SimBriefXML $ofp
*
* @return \SimpleXMLElement|null
*/
public function getAcarsOFP(SimBriefXML $ofp)
{
$url = $ofp->getAcarsXmlUrl();
if (empty($url)) {
return null;
}
$opts = [
'connect_timeout' => 2, // wait two seconds by default
'allow_redirects' => true,
];
try {
$response = $this->httpClient->request('GET', $url, $opts);
if ($response->getStatusCode() !== 200) {
return null;
}
} catch (GuzzleException $e) {
Log::error('Simbrief HTTP Error: '.$e->getMessage());
dd($e);
return null;
}
$body = $response->getBody()->getContents();
return simplexml_load_string($body);
}
/**
* Create a prefiled PIREP from a given brief.
*