diff --git a/app/Console/Commands/AcarsReplay.php b/app/Console/Commands/AcarsReplay.php index 2d9ed74e..435fd71f 100644 --- a/app/Console/Commands/AcarsReplay.php +++ b/app/Console/Commands/AcarsReplay.php @@ -11,7 +11,7 @@ use App\Facades\Utils; class AcarsReplay extends Command { - protected $signature = 'phpvms:replay {files} {--manual}'; + protected $signature = 'phpvms:replay {files} {--manual} {--write-all}'; protected $description = 'Replay an ACARS file'; /** @@ -67,6 +67,7 @@ class AcarsReplay extends Command $response = $this->httpClient->post('/api/pirep/prefile', [ 'json' => [ 'airline_id' => 1, + 'flight_number' => '6028', 'aircraft_id' => 1, # TODO: Lookup 'dpt_airport_id' => $flight->planned_depairport, 'arr_airport_id' => $flight->planned_destairport, @@ -80,9 +81,24 @@ class AcarsReplay extends Command return $body->id; } + /** + * Mark the PIREP as filed + * @param $pirep_id + */ + protected function filePirep($pirep_id) + { + $response = $this->httpClient->post('/api/pirep/'.$pirep_id.'/file', [ + 'json'=> [] + ]); + + $body = \json_decode($response->getBody()->getContents()); + return $body; + } + /** * @param $pirep_id * @param $data + * @return array */ protected function postUpdate($pirep_id, $data) { @@ -100,9 +116,6 @@ class AcarsReplay extends Command $this->info("Update: $data->callsign, $upd[lat] x $upd[lon] \t\t" . "hdg: $upd[heading]\t\talt: $upd[altitude]\t\tgs: $upd[gs]"); - /*$this->table([], [[ - $data->callsign, $upd['lat'], $upd['lon'], $upd['heading'], $upd['altitude'], $upd['gs'] - ]]);*/ $response = $this->httpClient->post($uri, [ 'json' => $upd @@ -168,26 +181,26 @@ class AcarsReplay extends Command * Continue until we have no more flights and updates left */ while ($flights->count() > 0) { - $updated_rows = []; - $flights = $flights->each(function ($updates, $idx) - { + $flights = $flights->each(function ($updates, $idx) { $update = $updates->shift(); $pirep_id = $this->pirepList[$update->callsign]; - $row = $this->postUpdate($pirep_id, $update); - $updated_rows[] = $row; + $this->postUpdate($pirep_id, $update); + + # we're done + if($updates->count() === 0) { + $this->filePirep($pirep_id); + } })->filter(function ($updates, $idx) { return $updates->count() > 0; }); - /*$this->table( - ['callsign', 'lat', 'lon', 'hdg', 'alt', 'gs'], - $updated_rows);*/ - - if(!$this->option('manual')) { - sleep($this->sleepTime); - } else { - $this->confirm('Send next batch of updates?', true); + if(!$this->option('write-all')) { + if (!$this->option('manual')) { + sleep($this->sleepTime); + } else { + $this->confirm('Send next batch of updates?', true); + } } } } @@ -202,10 +215,14 @@ class AcarsReplay extends Command $files = $this->argument('files'); $manual_mode = $this->option('manual'); - if(!$manual_mode) { - $this->info('Going to send updates every 10s'); + if ($this->option('write-all')) { + $this->info('In "dump-all" mode, just writing it all in'); } else { - $this->info('In "manual advance" mode'); + if (!$manual_mode) { + $this->info('Going to send updates every 10s'); + } else { + $this->info('In "manual advance" mode'); + } } $this->runUpdates(explode(',', $files)); diff --git a/app/Database/seeds/dev.yml b/app/Database/seeds/dev.yml index 010b5835..21478beb 100644 --- a/app/Database/seeds/dev.yml +++ b/app/Database/seeds/dev.yml @@ -1,4 +1,4 @@ -# + airlines: - id: 1 icao: VMS @@ -222,122 +222,14 @@ flights: updated_at: NOW - id: flightid_2 airline_id: 1 - flight_number: 101 - dpt_airport_id: KJFK + flight_number: 6028 + dpt_airport_id: KIAH arr_airport_id: KAUS - dpt_time: 9AM EST - arr_time: 12PM CST - route: KJFK KAUS + dpt_time: 9AM CST + arr_time: 1030AM CST + route: PITZZ4 MNURE WLEEE4 created_at: NOW updated_at: NOW - - id: flightid_3 - airline_id: 1 - flight_number: 200 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 9AM EST - arr_time: 12PM CST - route: KJFK KAUS - created_at: NOW - updated_at: NOW - - id: flightid_4 - airline_id: 1 - flight_number: 201 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 10AM EST - arr_time: 1PM CST - route: KJFK KAUS - created_at: NOW - updated_at: NOW - - id: flightid_5 - airline_id: 1 - flight_number: 202 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 11AM EST - arr_time: 2PM CST - route: KJFK KAUS - - id: flightid_6 - airline_id: 1 - flight_number: 203 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 12PM EST - arr_time: 3PM CST - route: KJFK KAUS - - id: flightid_7 - airline_id: 1 - flight_number: 204 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 1PM EST - arr_time: 4PM CST - route: KJFK KAUS - - id: flightid_8 - airline_id: 1 - flight_number: 205 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 2PM EST - arr_time: 5PM CST - route: KJFK KAUS - - id: flightid_9 - airline_id: 1 - flight_number: 206 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 3PM EST - arr_time: 6PM CST - route: KJFK KAUS - - id: flightid_10 - airline_id: 1 - flight_number: 207 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 4PM EST - arr_time: 7PM CST - route: KJFK KAUS - - id: flightid_11 - airline_id: 1 - flight_number: 208 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 5PM EST - arr_time: 8PM CST - route: KJFK KAUS - - id: flightid_12 - airline_id: 1 - flight_number: 209 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 6PM EST - arr_time: 9PM CST - route: KJFK KAUS - - id: flightid_13 - airline_id: 1 - flight_number: 210 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 7PM EST - arr_time: 10PM CST - route: KJFK KAUS - - id: flightid_14 - airline_id: 1 - flight_number: 211 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 8PM EST - arr_time: 11PM CST - route: KJFK KAUS - - id: flightid_15 - airline_id: 1 - flight_number: 212 - dpt_airport_id: KJFK - arr_airport_id: KAUS - dpt_time: 9PM EST - arr_time: 12AM CST - route: KJFK KAUS flight_fields: - id: 1 diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php index f5a474cf..0ebc42f7 100644 --- a/app/Http/Controllers/Api/PirepController.php +++ b/app/Http/Controllers/Api/PirepController.php @@ -12,7 +12,7 @@ use App\Models\Enums\PirepStatus; use App\Http\Resources\Acars as AcarsResource; use App\Http\Resources\Pirep as PirepResource; - +use App\Services\PIREPService; use App\Repositories\AcarsRepository; use App\Repositories\PirepRepository; @@ -20,14 +20,16 @@ use App\Http\Controllers\AppBaseController; class PirepController extends AppBaseController { - protected $acarsRepo, $pirepRepo; + protected $acarsRepo, $pirepRepo, $pirepSvc; public function __construct( AcarsRepository $acarsRepo, - PirepRepository $pirepRepo + PirepRepository $pirepRepo, + PIREPService $pirepSvc ) { $this->acarsRepo = $acarsRepo; $this->pirepRepo = $pirepRepo; + $this->pirepSvc = $pirepSvc; } public function get($id) @@ -48,29 +50,37 @@ class PirepController extends AppBaseController Log::info('PIREP Prefile, user '. Auth::user()->pilot_id, $request->toArray()); - /*$validator = Validator::make($request, [ - 'aircraft_id' => 'required', - 'dpt_airport_id' => 'required', - 'arr_airport_id' => 'required', - 'altitude' => 'nullable|integer', - 'route' => 'nullable', - 'notes' => 'nullable', - ]);*/ + $check_attrs = [ + 'airline_id', + 'aircraft_id', + 'dpt_airport_id', + 'arr_airport_id', + 'flight_id', + 'flight_number', + 'route_leg', + 'route_code', + 'flight_time', + 'planned_flight_time', + 'altitude', + 'route', + 'notes', + ]; - $attr = []; - $attr['user_id'] = Auth::user()->id; - $attr['airline_id'] = $request->get('airline_id'); - $attr['aircraft_id'] = $request->get('aircraft_id'); - $attr['dpt_airport_id'] = $request->get('dpt_airport_id'); - $attr['arr_airport_id'] = $request->get('arr_airport_id'); - $attr['altitude'] = $request->get('altitude'); - $attr['route'] = $request->get('route'); - $attr['notes'] = $request->get('notes'); - $attr['state'] = PirepState::IN_PROGRESS; - $attr['status'] = PirepStatus::PREFILE; + $attrs = [ + 'user_id' => Auth::user()->id, + ]; + + foreach ($check_attrs as $attr) { + if ($request->filled($attr)) { + $attrs[$attr] = $request->get($attr); + } + } + + $attrs['state'] = PirepState::IN_PROGRESS; + $attrs['status'] = PirepStatus::PREFILE; try { - $pirep = $this->pirepRepo->create($attr); + $pirep = $this->pirepRepo->create($attrs); } catch(\Exception $e) { Log::error($e); } @@ -82,6 +92,58 @@ class PirepController extends AppBaseController return new PirepResource($pirep); } + /** + * File the PIREP + * @param $id + * @param Request $request + * @return PirepResource + */ + public function file($id, Request $request) + { + Log::info('PIREP Prefile, user ' . Auth::user()->pilot_id, + $request->toArray()); + + $attrs = []; + $check_attrs = [ + 'airline_id', + 'aircraft_id', + 'dpt_airport_id', + 'arr_airport_id', + 'flight_id', + 'flight_number', + 'route_leg', + 'route_code', + 'flight_time', + 'planned_flight_time', + 'altitude', + 'route', + 'notes', + ]; + + foreach($check_attrs as $attr) { + if($request->filled($attr)) { + $attrs[$attr] = $request->get($attr); + } + } + + if($request->filled('fields')) { + $pirep_fields = $request->get('fields'); + } + + $attrs['state'] = PirepState::PENDING; + $attrs['status'] = PirepStatus::ARRIVED; + + try { + $pirep = $this->pirepRepo->update($attrs, $id); + $pirep = $this->pirepSvc->create($pirep, $pirep_fields); + } catch (\Exception $e) { + Log::error($e); + } + + PirepResource::withoutWrapping(); + return new PirepResource($pirep); + } + /** * Get all of the ACARS updates for a PIREP * @param $id diff --git a/app/Routes/api.php b/app/Routes/api.php index f84909e8..e97ce731 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -25,6 +25,7 @@ Route::group([], function () Route::match(['get'], 'pirep/{id}', 'PirepController@get'); Route::match(['post'], 'pirep/prefile', 'PirepController@prefile'); + Route::match(['post'], 'pirep/{id}/file', 'PirepController@file'); Route::match(['get'], 'pirep/{id}/acars', 'PirepController@acars_get'); Route::match(['post'], 'pirep/{id}/acars', 'PirepController@acars_store'); diff --git a/resources/lang/en/system.php b/resources/lang/en/system.php index e34ccafd..31c4e799 100644 --- a/resources/lang/en/system.php +++ b/resources/lang/en/system.php @@ -16,6 +16,7 @@ return [ 'accepted' => 'Accepted', 'pending' => 'Pending', 'rejected' => 'Rejected', + 'in_progress' => 'In Progress', ], ], ]; diff --git a/resources/views/layouts/default/pireps/pirep_card.blade.php b/resources/views/layouts/default/pireps/pirep_card.blade.php index d9cfe26b..c85c7ea4 100644 --- a/resources/views/layouts/default/pireps/pirep_card.blade.php +++ b/resources/views/layouts/default/pireps/pirep_card.blade.php @@ -14,12 +14,15 @@
@if($pirep->state == PirepState::PENDING) -
Pending
+
@elseif($pirep->state == PirepState::ACCEPTED) -
Accepted
+
+ @elseif($pirep->state == PirepState::REJECTED) +
@else -
Rejected
+
@endif + {!! PirepState::label($pirep->state) !!}