* SimBrief integration #405 * Add briefing as API response; add acars_xml field #405
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Unit;
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
namespace App\Contracts;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
* Base class for a resource/response
|
||||
*/
|
||||
class Response extends Resource
|
||||
class Resource extends \Illuminate\Http\Resources\Json\Resource
|
||||
{
|
||||
/**
|
||||
* Iterate through the list of $fields and check if they're a "Unit"
|
||||
@@ -59,7 +59,7 @@ class Unit implements ArrayAccess
|
||||
{
|
||||
$response = [];
|
||||
foreach ($this->responseUnits as $unit) {
|
||||
$response[$unit] = $this[$unit];
|
||||
$response[$unit] = $this[$unit] ?? 0;
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
28
app/Cron/Nightly/ClearExpiredSimbrief.php
Normal file
28
app/Cron/Nightly/ClearExpiredSimbrief.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Cron\Nightly;
|
||||
|
||||
use App\Contracts\Listener;
|
||||
use App\Events\CronNightly;
|
||||
use App\Services\SimBriefService;
|
||||
|
||||
/**
|
||||
* Clear any expired SimBrief flight briefs that aren't attached to a PIREP
|
||||
*/
|
||||
class ClearExpiredSimbrief extends Listener
|
||||
{
|
||||
private $simbriefSvc;
|
||||
|
||||
public function __construct(SimBriefService $simbriefSvc)
|
||||
{
|
||||
$this->simbriefSvc = $simbriefSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Events\CronNightly $event
|
||||
*/
|
||||
public function handle(CronNightly $event): void
|
||||
{
|
||||
$this->simbriefSvc->removeExpiredEntries();
|
||||
}
|
||||
}
|
||||
21
app/Database/factories/SimbriefFactory.php
Normal file
21
app/Database/factories/SimbriefFactory.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Factory $factory */
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->define(App\Models\SimBrief::class, function (Faker $faker) {
|
||||
return [
|
||||
'id' => $faker->unique()->numberBetween(10, 10000000),
|
||||
'user_id' => null,
|
||||
'flight_id' => null,
|
||||
'pirep_id' => null,
|
||||
'acars_xml' => '',
|
||||
'ofp_xml' => '',
|
||||
'created_at' => Carbon::now('UTC')->toDateTimeString(),
|
||||
'updated_at' => function (array $sb) {
|
||||
return $sb['created_at'];
|
||||
},
|
||||
];
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* Add a table to store the Simbrief data
|
||||
*/
|
||||
class AddSimbriefTable extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
Schema::create('simbrief', function (Blueprint $table) {
|
||||
$table->string('id', 36); // The OFP ID
|
||||
$table->unsignedInteger('user_id');
|
||||
$table->string('flight_id', 36)->nullable();
|
||||
$table->string('pirep_id', 36)->nullable();
|
||||
$table->mediumText('acars_xml');
|
||||
$table->mediumText('ofp_xml');
|
||||
$table->timestamps();
|
||||
|
||||
$table->primary('id');
|
||||
$table->index(['user_id', 'flight_id']);
|
||||
$table->index('pirep_id');
|
||||
$table->unique('pirep_id'); // Can only belong to a single PIREP
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::drop('simbrief');
|
||||
}
|
||||
}
|
||||
1
app/Database/seeds/dev/.gitignore
vendored
Normal file
1
app/Database/seeds/dev/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
local.yml
|
||||
@@ -152,6 +152,20 @@
|
||||
options: ''
|
||||
type: number
|
||||
description: 'How much the load factor can vary per-flight'
|
||||
- key: simbrief.api_key
|
||||
name: 'SimBrief API Key'
|
||||
group: simbrief
|
||||
value: ''
|
||||
options: ''
|
||||
type: string
|
||||
description: 'Your SimBrief API key'
|
||||
- key: simbrief.expire_days
|
||||
name: 'SimBrief Expire Time'
|
||||
group: simbrief
|
||||
value: 5
|
||||
options: ''
|
||||
type: number
|
||||
description: 'Days after how long to remove unused briefs'
|
||||
- key: pireps.duplicate_check_time
|
||||
name: 'PIREP duplicate time check'
|
||||
group: pireps
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Contracts\Controller;
|
||||
use App\Exceptions\AssetNotFound;
|
||||
use App\Http\Resources\Flight as FlightResource;
|
||||
use App\Http\Resources\Navdata as NavdataResource;
|
||||
use App\Models\SimBrief;
|
||||
use App\Repositories\Criteria\WhereCriteria;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\FlightService;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Prettus\Repository\Criteria\RequestCriteria;
|
||||
@@ -51,7 +54,18 @@ class FlightController extends Controller
|
||||
*/
|
||||
public function get($id)
|
||||
{
|
||||
$flight = $this->flightRepo->find($id);
|
||||
$user = Auth::user();
|
||||
$flight = $this->flightRepo->with([
|
||||
'airline',
|
||||
'subfleets',
|
||||
'subfleets.aircraft',
|
||||
'subfleets.fares',
|
||||
'field_values',
|
||||
'simbrief' => function ($query) use ($user) {
|
||||
return $query->where('user_id', $user->id);
|
||||
},
|
||||
])->find($id);
|
||||
|
||||
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
|
||||
|
||||
return new FlightResource($flight);
|
||||
@@ -64,6 +78,7 @@ class FlightController extends Controller
|
||||
*/
|
||||
public function search(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$where = [
|
||||
'active' => true,
|
||||
'visible' => true,
|
||||
@@ -95,6 +110,9 @@ class FlightController extends Controller
|
||||
'subfleets.aircraft',
|
||||
'subfleets.fares',
|
||||
'field_values',
|
||||
'simbrief' => function ($query) use ($user) {
|
||||
return $query->where('user_id', $user->id);
|
||||
},
|
||||
])
|
||||
->paginate();
|
||||
} catch (RepositoryException $e) {
|
||||
@@ -109,6 +127,32 @@ class FlightController extends Controller
|
||||
return FlightResource::collection($flights);
|
||||
}
|
||||
|
||||
/**
|
||||
* Output the flight briefing from simbrief or whatever other format
|
||||
*
|
||||
* @param string $id The flight ID
|
||||
*
|
||||
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
|
||||
*/
|
||||
public function briefing($id)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$w = [
|
||||
'user_id' => $user->id,
|
||||
'flight_id' => $id,
|
||||
];
|
||||
|
||||
$simbrief = SimBrief::where($w)->first();
|
||||
|
||||
if ($simbrief === null) {
|
||||
throw new AssetNotFound(new Exception('Flight briefing not found'));
|
||||
}
|
||||
|
||||
return response($simbrief->acars_xml, 200, [
|
||||
'Content-Type' => 'application/xml',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a flight's route
|
||||
*
|
||||
|
||||
@@ -5,10 +5,8 @@ namespace App\Http\Controllers\Api;
|
||||
use App\Contracts\Controller;
|
||||
use App\Events\PirepPrefiled;
|
||||
use App\Events\PirepUpdated;
|
||||
use App\Exceptions\AircraftNotAtAirport;
|
||||
use App\Exceptions\AircraftPermissionDenied;
|
||||
use App\Exceptions\PirepCancelled;
|
||||
use App\Exceptions\UserNotAtAirport;
|
||||
use App\Http\Requests\Acars\CommentRequest;
|
||||
use App\Http\Requests\Acars\FieldsRequest;
|
||||
use App\Http\Requests\Acars\FileRequest;
|
||||
@@ -22,7 +20,6 @@ use App\Http\Resources\PirepComment as PirepCommentResource;
|
||||
use App\Http\Resources\PirepFieldCollection;
|
||||
use App\Models\Acars;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Enums\PirepFieldSource;
|
||||
use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
@@ -113,7 +110,7 @@ class PirepController extends Controller
|
||||
protected function checkCancelled(Pirep $pirep)
|
||||
{
|
||||
if ($pirep->cancelled) {
|
||||
throw new PirepCancelled();
|
||||
throw new PirepCancelled($pirep);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,49 +196,9 @@ class PirepController extends Controller
|
||||
$user = Auth::user();
|
||||
|
||||
$attrs = $this->parsePirep($request);
|
||||
$attrs['user_id'] = $user->id;
|
||||
$attrs['source'] = PirepSource::ACARS;
|
||||
$attrs['state'] = PirepState::IN_PROGRESS;
|
||||
|
||||
if (!array_key_exists('status', $attrs)) {
|
||||
$attrs['status'] = PirepStatus::INITIATED;
|
||||
}
|
||||
|
||||
$pirep = new Pirep($attrs);
|
||||
|
||||
// See if this user is at the current airport
|
||||
/* @noinspection NotOptimalIfConditionsInspection */
|
||||
if (setting('pilots.only_flights_from_current')
|
||||
&& $user->curr_airport_id !== $pirep->dpt_airport_id) {
|
||||
throw new UserNotAtAirport($user, $pirep->dpt_airport);
|
||||
}
|
||||
|
||||
// See if this user is allowed to fly this aircraft
|
||||
if (setting('pireps.restrict_aircraft_to_rank', false)
|
||||
&& !$this->userSvc->aircraftAllowed($user, $pirep->aircraft_id)) {
|
||||
throw new AircraftPermissionDenied($user, $pirep->aircraft);
|
||||
}
|
||||
|
||||
// See if this aircraft is at the departure airport
|
||||
/* @noinspection NotOptimalIfConditionsInspection */
|
||||
if (setting('pireps.only_aircraft_at_dpt_airport')
|
||||
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
||||
throw new AircraftNotAtAirport($pirep->aircraft);
|
||||
}
|
||||
|
||||
// Find if there's a duplicate, if so, let's work on that
|
||||
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
||||
if ($dupe_pirep !== false) {
|
||||
$pirep = $dupe_pirep;
|
||||
$this->checkCancelled($pirep);
|
||||
}
|
||||
|
||||
// Default to a scheduled passenger flight
|
||||
if (!array_key_exists('flight_type', $attrs)) {
|
||||
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
||||
}
|
||||
|
||||
$pirep->save();
|
||||
$pirep = $this->pirepSvc->prefile($user, $attrs);
|
||||
|
||||
Log::info('PIREP PREFILED');
|
||||
Log::info($pirep->id);
|
||||
|
||||
@@ -26,8 +26,6 @@ class FlightController extends Controller
|
||||
private $geoSvc;
|
||||
|
||||
/**
|
||||
* FlightController constructor.
|
||||
*
|
||||
* @param AirlineRepository $airlineRepo
|
||||
* @param AirportRepository $airportRepo
|
||||
* @param FlightRepository $flightRepo
|
||||
@@ -114,6 +112,7 @@ class FlightController extends Controller
|
||||
'arr_icao' => $request->input('arr_icao'),
|
||||
'dep_icao' => $request->input('dep_icao'),
|
||||
'subfleet_id' => $request->input('subfleet_id'),
|
||||
'simbrief' => !empty(setting('simbrief.api_key')),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -138,6 +137,7 @@ class FlightController extends Controller
|
||||
'flights' => $flights,
|
||||
'saved' => $saved_flights,
|
||||
'subfleets' => $this->subfleetRepo->selectBoxList(true),
|
||||
'simbrief' => !empty(setting('simbrief.api_key')),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Enums\PirepStatus;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\SimBrief;
|
||||
use App\Repositories\AircraftRepository;
|
||||
use App\Repositories\AirlineRepository;
|
||||
use App\Repositories\AirportRepository;
|
||||
@@ -19,6 +20,7 @@ use App\Repositories\PirepRepository;
|
||||
use App\Services\FareService;
|
||||
use App\Services\GeoService;
|
||||
use App\Services\PirepService;
|
||||
use App\Services\SimBriefService;
|
||||
use App\Services\UserService;
|
||||
use App\Support\Units\Fuel;
|
||||
use App\Support\Units\Time;
|
||||
@@ -28,9 +30,6 @@ use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laracasts\Flash\Flash;
|
||||
|
||||
/**
|
||||
* Class PirepController
|
||||
*/
|
||||
class PirepController extends Controller
|
||||
{
|
||||
private $aircraftRepo;
|
||||
@@ -199,7 +198,7 @@ class PirepController extends Controller
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$pirep = $this->pirepRepo->find($id);
|
||||
$pirep = $this->pirepRepo->with(['simbrief'])->find($id);
|
||||
if (empty($pirep)) {
|
||||
Flash::error('Pirep not found');
|
||||
return redirect(route('frontend.pirep.index'));
|
||||
@@ -245,10 +244,20 @@ class PirepController extends Controller
|
||||
// See if request has a ?flight_id, so we can pre-populate the fields from the flight
|
||||
// Makes filing easier, but we can also more easily find a bid and close it
|
||||
if ($request->has('flight_id')) {
|
||||
$flight = $this->flightRepo->find($request->get('flight_id'));
|
||||
$flight = $this->flightRepo->find($request->input('flight_id'));
|
||||
$pirep = Pirep::fromFlight($flight);
|
||||
}
|
||||
|
||||
/**
|
||||
* They have a SimBrief ID, load that up and figure out the flight that it's from
|
||||
*/
|
||||
$simbrief_id = null;
|
||||
if ($request->has('sb_id')) {
|
||||
$simbrief_id = $request->input('sb_id');
|
||||
$brief = SimBrief::find($simbrief_id);
|
||||
$pirep = Pirep::fromSimBrief($brief);
|
||||
}
|
||||
|
||||
return view('pireps.create', [
|
||||
'aircraft' => null,
|
||||
'pirep' => $pirep,
|
||||
@@ -258,6 +267,7 @@ class PirepController extends Controller
|
||||
'airport_list' => $this->airportRepo->selectBoxList(true),
|
||||
'pirep_fields' => $this->pirepFieldRepo->all(),
|
||||
'field_values' => [],
|
||||
'simbrief_id' => $simbrief_id,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -338,6 +348,14 @@ class PirepController extends Controller
|
||||
$this->saveFares($pirep, $request);
|
||||
$this->pirepSvc->saveRoute($pirep);
|
||||
|
||||
if ($request->has('sb_id')) {
|
||||
$brief = SimBrief::find($request->input('sb_id'));
|
||||
|
||||
/** @var SimBriefService $sbSvc */
|
||||
$sbSvc = app(SimBriefService::class);
|
||||
$sbSvc->attachSimbriefToPirep($pirep, $brief);
|
||||
}
|
||||
|
||||
// Depending on the button they selected, set an initial state
|
||||
// Can be saved as a draft or just submitted
|
||||
if ($attrs['submit'] === 'save') {
|
||||
@@ -375,6 +393,11 @@ class PirepController extends Controller
|
||||
$pirep->aircraft->load('subfleet.fares');
|
||||
}
|
||||
|
||||
$simbrief_id = null;
|
||||
if ($pirep->simbrief) {
|
||||
$simbrief_id = $pirep->simbrief->id;
|
||||
}
|
||||
|
||||
$time = new Time($pirep->flight_time);
|
||||
$pirep->hours = $time->hours;
|
||||
$pirep->minutes = $time->minutes;
|
||||
@@ -402,6 +425,7 @@ class PirepController extends Controller
|
||||
'airline_list' => $this->airlineRepo->selectBoxList(),
|
||||
'airport_list' => $this->airportRepo->selectBoxList(),
|
||||
'pirep_fields' => $this->pirepFieldRepo->all(),
|
||||
'simbrief_id' => $simbrief_id,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
166
app/Http/Controllers/Frontend/SimBriefController.php
Normal file
166
app/Http/Controllers/Frontend/SimBriefController.php
Normal file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Frontend;
|
||||
|
||||
use App\Exceptions\AssetNotFound;
|
||||
use App\Models\SimBrief;
|
||||
use App\Repositories\FlightRepository;
|
||||
use App\Services\SimBriefService;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class SimBriefController
|
||||
{
|
||||
private $flightRepo;
|
||||
private $simBriefSvc;
|
||||
|
||||
public function __construct(FlightRepository $flightRepo, SimBriefService $simBriefSvc)
|
||||
{
|
||||
$this->flightRepo = $flightRepo;
|
||||
$this->simBriefSvc = $simBriefSvc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the main OFP form
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function generate(Request $request)
|
||||
{
|
||||
$flight_id = $request->input('flight_id');
|
||||
$flight = $this->flightRepo->find($flight_id);
|
||||
if (!$flight) {
|
||||
flash()->error('Unknown flight');
|
||||
return redirect(route('frontend.flights.index'));
|
||||
}
|
||||
|
||||
$apiKey = setting('simbrief.api_key');
|
||||
if (empty($apiKey)) {
|
||||
flash()->error('Invalid SimBrief API key!');
|
||||
return redirect(route('frontend.flights.index'));
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
$simbrief = SimBrief::select('id')->where([
|
||||
'flight_id' => $flight_id,
|
||||
'user_id' => $user->id,
|
||||
])->first();
|
||||
|
||||
if ($simbrief) {
|
||||
return redirect(route('frontend.simbrief.briefing', [$simbrief->id]));
|
||||
}
|
||||
|
||||
return view('flights.simbrief_form', [
|
||||
'flight' => $flight,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the briefing
|
||||
*
|
||||
* @param string $id The OFP ID
|
||||
*
|
||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
|
||||
*/
|
||||
public function briefing($id)
|
||||
{
|
||||
$simbrief = SimBrief::find($id);
|
||||
if (!$simbrief) {
|
||||
flash()->error('SimBrief briefing not found');
|
||||
return redirect(route('frontend.flights.index'));
|
||||
}
|
||||
|
||||
return view('flights.simbrief_briefing', [
|
||||
'simbrief' => $simbrief,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a prefile of this PIREP with a given OFP. Then redirect the
|
||||
* user to the newly prefiled PIREP
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function prefile(Request $request)
|
||||
{
|
||||
$sb = SimBrief::find($request->id);
|
||||
if (!$sb) {
|
||||
return redirect(route('frontend.flights.index'));
|
||||
}
|
||||
|
||||
// Redirect to the prefile page, with the flight_id and a simbrief_id
|
||||
$rd = route('frontend.pireps.create').'?sb_id='.$sb->id;
|
||||
return redirect($rd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the SimBrief request
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function cancel(Request $request)
|
||||
{
|
||||
$sb = SimBrief::find($request->id);
|
||||
if (!$sb) {
|
||||
$sb->delete();
|
||||
}
|
||||
|
||||
return redirect(route('frontend.simbrief.prefile', ['id' => $request->id]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the OFP was generated. Pass in two items, the flight_id and ofp_id
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
*/
|
||||
public function check_ofp(Request $request)
|
||||
{
|
||||
$ofp_id = $request->input('ofp_id');
|
||||
$flight_id = $request->input('flight_id');
|
||||
|
||||
$simbrief = $this->simBriefSvc->checkForOfp(Auth::user()->id, $ofp_id, $flight_id);
|
||||
if ($simbrief === null) {
|
||||
$error = new AssetNotFound(new Exception('Simbrief OFP not found'));
|
||||
return $error->getResponse();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'id' => $simbrief->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the API code
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function api_code(Request $request)
|
||||
{
|
||||
$apiKey = setting('simbrief.api_key', null);
|
||||
if (empty($apiKey)) {
|
||||
flash()->error('Invalid SimBrief API key!');
|
||||
return redirect(route('frontend.flights.index'));
|
||||
}
|
||||
|
||||
$api_code = md5($apiKey.$request->input('api_req'));
|
||||
|
||||
return response()->json([
|
||||
'api_code' => $api_code,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
use App\Support\Units\Distance;
|
||||
use App\Support\Units\Fuel;
|
||||
|
||||
class Acars extends Response
|
||||
class Acars extends Resource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
|
||||
/**
|
||||
* ACARS table but only include the fields for the routes
|
||||
* Class AcarsRoute
|
||||
*/
|
||||
class AcarsLog extends Response
|
||||
class AcarsLog extends Resource
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
|
||||
/**
|
||||
* ACARS table but only include the fields for the routes
|
||||
* Class AcarsRoute
|
||||
*/
|
||||
class AcarsRoute extends Response
|
||||
class AcarsRoute extends Resource
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class Aircraft extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Aircraft extends Resource
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class Airline extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Airline extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class Airport extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Airport extends Resource
|
||||
{
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class AirportDistance extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class AirportDistance extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class Award extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Award extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class Bid extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Bid extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
/**
|
||||
* @mixin \App\Models\Fare
|
||||
|
||||
@@ -2,13 +2,15 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
use App\Http\Resources\SimBrief as SimbriefResource;
|
||||
use App\Support\Units\Distance;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @mixin \App\Models\Flight
|
||||
*/
|
||||
class Flight extends Response
|
||||
class Flight extends Resource
|
||||
{
|
||||
/**
|
||||
* Set the fields on the flight object
|
||||
@@ -55,9 +57,12 @@ class Flight extends Response
|
||||
$res['distance'] = $distance->getResponseUnits();
|
||||
|
||||
$res['airline'] = new Airline($this->airline);
|
||||
$res['subfleets'] = Subfleet::collection($this->subfleets);
|
||||
$res['subfleets'] = Subfleet::collection($this->whenLoaded('subfleets'));
|
||||
$res['fields'] = $this->setFields();
|
||||
|
||||
// Simbrief info
|
||||
$res['simbrief'] = new SimbriefResource($this->whenLoaded('simbrief'));
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
class JournalTransaction extends Response
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class JournalTransaction extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
use App\Models\Enums\NavaidType;
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
|
||||
class Navdata extends Resource
|
||||
{
|
||||
|
||||
@@ -2,11 +2,8 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
*/
|
||||
class News extends Resource
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -2,11 +2,16 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Contracts\Resource;
|
||||
use App\Http\Resources\SimBrief as SimbriefResource;
|
||||
use App\Models\Enums\PirepStatus;
|
||||
use App\Support\Units\Distance;
|
||||
use App\Support\Units\Fuel;
|
||||
|
||||
class Pirep extends Response
|
||||
/**
|
||||
* @mixin \App\Models\Pirep
|
||||
*/
|
||||
class Pirep extends Resource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
@@ -75,6 +80,9 @@ class Pirep extends Response
|
||||
// format to kvp
|
||||
$res['fields'] = new PirepFieldCollection($this->fields);
|
||||
|
||||
// Simbrief info
|
||||
$res['simbrief'] = new SimbriefResource($this->whenLoaded('simbrief'));
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,8 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
*/
|
||||
class PirepComment extends Resource
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Rank extends Resource
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Setting extends Resource
|
||||
{
|
||||
|
||||
19
app/Http/Resources/SimBrief.php
Normal file
19
app/Http/Resources/SimBrief.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
|
||||
/**
|
||||
* @mixin \App\Models\SimBrief
|
||||
*/
|
||||
class SimBrief extends Resource
|
||||
{
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'url' => url(route('api.flights.briefing', ['id' => $this->id])),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class Subfleet extends Resource
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class User extends Resource
|
||||
{
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\Resource;
|
||||
use App\Contracts\Resource;
|
||||
|
||||
class UserBid extends Resource
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Providers;
|
||||
use App\Cron\Hourly\RemoveExpiredBids;
|
||||
use App\Cron\Hourly\RemoveExpiredLiveFlights;
|
||||
use App\Cron\Nightly\ApplyExpenses;
|
||||
use App\Cron\Nightly\ClearExpiredSimbrief;
|
||||
use App\Cron\Nightly\NewVersionCheck;
|
||||
use App\Cron\Nightly\PilotLeave;
|
||||
use App\Cron\Nightly\RecalculateBalances;
|
||||
@@ -29,6 +30,7 @@ class CronServiceProvider extends ServiceProvider
|
||||
SetActiveFlights::class,
|
||||
RecalculateStats::class,
|
||||
NewVersionCheck::class,
|
||||
ClearExpiredSimbrief::class,
|
||||
],
|
||||
|
||||
CronWeekly::class => [
|
||||
|
||||
@@ -72,6 +72,14 @@ class RouteServiceProvider extends ServiceProvider
|
||||
Route::get('profile/regen_apikey', 'ProfileController@regen_apikey')->name('profile.regen_apikey');
|
||||
|
||||
Route::resource('profile', 'ProfileController');
|
||||
|
||||
// SimBrief stuff
|
||||
Route::get('simbrief/generate', 'SimBriefController@generate')->name('simbrief.generate');
|
||||
Route::post('simbrief/apicode', 'SimBriefController@api_code')->name('simbrief.api_code');
|
||||
Route::get('simbrief/check_ofp', 'SimBriefController@check_ofp')->name('simbrief.check_ofp');
|
||||
Route::get('simbrief/{id}', 'SimBriefController@briefing')->name('simbrief.briefing');
|
||||
Route::get('simbrief/{id}/prefile', 'SimBriefController@prefile')->name('simbrief.prefile');
|
||||
Route::get('simbrief/{id}/cancel', 'SimBriefController@cancel')->name('simbrief.cancel');
|
||||
});
|
||||
|
||||
Route::group([
|
||||
@@ -412,6 +420,7 @@ class RouteServiceProvider extends ServiceProvider
|
||||
Route::get('flights', 'FlightController@index');
|
||||
Route::get('flights/search', 'FlightController@search');
|
||||
Route::get('flights/{id}', 'FlightController@get');
|
||||
Route::get('flights/{id}/briefing', 'FlightController@briefing')->name('flights.briefing');
|
||||
Route::get('flights/{id}/route', 'FlightController@route');
|
||||
|
||||
Route::get('pireps', 'UserController@pireps');
|
||||
|
||||
@@ -130,7 +130,11 @@ class FlightService extends Service
|
||||
*/
|
||||
public function filterSubfleets($user, $flight)
|
||||
{
|
||||
/** @var \Illuminate\Support\Collection $subfleets */
|
||||
$subfleets = $flight->subfleets;
|
||||
if ($subfleets === null || $subfleets->count() === 0) {
|
||||
return $flight;
|
||||
}
|
||||
|
||||
/*
|
||||
* Only allow aircraft that the user has access to in their rank
|
||||
|
||||
@@ -8,9 +8,13 @@ use App\Events\PirepCancelled;
|
||||
use App\Events\PirepFiled;
|
||||
use App\Events\PirepRejected;
|
||||
use App\Events\UserStatsChanged;
|
||||
use App\Exceptions\AircraftNotAtAirport;
|
||||
use App\Exceptions\AircraftPermissionDenied;
|
||||
use App\Exceptions\PirepCancelNotAllowed;
|
||||
use App\Exceptions\UserNotAtAirport;
|
||||
use App\Models\Acars;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Enums\FlightType;
|
||||
use App\Models\Enums\PirepSource;
|
||||
use App\Models\Enums\PirepState;
|
||||
use App\Models\Enums\PirepStatus;
|
||||
@@ -27,24 +31,84 @@ use Illuminate\Support\Facades\Log;
|
||||
class PirepService extends Service
|
||||
{
|
||||
private $geoSvc;
|
||||
private $pilotSvc;
|
||||
private $userSvc;
|
||||
private $pirepRepo;
|
||||
|
||||
/**
|
||||
* @param GeoService $geoSvc
|
||||
* @param PirepRepository $pirepRepo
|
||||
* @param UserService $pilotSvc
|
||||
* @param UserService $userSvc
|
||||
*/
|
||||
public function __construct(
|
||||
GeoService $geoSvc,
|
||||
PirepRepository $pirepRepo,
|
||||
UserService $pilotSvc
|
||||
UserService $userSvc
|
||||
) {
|
||||
$this->geoSvc = $geoSvc;
|
||||
$this->pilotSvc = $pilotSvc;
|
||||
$this->userSvc = $userSvc;
|
||||
$this->pirepRepo = $pirepRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a prefiled PIREP
|
||||
*
|
||||
* @param \App\Models\User $user
|
||||
* @param array $attrs
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return \App\Models\Pirep
|
||||
*/
|
||||
public function prefile(User $user, array $attrs): Pirep
|
||||
{
|
||||
$attrs['user_id'] = $user->id;
|
||||
$attrs['state'] = PirepState::IN_PROGRESS;
|
||||
|
||||
if (!array_key_exists('status', $attrs)) {
|
||||
$attrs['status'] = PirepStatus::INITIATED;
|
||||
}
|
||||
|
||||
// Default to a scheduled passenger flight
|
||||
if (!array_key_exists('flight_type', $attrs)) {
|
||||
$attrs['flight_type'] = FlightType::SCHED_PAX;
|
||||
}
|
||||
|
||||
$pirep = new Pirep($attrs);
|
||||
|
||||
// See if this user is at the current airport
|
||||
/* @noinspection NotOptimalIfConditionsInspection */
|
||||
if (setting('pilots.only_flights_from_current')
|
||||
&& $user->curr_airport_id !== $pirep->dpt_airport_id) {
|
||||
throw new UserNotAtAirport($user, $pirep->dpt_airport);
|
||||
}
|
||||
|
||||
// See if this user is allowed to fly this aircraft
|
||||
if (setting('pireps.restrict_aircraft_to_rank', false)
|
||||
&& !$this->userSvc->aircraftAllowed($user, $pirep->aircraft_id)) {
|
||||
throw new AircraftPermissionDenied($user, $pirep->aircraft);
|
||||
}
|
||||
|
||||
// See if this aircraft is at the departure airport
|
||||
/* @noinspection NotOptimalIfConditionsInspection */
|
||||
if (setting('pireps.only_aircraft_at_dpt_airport')
|
||||
&& $pirep->aircraft_id !== $pirep->dpt_airport_id) {
|
||||
throw new AircraftNotAtAirport($pirep->aircraft);
|
||||
}
|
||||
|
||||
// Find if there's a duplicate, if so, let's work on that
|
||||
$dupe_pirep = $this->findDuplicate($pirep);
|
||||
if ($dupe_pirep !== false) {
|
||||
$pirep = $dupe_pirep;
|
||||
if ($pirep->cancelled) {
|
||||
throw new \App\Exceptions\PirepCancelled($pirep);
|
||||
}
|
||||
}
|
||||
|
||||
$pirep->save();
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PIREP with some given fields
|
||||
*
|
||||
@@ -348,9 +412,9 @@ class PirepService extends Service
|
||||
$ft = $pirep->flight_time;
|
||||
$pilot = $pirep->user;
|
||||
|
||||
$this->pilotSvc->adjustFlightTime($pilot, $ft);
|
||||
$this->pilotSvc->adjustFlightCount($pilot, +1);
|
||||
$this->pilotSvc->calculatePilotRank($pilot);
|
||||
$this->userSvc->adjustFlightTime($pilot, $ft);
|
||||
$this->userSvc->adjustFlightCount($pilot, +1);
|
||||
$this->userSvc->calculatePilotRank($pilot);
|
||||
$pirep->user->refresh();
|
||||
|
||||
// Change the status
|
||||
@@ -387,9 +451,9 @@ class PirepService extends Service
|
||||
$user = $pirep->user;
|
||||
$ft = $pirep->flight_time * -1;
|
||||
|
||||
$this->pilotSvc->adjustFlightTime($user, $ft);
|
||||
$this->pilotSvc->adjustFlightCount($user, -1);
|
||||
$this->pilotSvc->calculatePilotRank($user);
|
||||
$this->userSvc->adjustFlightTime($user, $ft);
|
||||
$this->userSvc->adjustFlightCount($user, -1);
|
||||
$this->userSvc->calculatePilotRank($user);
|
||||
$pirep->user->refresh();
|
||||
}
|
||||
|
||||
|
||||
155
app/Services/SimBriefService.php
Normal file
155
app/Services/SimBriefService.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Models\Acars;
|
||||
use App\Models\Enums\AcarsType;
|
||||
use App\Models\Pirep;
|
||||
use App\Models\SimBrief;
|
||||
use Carbon\Carbon;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class SimBriefService extends Service
|
||||
{
|
||||
private $httpClient;
|
||||
|
||||
public function __construct(GuzzleClient $httpClient)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check to see if the OFP exists server-side. If it does, download it and
|
||||
* cache it immediately
|
||||
*
|
||||
* @param string $user_id User who generated this
|
||||
* @param string $ofp_id The SimBrief OFP ID
|
||||
* @param string $flight_id The flight ID
|
||||
*
|
||||
* @return SimBrief|null
|
||||
*/
|
||||
public function checkForOfp(string $user_id, string $ofp_id, string $flight_id): SimBrief
|
||||
{
|
||||
$uri = str_replace('{id}', $ofp_id, config('phpvms.simbrief_url'));
|
||||
|
||||
$opts = [
|
||||
'connect_timeout' => 2, // wait two seconds by default
|
||||
'allow_redirects' => false,
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->httpClient->request('GET', $uri, $opts);
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
return null;
|
||||
}
|
||||
} catch (GuzzleException $e) {
|
||||
Log::error('Simbrief HTTP Error: '.$e->getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
$body = $response->getBody()->getContents();
|
||||
|
||||
$attrs = [
|
||||
'user_id' => $user_id,
|
||||
'flight_id' => $flight_id,
|
||||
'ofp_xml' => $body,
|
||||
];
|
||||
|
||||
// TODO: Retrieve the ACARS XML and store that. For now, replace the doctype
|
||||
|
||||
$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();
|
||||
|
||||
// Save this into the Simbrief table, if it doesn't already exist
|
||||
return SimBrief::updateOrCreate(
|
||||
['id' => $ofp_id],
|
||||
$attrs
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a prefiled PIREP from a given brief.
|
||||
*
|
||||
* 1. Read from the XML the basic PIREP info (dep, arr), and then associate the PIREP
|
||||
* to the flight ID
|
||||
* 2. Remove the flight ID from the SimBrief field and assign the pirep_id to the row
|
||||
* 3. Update the planned flight route in the acars table
|
||||
* 4. Add additional flight fields (ones which match ACARS)
|
||||
*
|
||||
* @param $pirep
|
||||
* @param SimBrief $simBrief The briefing to create the PIREP from
|
||||
*
|
||||
* @return \App\Models\Pirep
|
||||
*/
|
||||
public function attachSimbriefToPirep($pirep, SimBrief $simBrief): Pirep
|
||||
{
|
||||
$this->addRouteToPirep($pirep, $simBrief);
|
||||
|
||||
$simBrief->pirep_id = $pirep->id;
|
||||
$simBrief->flight_id = null;
|
||||
$simBrief->save();
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the route from a SimBrief flight plan to a PIREP
|
||||
*
|
||||
* @param Pirep $pirep
|
||||
* @param SimBrief $simBrief
|
||||
*
|
||||
* @return Pirep
|
||||
*/
|
||||
protected function addRouteToPirep($pirep, SimBrief $simBrief): Pirep
|
||||
{
|
||||
// Clear previous entries
|
||||
Acars::where(['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE])->delete();
|
||||
|
||||
// Create the flight route
|
||||
$order = 1;
|
||||
foreach ($simBrief->xml->getRoute() as $fix) {
|
||||
$position = [
|
||||
'name' => $fix->ident,
|
||||
'pirep_id' => $pirep->id,
|
||||
'type' => AcarsType::ROUTE,
|
||||
'order' => $order++,
|
||||
'lat' => $fix->pos_lat,
|
||||
'lon' => $fix->pos_long,
|
||||
];
|
||||
|
||||
$acars = new Acars($position);
|
||||
$acars->save();
|
||||
}
|
||||
|
||||
return $pirep;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove any expired entries from the SimBrief table. Expired means there's
|
||||
* a flight_id attached to it, but no pirep_id (meaning it was never used for
|
||||
* an actual flight)
|
||||
*/
|
||||
public function removeExpiredEntries(): void
|
||||
{
|
||||
$expire_days = setting('simbrief.expire_days', 5);
|
||||
$expire_time = Carbon::now('UTC')->subDays($expire_days)->toDateTimeString();
|
||||
|
||||
$briefs = SimBrief::where([
|
||||
['pirep_id', '=', ''],
|
||||
['created_at', '<', $expire_time],
|
||||
])->get();
|
||||
|
||||
foreach ($briefs as $brief) {
|
||||
$brief->delete();
|
||||
|
||||
// TODO: Delete any assets
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -152,8 +152,6 @@ if (!function_exists('setting')) {
|
||||
* @param $key
|
||||
* @param mixed $default
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
function setting($key, $default = null)
|
||||
@@ -193,7 +191,6 @@ if (!function_exists('public_asset')) {
|
||||
{
|
||||
$publicBaseUrl = app()->publicUrlPath();
|
||||
$path = $publicBaseUrl.$path;
|
||||
|
||||
$path = str_replace('//', '/', $path);
|
||||
|
||||
return url($path, $parameters);
|
||||
|
||||
Reference in New Issue
Block a user