Block aircraft with Simbrief (#1213)

* 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>
This commit is contained in:
B.Fatih KOZ 2021-06-03 23:17:16 +03:00 committed by GitHub
parent ad86e996d7
commit 17447c6903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 99 additions and 22 deletions

View File

@ -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;
@ -22,9 +22,9 @@ class ClearExpiredSimbrief extends Listener
/**
* @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();
}
}

View File

@ -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();
}
}

View File

@ -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
@ -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

View File

@ -399,7 +399,13 @@ class PirepController extends Controller
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);
}
}
}

View File

@ -100,6 +100,13 @@ class SimBriefController
$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,

View File

@ -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;
@ -31,7 +31,6 @@ class CronServiceProvider extends ServiceProvider
SetActiveFlights::class,
RecalculateStats::class,
NewVersionCheck::class,
ClearExpiredSimbrief::class,
],
CronWeekly::class => [
@ -45,6 +44,7 @@ class CronServiceProvider extends ServiceProvider
DeletePireps::class,
RemoveExpiredBids::class,
RemoveExpiredLiveFlights::class,
ClearExpiredSimbrief::class,
],
];
}

View File

@ -148,6 +148,18 @@ class PirepService extends Service
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) {
@ -164,9 +176,20 @@ class PirepService extends Service
}
}
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;
}
@ -427,6 +450,19 @@ class PirepService extends Service
}
}
// 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));

View File

@ -132,21 +132,23 @@ class SimBriefService extends Service
*
* 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;
@ -185,14 +187,14 @@ class SimBriefService extends Service
}
/**
* 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],
@ -202,7 +204,7 @@ class SimBriefService extends Service
foreach ($briefs as $brief) {
$brief->delete();
// TODO: Delete any assets
// TODO: Delete any assets (Which assets ?)
}
}
}