src/Controller/JavaAppliController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\AppBundle\Service\RouterRequest;
  4. use App\Entity\ActivityBlueprint;
  5. use App\Entity\Routeur;
  6. use App\Entity\User;
  7. use App\Form\ActivityBlueprintType;
  8. use App\Form\RegistrationFormType;
  9. use App\Form\RouteurType;
  10. use App\Security\UserAuthenticator;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  19. class JavaAppliController extends AbstractController
  20. {
  21.     #[Route('/', name: 'app_java_appli')]
  22.     public function index(): Response
  23.     {
  24.         return $this->render('java_appli/index.html.twig');
  25.     }
  26.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27.     /// USER!
  28.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29.     #[Route('/list_user', name: 'app_list_user')]
  30.     public function list_user(EntityManagerInterface $entityManager): Response
  31.     {
  32.         if (!$this->getUser()) {
  33.             return $this->redirectToRoute('index');
  34.         }
  35.         $users = $entityManager->getRepository(User::class)->findAll();
  36.         return $this->render('java_appli/user/list_user.html.twig', [
  37.             'users' => $users,
  38.             'title' => 'List of users!'
  39.         ]);
  40.     }
  41.     #[Route('/user/{id}', name: 'app_one_user')]
  42.     public function one_user(int $id, EntityManagerInterface $entityManager): Response
  43.     {
  44.         if (!$this->getUser()) {
  45.             return $this->redirectToRoute('index');
  46.         }
  47.         $users = $entityManager->getRepository(User::class)->findBy(["id" => $id]);
  48.         if (count($users)<1){
  49.             throw $this->createNotFoundException(
  50.                 'No users found'
  51.             );
  52.         }
  53.         return $this->render('java_appli/user/list_user.html.twig', [
  54.             'users' => $users,
  55.             'title' => 'Show one user'
  56.         ]);
  57.     }
  58.     #[Route('/user/{id}/delete', name: 'app_delete_user')]
  59.     public function delete_user(int $id, EntityManagerInterface $entityManager): Response
  60.     {
  61.         if (!$this->getUser()) {
  62.             return $this->redirectToRoute('index');
  63.         }
  64.         $user = $entityManager->getRepository(User::class)->find($id);
  65.         if (!$user) {
  66.             throw $this->createNotFoundException(
  67.                 'No user found for id '.$id
  68.             );
  69.         }
  70.         $entityManager->remove($user);
  71.         $entityManager->flush();
  72.         return $this->redirectToRoute('list_user');
  73.     }
  74.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  75.     /// API
  76.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  77.     #[Route('/api/list_user', name: 'api_list_user')]
  78.     public function api_list_user(EntityManagerInterface $entityManager)
  79.     {
  80.         $response = $entityManager->getRepository(User::class)->findAll();
  81.         $user_data = [];
  82.         $counting = 1;
  83.         foreach ($response as $user){
  84.             $user_name = $user->getUserIdentifier();
  85.             $user_token = $user->getAuthToken();
  86.             $user_data[] = ["nr"=> $counting,"user name" => $user_name, "user token" => $user_token];
  87.             $counting++;
  88.         }
  89.         return new JsonResponse($user_data);
  90.     }
  91.     #[Route('api/list_router', name: 'api_list_router')]
  92.     public function api_list_router(EntityManagerInterface $entityManager): Response
  93.     {
  94.         $routers = $entityManager->getRepository(Routeur::class)->findAll();
  95.         $router_data = [];
  96.         foreach ($routers as $router){
  97.             $router_name = $router->getSignature();
  98.             $auth_token = $router->getAuthToken();
  99.             $router_data[] = ["router signature" => $router_name, "auth token" => $auth_token];
  100.         }
  101.         return new JsonResponse($router_data);
  102.     }
  103.     #[Route('api/say_my_name', name: 'api_say_my_name')]
  104.     public function api_say_my_name(EntityManagerInterface $entityManager): Response
  105.     {
  106.         $user = $this->getUser();
  107.         $router_data[] = ["my_name" => $user->getUserIdentifier()];
  108.         return new JsonResponse($router_data);
  109.     }
  110.     #[Route('api/activity_blueprint', name: 'api_activity_blueprint')]
  111.     public function api_activity_blueprint(EntityManagerInterface $entityManager): Response
  112.     {
  113.         $activities = $entityManager->getRepository(ActivityBlueprint::class)->findAll();
  114.         $activity_data = [];
  115.         foreach ($activities as $activity){
  116.             $id = $activity->getId();
  117.             $id_on_list = $activity->getIdOnList();
  118.             $title = $activity->getTitle();
  119.             $description = $activity->getDescription();
  120.             $nb_list = $activity->getNbList();
  121.             $entry_position = $activity->getEntryPosition();
  122.             $last_update = $activity->getUpdateDate();
  123. //            $deleted = $activity->getDeleted();
  124.             $activity_data[] = ["id"=>$id,
  125.                 "id_on_list" => $id_on_list,
  126.                 "title" => $title,
  127.                 "description" => $description,
  128.                 "nb_list" => $nb_list,
  129.                 "entry_position" => $entry_position,
  130.                 "last_update" => $last_update];
  131.         }
  132.         return new JsonResponse($activity_data);
  133.     }
  134.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135.     /// Check List!
  136.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  137.     #[Route('/task/list', name: 'list_task')]
  138.     public function list_task(EntityManagerInterface $entityManager): Response
  139.     {
  140.         if (!$this->getUser()) {
  141.             return $this->redirectToRoute('index');
  142.         }
  143.         $tasks = $entityManager->getRepository(ActivityBlueprint::class)->findAll();
  144.         return $this->render('java_appli/check_list/check_list.html.twig', [
  145.             'tasks' => $tasks,
  146.             'title' => 'List of all tasks!'
  147.         ]);
  148.     }
  149.     #[Route('/task/create', name: 'app_create_task')]
  150.     public function create_task(Request $request, EntityManagerInterface $entityManager): Response
  151.     {
  152.         if (!$this->getUser()) {
  153.             return $this->redirectToRoute('index');
  154.         }
  155.         $task = new ActivityBlueprint();
  156.         $form = $this->createForm(ActivityBlueprintType::class, $task);
  157.         $last_on_list = $entityManager->getRepository(ActivityBlueprint::class)->findLast();
  158.         $last_nr_on_list = $last_on_list[0]->getIdOnList() ;
  159.         $task->setUpdateDate(new \DateTime("now"));
  160.         $task->setIdOnList($last_nr_on_list+1);
  161.         $form->handleRequest($request);
  162.         if ($form->isSubmitted() && $form->isValid()) {
  163.             try {
  164.                 $entityManager->persist($task);
  165.                 $entityManager->flush();
  166.                 return $this->render('java_appli/check_list/new_task.html.twig', [
  167.                     'mode' => 'created'
  168.                 ]);
  169.             }catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e){
  170.                 return $this->render('java_appli/check_list/create_task.html.twig', [
  171.                     'taskForm' => $form->createView(),
  172.                     'mode' => 'Create',
  173.                     'error' => 'This combination of list number and entry position already exists.'
  174.                 ]);
  175.             }
  176.         }
  177.         return $this->render('java_appli/check_list/create_task.html.twig', [
  178.             'taskForm' => $form->createView(),
  179.             'mode' => 'Create'
  180.         ]);
  181.     }
  182.     #[Route('/task/{id}', name: 'app_one_task')]
  183.     public function one_task(string $id, EntityManagerInterface $entityManager): Response
  184.     {
  185.         if (!$this->getUser()) {
  186.             return $this->redirectToRoute('index');
  187.         }
  188.         $task = $entityManager->getRepository(ActivityBlueprint::class)->findBy(["id" => $id]);
  189.         if (!$task) {
  190.             throw $this->createNotFoundException(
  191.                 'No task found with this id: '. $id
  192.             );
  193.         }
  194.         return $this->render('java_appli/check_list/check_list.html.twig', [
  195.             'tasks' => $task,
  196.             'title' => 'One of the tasks!'
  197.         ]);
  198.     }
  199.     #[Route('/task/{id}/edit', name: 'app_edit_task')]
  200.     public function edit_task(int $id, Request $request, EntityManagerInterface $entityManager): Response
  201.     {
  202.         if (!$this->getUser()) {
  203.             return $this->redirectToRoute('index');
  204.         }
  205.         $task = $entityManager->getRepository(ActivityBlueprint::class)->findOneBy(["id" => $id]);
  206.         if (!$task) {
  207.             throw $this->createNotFoundException(
  208.                 'No task found with this id: '. $id
  209.             );
  210.         }
  211.         $form = $this->createForm(ActivityBlueprintType::class, $task);
  212.         $task->setUpdateDate(new \DateTime("now"));
  213.         $form->handleRequest($request);
  214.         if ($form->isSubmitted() && $form->isValid()) {
  215.             try {
  216.                 $entityManager->persist($task);
  217.                 $entityManager->flush();
  218.                 return $this->render('java_appli/check_list/new_task.html.twig', [
  219.                     'mode' => 'edit'
  220.                 ]);
  221.             }catch (\Doctrine\DBAL\Exception\UniqueConstraintViolationException $e){
  222.                 return $this->render('java_appli/check_list/create_task.html.twig', [
  223.                     'taskForm' => $form->createView(),
  224.                     'mode' => 'Modify',
  225.                     'error' => 'This combination of list number and entry position already exists.'
  226.                 ]);
  227.             }
  228.         }
  229.         return $this->render('java_appli/check_list/create_task.html.twig', [
  230.             'taskForm' => $form->createView(),
  231.             'mode' => 'Modify'
  232.         ]);
  233.     }
  234.     #[Route('/task/{id}/delete', name: 'app_delete_task')]
  235.     public function delete_task(int $id, EntityManagerInterface $entityManager): Response
  236.     {
  237.         if (!$this->getUser()) {
  238.             return $this->redirectToRoute('index');
  239.         }
  240.         $task = $entityManager->getRepository(ActivityBlueprint::class)->findOneBy(["id" => $id]);
  241.         if (!$task) {
  242.             throw $this->createNotFoundException(
  243.                 'No task found with this id: '. $id
  244.             );
  245.         }
  246.         $entityManager->remove($task);
  247.         $entityManager->flush();
  248.         return $this->redirectToRoute('list_task');
  249.     }
  250.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  251.     /// ROUTER!
  252.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  253.     #[Route('/router/list', name: 'app_list_router')]
  254.     public function list_router(EntityManagerInterface $entityManager): Response
  255.     {
  256.         if (!$this->getUser()) {
  257.             return $this->redirectToRoute('index');
  258.         }
  259.         $routers = $entityManager->getRepository(Routeur::class)->findAll();
  260.         return $this->render('java_appli/router/list_router.html.twig', [
  261.             'routers' => $routers,
  262.             'title' => 'List of all routers!'
  263.         ]);
  264.     }
  265.     #[Route('/router/create', name: 'app_create_router')]
  266.     public function create_router(Request $request, EntityManagerInterface $entityManager): Response
  267.     {
  268.         if (!$this->getUser()) {
  269.             return $this->redirectToRoute('index');
  270.         }
  271.         $router = new Routeur();
  272.         $form = $this->createForm(RouteurType::class, $router);
  273.         $form->handleRequest($request);
  274.         if ($form->isSubmitted() && $form->isValid()) {
  275.             $entityManager->persist($router);
  276.             $entityManager->flush();
  277.             return $this->render('java_appli/router/new_router.html.twig', [
  278.                 'mode' => 'created'
  279.             ]);
  280.         }
  281.         return $this->render('java_appli/router/create_router.html.twig', [
  282.             'registrationForm' => $form->createView(),
  283.             'mode' => 'Create'
  284.         ]);
  285.     }
  286.     #[Route('/router/{signature}', name: 'app_one_router')]
  287.     public function one_router(string $signature, EntityManagerInterface $entityManager): Response
  288.     {
  289.         if (!$this->getUser()) {
  290.             return $this->redirectToRoute('index');
  291.         }
  292.         $router = $entityManager->getRepository(Routeur::class)->findBy(["signature" => $signature]);
  293.         if (!$router) {
  294.             throw $this->createNotFoundException(
  295.                 'No router found with signature: '.$signature
  296.             );
  297.         }
  298.         return $this->render('java_appli/router/list_router.html.twig', [
  299.             'routers' => $router,
  300.             'title' => 'One of the routers!'
  301.         ]);
  302.     }
  303.     #[Route('/router/{signature}/edit', name: 'app_edit_router')]
  304.     public function edit_router(string $signature, Request $request, EntityManagerInterface $entityManager): Response
  305.     {
  306.         if (!$this->getUser()) {
  307.             return $this->redirectToRoute('index');
  308.         }
  309.         $router = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" => $signature]);
  310.         if (!$router) {
  311.             throw $this->createNotFoundException(
  312.                 'No router found with signature: '.$signature
  313.             );
  314.         }
  315.         $form = $this->createForm(RouteurType::class, $router);
  316.         $form->handleRequest($request);
  317.         if ($form->isSubmitted() && $form->isValid()) {
  318.             $entityManager->persist($router);
  319.             $entityManager->flush();
  320.             // do anything else you need here, like send an email
  321.             return $this->render('java_appli/router/new_router.html.twig', [
  322.                 'mode' => 'edit'
  323.             ]);
  324.         }
  325.         return $this->render('java_appli/router/create_router.html.twig', [
  326.             'registrationForm' => $form->createView(),
  327.             'mode' => 'Modify'
  328.         ]);
  329.     }
  330.     #[Route('/router/{signature}/delete', name: 'app_delete_router')]
  331.     public function delete_router(string $signature, EntityManagerInterface $entityManager): Response
  332.     {
  333.         if (!$this->getUser()) {
  334.             return $this->redirectToRoute('index');
  335.         }
  336.         $router = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" => $signature]);
  337.         if (!$router) {
  338.             throw $this->createNotFoundException(
  339.                 'No router found with signature: '.$signature
  340.             );
  341.         }
  342.         $entityManager->remove($router);
  343.         $entityManager->flush();
  344.         return $this->redirectToRoute('list_router');
  345.     }
  346.     //////////////////////////////////////////////////////////////
  347.     ///             Request API for VRM
  348.     //////////////////////////////////////////////////////////////
  349.     #[Route('/api/vrm/{signature}', name: 'app_VRM_get_info')]
  350.     public function info_from_VRM(string $signature, RouterRequest $router_request, EntityManagerInterface $entityManager): Response
  351.     {
  352.         $gps = $router_request->actualisationVRM($signature, "/widgets/GPS");
  353.         if($gps["success"]){
  354.             $lat = $gps["records"]["data"]["attributes"][4]["value"];
  355.             $lng = $gps["records"]["data"]["attributes"][5]["value"];
  356.             $gps = $gps["records"]["data"]["attributes"];
  357.         }else{
  358.             $lat = [];
  359.             $lng = [];
  360.             $gps = [];
  361.         }
  362.         dd($gps);   ////////////////////////////////////////////
  363.                     ///  ------------------------------------ add more things from page VRM
  364.                     ///
  365.                     ///
  366.                     ///
  367.                     ///
  368.         $diagnostics = $router_request->actualisationVRM($signature, "/widgets/Graph?attributeIds%5B%5D=51&start=1659020015");
  369.         $overview = $router_request->actualisationVRM($signature, "/system-overview"); //get info from VRM with $signature
  370.         if($overview["success"]){
  371.             $devices = $overview["records"]["devices"];
  372.         }else{
  373.             $devices = [];
  374.         }
  375.         return $this->render('java_appli/router/router.html.twig', [
  376.             'lat' => $lat,
  377.             'lng' => $lng,
  378.             'gps' => $gps ,
  379.             'devices' => $devices,
  380.         ]);
  381.     }
  382. //    #[Route('/api/router/{signature}/update', name: 'app_java_update_router', methods: ["POST"])]
  383. //    public function update_router(string $signature, Request $request, EntityManagerInterface $entityManager): Response
  384. //    {
  385. //        $routerAuth = $entityManager->getRepository(Routeur::class)->findOneBy(["signature" =>$signature]);
  386. //        $headers = $request->headers;
  387. //        $authToken = $headers->get("AuthToken");
  388. //        if($routerAuth->getAuthToken() == $authToken){
  389. //            //update info about  (add new line with info to table with date etc)
  390. //            return new Response("Auth ok, with this u can edit info in DB router!", 200, [
  391. //                "Content-Type" => "application/json"
  392. //            ]);
  393. //        }
  394. //        $response = $request->getContent();
  395. //        return new Response("Authorization denied.", 401, [
  396. //            "Content-Type" => "application/json"
  397. //        ]);
  398. //    }
  399. }