Skip to content

Commit

Permalink
Block aircraft with Simbrief (#1213)
Browse files Browse the repository at this point in the history
* Block Aircraft with SimBrief

Changes aim to have the ability to block an aircraft's usage if it is used to generate a SimBrief OFP.

Unused/Expired briefings will be deleted by cron like before but will now be checked by HourlyCron, so admins can define more precise restrictions for them (and the blockage period of their aircraft)

Owner of the SimBrief OFP will be able to start a flight with acars using that particular aircraft, but pilots will get an Aircraft Not Available error (similar to Aircraft State check)

To prevent SimBrief OFP packs being marked as expired/unused, during pirep prefile, pirep_id will be saved to SimBrief model along with flight_id.

And when a flight is finished (pirep file), flight_id will be removed from SimBrief model as before. Only pirep_id will remain and aircraft will be available for another OFP generation.

* Update PirepController

In case a pirep is being saved/submitted with manual entry (but the va is using simbrief effectively) same logic should be applied during save/submit button selection.
Save will act like a pirep prefile , Submit will be pirep file.

* Manual Pirep Checks

Manual pireps, prefiled from a generated simbrief should be checked too. Also pirep.show blade's submit button should provide the same simbrief checks.

* Update PirepService.php

* Change settings and move sb cron to hourly

* StyleFix (SimBriefService)

* Another StyleFix (SimBriefService)

* Update SimBriefController

Removed null check of pirep_id for aircraft list generation to prevent live flights' aircraft being listed for another ofp generation.
( Active acars flights will have both flight_id and pirep_id at simbrief table)

* Update PirepService.php

Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
  • Loading branch information
FatihKoz and nabeelio committed Jun 3, 2021
1 parent ad86e99 commit 17447c6
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace App\Cron\Nightly;
namespace App\Cron\Hourly;

use App\Contracts\Listener;
use App\Events\CronNightly;
use App\Events\CronHourly;
use App\Services\SimBriefService;
use Illuminate\Support\Facades\Log;

Expand All @@ -22,9 +22,9 @@ public function __construct(SimBriefService $simbriefSvc)
/**
* @param \App\Events\CronNightly $event
*/
public function handle(CronNightly $event): void
public function handle(CronHourly $event): void
{
Log::info('Nightly: Removing expired Simbrief entries');
Log::info('Hourly: Removing expired Simbrief entries');
$this->simbriefSvc->removeExpiredEntries();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class RemoveSettingSimbriefExpireDays extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::table('settings')
->where(['key' => 'simbrief.expire_days'])
->delete();
}
}
13 changes: 10 additions & 3 deletions app/Database/seeds/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@
options: ''
type: boolean
description: 'Only allow briefs to be created for bidded flights'
- key: simbrief.expire_days
- key: simbrief.expire_hours
name: 'Simbrief Expire Time'
group: simbrief
value: 5
value: 6
options: ''
type: number
description: 'Days after how long to remove unused briefs'
description: 'Hours after how long to remove unused briefs'
- key: simbrief.noncharter_pax_weight
name: 'Non-Charter Passenger Weight'
group: simbrief
Expand Down Expand Up @@ -256,6 +256,13 @@
options: ''
type: boolean
description: 'Use privatized user name as SimBrief OFP captain name'
- key: simbrief.block_aircraft
name: 'Restrict Aircraft'
group: simbrief
value: false
options: ''
type: boolean
description: 'When enabled, an aircraft can only be used for one active SimBrief OFP and Flight/Pirep'
- key: pireps.duplicate_check_time
name: 'PIREP duplicate time check'
group: pireps
Expand Down
8 changes: 7 additions & 1 deletion app/Http/Controllers/Frontend/PirepController.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,13 @@ public function store(CreatePirepRequest $request)
if ($brief !== null) {
/** @var SimBriefService $sbSvc */
$sbSvc = app(SimBriefService::class);
$sbSvc->attachSimbriefToPirep($pirep, $brief);
// Keep the flight_id with SimBrief depending on the button selected
// Save = Keep the flight_id , Submit = Remove the flight_id
if ($attrs['submit'] === 'save') {
$sbSvc->attachSimbriefToPirep($pirep, $brief, true);
} elseif ($attrs['submit'] === 'submit') {
$sbSvc->attachSimbriefToPirep($pirep, $brief);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/Http/Controllers/Frontend/SimBriefController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public function generate(Request $request)
$aircrafts = $aircrafts->where('airport_id', $flight->dpt_airport_id);
}

if (setting('simbrief.block_aircraft')) {
// Build a list of aircraft_id's being used for active sb packs
$sb_aircraft = SimBrief::whereNotNull('flight_id')->pluck('aircraft_id');
// Filter aircraft list to non used/blocked ones
$aircrafts = $aircrafts->whereNotIn('id', $sb_aircraft);
}

return view('flights.simbrief_aircraft', [
'flight' => $flight,
'aircrafts' => $aircrafts,
Expand Down
4 changes: 2 additions & 2 deletions app/Providers/CronServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Providers;

use App\Cron\Hourly\ClearExpiredSimbrief;
use App\Cron\Hourly\DeletePireps;
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;
Expand All @@ -31,7 +31,6 @@ class CronServiceProvider extends ServiceProvider
SetActiveFlights::class,
RecalculateStats::class,
NewVersionCheck::class,
ClearExpiredSimbrief::class,
],

CronWeekly::class => [
Expand All @@ -45,6 +44,7 @@ class CronServiceProvider extends ServiceProvider
DeletePireps::class,
RemoveExpiredBids::class,
RemoveExpiredLiveFlights::class,
ClearExpiredSimbrief::class,
],
];
}
40 changes: 38 additions & 2 deletions app/Services/PirepService.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ public function prefile(User $user, array $attrs): Pirep
throw new AircraftNotAvailable($pirep->aircraft);
}

