2017-12-26 05:19:34 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2019-07-16 03:51:35 +08:00
|
|
|
use App\Contracts\Controller;
|
2020-06-09 21:10:40 +08:00
|
|
|
use App\Events\AcarsUpdate;
|
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;
|
2019-09-13 23:21:40 +08:00
|
|
|
use App\Http\Resources\Pirep as PirepResource;
|
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 Carbon\Carbon;
|
2019-08-06 01:07:22 +08:00
|
|
|
use Illuminate\Database\QueryException;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Illuminate\Http\Request;
|
2019-08-06 01:07:22 +08:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-12-26 05:19:34 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
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) {
|
2019-08-21 20:17:44 +08:00
|
|
|
throw new PirepCancelled($pirep);
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|
|
|
|
|
2019-09-13 23:21:40 +08:00
|
|
|
/**
|
|
|
|
* Get all the active PIREPs
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function live_flights()
|
|
|
|
{
|
|
|
|
$pireps = $this->acarsRepo->getPositions(setting('acars.live_time'))->filter(function ($pirep) {
|
|
|
|
return $pirep->position !== null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return PirepResource::collection($pireps);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2019-09-13 23:21:40 +08:00
|
|
|
public function pireps_geojson(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
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
return response()->json([
|
|
|
|
'data' => $positions,
|
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
|
|
|
*
|
2019-08-30 20:08:00 +08:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-05-10 01:45:24 +08:00
|
|
|
*/
|
|
|
|
public function acars_geojson($pirep_id, Request $request)
|
|
|
|
{
|
|
|
|
$pirep = Pirep::find($pirep_id);
|
|
|
|
$geodata = $this->geoSvc->getFeatureFromAcars($pirep);
|
|
|
|
|
2019-08-30 20:08:00 +08:00
|
|
|
return response()->json([
|
|
|
|
'data' => $geodata,
|
2018-05-10 01:45:24 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
2020-04-01 05:26:55 +08:00
|
|
|
$acars = Acars::with(['pirep'])
|
|
|
|
->where([
|
|
|
|
'pirep_id' => $id,
|
|
|
|
'type' => AcarsType::FLIGHT_PATH,
|
|
|
|
])
|
|
|
|
->orderBy('sim_time', 'asc')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return new AcarsRouteResource($acars);
|
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(
|
2019-07-17 01:54:14 +08:00
|
|
|
'Posting ACARS update (user: '.Auth::user()->ident.', pirep id :'.$id.'): ',
|
2018-05-10 01:45:24 +08:00
|
|
|
$request->post()
|
|
|
|
);
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
$positions = $request->post('positions');
|
|
|
|
foreach ($positions as $position) {
|
|
|
|
$position['pirep_id'] = $id;
|
|
|
|
$position['type'] = AcarsType::FLIGHT_PATH;
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($position['sim_time'])) {
|
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
|
|
|
}
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($position['created_at'])) {
|
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
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
try {
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($position['id'])) {
|
|
|
|
Acars::updateOrInsert(
|
|
|
|
['id' => $position['id']],
|
|
|
|
$position
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$update = Acars::create($position);
|
|
|
|
$update->save();
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
$count++;
|
|
|
|
} catch (QueryException $ex) {
|
|
|
|
Log::info('Error on adding ACARS position: '.$ex->getMessage());
|
|
|
|
}
|
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
|
2021-06-14 03:28:57 +08:00
|
|
|
/*if ($pirep->status === PirepStatus::INITIATED) {
|
2018-05-10 01:45:24 +08:00
|
|
|
$pirep->status = PirepStatus::AIRBORNE;
|
2021-06-14 03:28:57 +08:00
|
|
|
}*/
|
2018-05-10 01:45:24 +08:00
|
|
|
|
|
|
|
$pirep->save();
|
|
|
|
|
2020-06-09 21:10:40 +08:00
|
|
|
// Post a new update for this ACARS position
|
|
|
|
event(new AcarsUpdate($pirep, $pirep->position));
|
|
|
|
|
2018-05-10 01:45:24 +08:00
|
|
|
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;
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['sim_time'])) {
|
2018-05-11 04:28:42 +08:00
|
|
|
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['created_at'])) {
|
2018-05-11 04:28:42 +08:00
|
|
|
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
try {
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['id'])) {
|
|
|
|
Acars::updateOrInsert(
|
|
|
|
['id' => $log['id']],
|
|
|
|
$log
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$acars = Acars::create($log);
|
|
|
|
$acars->save();
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
$count++;
|
|
|
|
} catch (QueryException $ex) {
|
|
|
|
Log::info('Error on adding ACARS position: '.$ex->getMessage());
|
|
|
|
}
|
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'];
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['sim_time'])) {
|
2018-05-11 04:28:42 +08:00
|
|
|
$log['sim_time'] = Carbon::createFromTimeString($log['sim_time']);
|
|
|
|
}
|
|
|
|
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['created_at'])) {
|
2018-05-11 04:28:42 +08:00
|
|
|
$log['created_at'] = Carbon::createFromTimeString($log['created_at']);
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
try {
|
2020-02-23 05:03:01 +08:00
|
|
|
if (isset($log['id'])) {
|
|
|
|
Acars::updateOrInsert(
|
|
|
|
['id' => $log['id']],
|
|
|
|
$log
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$acars = Acars::create($log);
|
|
|
|
$acars->save();
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:07:22 +08:00
|
|
|
$count++;
|
|
|
|
} catch (QueryException $ex) {
|
|
|
|
Log::info('Error on adding ACARS position: '.$ex->getMessage());
|
|
|
|
}
|
2018-05-10 01:45:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->message($count.' logs added', $count);
|
|
|
|
}
|
2017-12-26 05:19:34 +08:00
|
|
|
}
|