src/Controller/Api/v3/ThinclientController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\v3;
  3. use App\Entity\QuestionAnswer;
  4. use App\Entity\SaltOperation;
  5. use App\Entity\Survey;
  6. use App\Entity\SurveyAnswer;
  7. use App\Entity\SurveyQuestion;
  8. use App\Entity\SurveyQuestionChoice;
  9. use App\Entity\ThinClient;
  10. use App\Entity\ThinClientCommand;
  11. use App\Service\PollManagerService;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. #[Route(path'/api/v3')]
  19. class ThinclientController extends AbstractController
  20. {
  21.     #[Route("/thinclients/{token}"methods: ["POST"])]
  22.     public function startThinclients(String $tokenRequest $requestManagerRegistry $doctrine): JsonResponse
  23.     {
  24.         $thinclient $doctrine->getRepository(ThinClient::class)->findOneBySoftwareId($token);
  25.         // --- Gestion de la mise à jour des données de l'écran:
  26.         $startData json_decode($request->getContent(), true);
  27.         if ($thinclient) {
  28.             $thinclient->setLastStatusUpdate(new \DateTime());
  29.             if ($startData["action"] == "start") {
  30.                 $thinclient->setSoftwareVersion($startData["app_version"]);
  31.                 $thinclient->setToken($startData["sys_version"]);
  32.             }
  33.             // Gestion de la qualité réseau :
  34.             if (array_key_exists('network_quality'$startData)) {
  35.                 $thinclient->setNetworkQuality($startData["network_quality"]);
  36.             }
  37.             $doctrine->getManager()->persist($thinclient);
  38.             $doctrine->getManager()->flush();
  39.         }
  40.         return new JsonResponse([
  41.             "thinclient_id" => $thinclient $thinclient->getSoftwareId() : 0,
  42.             "thinclient_accept" => $thinclient $thinclient->getRegistration() : null,
  43.             "thinclient_name" => $thinclient $thinclient->getName() : null,
  44.             "thinclient_attach" => $thinclient
  45.                 $thinclient->getEntrance() ? $thinclient->getEntrance()->getId() : null
  46.                 null,
  47.         ]);
  48.     }
  49.     #[Route("/thinclients/register/{token}"methods: ["POST"])]
  50.     public function registerThinclients(String $tokenRequest $requestManagerRegistry $doctrine): JsonResponse
  51.     {
  52.         $registerData json_decode($request->getContent(), true);
  53.         $thinclient $doctrine->getRepository(ThinClient::class)->findOneBySoftwareId($token);
  54.         if ($thinclient) {
  55.             return new JsonResponse([
  56.                 "error" => "THINCLIENT EXISTING",
  57.             ]);
  58.         }
  59.         $thinClient = new ThinClient();
  60.         $thinClient->setName($registerData["screen_name"]);
  61.         $thinClient->setSoftwareVersion($registerData["screen_app_version"]);
  62.         $thinClient->setMacAddress($registerData["connection_mode"]);
  63.         $thinClient->setImsi($registerData["sim_card"]);
  64.         $thinClient->setToken($registerData["sys_version"]);
  65.         $thinClient->setLastKnownStatus(ThinClient::STATUS_UNACCEPTED);
  66.         $thinClient->resetDataConsumption();
  67.         $thinClient->setSoftwareId($token);
  68.         $thinClient->setIpAddress(date('j n Y h:i:s A'));
  69.         $registerOperation = new SaltOperation();
  70.         $registerOperation->setStatus(2);
  71.         $registerOperation->setDate(new \DateTime('now', new \DateTimeZone('UTC')));
  72.         $registerOperation->setType('production');
  73.         $registerOperation->setThinclient($thinClient);
  74.         $thinClient->addSaltOperation($registerOperation);
  75.         $doctrine->getManager()->persist($thinClient);
  76.         $doctrine->getManager()->persist($registerOperation);
  77.         $doctrine->getManager()->flush();
  78.         return new JsonResponse([
  79.             "error" => null,
  80.             "test" => $registerData["screen_name"],
  81.         ]);
  82.     }
  83.     #[Route("/thinclients/poll/{token}"methods: ["POST"])]
  84.     public function pollThinclients(String $tokenRequest $requestManagerRegistry $doctrinePollManagerService $pollManager): JsonResponse
  85.     {
  86.         $thinclient $doctrine->getRepository(ThinClient::class)->findOneBySoftwareId($token);
  87.         // --- Mise à jour du status et de la date du ping:
  88.         $thinClientStatus $thinclient->getLastKnownStatus();
  89.         if (ThinClient::STATUS_UNKNOWN == $thinClientStatus) {
  90.             $thinclient->setLastKnownStatus(ThinClient::STATUS_UP);
  91.         }
  92.         $thinclient->setLastStatusUpdate(new \DateTime());
  93.         // --- Décompte des pings:
  94.         $lastCounter_db $thinclient->getIpVpn();
  95.         $counter null;
  96.         $lastCounter null;
  97.         $now = new \DateTime();
  98.         if ($lastCounter_db == "nc" || !$lastCounter_db) {
  99.             $counter = [];
  100.             $counter["timestamp"] = $now->setTime(2300)->getTimestamp();
  101.             $counter["value"] = 1;
  102.         } else {
  103.             $lastCounter json_decode($lastCounter_dbtrue);
  104.             if ($now->getTimestamp() > $lastCounter["timestamp"]) {
  105.                 $counter["value"] = 1;
  106.                 $counter["timestamp"] = $now->setTime(2300)->getTimestamp();
  107.             } else {
  108.                 $counter["timestamp"] = $lastCounter["timestamp"];
  109.                 $counter["value"] = $lastCounter["value"] + 1;
  110.             }
  111.         }
  112.         $thinclient->setIpVpn(json_encode($counter));
  113.         $doctrine->getManager()->persist($thinclient);
  114.         $doctrine->getManager()->flush();
  115.         $pollData json_decode($request->getContent(), true);
  116.         $result $pollManager->processThinclientRequest($thinclient$pollData);
  117.         // Gestion de la qualité réseau :
  118.         if (array_key_exists('network_quality'$pollData)) {
  119.             $thinclient->setNetworkQuality($pollData["network_quality"]);
  120.             $doctrine->getManager()->persist($thinclient);
  121.             $doctrine->getManager()->flush();
  122.         }
  123.         // Gestion des commandes:
  124.         if (array_key_exists('commands'$pollData)) {
  125.             $returnCommands $pollData["commands"];
  126.             for ($i=0$i < (is_countable($returnCommands) ? count($returnCommands) : 0); $i++) { 
  127.                 $cmd $doctrine->getRepository(ThinClientCommand::class)->find($returnCommands[$i]["id"]);
  128.                 if ($returnCommands[$i]["result"] == 1) {
  129.                     $cmd->setStatus(1);
  130.                     $cmd->setDateSuccess(new \DateTime());
  131.                 } else {
  132.                     $cmd->setStatus(2);
  133.                 }
  134.                 $doctrine->getManager()->persist($cmd);
  135.                 $doctrine->getManager()->flush();
  136.             }
  137.         }
  138.         $commandsList = [];
  139.         $commands $doctrine->getRepository(ThinClientCommand::class)->findBy(
  140.             ["thinclient" => $thinclient->getId()]
  141.         );
  142.         for ($i=0$i count($commands); $i++) {
  143.             if ($commands[$i]->getStatus() == 0) {
  144.                 $cmd = [];
  145.                 $cmd["id"] = $commands[$i]->getId();
  146.                 $cmd["operation"] = $commands[$i]->getOperation();
  147.                 array_push($commandsList$cmd);
  148.             }
  149.         }
  150.         // --- Gestion des enquêtes reçues :
  151.         $surveysSaved = [];
  152.         if (array_key_exists('surveys'$pollData)) {
  153.             $returnSurveys $pollData["surveys"];
  154.             for ($i=0$i count($returnSurveys); $i++) {
  155.                 // --- Création d'une nouvelle réponse à une enquête :
  156.                 $surveyAnswer = new SurveyAnswer();
  157.                 $date = (new \DateTimeImmutable())->createFromFormat("Y-m-d\TH:i:s.u\Z"$returnSurveys[$i]["date"]);
  158.                 $surveyAnswer->setCreatedAt($date);
  159.                 $survey $doctrine->getRepository(Survey::class)->find($returnSurveys[$i]["surveyId"]);
  160.                 if ($survey) {
  161.                     $surveyAnswer->setSurvey($survey);
  162.                 }
  163.                 // --- Création des réponses aux questions de l'enquête :
  164.                 for ($j=0$j count($returnSurveys[$i]["answers"]); $j++) {
  165.                     
  166.                     $questionAnswer = new QuestionAnswer();
  167.                     $questionAnswer->setSurveyAnswer($surveyAnswer);
  168.                     $questionAnswer->setCreatedAt($date);
  169.                     $asw $returnSurveys[$i]["answers"][$j];
  170.                     $choice $doctrine->getRepository(SurveyQuestionChoice::class)->find($asw["id"]);
  171.                     $questionAnswer->setSelectedChoice($choice);
  172.                     $questionAnswer->setQuestion($choice->getSurveyQuestion());
  173.                     $doctrine->getManager()->persist($questionAnswer);
  174.                 }
  175.                 $doctrine->getManager()->persist($surveyAnswer);
  176.                 $doctrine->getManager()->flush();
  177.                 array_push($surveysSaved$returnSurveys[$i]["id"]);
  178.             }
  179.         }
  180.         $pollingFrequency $thinclient && $thinclient->getEntrance() && $thinclient->getEntrance()->getPollingFrequency()
  181.             ? $thinclient->getEntrance()->getPollingFrequency()
  182.             : 60;
  183.         return new JsonResponse([
  184.             "error" => null,
  185.             "pullserver_token" => time() + $pollingFrequency,
  186.             "data" => $result['data'],
  187.             "data_v2" => $result['data_v2'],
  188.             "commands" => $commandsList,
  189.             "surveys_saved" => $surveysSaved,
  190.             "thinclient_attach" => $thinclient
  191.                 $thinclient->getEntrance() ? $thinclient->getEntrance()->getId() : null
  192.                 null
  193.         ]);
  194.     }
  195. }