2017-08-15 12:36:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
use Log;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
use App\Models\Acars;
|
2018-01-02 00:30:31 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
|
|
|
use App\Models\Enums\PirepStatus;
|
|
|
|
|
2018-01-02 00:30:31 +08:00
|
|
|
use App\Services\GeoService;
|
2017-12-27 04:54:28 +08:00
|
|
|
use App\Services\PIREPService;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Repositories\AcarsRepository;
|
2017-12-13 01:49:35 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2017-08-15 12:36:49 +08:00
|
|
|
|
2018-01-02 00:30:31 +08:00
|
|
|
use App\Http\Resources\Acars as AcarsResource;
|
|
|
|
use App\Http\Resources\Pirep as PirepResource;
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Http\Controllers\AppBaseController;
|
2017-08-15 12:36:49 +08:00
|
|
|
|
|
|
|
class PirepController extends AppBaseController
|
|
|
|
{
|
2017-12-28 10:52:37 +08:00
|
|
|
protected $acarsRepo,
|
|
|
|
$geoSvc,
|
|
|
|
$pirepRepo,
|
|
|
|
$pirepSvc;
|
2017-12-13 01:49:35 +08:00
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
public function __construct(
|
|
|
|
AcarsRepository $acarsRepo,
|
2017-12-28 10:52:37 +08:00
|
|
|
GeoService $geoSvc,
|
2017-12-27 04:54:28 +08:00
|
|
|
PirepRepository $pirepRepo,
|
|
|
|
PIREPService $pirepSvc
|
2017-12-26 05:19:34 +08:00
|
|
|
) {
|
|
|
|
$this->acarsRepo = $acarsRepo;
|
2017-12-28 10:52:37 +08:00
|
|
|
$this->geoSvc = $geoSvc;
|
2017-12-13 01:49:35 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
2017-12-27 04:54:28 +08:00
|
|
|
$this->pirepSvc = $pirepSvc;
|
2017-12-13 01:49:35 +08:00
|
|
|
}
|
|
|
|
|
2017-08-15 12:36:49 +08:00
|
|
|
public function get($id)
|
|
|
|
{
|
2017-12-13 01:49:35 +08:00
|
|
|
PirepResource::withoutWrapping();
|
|
|
|
return new PirepResource($this->pirepRepo->find($id));
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new PIREP and place it in a "inprogress" and "prefile" state
|
|
|
|
* Once ACARS updates are being processed, then it can go into an 'ENROUTE'
|
|
|
|
* status, and whatever other statuses may be defined
|
|
|
|
*
|
|
|
|
* TODO: Allow extra fields, etc to be set. Aircraft, etc
|
|
|
|
*/
|
|
|
|
public function prefile(Request $request)
|
|
|
|
{
|
|
|
|
Log::info('PIREP Prefile, user '. Auth::user()->pilot_id,
|
|
|
|
$request->toArray());
|
|
|
|
|
2017-12-27 04:54:28 +08:00
|
|
|
$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',
|
2018-01-02 03:48:02 +08:00
|
|
|
'level',
|
2017-12-27 04:54:28 +08:00
|
|
|
'route',
|
|
|
|
'notes',
|
|
|
|
];
|
|
|
|
|
|
|
|
$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;
|
2017-12-26 05:19:34 +08:00
|
|
|
|
|
|
|
try {
|
2017-12-27 04:54:28 +08:00
|
|
|
$pirep = $this->pirepRepo->create($attrs);
|
2017-12-26 05:19:34 +08:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('PIREP PREFILED');
|
|
|
|
Log::info($pirep->id);
|
|
|
|
|
|
|
|
PirepResource::withoutWrapping();
|
2017-12-27 04:54:28 +08:00
|
|
|
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',
|
2018-01-02 03:48:02 +08:00
|
|
|
'level',
|
2017-12-27 04:54:28 +08:00
|
|
|
'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();
|
2017-12-26 05:19:34 +08:00
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-29 04:35:28 +08:00
|
|
|
* Return the GeoJSON for the ACARS line
|
2017-12-26 05:19:34 +08:00
|
|
|
* @param $id
|
2017-12-29 04:35:28 +08:00
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory
|
2017-12-26 05:19:34 +08:00
|
|
|
*/
|
2017-12-29 04:35:28 +08:00
|
|
|
public function acars_get($id, Request $request)
|
2017-12-26 05:19:34 +08:00
|
|
|
{
|
2017-12-26 08:22:46 +08:00
|
|
|
$pirep = $this->pirepRepo->find($id);
|
2017-12-29 04:35:28 +08:00
|
|
|
$geodata = $this->geoSvc->getFeatureFromAcars($pirep);
|
2017-12-26 08:22:46 +08:00
|
|
|
|
2017-12-29 04:35:28 +08:00
|
|
|
return response(\json_encode($geodata), 200, [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
]);
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ACARS updates for a PIREP
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return AcarsResource
|
|
|
|
*/
|
|
|
|
public function acars_store($id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
Log::info('Posting ACARS update', $request->toArray());
|
|
|
|
$attrs = $request->toArray();
|
2018-01-02 00:30:31 +08:00
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
$attrs['pirep_id'] = $id;
|
2018-01-02 00:30:31 +08:00
|
|
|
$attrs['type'] = AcarsType::FLIGHT_PATH;
|
2017-12-26 05:19:34 +08:00
|
|
|
|
|
|
|
$update = Acars::create($attrs);
|
|
|
|
$update->save();
|
|
|
|
|
|
|
|
# Change the PIREP status
|
|
|
|
$pirep->status = PirepStatus::ENROUTE;
|
|
|
|
$pirep->save();
|
|
|
|
|
|
|
|
AcarsResource::withoutWrapping();
|
|
|
|
return new AcarsResource($update);
|
|
|
|
}
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|