<?php
namespace App\Controller;
//Load core
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use \DateTime;
//Load json rest
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\Annotations as Rest; // alias pour toutes les annotations
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Services\Utils;
use App\Services\Tools;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
//Load entity
use App\Entity\PositionGps;
use App\Entity\Hobit;
use App\Entity\User;
use App\Entity\HobitDate;
class HobitController extends AbstractController
{
protected $em;
protected $utils;
protected $tools;
public function __construct(EntityManagerInterface $entityManager, Utils $utils, TranslatorInterface $translator, Tools $tools) {
$this->em = $entityManager;
$this->utils = $utils;
$this->translator = $translator;
$this->tools = $tools;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/sync")
*/
public function syncHobitApiAction(Request $request)
{
$authTokenHeader = $request->headers->get('X-Auth-Token');
$auth = $this->em
->getRepository(AuthToken::class)
->findOneBy(array("value" => $authTokenHeader));
$tabHobit = [];
//For the distance between current position and last position default 5 (in metter))
$limitWidthHobit = 10;
//Time minimum between current position and last position, in second
$limitTimeHobit = 300;
//default : 20
$limitForCreateHobit = 2;
//For create hobit, remove error and driver
$limitStable = 1;
//For sum in XX address (Good points in target)
$maxSum = 100;
//last value, interval (date printed and date server) for get last data. The new value work with th background. (warning, change the number in position save controller). This value can stop $limitForCreateHobit and $limitStable.
$numberIntervalPrintedAndServer = 6;
//Debug
$debug = true;
$debugAndFlush = false;
//Write 'null' for disable
$dateStartForce = null;
$dateEndForce = null;
//$dateEndForce = "20220528170000";
$tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $auth->getUser(), $dateEndForce, $dateStartForce);
$hobits = $this->tools->createHobit($tabHobit, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $maxSum, $auth->getUser());
return true;
}
/**
* @Route("/hobit/sync", name="hobit_sync")
*/
public function syncHobitAction(Request $request)
{
$this->syncHobitDebugAction($request, true);
return $this->redirectToRoute('information_live_debug');
}
/**
* @Route("/debug/hobit", name="hobit_debug")
*/
public function syncHobitDebugAction(Request $request, $flush = false)
{
//Config
/*
$lat1 = "45.759465500637";
$lng1 = "4.8491488866881";
$lat2 = "45.759445726474";
$lng2 = "4.8491441637739";
$earth_radius = 6378137;
$rlo1 = deg2rad($lng1);
$rla1 = deg2rad($lat1);
$rlo2 = deg2rad($lng2);
$rla2 = deg2rad($lat2);
$dlo = ($rlo2 - $rlo1) / 2;
$dla = ($rla2 - $rla1) / 2;
$a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo));
$d = 2 * atan2(sqrt($a), sqrt(1 - $a));
$valeur = ($earth_radius * $d);
echo $valeur;
die;
*/
$tabHobit = [];
//For the distance between current position and last position default 5 (in metter))
$limitWidthHobit = 10;
//Time minimum between current position and last position, in second
$limitTimeHobit = 120;
//default : 20
$limitForCreateHobit = 2;
//For create hobit, remove error and driver
$limitStable = 1;
//For sum in XX address (Good points in target)
$maxSum = 100;
//last value, interval (date printed and date server) for get last data. The new value work with th background. (warning, change the number in position save controller). This value can stop $limitForCreateHobit and $limitStable.
$numberIntervalPrintedAndServer = 6;
//Debug
$debug = true;
$debugAndFlush = $flush;
$session = new Session();
$userId = $session->get('currentUser');
if(!$userId){
$userId = 4;
}
$userCurrent = $this->em->getRepository(User::class)->find($userId);
//Write 'null' for disable
$dateStartForce = "20220622080000";
$dateEndForce = null;
//$dateEndForce = "20220528170000";
$tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $userCurrent, $dateEndForce, $dateStartForce);
if($debugAndFlush){
$hobits = $this->tools->createHobit($tabHobit, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $maxSum, $userCurrent);
$this->addFlash('success', 'User has been sync.');
return true;
}
else{
$hobits = $tabHobit;
}
//var_dump($tabHobit);
//die;
/*
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://maps.googleapis.com/maps/api/streetview?size=600x300&location=45.763451584654,4.8423398764755&heading=151.78&pitch=-0.76&key=AIzaSyCq_-9DHJzN2z1wPx4O_v5PvVFvLhZlkmI',
CURLOPT_USERAGENT => 'Codular Sample cURL Request'
]);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
return $resp;
// close curl resource to free up system resources
die;
*/
return $this->render('debug/hobit.html.twig', Array(
"tabHobit" => $hobits,
"debugAndFlush" => $debugAndFlush
));
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/list")
*/
public function getHobitAction(Request $request)
{
//Load base
$em = $this->em;
//All without "NOW"
/*
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitAllWithoutNow();
*/
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitAll();
return $hobits;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/hobit-date-list")
*/
public function getHobitDateAction(Request $request)
{
//Load base
$em = $this->em;
//All without "NOW"
/*
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitAllWithoutNow();
*/
$hobitsDate = $this->em
->getRepository(HobitDate::class)
->findAll();
$hobitsClean = [];
foreach ($hobitsDate as $key => $hobitDate) {
$hobitsClean[$key]['id'] = $hobitDate->getHobit()->getId();
$hobitsClean[$key]['address'] = $hobitDate->getHobit()->getAddress();
$hobitsClean[$key]['latitude'] = $hobitDate->getHobit()->getLatitude();
$hobitsClean[$key]['longitude'] = $hobitDate->getHobit()->getLongitude();
$hobitsClean[$key]['dateStartHobit'] = $hobitDate->getDateStartHobit();
$hobitsClean[$key]['dateEndHobit'] = $hobitDate->getDateEndHobit();
$hobitsClean[$key]['groupName'] = $hobitDate->getHobit()->getGroupName();
$hobitsClean[$key]['groupNameTranslate'] = $hobitDate->getHobit()->getGroupNameTranslate();
$hobitsClean[$key]['groupVerif'] = $hobitDate->getHobit()->getGroupVerif();
}
$hobitsClean = array_reverse($hobitsClean);
return $hobitsClean;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/current")
*/
public function getHobitCurrentAction(Request $request)
{
//Load base
$em = $this->em;
//Load get
$hobit = $this->em
->getRepository(Hobit::class)
->findHobitCurrent();
return $hobit;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/history/{idHobit}")
*/
public function getHobitHistoryAction(Request $request)
{
//Load base
$em = $this->em;
//Load get
$idHobit = $request->get('idHobit');
$hobits = $this->em
->getRepository(Hobit::class)
->findAllHobitdateByHobit($idHobit);
/*
$tabHobits = [];
foreach ($hobits as $key -> $hobit) {
$tabHobits[$key] =
}
*/
return $hobits;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/group")
*/
public function getHobitGroupAction(Request $request)
{
//Load base
$em = $this->em;
//Load get
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitGroup();
return $hobits;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/group-no-verif")
*/
public function getHobitGroupVerifAction(Request $request)
{
//Load base
$em = $this->em;
//Load get
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitGroupNoVerif();
$hobitsClean = [];
foreach ($hobits as $key => $hobit) {
$hobitsClean[$key]['id'] = $hobit->getId();
$hobitsClean[$key]['address'] = $hobit->getAddress();
$hobitsClean[$key]['latitude'] = $hobit->getLatitude();
$hobitsClean[$key]['longitude'] = $hobit->getLongitude();
$hobitsClean[$key]['groupName'] = $hobit->getGroupName();
$hobitsClean[$key]['groupNameTranslate'] = $hobit->getGroupNameTranslate();
$hobitsClean[$key]['groupVerif'] = $hobit->getGroupVerif();
}
return $hobitsClean;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/city")
*/
public function getHobitCityAction(Request $request)
{
//Load base
$em = $this->em;
//Load get
$hobits = $this->em
->getRepository(Hobit::class)
->findHobitCity();
$hobitsClean = [];
foreach ($hobits as $key => $hobit) {
$hobitsClean[$key]['city'] = $hobit['city'];
/*
$hobitsClean[$key]['id'] = $hobit['id'];
$hobitsClean[$key]['address'] = $hobit['address'];
$hobitsClean[$key]['latitude'] = $hobit['latitude'];
$hobitsClean[$key]['longitude'] = $hobit['longitude'];
$hobitsClean[$key]['groupName'] = $hobit['group_name'];
$hobitsClean[$key]['groupVerif'] = $hobit['group_verif'];
*/
}
return $hobitsClean;
}
/**
* @Rest\View(statusCode=201)
* @Rest\Get("/api/hobit/group-change-verif/{hobitId}/{type}")
*/
public function getHobitGroupChangeVerifAction(Request $request, $hobitId, $type)
{
//Load base
$em = $this->em;
//Load get
$hobit = $this->em
->getRepository(Hobit::class)
->findOneBy(array("id" => $hobitId));
if($hobit){
if($type == "yes"){
$hobit->setGroupVerif(1);
}
else if($type == "no"){
$hobit->setGroupVerif(1);
$hobit->setGroupName(null);
}
else if($type == "never"){
$hobit->setGroupVerif(1);
$hobit->setGroupName("never");
}
$em->persist($hobit);
$em->flush();
}
return true;
}
}