2017-08-15 12:36:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
use Log;
|
2018-01-04 00:25:55 +08:00
|
|
|
use Auth;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2017-12-26 05:19:34 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
use App\Models\Acars;
|
2018-01-03 03:17:22 +08:00
|
|
|
use App\Models\Pirep;
|
|
|
|
|
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;
|
2018-01-05 09:33:23 +08:00
|
|
|
use App\Http\Resources\AcarsLog as AcarsLogResource;
|
|
|
|
use App\Http\Resources\AcarsRoute as AcarsRouteResource;
|
2018-01-02 00:30:31 +08:00
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2017-08-15 12:36:49 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
class PirepController extends RestController
|
2017-08-15 12:36:49 +08:00
|
|
|
{
|
2018-01-05 09:33:23 +08:00
|
|
|
public static $acars_rules = [
|
|
|
|
'altitude',
|
|
|
|
'level',
|
|
|
|
'heading',
|
|
|
|
'vs',
|
|
|
|
'gs',
|
|
|
|
'transponder',
|
|
|
|
'autopilot',
|
|
|
|
'fuel_flow',
|
|
|
|
'log',
|
|
|
|
'lat',
|
|
|
|
'lon',
|
|
|
|
'created_at',
|
|
|
|
];
|
2017-12-13 01:49:35 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
public static $pirep_rules = [
|
2018-01-02 06:01:01 +08:00
|
|
|
'airline_id',
|
|
|
|
'aircraft_id',
|
|
|
|
'dpt_airport_id',
|
|
|
|
'arr_airport_id',
|
|
|
|
'flight_id',
|
|
|
|
'flight_number',
|
|
|
|
'route_code',
|
2018-01-03 04:31:00 +08:00
|
|
|
'route_leg',
|
2018-01-02 06:01:01 +08:00
|
|
|
'flight_time',
|
|
|
|
'planned_flight_time',
|
|
|
|
'level',
|
|
|
|
'route',
|
|
|
|
'notes',
|
|
|
|
];
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
protected $acarsRepo,
|
|
|
|
$geoSvc,
|
|
|
|
$pirepRepo,
|
|
|
|
$pirepSvc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PirepController constructor.
|
|
|
|
* @param AcarsRepository $acarsRepo
|
|
|
|
* @param GeoService $geoSvc
|
|
|
|
* @param PirepRepository $pirepRepo
|
|
|
|
* @param PIREPService $pirepSvc
|
|
|
|
*/
|
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
|
2018-01-05 09:33:23 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2017-12-26 05:19:34 +08:00
|
|
|
*/
|
|
|
|
public function prefile(Request $request)
|
|
|
|
{
|
2018-01-04 00:25:55 +08:00
|
|
|
Log::info('PIREP Prefile, user '.Auth::user()->id, $request->toArray());
|
2017-12-27 04:54:28 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$attrs = $this->getFromReq($request, self::$pirep_rules, [
|
|
|
|
'user_id' => Auth::user()->id,
|
|
|
|
'state' => PirepState::IN_PROGRESS,
|
|
|
|
'status' => PirepStatus::PREFILE,
|
|
|
|
]);
|
2017-12-27 04:54:28 +08:00
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
$pirep = new Pirep($attrs);
|
|
|
|
|
|
|
|
# Find if there's a duplicate, if so, let's work on that
|
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
if($dupe_pirep !== false) {
|
|
|
|
$pirep = $dupe_pirep;
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
$pirep->save();
|
|
|
|
|
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
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
|
2018-01-04 00:25:55 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
2017-12-27 04:54:28 +08:00
|
|
|
*/
|
|
|
|
public function file($id, Request $request)
|
|
|
|
{
|
2018-01-02 06:01:01 +08:00
|
|
|
Log::info('PIREP Prefile, user ' . Auth::user()->pilot_id, $request->toArray());
|
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
if (empty($pirep)) {
|
|
|
|
throw new ModelNotFoundException('PIREP not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check if the status is cancelled...
|
|
|
|
if($pirep->state === PirepState::CANCELLED) {
|
|
|
|
throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted');
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$attrs = $this->getFromReq($request, self::$pirep_rules, [
|
|
|
|
'state' => PirepState::PENDING,
|
|
|
|
'status' => PirepStatus::ARRIVED,
|
|
|
|
]);
|
2017-12-27 04:54:28 +08:00
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
$pirep_fields = [];
|
2017-12-27 04:54:28 +08:00
|
|
|
if($request->filled('fields')) {
|
|
|
|
$pirep_fields = $request->get('fields');
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
/**
|
|
|
|
* Cancel the PIREP
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return PirepResource
|
|
|
|
*/
|
|
|
|
public function cancel($id, Request $request)
|
|
|
|
{
|
|
|
|
Log::info('PIREP Cancel, user ' . Auth::user()->pilot_id, $request->toArray());
|
|
|
|
|
|
|
|
$attrs = [
|
|
|
|
'state' => PirepState::CANCELLED,
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$pirep = $this->pirepRepo->update($attrs, $id);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
PirepResource::withoutWrapping();
|
|
|
|
return new PirepResource($pirep);
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
/**
|
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
|
|
|
*/
|
2018-01-05 09:33:23 +08:00
|
|
|
public function acars_geojson($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
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
* Return the GeoJSON for the ACARS line
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return AcarsRouteResource
|
|
|
|
*/
|
|
|
|
public function acars_get($id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
AcarsRouteResource::withoutWrapping();
|
|
|
|
return new AcarsRouteResource(Acars::where([
|
|
|
|
'pirep_id' => $id,
|
|
|
|
'type' => AcarsType::FLIGHT_PATH
|
|
|
|
])->orderBy('created_at', 'asc')->get());
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
/**
|
|
|
|
* Post ACARS updates for a PIREP
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
2018-01-05 09:33:23 +08:00
|
|
|
* @return AcarsRouteResource
|
2018-01-04 00:25:55 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2017-12-26 05:19:34 +08:00
|
|
|
*/
|
|
|
|
public function acars_store($id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
# Check if the status is cancelled...
|
|
|
|
if ($pirep->state === PirepState::CANCELLED) {
|
|
|
|
throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted');
|
|
|
|
}
|
|
|
|
|
2017-12-26 05:19:34 +08:00
|
|
|
Log::info('Posting ACARS update', $request->toArray());
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$this->validate($request, ['positions' => 'required']);
|
|
|
|
$positions = $request->post()['positions'];
|
|
|
|
|
|
|
|
foreach($positions as $position)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$attrs = $this->getFromReq(
|
|
|
|
$position,
|
|
|
|
self::$acars_rules,
|
|
|
|
['pirep_id' => $id, 'type' => AcarsType::FLIGHT_PATH]
|
|
|
|
);
|
|
|
|
|
|
|
|
$update = Acars::create($attrs);
|
|
|
|
$update->save();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
|
|
|
|
# Change the PIREP status
|
|
|
|
$pirep->status = PirepStatus::ENROUTE;
|
|
|
|
$pirep->save();
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
return $this->acars_get($id, $request);
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
2018-01-04 12:16:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ACARS LOG update for a PIREP. These updates won't show up on the map
|
|
|
|
* But rather in a log file.
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
2018-01-05 09:33:23 +08:00
|
|
|
* @return AcarsLogResource
|
2018-01-04 12:16:18 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
|
|
|
*/
|
|
|
|
public function acars_log($id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
# Check if the status is cancelled...
|
|
|
|
if ($pirep->state === PirepState::CANCELLED) {
|
|
|
|
throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted');
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
Log::info('Posting ACARS log', $request->toArray());
|
2018-01-04 12:16:18 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$attrs = $this->getFromReq($request, [
|
2018-01-05 09:39:41 +08:00
|
|
|
'log' => 'required',
|
|
|
|
'lat' => 'nullable',
|
|
|
|
'lon' => 'nullable',
|
|
|
|
'created_at' => 'nullable',
|
2018-01-05 09:33:23 +08:00
|
|
|
], ['pirep_id' => $id, 'type' => AcarsType::LOG]);
|
2018-01-04 12:16:18 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$acars = Acars::create($attrs);
|
|
|
|
$acars->save();
|
2018-01-04 12:16:18 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
AcarsLogResource::withoutWrapping();
|
|
|
|
return new AcarsLogResource($acars);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return AcarsRouteResource
|
|
|
|
*/
|
|
|
|
public function route_get($id, Request $request)
|
|
|
|
{
|
|
|
|
$this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
AcarsRouteResource::withoutWrapping();
|
|
|
|
return new AcarsRouteResource(Acars::where([
|
|
|
|
'pirep_id' => $id,
|
|
|
|
'type' => AcarsType::ROUTE
|
|
|
|
])->orderBy('order', 'asc')->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post the ROUTE for a PIREP, can be done from the ACARS log
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return AcarsRouteResource
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
|
|
|
*/
|
|
|
|
public function route_post($id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = $this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
# Check if the status is cancelled...
|
|
|
|
if ($pirep->state === PirepState::CANCELLED) {
|
|
|
|
throw new BadRequestHttpException('PIREP has been cancelled, updates can\'t be posted');
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Posting ACARS ROUTE', $request->toArray());
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'route.*.name' => 'required',
|
|
|
|
'route.*.order' => 'required|int',
|
|
|
|
'route.*.nav_type' => 'nullable|int',
|
|
|
|
'route.*.lat' => 'required|numeric',
|
|
|
|
'route.*.lon' => 'required|numeric',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$route = $request->all()['route'];
|
|
|
|
foreach($route as $position) {
|
|
|
|
$attrs = [
|
|
|
|
'pirep_id' => $id,
|
|
|
|
'type' => AcarsType::ROUTE,
|
|
|
|
'name' => $position['name'],
|
|
|
|
'order' => $position['order'],
|
|
|
|
'lat' => $position['lat'],
|
|
|
|
'lon' => $position['lon'],
|
|
|
|
];
|
|
|
|
|
|
|
|
if(array_key_exists('nav_type', $position)) {
|
|
|
|
$attrs['nav_type'] = $position['nav_type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$acars = Acars::create($attrs);
|
|
|
|
$acars->save();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->route_get($id, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function route_delete($id, Request $request)
|
|
|
|
{
|
|
|
|
$this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
Acars::where([
|
|
|
|
'pirep_id' => $id,
|
|
|
|
'type' => AcarsType::ROUTE
|
|
|
|
])->delete();
|
2018-01-04 12:16:18 +08:00
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
return $this->message('Route deleted');
|
2018-01-04 12:16:18 +08:00
|
|
|
}
|
2017-08-15 12:36:49 +08:00
|
|
|
}
|