src/Controller/HobitController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. //Load core
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use \DateTime;
  6. //Load json rest
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use FOS\RestBundle\Controller\Annotations as Rest; // alias pour toutes les annotations
  12. use Geocoder\Query\GeocodeQuery;
  13. use Geocoder\Query\ReverseQuery;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use App\Services\Utils;
  16. use App\Services\Tools;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. //Load entity
  20. use App\Entity\PositionGps;
  21. use App\Entity\Hobit;
  22. use App\Entity\User;
  23. use App\Entity\HobitDate;
  24. class HobitController extends AbstractController
  25. {
  26. protected $em;
  27. protected $utils;
  28. protected $tools;
  29. public function __construct(EntityManagerInterface $entityManager, Utils $utils, TranslatorInterface $translator, Tools $tools) {
  30. $this->em = $entityManager;
  31. $this->utils = $utils;
  32. $this->translator = $translator;
  33. $this->tools = $tools;
  34. }
  35. /**
  36. * @Rest\View(statusCode=201)
  37. * @Rest\Get("/api/hobit/sync")
  38. */
  39. public function syncHobitApiAction(Request $request)
  40. {
  41. $authTokenHeader = $request->headers->get('X-Auth-Token');
  42. $auth = $this->em
  43. ->getRepository(AuthToken::class)
  44. ->findOneBy(array("value" => $authTokenHeader));
  45. $tabHobit = [];
  46. //For the distance between current position and last position default 5 (in metter))
  47. $limitWidthHobit = 10;
  48. //Time minimum between current position and last position, in second
  49. $limitTimeHobit = 300;
  50. //default : 20
  51. $limitForCreateHobit = 2;
  52. //For create hobit, remove error and driver
  53. $limitStable = 1;
  54. //For sum in XX address (Good points in target)
  55. $maxSum = 100;
  56. //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.
  57. $numberIntervalPrintedAndServer = 6;
  58. //Debug
  59. $debug = true;
  60. $debugAndFlush = false;
  61. //Write 'null' for disable
  62. $dateStartForce = null;
  63. $dateEndForce = null;
  64. //$dateEndForce = "20220528170000";
  65. $tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $auth->getUser(), $dateEndForce, $dateStartForce);
  66. $hobits = $this->tools->createHobit($tabHobit, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $maxSum, $auth->getUser());
  67. return true;
  68. }
  69. /**
  70. * @Route("/hobit/sync", name="hobit_sync")
  71. */
  72. public function syncHobitAction(Request $request)
  73. {
  74. $this->syncHobitDebugAction($request, true);
  75. return $this->redirectToRoute('information_live_debug');
  76. }
  77. /**
  78. * @Route("/debug/hobit", name="hobit_debug")
  79. */
  80. public function syncHobitDebugAction(Request $request, $flush = false)
  81. {
  82. //Config
  83. /*
  84. $lat1 = "45.759465500637";
  85. $lng1 = "4.8491488866881";
  86. $lat2 = "45.759445726474";
  87. $lng2 = "4.8491441637739";
  88. $earth_radius = 6378137;
  89. $rlo1 = deg2rad($lng1);
  90. $rla1 = deg2rad($lat1);
  91. $rlo2 = deg2rad($lng2);
  92. $rla2 = deg2rad($lat2);
  93. $dlo = ($rlo2 - $rlo1) / 2;
  94. $dla = ($rla2 - $rla1) / 2;
  95. $a = (sin($dla) * sin($dla)) + cos($rla1) * cos($rla2) * (sin($dlo) * sin($dlo));
  96. $d = 2 * atan2(sqrt($a), sqrt(1 - $a));
  97. $valeur = ($earth_radius * $d);
  98. echo $valeur;
  99. die;
  100. */
  101. $tabHobit = [];
  102. //For the distance between current position and last position default 5 (in metter))
  103. $limitWidthHobit = 10;
  104. //Time minimum between current position and last position, in second
  105. $limitTimeHobit = 120;
  106. //default : 20
  107. $limitForCreateHobit = 2;
  108. //For create hobit, remove error and driver
  109. $limitStable = 1;
  110. //For sum in XX address (Good points in target)
  111. $maxSum = 100;
  112. //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.
  113. $numberIntervalPrintedAndServer = 6;
  114. //Debug
  115. $debug = true;
  116. $debugAndFlush = $flush;
  117. $session = new Session();
  118. $userId = $session->get('currentUser');
  119. if(!$userId){
  120. $userId = 4;
  121. }
  122. $userCurrent = $this->em->getRepository(User::class)->find($userId);
  123. //Write 'null' for disable
  124. $dateStartForce = "20220622080000";
  125. $dateEndForce = null;
  126. //$dateEndForce = "20220528170000";
  127. $tabHobit = $this->tools->dreamHobit($debug, $debugAndFlush, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $limitStable, $maxSum, $numberIntervalPrintedAndServer, $userCurrent, $dateEndForce, $dateStartForce);
  128. if($debugAndFlush){
  129. $hobits = $this->tools->createHobit($tabHobit, $limitWidthHobit, $limitTimeHobit, $limitForCreateHobit, $maxSum, $userCurrent);
  130. $this->addFlash('success', 'User has been sync.');
  131. return true;
  132. }
  133. else{
  134. $hobits = $tabHobit;
  135. }
  136. //var_dump($tabHobit);
  137. //die;
  138. /*
  139. $curl = curl_init();
  140. // Set some options - we are passing in a useragent too here
  141. curl_setopt_array($curl, [
  142. CURLOPT_RETURNTRANSFER => 1,
  143. 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',
  144. CURLOPT_USERAGENT => 'Codular Sample cURL Request'
  145. ]);
  146. // Send the request & save response to $resp
  147. $resp = curl_exec($curl);
  148. // Close request to clear up some resources
  149. curl_close($curl);
  150. return $resp;
  151. // close curl resource to free up system resources
  152. die;
  153. */
  154. return $this->render('debug/hobit.html.twig', Array(
  155. "tabHobit" => $hobits,
  156. "debugAndFlush" => $debugAndFlush
  157. ));
  158. }
  159. /**
  160. * @Rest\View(statusCode=201)
  161. * @Rest\Get("/api/hobit/list")
  162. */
  163. public function getHobitAction(Request $request)
  164. {
  165. //Load base
  166. $em = $this->em;
  167. //All without "NOW"
  168. /*
  169. $hobits = $this->em
  170. ->getRepository(Hobit::class)
  171. ->findHobitAllWithoutNow();
  172. */
  173. $hobits = $this->em
  174. ->getRepository(Hobit::class)
  175. ->findHobitAll();
  176. return $hobits;
  177. }
  178. /**
  179. * @Rest\View(statusCode=201)
  180. * @Rest\Get("/api/hobit/hobit-date-list")
  181. */
  182. public function getHobitDateAction(Request $request)
  183. {
  184. //Load base
  185. $em = $this->em;
  186. //All without "NOW"
  187. /*
  188. $hobits = $this->em
  189. ->getRepository(Hobit::class)
  190. ->findHobitAllWithoutNow();
  191. */
  192. $hobitsDate = $this->em
  193. ->getRepository(HobitDate::class)
  194. ->findAll();
  195. $hobitsClean = [];
  196. foreach ($hobitsDate as $key => $hobitDate) {
  197. $hobitsClean[$key]['id'] = $hobitDate->getHobit()->getId();
  198. $hobitsClean[$key]['address'] = $hobitDate->getHobit()->getAddress();
  199. $hobitsClean[$key]['latitude'] = $hobitDate->getHobit()->getLatitude();
  200. $hobitsClean[$key]['longitude'] = $hobitDate->getHobit()->getLongitude();
  201. $hobitsClean[$key]['dateStartHobit'] = $hobitDate->getDateStartHobit();
  202. $hobitsClean[$key]['dateEndHobit'] = $hobitDate->getDateEndHobit();
  203. $hobitsClean[$key]['groupName'] = $hobitDate->getHobit()->getGroupName();
  204. $hobitsClean[$key]['groupNameTranslate'] = $hobitDate->getHobit()->getGroupNameTranslate();
  205. $hobitsClean[$key]['groupVerif'] = $hobitDate->getHobit()->getGroupVerif();
  206. }
  207. $hobitsClean = array_reverse($hobitsClean);
  208. return $hobitsClean;
  209. }
  210. /**
  211. * @Rest\View(statusCode=201)
  212. * @Rest\Get("/api/hobit/current")
  213. */
  214. public function getHobitCurrentAction(Request $request)
  215. {
  216. //Load base
  217. $em = $this->em;
  218. //Load get
  219. $hobit = $this->em
  220. ->getRepository(Hobit::class)
  221. ->findHobitCurrent();
  222. return $hobit;
  223. }
  224. /**
  225. * @Rest\View(statusCode=201)
  226. * @Rest\Get("/api/hobit/history/{idHobit}")
  227. */
  228. public function getHobitHistoryAction(Request $request)
  229. {
  230. //Load base
  231. $em = $this->em;
  232. //Load get
  233. $idHobit = $request->get('idHobit');
  234. $hobits = $this->em
  235. ->getRepository(Hobit::class)
  236. ->findAllHobitdateByHobit($idHobit);
  237. /*
  238. $tabHobits = [];
  239. foreach ($hobits as $key -> $hobit) {
  240. $tabHobits[$key] =
  241. }
  242. */
  243. return $hobits;
  244. }
  245. /**
  246. * @Rest\View(statusCode=201)
  247. * @Rest\Get("/api/hobit/group")
  248. */
  249. public function getHobitGroupAction(Request $request)
  250. {
  251. //Load base
  252. $em = $this->em;
  253. //Load get
  254. $hobits = $this->em
  255. ->getRepository(Hobit::class)
  256. ->findHobitGroup();
  257. return $hobits;
  258. }
  259. /**
  260. * @Rest\View(statusCode=201)
  261. * @Rest\Get("/api/hobit/group-no-verif")
  262. */
  263. public function getHobitGroupVerifAction(Request $request)
  264. {
  265. //Load base
  266. $em = $this->em;
  267. //Load get
  268. $hobits = $this->em
  269. ->getRepository(Hobit::class)
  270. ->findHobitGroupNoVerif();
  271. $hobitsClean = [];
  272. foreach ($hobits as $key => $hobit) {
  273. $hobitsClean[$key]['id'] = $hobit->getId();
  274. $hobitsClean[$key]['address'] = $hobit->getAddress();
  275. $hobitsClean[$key]['latitude'] = $hobit->getLatitude();
  276. $hobitsClean[$key]['longitude'] = $hobit->getLongitude();
  277. $hobitsClean[$key]['groupName'] = $hobit->getGroupName();
  278. $hobitsClean[$key]['groupNameTranslate'] = $hobit->getGroupNameTranslate();
  279. $hobitsClean[$key]['groupVerif'] = $hobit->getGroupVerif();
  280. }
  281. return $hobitsClean;
  282. }
  283. /**
  284. * @Rest\View(statusCode=201)
  285. * @Rest\Get("/api/hobit/city")
  286. */
  287. public function getHobitCityAction(Request $request)
  288. {
  289. //Load base
  290. $em = $this->em;
  291. //Load get
  292. $hobits = $this->em
  293. ->getRepository(Hobit::class)
  294. ->findHobitCity();
  295. $hobitsClean = [];
  296. foreach ($hobits as $key => $hobit) {
  297. $hobitsClean[$key]['city'] = $hobit['city'];
  298. /*
  299. $hobitsClean[$key]['id'] = $hobit['id'];
  300. $hobitsClean[$key]['address'] = $hobit['address'];
  301. $hobitsClean[$key]['latitude'] = $hobit['latitude'];
  302. $hobitsClean[$key]['longitude'] = $hobit['longitude'];
  303. $hobitsClean[$key]['groupName'] = $hobit['group_name'];
  304. $hobitsClean[$key]['groupVerif'] = $hobit['group_verif'];
  305. */
  306. }
  307. return $hobitsClean;
  308. }
  309. /**
  310. * @Rest\View(statusCode=201)
  311. * @Rest\Get("/api/hobit/group-change-verif/{hobitId}/{type}")
  312. */
  313. public function getHobitGroupChangeVerifAction(Request $request, $hobitId, $type)
  314. {
  315. //Load base
  316. $em = $this->em;
  317. //Load get
  318. $hobit = $this->em
  319. ->getRepository(Hobit::class)
  320. ->findOneBy(array("id" => $hobitId));
  321. if($hobit){
  322. if($type == "yes"){
  323. $hobit->setGroupVerif(1);
  324. }
  325. else if($type == "no"){
  326. $hobit->setGroupVerif(1);
  327. $hobit->setGroupName(null);
  328. }
  329. else if($type == "never"){
  330. $hobit->setGroupVerif(1);
  331. $hobit->setGroupName("never");
  332. }
  333. $em->persist($hobit);
  334. $em->flush();
  335. }
  336. return true;
  337. }
  338. }