Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimBrief Airframes & Aircraft Weight Casting #1892

Merged
merged 19 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/Cron/Weekly/UpdateSimbriefData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Cron\Weekly;

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

class UpdateSimbriefData extends Listener
{
/**
* Update SimBrief Support Data
*
* @param CronWeekly $event
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function handle(CronWeekly $event): void
{
Log::info('Weekly: Updating SimBrief Support Data');
$SimBriefSVC = app(SimBriefService::class);
$SimBriefSVC->getAircraftAndAirframes();
$SimBriefSVC->GetBriefingLayouts();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
if (!Schema::hasTable('simbrief_aircraft')) {
Schema::create('simbrief_aircraft', function (Blueprint $table) {
$table->increments('id');
$table->string('icao');
$table->string('name');
$table->mediumText('details')->nullable();
$table->timestamps();
});
}

if (!Schema::hasTable('simbrief_airframes')) {
Schema::create('simbrief_airframes', function (Blueprint $table) {
$table->increments('id');
$table->string('icao');
$table->string('name');
$table->string('airframe_id')->nullable();
$table->unsignedTinyInteger('source')->nullable();
$table->mediumText('details')->nullable();
$table->mediumText('options')->nullable();
$table->timestamps();
});
}

if (!Schema::hasTable('simbrief_layouts')) {
Schema::create('simbrief_layouts', function (Blueprint $table) {
$table->string('id');
$table->string('name');
$table->string('name_long');
$table->timestamps();
});
}
}

public function down(): void
{
Schema::dropIfExists('simbrief_aircraft');
Schema::dropIfExists('simbrief_airframes');
Schema::dropIfExists('simbrief_layouts');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use App\Services\SimBriefService;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
if (Schema::hasTable('simbrief_aircraft') && Schema::hasTable('simbrief_airframes')) {
$SimBriefSVC = app(SimBriefService::class);
$SimBriefSVC->getAircraftAndAirframes();
$SimBriefSVC->GetBriefingLayouts();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

return new class() extends Migration {
public function up(): void
{
if (setting('units.weight') == 'kg') {
// Get all aircraft data, directly from database which had weights defined
$aircraft = DB::table('aircraft')->whereNotNull('dow')->orWhereNotNull('zfw')->orWhereNotNull('mtow')->orWhereNotNull('mlw')->orderBy('id')->get();
Log::debug('Begin weight conversion for '.$aircraft->count().' aircraft records');
foreach ($aircraft as $ac) {
Log::debug('Converting and Updating Weights for '.$ac->registration);
DB::table('aircraft')->where('id', $ac->id)->update([
'dow' => $this->PoundsConversion($ac->dow),
'zfw' => $this->PoundsConversion($ac->zfw),
'mtow' => $this->PoundsConversion($ac->mtow),
'mlw' => $this->PoundsConversion($ac->mlw),
]);
}
}
}

public function PoundsConversion($value)
{
if ($value > 0) {
return round($value / 0.45359237, 2);
}

return null;
}
};
14 changes: 14 additions & 0 deletions app/Database/seeds/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,20 @@
options: ''
type: boolean
description: 'When enabled, an aircraft can only be used for one active SimBrief OFP and Flight/Pirep'
- key: simbrief.use_standard_weights
name: 'Use Only phpVMS Weights'
group: simbrief
value: false
options: ''
type: boolean
description: 'When enabled, only phpVMS Passenger and Baggage weights will be used (instead of Airframe definitions)'
- key: simbrief.use_custom_airframes
name: 'Use Only Custom Airframes'
group: simbrief
value: false
options: ''
type: boolean
description: 'When enabled, only phpVMS Airframes will be listed for flight planning (instead of combined list)'
- key: pireps.duplicate_check_time
name: 'PIREP duplicate time check'
group: pireps
Expand Down
13 changes: 13 additions & 0 deletions app/Http/Controllers/Admin/AircraftController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Services\FileService;
use App\Services\FinanceService;
use App\Services\ImportService;
use App\Support\Units\Mass;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
Expand Down Expand Up @@ -119,6 +120,12 @@ public function create(Request $request): View
public function store(CreateAircraftRequest $request): RedirectResponse
{
$attrs = $request->all();
// Set the correct mass units
$attrs['dow'] = (filled($attrs['dow']) && $attrs['dow'] > 0) ? Mass::make((float) $request->input('dow'), setting('units.weight')) : null;
$attrs['zfw'] = (filled($attrs['zfw']) && $attrs['zfw'] > 0) ? Mass::make((float) $request->input('zfw'), setting('units.weight')) : null;
$attrs['mtow'] = (filled($attrs['mtow']) && $attrs['mtow'] > 0) ? Mass::make((float) $request->input('mtow'), setting('units.weight')) : null;
$attrs['mlw'] = (filled($attrs['mlw']) && $attrs['mlw'] > 0) ? Mass::make((float) $request->input('mlw'), setting('units.weight')) : null;

$aircraft = $this->aircraftRepo->create($attrs);

Flash::success('Aircraft saved successfully.');
Expand Down Expand Up @@ -204,6 +211,12 @@ public function update(int $id, UpdateAircraftRequest $request): RedirectRespons
}

$attrs = $request->all();
// Set the correct mass units
$attrs['dow'] = (filled($attrs['dow']) && $attrs['dow'] > 0) ? Mass::make((float) $request->input('dow'), setting('units.weight')) : null;
$attrs['zfw'] = (filled($attrs['zfw']) && $attrs['zfw'] > 0) ? Mass::make((float) $request->input('zfw'), setting('units.weight')) : null;
$attrs['mtow'] = (filled($attrs['mtow']) && $attrs['mtow'] > 0) ? Mass::make((float) $request->input('mtow'), setting('units.weight')) : null;
$attrs['mlw'] = (filled($attrs['mlw']) && $attrs['mlw'] > 0) ? Mass::make((float) $request->input('mlw'), setting('units.weight')) : null;

$this->aircraftRepo->update($attrs, $id);

Flash::success('Aircraft updated successfully.');
Expand Down
174 changes: 174 additions & 0 deletions app/Http/Controllers/Admin/AirframeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Contracts\Controller;
use App\Http\Requests\CreateAirframeRequest;
use App\Http\Requests\UpdateAirframeRequest;
use App\Models\Aircraft;
use App\Models\Enums\AirframeSource;
use App\Repositories\AirframeRepository;
use App\Services\SimBriefService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Laracasts\Flash\Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
use Prettus\Validator\Exceptions\ValidatorException;

class AirframeController extends Controller
{
/**
* @param AirframeRepository $airframeRepo
*/
public function __construct(
private readonly AirframeRepository $airframeRepo
) {
}

/**
* @param Request $request
*
* @throws RepositoryException
*
* @return View
*/
public function index(Request $request): View
{
$this->airframeRepo->pushCriteria(new RequestCriteria($request));
$airframes = $this->airframeRepo->where('source', AirframeSource::INTERNAL)->orderby('icao', 'asc')->orderby('name', 'asc')->get();

return view('admin.airframes.index', [
'airframes' => $airframes,
]);
}

/**
* @return View
*/
public function create(): View
{
return view('admin.airframes.create', [
'icao_codes' => Aircraft::whereNotNull('icao')->groupBy('icao')->pluck('icao')->toArray(),
]);
}

/**
* @param CreateAirframeRequest $request
*
* @throws ValidatorException
*
* @return RedirectResponse
*/
public function store(CreateAirframeRequest $request): RedirectResponse
{
$input = $request->all();

$model = $this->airframeRepo->create($input);
Flash::success('Airframe saved successfully.');

return redirect(route('admin.airframes.index'));
}

/**
* @param int $id
*
* @return RedirectResponse|View
*/
public function show(int $id): RedirectResponse|View
{
$airframe = $this->airframeRepo->findWithoutFail($id);

if (empty($airframe)) {
Flash::error('SimBrief Airframe not found');

return redirect(route('admin.airframes.index'));
}

return view('admin.airframes.show', [
'airframe' => $airframe,
]);
}

/**
* @param int $id
*
* @return RedirectResponse|View
*/
public function edit(int $id): RedirectResponse|View
{
$airframe = $this->airframeRepo->findWithoutFail($id);

if (empty($airframe)) {
Flash::error('SimBrief Airframe not found');

return redirect(route('admin.airframes.index'));
}

return view('admin.airframes.edit', [
'airframe' => $airframe,
'icao_codes' => Aircraft::whereNotNull('icao')->groupBy('icao')->pluck('icao')->toArray(),
]);
}

/**
* @param int $id
* @param UpdateAirframeRequest $request
*
* @throws ValidatorException
*
* @return RedirectResponse
*/
public function update(int $id, UpdateAirframeRequest $request): RedirectResponse
{
$airframe = $this->airframeRepo->findWithoutFail($id);

if (empty($airframe)) {
Flash::error('SimBrief Airframe not found');

return redirect(route('admin.airframes.index'));
}

$airframe = $this->airframeRepo->update($request->all(), $id);
Flash::success('SimBrief Airport updated successfully.');

return redirect(route('admin.airframes.index'));
}

/**
* @param int $id
*
* @return RedirectResponse
*/
public function destroy(int $id): RedirectResponse
{
$airframe = $this->airframeRepo->findWithoutFail($id);

if (empty($airframe)) {
Flash::error('SimBrief Airframe not found');

return redirect(route('admin.airframes.index'));
}

$this->airframeRepo->delete($id);

Flash::success('SimBrief Airframe deleted successfully.');

return redirect(route('admin.airframes.index'));
}

// Manually trigger update of SimBrief Airframe and Layouts
public function updateSimbriefData()
{
Log::debug('Manually Updating SimBrief Support Data');
$SimBriefSVC = app(SimBriefService::class);
$SimBriefSVC->getAircraftAndAirframes();
$SimBriefSVC->GetBriefingLayouts();

Flash::success('SimBrief Airframe and Layouts updated successfully.');

return redirect(route('admin.airframes.index'));
}
}
Loading
Loading