<?php
namespace App\Controller;
use PointAddressChecker;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Application\MTHelper;
use App\Application\MTRouteHelper;
use App\Application\MTRoute;
use App\Application\MTRouteInfo;
use App\Application\MTWeather;
use App\Application\MTSunsun;
use App\Entity\RouteHashBlock;
class MotorouteController extends AbstractController
{
/**
* @Route("/motoroute", name="motoroute")
*/
public function index(Request $request)
{
$timeDebug[0] = microtime(true);
$doctrine = $this->getDoctrine();
MTHelper::execEvent('MotorouteStart');
//@todo: check API_KEY somewhere here
//@todo: make API_KEY managed in db
//$mt_params = json_decode($request->request->get("mt_params"), true);
$mt_params = json_decode($_REQUEST['mt_params'], true);
//@todo: needa put params from hash_f to data->params
//test route:
//https://moto.tours/motoroute/mrr1/34054ac99dcee70f3ae403f821226706/
if(isset($mt_params['open_hash']) && strlen($mt_params['open_hash']) == 32) {
$timeDebug[] = microtime(true);
$hfem = $doctrine->getRepository(RouteHashBlock::class);
$hfres = $hfem->findOneBy(['hashFull' => $mt_params['open_hash']]);
//dd($hrefs);
$hfbasic = $hfres->getHashBasic();
/*$resp = [
"basic" => $hfbacic,
"ophash" => $mt_params['open_hash'],
];
//$response = new Response($hfbacic);
$response = new Response(json_encode($resp));
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', 'https://moto.tours/');
return $response;*/
$mt = new MTRoute($doctrine, $hfbasic);
$data = $mt->getRoute($doctrine, $hfbasic);
$timeDebug[] = microtime(true);
} else {
// if default with params not with hash
if (!isset($mt_params["from"]) || empty($mt_params["from"])) {
$response_data = [
"error" => "Error: no FROM",
"params_test" => $mt_params,
];
$response = new Response(json_encode($response_data));
$response->headers->set('Content-Type', 'application/json');
$response->headers->set('Access-Control-Allow-Origin', 'https://moto.tours/');
return $response;
//die('{"error":"Error: no FROM"}');/*error*/
}
if (!isset($mt_params["to"]) || empty($mt_params["to"])) {
die('{"error":"Error: no TO"}');/*error*/
}
if (!isset($mt_params["points"]) || empty($mt_params["points"])) { /*do_nothing*/
}
if (!isset($mt_params["engine_volume"]) || empty($mt_params["engine_volume"])) $mt_params["engine_volume"] = MTRouteHelper::getDefaultParam("engine_volume");
if (!isset($mt_params["speed_rm"]) || empty($mt_params["speed_rm"])) $mt_params["speed_rm"] = MTRouteHelper::getDefaultParam("speed_rm");
if (!isset($mt_params["speed_rp"]) || empty($mt_params["speed_rp"])) $mt_params["speed_rp"] = MTRouteHelper::getDefaultParam("speed_rp");
if (!isset($mt_params["speed_rs"]) || empty($mt_params["speed_rs"])) $mt_params["speed_rs"] = MTRouteHelper::getDefaultParam("speed_rs");
if (!isset($mt_params["speed_ru"]) || empty($mt_params["speed_ru"])) $mt_params["speed_ru"] = MTRouteHelper::getDefaultParam("speed_ru");
if (!isset($mt_params["divide_route"]) || empty($mt_params["divide_route"])) $mt_params["divide_route"] = MTRouteHelper::getDefaultParam("divide_route");
if (!isset($mt_params["divide_by"]) || empty($mt_params["divide_by"])) $mt_params["divide_by"] = MTRouteHelper::getDefaultParam("divide_by");
if (!isset($mt_params["divide_value"]) || empty($mt_params["divide_value"])) $mt_params["divide_value"] = MTRouteHelper::getDefaultParam("divide_value");
if (!isset($mt_params["fuel_consumption"]) || empty($mt_params["fuel_consumption"])) $mt_params["fuel_consumption"] = MTRouteHelper::getDefaultParam("fuel_consumption");
if (!isset($mt_params["fuel_tank_volume"]) || empty($mt_params["fuel_tank_volume"])) $mt_params["fuel_tank_volume"] = MTRouteHelper::getDefaultParam("fuel_tank_volume");
if (!isset($mt_params["date_start"]) || empty($mt_params["date_start"])) $mt_params["date_start"] = MTRouteHelper::getDefaultParam("date_start");
if (!$mt_params) {
//$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"}}';
$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"}}';
$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"}}';
$mt_params = json_decode($mt_params, true);
}
/*
$f = fopen($_SERVER['DOCUMENT_ROOT'] . "/t_incoming_and_added_defaults_mt_params_dump.txt", "a");
fputs($f, print_r($mt_params, true) );
//fputs($f, print_r($_POST, true) );
fputs($f, "\n");
fclose($f);*/
$mt = new MTRoute();
if (isset($mt_params["from"]) && !empty($mt_params["from"])) {
$mt->setParam('from', MTRouteHelper::checkCapital($mt_params["from"]));
}
if (isset($mt_params["to"]) && !empty($mt_params["to"])) {
$mt->setParam('to', MTRouteHelper::checkCapital($mt_params["to"]));
}
if (isset($mt_params["points"]) && !empty($mt_params["points"][0])) {
foreach ($mt_params["points"] as $point) {
$mt->setParam("point", $point);
}
unset($point);
}
if (isset($mt_params["speed_rm"]) && !empty($mt_params["speed_rm"])) {
$mt->setParam('speed_rm', (int)$mt_params["speed_rm"]);
}
if (isset($mt_params["speed_rp"]) && !empty($mt_params["speed_rp"])) {
$mt->setParam('speed_rp', (int)$mt_params["speed_rp"]);
}
if (isset($mt_params["speed_rs"]) && !empty($mt_params["speed_rs"])) {
$mt->setParam('speed_rs', (int)$mt_params["speed_rs"]);
}
if (isset($mt_params["speed_ru"]) && !empty($mt_params["speed_ru"])) {
$mt->setParam('speed_ru', (int)$mt_params["speed_ru"]);
}
//@todo: add other params to router!
//$data = print_r($mt->getRoute($doctrine), true);
$data = $mt->getRoute($doctrine);
//$data = print_r($mt_params, true);
MTHelper::execEvent('MotorouteEnd');
//return new Response("<pre>$data</pre>");
}
///--------------------------------------------------
/// getting route info:
$route_info = new MTRouteInfo($data['hash_basic'], $doctrine);
$timeDebug[] = microtime(true);
if (isset($mt_params['engine_volume']) && !empty($mt_params['engine_volume'])) $route_info->setParam("engine_volume", (int)$mt_params['engine_volume']);
if (isset($mt_params['divide_route']) && !empty($mt_params['divide_route'])) $route_info->setParam("divide_route", $mt_params['divide_route']);
if (isset($mt_params['divide_by']) && !empty($mt_params['divide_by'])) $route_info->setParam("divide_by", $mt_params['divide_by']);
if (isset($mt_params['divide_value']) && !empty($mt_params['divide_value'])) $route_info->setParam("divide_value", (int)$mt_params['divide_value']);
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']);
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']);
if (isset($mt_params["points"]) && !empty($mt_params["points"][0])) {
foreach ($mt_params["points"] as $point) {
$route_info->setParam("point", $point);
}
unset($point);
}
if (isset($mt_params['date_start']) && !empty($mt_params['date_start'])) {
$route_info->setParam("date_start", $mt_params['date_start']);
} else {
$route_info->setParam("date_start", date("Y-m-d"));
}
//@todo: fix date_start issue
//$route_info->setParam("date_start", date("Y-m-d"));
//$route_info->getRouteInfo($data['hash_basic'], $doctrine);
if(isset($hfres) && !empty($hfres->getHashFull()))
$hf = $hfres->getHashFull();
else
$hf = false;
//$route_info->getRouteInfo($data['hash_basic'], $doctrine, false, $mt_params);
$timeDebug[20] = microtime(true);
$route_info->getRouteInfo($data['hash_basic'], $doctrine, $hf, $mt_params);
//$mt_params = $route_info->mt_params;
$timeDebug[30] = microtime(true);
//$route_info->getRouteInfo();
$data['description'] = $route_info->getRouteDescription();
$data['division'] = $route_info->getRouteDivision();
$timeDebug[40] = microtime(true);
///--------------------------------------------------
///---weather---
/*
$weather_forecast = [];
foreach($data['division'] as $i => $day){
if(isset($day['cities_full']) && count($day['cities_full']) > 0){
$weather_forecast[ $i ] = MTWeather::getWeather($day['cities_full'], $mt->getParam('speed_rm') );
}
}
$data['weather_forecast'] = $weather_forecast;
*/
$data['weather_forecast'] = MTWeather::getWeather($route_info->getCL(), $mt->getParam('speed_rm') );
$timeDebug[50] = microtime(true);
//$data['sunsun'] = MTSunsun::getSunset($data['division'][1]['cities_full'][0], $data['weather_forecast'][0]['date'], count($data['division']));
//$data['sunsun'] = MTSunsun::getSunsun($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
$data['sunsun'] = MTSunsun::getSunsunOurApi($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
// $data['sunsun2'] = MTSunsun::getSunsunOurApi($data['division'], $data['weather_forecast'][0]['date']->time_zone_offset);
// $data['sunsun3'] = "5550666";
// $data['sunsun4'] = $data['division'];
// $data['sunsun5'] = $data['weather_forecast'][0]['date']->time_zone_offset;
$timeDebug[] = microtime(true);
///--------------------------------------------------
$data['routehelper'] = [];
//@note: thats a stupid patch - get the value from the hash_f
if(!isset($mt_params['speed_rm'])) $mt_params['speed_rm'] = 120;
$data['dna'][0] = MTRouteHelper::getDnaX($mt_params['speed_rm']);
//$data['dna'][1] = MTRouteHelper::getDnaY($data['dna'][0]);
$data['dna'][1] = ($data['dna'][0] / 1.7);
///--------------------------------------------------
$timeDebug[80] = microtime(true);
$data['polyline_arr'] = MTRouteHelper::decodePolyline($data['polyline']);
unset($data['segments']);
$timeDebug[90] = microtime(true);
if(isset($route_info->mt_params_db) && count($route_info->mt_params_db) > 0){
$the_real_params = $route_info->mt_params_db;
} else {
$the_real_params = $mt_params;
}
if(isset($mt_params['short_mode']) && $mt_params['short_mode'] === 'short'){
unset($data['dna']);
unset($data['description']);
unset($data['distance']);
unset($data['division']);
unset($data['params_basic']);
unset($data['polyline']);
unset($data['sunsun']);
unset($data['weather_forecast']);
}
$timeDebug[] = microtime(true);
/*ob_start();
$first = 0.0;
$last = 0.0;
foreach ($timeDebug as $k => $v) {
if($k == 0){
$first = $v;
$last = $v;
echo $k . ":\t\t" . $v . "\n";
} else {
echo $k . "\t" . $v . "\t" . ($v - $last) . " s\n";
$last = $v;
}
}
echo "Total: " . ($last - $first) . " s\n";
$buf = ob_get_contents();
ob_end_clean();*/
$response_data = [
"status" => "success",
"hash_b" => $route_info->getHB(),
"hash_f" => $route_info->getHF(),
//"params" => $mt_params,
//"bbb" => print_r($hfres, true),
"params" => $the_real_params, //(count($route_info->mt_params_db) > 0) ? $route_info->mt_params_db : $mt_params,
//"params_real" => $the_real_params,
//"params_test" => $route_info->mt_params_db,
//"params_test_count" => count($route_info->mt_params_db),
//"params_test2" => $mt_params,
"data" => $data,
"errors" => "",
//"debug" => $buf,
];
//$response = new Response('<pre>' . print_r($response_data, true) . '</pre>');
$response = new Response(json_encode($response_data));
$response->headers->set('Content-Type', 'application/json');
//$response->headers->set('Access-Control-Allow-Origin', 'http://localhost:8080');
$response->headers->set('Access-Control-Allow-Origin', 'https://moto.tours/');
return $response;
}
}