// See if this aircraft is being used by another user's active simbrief ofp
if (setting('simbrief.block_aircraft', false)) {
$sb_aircraft = SimBrief::select('aircraft_id')
->where('aircraft_id', $pirep->aircraft_id)
->where('user_id', '!=', $pirep->user_id)
->whereNotNull('flight_id')
->count();
if ($sb_aircraft > 0) {
throw new AircraftNotAvailable($pirep->aircraft);
}
}

// See if this aircraft is at the departure airport
/* @noinspection NotOptimalIfConditionsInspection */
if (setting('pireps.only_aircraft_at_dpt_airport') && $aircraft->airport_id !== $pirep->dpt_airport_id) {
Expand All @@ -164,9 +176,20 @@ public function prefile(User $user, array $attrs): Pirep
}
}

event(new PirepPrefiled($pirep));

$pirep->save();
$pirep->refresh();

// Check if there is a simbrief_id, update it to have the pirep_id
// Keep the flight_id until the end of flight (pirep file)
if (array_key_exists('simbrief_id', $attrs)) {
/** @var SimBrief $simbrief */
$simbrief = SimBrief::find($attrs['simbrief_id']);
if ($simbrief) {
$this->simBriefSvc->attachSimbriefToPirep($pirep, $simbrief, true);
}
}

event(new PirepPrefiled($pirep));

return $pirep;
}
Expand Down Expand Up @@ -427,6 +450,19 @@ public function submit(Pirep $pirep)
}
}

// Check if there is a simbrief_id, change it to be set to the PIREP
// at the end of the flight when it's been submitted finally.
// Prefile, Save (as draft) and File already have this but the Submit button
// visible at pireps.show blade uses this function so Simbrief also needs to
// checked here too (to remove the flight_id and release the aircraft)
if (!empty($pirep->simbrief)) {
/** @var SimBrief $simbrief */
$simbrief = SimBrief::find($pirep->simbrief->id);
if ($simbrief) {
$this->simBriefSvc->attachSimbriefToPirep($pirep, $simbrief);
}
}

Log::info('New PIREP filed', [$pirep]);
event(new PirepFiled($pirep));

Expand Down
22 changes: 12 additions & 10 deletions app/Services/SimBriefService.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,23 @@ public function getAcarsOFP(SimBriefXML $ofp)
*
* 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
* 2. Remove the flight ID from the SimBrief model and assign the pirep ID to the row
* at the end of the flight. Keep flight ID until the flight ends (pirep file).
* 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
* @param SimBrief $simBrief The briefing to create the PIREP from
* @param bool $keep_flight True keeps the flight_id, default is false
*
* @return \App\Models\Pirep
*/
public function attachSimbriefToPirep($pirep, SimBrief $simBrief): Pirep
public function attachSimbriefToPirep($pirep, SimBrief $simBrief, $keep_flight = false): Pirep
{
$this->addRouteToPirep($pirep, $simBrief);

$simBrief->pirep_id = $pirep->id;
$simBrief->flight_id = null;
$simBrief->flight_id = !empty($keep_flight) ? $pirep->flight_id : null;
$simBrief->save();

return $pirep;
Expand Down Expand Up @@ -185,14 +187,14 @@ protected function addRouteToPirep($pirep, SimBrief $simBrief): 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)
* 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);
$expire_hours = setting('simbrief.expire_hours', 6);
$expire_time = Carbon::now('UTC')->subHours($expire_hours);

$briefs = SimBrief::where([
['pirep_id', null],
Expand All @@ -202,7 +204,7 @@ public function removeExpiredEntries(): void
foreach ($briefs as $brief) {
$brief->delete();

// TODO: Delete any assets
// TODO: Delete any assets (Which assets ?)
}
}
}

0 comments on commit 17447c6

Please sign in to comment.