/**
  * Display route detail information
  *
  * @param $rute_id
  * @return \Illuminate\Http\JsonResponse
  */
 public function busRouteDetail($rute_id)
 {
     $response = array();
     try {
         $routeModel = new Route();
         $busRouteModel = new BusRoute();
         $busOperationModel = new BusOperation();
         $infoRoute = $routeModel->where('rute_id', '=', $rute_id)->get()->toArray();
         $totalBusStop = $busRouteModel->select(DB::raw('count(*) as total_halte'))->where('rute_id', '=', $rute_id)->get()->toArray();
         $totalBusOperation = $busOperationModel->select(DB::raw('count(*) as total_bus'))->where('rute_id', '=', $rute_id)->get()->toArray();
         if (isset($infoRoute[0]) && isset($totalBusStop[0]) && $totalBusOperation[0]) {
             $container = array();
             $container['rute_id'] = $infoRoute[0]['rute_id'];
             $container['deskripsi'] = $infoRoute[0]['deskripsi'];
             $container['total_halte'] = $totalBusStop[0]['total_halte'];
             $container['total_bus'] = $totalBusOperation[0]['total_bus'];
             $response['code'] = 200;
             $response['data'] = $container;
         } else {
             $response['code'] = 400;
             $response['data']['msg'] = 'no route information can be found, make sure you attach a correct route identifier';
         }
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = "internal server error, please contact administrator";
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 /**
  * get nearest bus heading to certain bus stop
  *
  * @param $halte_id
  * @return \Illuminate\Http\JsonResponse
  */
 public function getNearestArrivalEstimation($halte_id)
 {
     $arrivalEstimationModel = new ArrivalEstimation();
     try {
         $arrivalEstimation = $arrivalEstimationModel->where('halte_id_tujuan', $halte_id)->orderBy('waktu_kedatangan', 'asc')->take(1)->get()->toArray();
         if (isset($arrivalEstimation[0])) {
             $busOperationModel = new BusOperation();
             $lastPosition = $busOperationModel->select('last_latitude', 'last_longitude')->where('plat_nomor', '=', $arrivalEstimation[0]['plat_nomor'])->first();
             $this->nearestArrivalEtimation = $arrivalEstimation[0];
             $response = array();
             $response['code'] = 200;
             $response['data'] = $this->nearestArrivalEtimation;
             $response['data']['bus_latitude'] = $lastPosition['last_latitude'];
             $response['data']['bus_longitude'] = $lastPosition['last_longitude'];
         } else {
             $response['code'] = 400;
             $response['data']['msg'] = 'cannot find nearest bus, please try again later';
         }
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = 'internal error, please try again later or contact administrator';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }
 /**
  * check bus post current position iteration. current iteration is every 1,5 minutes
  * so, if iteration is 160 (40 mins of total current location post request), system will make request to google maps
  * api (directions) to avoid over quota
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function checkBusIteration()
 {
     $busOperationModel = new BusOperation();
     $busOperation = $busOperationModel->where('plat_nomor', '=', $this->plat_nomor)->get()->toArray();
     if (sizeof($busOperation) > 0 && $busOperation != null) {
         //if there is a record about that bus, we will take action
         if ($busOperation[0]['iterasi_arrival_check'] >= 4) {
             $busOperationModel->where('plat_nomor', '=', $this->plat_nomor)->update(['iterasi_arrival_check' => 0]);
             return true;
         } else {
             $busOperationModel->where('plat_nomor', '=', $this->plat_nomor)->update(['iterasi_arrival_check' => $busOperation[0]['iterasi_arrival_check'] + 1]);
             return false;
         }
     } else {
         //if there is no record, exception will be thrown
         throw new Exception("Bus Not Found");
     }
 }
 /**
  * delete bus operation from database based on plat nomor
  *
  * @param $plat_nomor
  * @return \Illuminate\Http\JsonResponse
  */
 public function deleteBusOperation($plat_nomor)
 {
     $busOperationModel = new BusOperation();
     $response = array();
     try {
         $busOperationModel->where('plat_nomor', '=', $plat_nomor)->delete();
         $response['code'] = 200;
         $response['data']['msg'] = 'bus has been successfully deleted from database';
     } catch (\Exception $e) {
         $response['code'] = 500;
         $response['data']['msg'] = 'failed to delete bus, please make sure that plat nomor is correct';
     }
     header("Access-Control-Allow-Origin: *");
     return response()->json($response);
 }