2017-12-26 05:19:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
use App\Exceptions\PirepCancelled;
|
|
|
|
use App\Http\Requests\Acars\EventRequest;
|
|
|
|
use App\Http\Requests\Acars\LogRequest;
|
|
|
|
use App\Http\Requests\Acars\PositionRequest;
|
|
|
|
use App\Http\Resources\AcarsRoute as AcarsRouteResource;
|
2018-03-20 09:50:40 +08:00
|
|
|
use App\Interfaces\Controller;
|
2018-05-10 01:45:24 +08:00
|
|
|
use App\Models\Acars;
|
|
|
|
use App\Models\Enums\AcarsType;
|
|
|
|
use App\Models\Enums\PirepStatus;
|
|
|
|
use App\Models\Pirep;
|
2017-12-26 05:19:34 +08:00
|
|
|
use App\Repositories\AcarsRepository;
|
2018-05-10 01:45:24 +08:00
|
|
|
use App\Repositories\PirepRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\GeoService;
|
2018-05-10 01:45:24 +08:00
|
|
|
use Auth;
|
|
|
|
use Carbon\Carbon;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2018-05-10 01:45:24 +08:00
|
|
|
use Log;
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class AcarsController
|
|
|
|
*/
|
|
|
|
class AcarsController extends Controller
|
2017-12-26 05:19:34 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
private $acarsRepo;
|
|
|
|
private $geoSvc;
|
|
|
|
private $pirepRepo;
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2018-02-21 12:33:09 +08:00
|
|
|
/**
|
|
|
|
* AcarsController constructor.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param AcarsRepository $acarsRepo
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param GeoService $geoSvc
|
|
|
|
* @param PirepRepository $pirepRepo
|
2018-02-21 12:33:09 +08:00
|
|
|
*/
|
2017-12-26 05:19:34 +08:00
|
|
|
public function __construct(
|
2018-05-10 01:45:24 +08:00
|
|
|
AcarsRepository $acarsRepo,
|
2017-12-28 06:47:22 +08:00
|
|
|
GeoService $geoSvc,
|
2018-08-27 02:50:08 +08:00
|
|
|
PirepRepository $pirepRepo
|
2017-12-26 05:19:34 +08:00
|
|
|
) {
|
2017-12-28 06:47:22 +08:00
|
|
|
$this->geoSvc = $geoSvc;
|
2017-12-26 05:19:34 +08:00
|
|
|
$this->acarsRepo = $acarsRepo;
|
2018-05-10 01:45:24 +08:00
|
|
|
$this->pirepRepo = $pirepRepo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a PIREP is cancelled
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
*/
|
|
|
|
protected function checkCancelled(Pirep $pirep)
|
|
|
|
{
|
2018-05-12 01:08:55 +08:00
|
|
|
if ($pirep->cancelled) {
|
2018-05-10 01:45:24 +08:00
|
|
|
throw new PirepCancelled();
|
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-28 06:47:22 +08:00
|
|
|
* Return all of the flights (as points) in GeoJSON format
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-02-21 12:33:09 +08:00
|
|
|
* @return mixed
|
2017-12-26 05:19:34 +08:00
|
|
|
*/
|
2017-12-28 06:47:22 +08:00
|
|
|
public function index(Request $request)
|
2017-12-26 05:19:34 +08:00
|
|
|
{
|
2018-09-21 02:27:39 +08:00
|
|
|
$pireps = $this->acarsRepo->getPositions(setting('acars.live_time'));
|
2017-12-28 06:47:22 +08:00
|
|
|
$positions = $this->geoSvc->getFeatureForLiveFlights($pireps);
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2017-12-28 06:47:22 +08:00
|
|
|
return response(json_encode($positions), 200, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'Content-type' => 'application/json',
|
2017-12-28 06:47:22 +08:00
|
|
|
]);
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
2018-05-10 01:45:24 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the GeoJSON for the ACARS line
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $pirep_id
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory
|
|
|
|
*/
|
|
|
|
public function acars_geojson($pirep_id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = Pirep::find($pirep_id);
|
|
|
|
$geodata = $this->geoSvc->getFeatureFromAcars($pirep);
|
|
|
|
|
|
|
|
return response(\json_encode($geodata), 200, [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the routes for the ACARS line
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @return AcarsRouteResource
|
|
|
|
*/
|
|
|
|
public function acars_get($id, Request $request)
|
|
|
|
{
|
|
|
|
$this->pirepRepo->find($id);
|
|
|
|
|
|
|
|
return new AcarsRouteResource(Acars::where([
|
|
|
|
'pirep_id' => $id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'type' => AcarsType::FLIGHT_PATH,
|
2018-05-29 07:15:51 +08:00
|
|
|
])->orderBy('sim_time', 'asc')->get());
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ACARS updates for a PIREP
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $id
|
|
|
|
* @param PositionRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-05-10 01:45:24 +08:00
|
|
|
*/
|
|
|
|
public function acars_store($id, PositionRequest $request)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the status is cancelled...
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = Pirep::find($id);
|
|
|
|
$this->checkCancelled($pirep);
|
|
|
|
|
|
|
|
Log::debug(
|
|
|
|
'Posting ACARS update (user: '.Auth::user()->pilot_id.', pirep id :'.$id.'): ',
|
|
|
|
$request->post()
|
|
|
|
);
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$positions = $request->post('positions');
|
|
|
|
foreach ($positions as $position) {
|
|
|
|
$position['pirep_id'] = $id;
|
|
|
|
$position['type'] = AcarsType::FLIGHT_PATH;
|
|
|
|
|
|
|
|
if (array_key_exists('sim_time', $position)) {
|
2018-08-27 01:25:51 +08:00
|
|
|
if ($position['sim_time'] instanceof \DateTime) {
|
|
|
|
$position['sim_time'] = Carbon::instance($position['sim_time']);
|
|
|
|
} else {
|
|
|
|
$position['sim_time'] = Carbon::createFromTimeString($position['sim_time']);
|
|
|
|
}
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('created_at', $position)) {
|
2018-08-27 01:25:51 +08:00
|
|
|
if ($position['created_at'] instanceof \DateTime) {
|
|
|
|
$position['created_at'] = Carbon::instance($position['created_at']);
|
|
|
|
} else {
|
|
|
|
$position['created_at'] = Carbon::createFromTimeString($position['created_at']);
|
|
|
|
}
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$update = Acars::create($position);
|
|
|
|
$update->save();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Change the PIREP status if it's as SCHEDULED before
|
2018-05-10 01:45:24 +08:00
|
|
|
if ($pirep->status === PirepStatus::INITIATED) {
|
|
|
|
$pirep->status = PirepStatus::AIRBORNE;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pirep->save();
|
|
|
|
|
|
|
|
return $this->message($count.' positions added', $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ACARS LOG update for a PIREP. These updates won't show up on the map
|
|
|
|
* But rather in a log file.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $id
|
|
|
|
* @param LogRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-05-10 01:45:24 +08:00
|
|
|
*/
|
|
|
|
public function acars_logs($id, LogRequest $request)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the status is cancelled...
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = Pirep::find($id);
|
|
|
|
$this->checkCancelled($pirep);
|
|
|
|
|
|
|
|
Log::debug('Posting ACARS log, PIREP: '.$id, $request->post());
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$logs = $request->post('logs');
|
|
|
|
foreach ($logs as $log) {
|
|
|
|
$log['pirep_id'] = $id;
|
|
|
|
$log['type'] = AcarsType::LOG;
|
|
|
|
|
2018-05-11 04:28:42 +08:00
|
|
|
if (array_key_exists('sim_time', $log)) {
|
|
|
|
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('created_at', $log)) {
|
|
|
|
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$acars = Acars::create($log);
|
|
|
|
$acars->save();
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->message($count.' logs added', $count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post ACARS LOG update for a PIREP. These updates won't show up on the map
|
|
|
|
* But rather in a log file.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @param $id
|
|
|
|
* @param EventRequest $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-05-10 01:45:24 +08:00
|
|
|
* @throws \App\Exceptions\PirepCancelled
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-05-10 01:45:24 +08:00
|
|
|
*/
|
|
|
|
public function acars_events($id, EventRequest $request)
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check if the status is cancelled...
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep = Pirep::find($id);
|
|
|
|
$this->checkCancelled($pirep);
|
|
|
|
|
|
|
|
Log::debug('Posting ACARS event, PIREP: '.$id, $request->post());
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$logs = $request->post('events');
|
|
|
|
foreach ($logs as $log) {
|
|
|
|
$log['pirep_id'] = $id;
|
|
|
|
$log['type'] = AcarsType::LOG;
|
|
|
|
$log['log'] = $log['event'];
|
|
|
|
|
2018-05-11 04:28:42 +08:00
|
|
|
if (array_key_exists('sim_time', $log)) {
|
|
|
|
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('created_at', $log)) {
|
|
|
|
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
|
|
|
|
}
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
$acars = Acars::create($log);
|
|
|
|
$acars->save();
|
2018-08-27 00:40:04 +08:00
|
|
|
$count++;
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->message($count.' logs added', $count);
|
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|