src/Controller/MotorouteController.php line 145

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use PointAddressChecker;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use App\Application\MTHelper;
  9. use App\Application\MTRouteHelper;
  10. use App\Application\MTRoute;
  11. use App\Application\MTRouteInfo;
  12. use App\Application\MTWeather;
  13. use App\Application\MTSunsun;
  14. use App\Entity\RouteHashBlock;
  15. class MotorouteController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/motoroute", name="motoroute")
  19.      */
  20.     public function index(Request $request)
  21.     {
  22.         $timeDebug[0] = microtime(true);
  23.         $doctrine $this->getDoctrine();
  24.         MTHelper::execEvent('MotorouteStart');
  25.         //@todo: check API_KEY somewhere here
  26.         //@todo: make API_KEY managed in db
  27.         //$mt_params = json_decode($request->request->get("mt_params"), true);
  28.         $mt_params json_decode($_REQUEST['mt_params'], true);
  29.         //@todo: needa put params from hash_f to data->params
  30.         //test route:
  31.         //https://moto.tours/motoroute/mrr1/34054ac99dcee70f3ae403f821226706/
  32.         if(isset($mt_params['open_hash']) && strlen($mt_params['open_hash']) == 32) {
  33.             $timeDebug[] = microtime(true);
  34.             $hfem $doctrine->getRepository(RouteHashBlock::class);
  35.             $hfres $hfem->findOneBy(['hashFull' => $mt_params['open_hash']]);
  36.             //dd($hrefs);
  37.             $hfbasic $hfres->getHashBasic();
  38.             /*$resp = [
  39.                 "basic" => $hfbacic,
  40.                 "ophash" => $mt_params['open_hash'],
  41.             ];
  42.             //$response = new Response($hfbacic);
  43.             $response = new Response(json_encode($resp));
  44.             $response->headers->set('Content-Type', 'application/json');
  45.             $response->headers->set('Access-Control-Allow-Origin', 'https://moto.tours/');
  46.             return $response;*/
  47.             $mt = new MTRoute($doctrine$hfbasic);
  48.             $data $mt->getRoute($doctrine$hfbasic);
  49.             $timeDebug[] = microtime(true);
  50.         } else {
  51.             // if default with params not with hash
  52.             if (!isset($mt_params["from"]) || empty($mt_params["from"])) {
  53.                 $response_data = [
  54.                     "error" => "Error: no FROM",
  55.                     "params_test" => $mt_params,
  56.                 ];
  57.                 $response = new Response(json_encode($response_data));
  58.                 $response->headers->set('Content-Type''application/json');
  59.                 $response->headers->set('Access-Control-Allow-Origin''https://moto.tours/');
  60.                 return $response;
  61.                 //die('{"error":"Error: no FROM"}');/*error*/
  62.             }
  63.             if (!isset($mt_params["to"]) || empty($mt_params["to"])) {
  64.                 die('{"error":"Error: no TO"}');/*error*/
  65.             }
  66.             if (!isset($mt_params["points"]) || empty($mt_params["points"])) { /*do_nothing*/
  67.             }
  68.             if (!isset($mt_params["engine_volume"]) || empty($mt_params["engine_volume"])) $mt_params["engine_volume"] = MTRouteHelper::getDefaultParam("engine_volume");
  69.             if (!isset($mt_params["speed_rm"]) || empty($mt_params["speed_rm"])) $mt_params["speed_rm"] = MTRouteHelper::getDefaultParam("speed_rm");
  70.             if (!isset($mt_params["speed_rp"]) || empty($mt_params["speed_rp"])) $mt_params["speed_rp"] = MTRouteHelper::getDefaultParam("speed_rp");
  71.             if (!isset($mt_params["speed_rs"]) || empty($mt_params["speed_rs"])) $mt_params["speed_rs"] = MTRouteHelper::getDefaultParam("speed_rs");
  72.             if (!isset($mt_params["speed_ru"]) || empty($mt_params["speed_ru"])) $mt_params["speed_ru"] = MTRouteHelper::getDefaultParam("speed_ru");
  73.             if (!isset($mt_params["divide_route"]) || empty($mt_params["divide_route"])) $mt_params["divide_route"] = MTRouteHelper::getDefaultParam("divide_route");
  74.             if (!isset($mt_params["divide_by"]) || empty($mt_params["divide_by"])) $mt_params["divide_by"] = MTRouteHelper::getDefaultParam("divide_by");
  75.             if (!isset($mt_params["divide_value"]) || empty($mt_params["divide_value"])) $mt_params["divide_value"] = MTRouteHelper::getDefaultParam("divide_value");
  76.             if (!isset($mt_params["fuel_consumption"]) || empty($mt_params["fuel_consumption"])) $mt_params["fuel_consumption"] = MTRouteHelper::getDefaultParam("fuel_consumption");
  77.             if (!isset($mt_params["fuel_tank_volume"]) || empty($mt_params["fuel_tank_volume"])) $mt_params["fuel_tank_volume"] = MTRouteHelper::getDefaultParam("fuel_tank_volume");
  78.             if (!isset($mt_params["date_start"]) || empty($mt_params["date_start"])) $mt_params["date_start"] = MTRouteHelper::getDefaultParam("date_start");
  79.             if (!$mt_params) {
  80.                 //$mt_params = '{"from":"РОССИЯ, МОСКВА","to":"СОЧИ","points":["Воронеж (Воронежская область, Россия)"],"engine_volume":600,"speed_limit":120,"speed_rr":{"rm":120,"rp":110,"rs":90,"ru":60},"use_additional_settings":false,"dont_concider_jams":false,"exclude_paid_roads":false,"exclude_ferry_crossings":false,"use_average_fuel_price":false,"use_average_fuel_consumption":false,"show_gas_stations":false,"show_hotels":false,"show_motohomes":false,"show_restaurants":false,"separate_route":false,"separate_by":"points","separate_value":0,"fuel_consumption":6,"fuel_tank_volume":18,"date_start":{"date":"2019-12-10 23:25:26.253697","timezone_type":3,"timezone":"Europe/Moscow"}}';
  81.                 $mt_params '{"from":"Москва","to":"Саратов, Россия","points":[],"engine_volume":600,"speed_limit":120,"speed_rr":{"rm":120,"rp":110,"rs":90,"ru":60},"use_additional_settings":false,"dont_concider_jams":false,"exclude_paid_roads":false,"exclude_ferry_crossings":false,"use_average_fuel_price":false,"use_average_fuel_consumption":false,"show_gas_stations":false,"show_hotels":false,"show_motohomes":false,"show_restaurants":false,"separate_route":false,"separate_by":"points","separate_value":0,"fuel_consumption":6,"fuel_tank_volume":18,"date_start":{"date":"2019-12-10 23:25:26.253697","timezone_type":3,"timezone":"Europe/Moscow"}}';
  82.                 $mt_params '{"from":"Москва","to":"Сочи","points":[],"engine_volume":600,"speed_limit":120,"speed_rr":{"rm":120,"rp":110,"rs":90,"ru":60},"use_additional_settings":false,"dont_concider_jams":false,"exclude_paid_roads":false,"exclude_ferry_crossings":false,"use_average_fuel_price":false,"use_average_fuel_consumption":false,"show_gas_stations":false,"show_hotels":false,"show_motohomes":false,"show_restaurants":false,"separate_route":false,"separate_by":"points","separate_value":0,"fuel_consumption":6,"fuel_tank_volume":18,"date_start":{"date":"2019-12-10 23:25:26.253697","timezone_type":3,"timezone":"Europe/Moscow"}}';
  83.                 $mt_params json_decode($mt_paramstrue);
  84.             }
  85.             /*
  86.             $f = fopen($_SERVER['DOCUMENT_ROOT'] . "/t_incoming_and_added_defaults_mt_params_dump.txt", "a");
  87.             fputs($f, print_r($mt_params, true) );
  88.             //fputs($f, print_r($_POST, true) );
  89.             fputs($f, "\n");
  90.             fclose($f);*/
  91.             $mt = new MTRoute();
  92.             if (isset($mt_params["from"]) && !empty($mt_params["from"])) {
  93.                 $mt->setParam('from'MTRouteHelper::checkCapital($mt_params["from"]));
  94.             }
  95.             if (isset($mt_params["to"]) && !empty($mt_params["to"])) {
  96.                 $mt->setParam('to'MTRouteHelper::checkCapital($mt_params["to"]));
  97.             }
  98.             if (isset($mt_params["points"]) && !empty($mt_params["points"][0])) {
  99.                 foreach ($mt_params["points"] as $point) {
  100.                     $mt->setParam("point"$point);
  101.                 }
  102.                 unset($point);
  103.             }
  104.             if (isset($mt_params["speed_rm"]) && !empty($mt_params["speed_rm"])) {
  105.                 $mt->setParam('speed_rm', (int)$mt_params["speed_rm"]);
  106.             }
  107.             if (isset($mt_params["speed_rp"]) && !empty($mt_params["speed_rp"])) {
  108.                 $mt->setParam('speed_rp', (int)$mt_params["speed_rp"]);
  109.             }
  110.             if (isset($mt_params["speed_rs"]) && !empty($mt_params["speed_rs"])) {
  111.                 $mt->setParam('speed_rs', (int)$mt_params["speed_rs"]);
  112.             }
  113.             if (isset($mt_params["speed_ru"]) && !empty($mt_params["speed_ru"])) {
  114.                 $mt->setParam('speed_ru', (int)$mt_params["speed_ru"]);
  115.             }
  116.             //@todo: add other params to router!
  117.             //$data = print_r($mt->getRoute($doctrine), true);
  118.             $data $mt->getRoute($doctrine);
  119.             //$data = print_r($mt_params, true);
  120.             MTHelper::execEvent('MotorouteEnd');
  121.             //return new Response("<pre>$data</pre>");
  122.         }
  123.         ///--------------------------------------------------
  124.         /// getting route info:
  125.         $route_info = new MTRouteInfo($data['hash_basic'], $doctrine);
  126.         $timeDebug[] = microtime(true);
  127.         if (isset($mt_params['engine_volume']) && !empty($mt_params['engine_volume'])) $route_info->setParam("engine_volume", (int)$mt_params['engine_volume']);
  128.         if (isset($mt_params['divide_route']) && !empty($mt_params['divide_route'])) $route_info->setParam("divide_route"$mt_params['divide_route']);
  129.         if (isset($mt_params['divide_by']) && !empty($mt_params['divide_by'])) $route_info->setParam("divide_by"$mt_params['divide_by']);
  130.         if (isset($mt_params['divide_value']) && !empty($mt_params['divide_value'])) $route_info->setParam("divide_value", (int)$mt_params['divide_value']);
  131.         if (isset($mt_params['fuel_consumption']) && !empty($mt_params['fuel_consumption']) && $mt_params['fuel_consumption'] != 0$route_info->setParam("fuel_consumption", (float)$mt_params['fuel_consumption']);
  132.         if (isset($mt_params['fuel_tank_volume']) && !empty($mt_params['fuel_tank_volume']) && $mt_params['fuel_tank_volume'] != 0$route_info->setParam("fuel_tank_volume", (float)$mt_params['fuel_tank_volume']);
  133.         if (isset($mt_params["points"]) && !empty($mt_params["points"][0])) {
  134.             foreach ($mt_params["points"] as $point) {
  135.                 $route_info->setParam("point"$point);
  136.             }
  137.             unset($point);
  138.         }
  139.         if (isset($mt_params['date_start']) && !empty($mt_params['date_start'])) {
  140.             $route_info->setParam("date_start"$mt_params['date_start']);
  141.         } else {
  142.             $route_info->setParam("date_start"date("Y-m-d"));
  143.         }
  144.         //@todo: fix date_start issue
  145.         //$route_info->setParam("date_start", date("Y-m-d"));
  146.         //$route_info->getRouteInfo($data['hash_basic'], $doctrine);
  147.         if(isset($hfres) && !empty($hfres->getHashFull()))
  148.             $hf $hfres->getHashFull();
  149.         else
  150.             $hf false;
  151.         //$route_info->getRouteInfo($data['hash_basic'], $doctrine, false, $mt_params);
  152.         $timeDebug[20] = microtime(true);
  153.         $route_info->getRouteInfo($data['hash_basic'], $doctrine$hf$mt_params);
  154.         //$mt_params = $route_info->mt_params;
  155.         $timeDebug[30] = microtime(true);
  156.         //$route_info->getRouteInfo();
  157.         $data['description'] = $route_info->getRouteDescription();
  158.         $data['division'] = $route_info->getRouteDivision();
  159.         $timeDebug[40] = microtime(true);
  160.         ///--------------------------------------------------
  161.         ///---weather---
  162.         /*
  163.         $weather_forecast = [];
  164.         foreach($data['division'] as $i => $day){
  165.             if(isset($day['cities_full']) && count($day['cities_full']) > 0){
  166.                 $weather_forecast[ $i ] = MTWeather::getWeather($day['cities_full'], $mt->getParam('speed_rm') );
  167.             }
  168.         }
  169.         $data['weather_forecast'] = $weather_forecast;
  170.         */
  171.         $data['weather_forecast'] = MTWeather::getWeather($route_info->getCL(), $mt->getParam('speed_rm') );
  172.         $timeDebug[50] = microtime(true);
  173.         //$data['sunsun'] = MTSunsun::getSunset($data['division'][1]['cities_full'][0], $data['weather_forecast'][0]['date'], count($data['division']));
  174.         //$data['sunsun'] = MTSunsun::getSunsun($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
  175.         $data['sunsun'] = MTSunsun::getSunsunOurApi($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
  176. //        $data['sunsun2'] = MTSunsun::getSunsunOurApi($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
  177. //        $data['sunsun3'] = "5550666";
  178. //        $data['sunsun4'] = $data['division'];
  179. //        $data['sunsun5'] = $data['weather_forecast'][0]['date']->time_zone_offset;
  180.         $timeDebug[] = microtime(true);
  181.         ///--------------------------------------------------
  182.         $data['routehelper'] = [];
  183.         //@note: thats a stupid patch - get the value from the hash_f
  184.         if(!isset($mt_params['speed_rm'])) $mt_params['speed_rm'] = 120;
  185.         $data['dna'][0] = MTRouteHelper::getDnaX($mt_params['speed_rm']);
  186.         //$data['dna'][1] = MTRouteHelper::getDnaY($data['dna'][0]);
  187.         $data['dna'][1] = ($data['dna'][0] / 1.7);
  188.         ///--------------------------------------------------
  189.         $timeDebug[80] = microtime(true);
  190.         $data['polyline_arr'] = MTRouteHelper::decodePolyline($data['polyline']);
  191.         unset($data['segments']);
  192.         $timeDebug[90] = microtime(true);
  193.         if(isset($route_info->mt_params_db) && count($route_info->mt_params_db) > 0){
  194.             $the_real_params $route_info->mt_params_db;
  195.         } else {
  196.             $the_real_params $mt_params;
  197.         }
  198.         if(isset($mt_params['short_mode']) && $mt_params['short_mode'] === 'short'){
  199.             unset($data['dna']);
  200.             unset($data['description']);
  201.             unset($data['distance']);
  202.             unset($data['division']);
  203.             unset($data['params_basic']);
  204.             unset($data['polyline']);
  205.             unset($data['sunsun']);
  206.             unset($data['weather_forecast']);
  207.         }
  208.         $timeDebug[] = microtime(true);
  209.         /*ob_start();
  210.         $first = 0.0;
  211.         $last = 0.0;
  212.         foreach ($timeDebug as $k => $v) {
  213.             if($k == 0){
  214.                 $first = $v;
  215.                 $last = $v;
  216.                 echo $k . ":\t\t" . $v . "\n";
  217.             } else {
  218.                 echo $k . "\t" . $v . "\t" . ($v - $last) . " s\n";
  219.                 $last = $v;
  220.             }
  221.         }
  222.         echo "Total: " . ($last - $first) . " s\n";
  223.         $buf = ob_get_contents();
  224.         ob_end_clean();*/
  225.         $response_data = [
  226.             "status" => "success",
  227.             "hash_b" => $route_info->getHB(),
  228.             "hash_f" => $route_info->getHF(),
  229.             //"params" => $mt_params,
  230.             //"bbb" => print_r($hfres, true),
  231.             "params" => $the_real_params//(count($route_info->mt_params_db) > 0) ? $route_info->mt_params_db : $mt_params,
  232.             //"params_real" => $the_real_params,
  233.             //"params_test" => $route_info->mt_params_db,
  234.             //"params_test_count" => count($route_info->mt_params_db),
  235.             //"params_test2" => $mt_params,
  236.             "data" => $data,
  237.             "errors" => "",
  238.             //"debug" => $buf,
  239.         ];
  240.         //$response = new Response('<pre>' . print_r($response_data, true) . '</pre>');
  241.         $response = new Response(json_encode($response_data));
  242.         $response->headers->set('Content-Type''application/json');
  243.         //$response->headers->set('Access-Control-Allow-Origin', 'http://localhost:8080');
  244.         $response->headers->set('Access-Control-Allow-Origin''https://moto.tours/');
  245.         return $response;
  246.     }
  247. }