src/Controller/GenericMateoMaticsController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Model\EwsNotificationModel;
  4. use DateTime;
  5. use Pimcore\Db;
  6. use App\Service\UserPermission;
  7. use Pimcore\Log\ApplicationLogger;
  8. use App\Service\MeteomaticApiService;
  9. use App\Service\MeteomaticsWeatherService;
  10. use App\Service\BassicAuthService;
  11. use App\Service\PublicUserPermissionService;
  12. use Pimcore\Controller\FrontendController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. /**
  21.  * Matches /blog exactly
  22.  *
  23.  * @Route("/api/ncm")
  24.  */
  25. class GenericMateoMaticsController extends FrontendController
  26. {
  27.     private $organizationModel;
  28.     private $userModel;
  29.     private $lang;
  30.     private $ewsNotificationModel;
  31.     public function __construct(
  32.         private TokenStorageInterface $tokenStorageInterface,
  33.         private JWTTokenManagerInterface $jwtManager,
  34.         private UserPermission $userPermission,
  35.         private PublicUserPermissionService $publicUserPermissionService,
  36.         protected TranslatorInterface $translator,
  37.         private ApplicationLogger $logger,
  38.         private MeteomaticApiService $meteomaticApiService,
  39.         private MeteomaticsWeatherService $meteomaticsWeatherService,
  40.         private BassicAuthService $bassicAuthService
  41.     ) {
  42.         header('Content-Type: application/json; charset=UTF-8');
  43.         header("Access-Control-Allow-Origin: *");
  44.         $this->meteomaticApiService $meteomaticApiService;
  45.         $this->ewsNotificationModel = new EwsNotificationModel();
  46.         $this->publicUserPermissionService $publicUserPermissionService;
  47.     }
  48.     /**
  49.      * @Route("/query", name="query", methods={"POST"})
  50.      */
  51.     public function fetchMeteomaticData(Request $request): JsonResponse
  52.     {
  53.         $params json_decode($request->getContent(), true);
  54.         $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  55.         // check user credentials and expiry
  56.         $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  57.         if ($response['success'] !== true) {
  58.             return $this->json($response);
  59.         }
  60.         if (!isset($params['format'])) {
  61.             throw new \InvalidArgumentException('Missing "format" parameter');
  62.         }
  63.         if ($params['format'] == "json") {
  64.             if (
  65.                 !isset($params['startdate']) ||
  66.                 !isset($params['enddate']) ||
  67.                 !isset($params['resolution']) ||
  68.                 !isset($params['parameters']) ||
  69.                 !isset($params['lat']) ||
  70.                 !isset($params['lon']) ||
  71.                 !isset($params['format'])
  72.             ) {
  73.                 throw new \Exception('Missing required parameters');
  74.             }
  75.             // date_default_timezone_set('UTC');
  76.             //$hour = $params['hour'];
  77.             $format $params['format'];
  78.             // $startDate = new DateTime($params['startdate']);
  79.             // $endDate = new DateTime($params['enddate']);
  80.             $startDate $params['startdate'];
  81.             $endDate $params['enddate'];
  82.             $resolution $params['resolution'];
  83.             $parameters $params['parameters'];
  84.             $model $params['model'];
  85.             $lat $params['lat'];
  86.             $lon $params['lon'];
  87.             $response $this->meteomaticApiService->timeSeriesQuery(
  88.                 $startDate,
  89.                 $endDate,
  90.                 $resolution,
  91.                 $parameters,
  92.                 $model,
  93.                 $lat,
  94.                 $lon,
  95.                 $format,
  96.                 $this->translator
  97.             );
  98.         } else {
  99.             $mandatoryParams = ['version''request''layers''crs''bbox''format''width''height''tiled'];
  100.             foreach ($mandatoryParams as $param) {
  101.                 if (!isset($params[$param]) || empty($params[$param])) {
  102.                     $missingParams[] = $param;
  103.                 }
  104.             }
  105.             if (!empty($missingParams)) {
  106.                 // Throw an exception with a message that includes the missing parameters
  107.                 $parameterList implode(", "$missingParams);
  108.                 throw new \InvalidArgumentException(sprintf($this->translator->trans("missing_or_empty_mandatory_parameter: %s"), $parameterList));
  109.             }
  110.             header('Content-Type: image/png');
  111.             $result $this->meteomaticsWeatherService->getWeatherMap($params['version'], $params['request'], $params['layers'], $params['crs'], $params['bbox'], $params['format'], $params['width'], $params['height'], $params['tiled']);
  112.             echo $result;
  113.             exit;
  114.         }
  115.         // You can return or process the $data as needed
  116.         // For demonstration purposes, we will just return a JSON response
  117.         return $this->json($response);
  118.     }
  119.     /**
  120.      * @Route("/public-query", name="public_query", methods={"POST"})
  121.      */
  122.     public function publicMetarQuery(Request $request): JsonResponse
  123.     {
  124.         try {
  125.             $params json_decode($request->getContent(), true);
  126.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  127.             //check user credentials and expiry
  128.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  129.             if ($response['success'] !== true) {
  130.                 return $this->json($response);
  131.             }
  132.             if (
  133.                 !isset($params['startdate']) ||
  134.                 !isset($params['enddate']) ||
  135.                 !isset($params['resolution']) ||
  136.                 !isset($params['parameters']) ||
  137.                 !isset($params['coordinate']) ||
  138.                 !isset($params['format'])
  139.             ) {
  140.                 throw new \Exception('Missing required parameters');
  141.             }
  142.             // date_default_timezone_set('UTC');
  143.             //$hour = $params['hour'];
  144.             $format $params['format'];
  145.             $startDate $params['startdate'];
  146.             $endDate $params['enddate'];
  147.             $resolution $params['resolution'];
  148.             $parametersArray $params['parameters'];
  149.             $coordinate $params['coordinate'];
  150.             $parameters implode(','$parametersArray);
  151.             $model '';
  152.             if (isset($params['model']) && !empty($params['model'])) {
  153.                 $model $params['model'];
  154.             }
  155.             $source '';
  156.             if (isset($params['source']) && !empty($params['source'])) {
  157.                 $source $params['source'];
  158.             }
  159.             $onInvalid '';
  160.             if (isset($params['on_invalid']) && !empty($params['on_invalid'])) {
  161.                 $onInvalid $params['on_invalid'];
  162.             }
  163.             // Convert dates to the desired format (assuming start time is 00:00:00 and end time is 23:59:59)
  164.             $startDate = new DateTime($params['startdate']);
  165.             $endDate = new DateTime($params['enddate']);
  166.             // $startDateStr = $startDate->format(DateTime::ISO8601);
  167.             // $endDateStr = $endDate->format(DateTime::ISO8601);
  168.             // $dateRange = $startDateStr . "--" . $endDateStr . ':' . $resolution;
  169.             $response $this->meteomaticApiService->publicRouteQuery(
  170.                 $startDate,
  171.                 $endDate,
  172.                 $resolution,
  173.                 $parametersArray,
  174.                 $coordinate,
  175.                 $format,
  176.                 $model,
  177.                 $source,
  178.                 $onInvalid,
  179.             );
  180.             // You can return or process the $data as needed
  181.             // For demonstration purposes, we will just return a JSON response
  182.             return $this->json($response);
  183.         } catch (\Exception $ex) {
  184.             $this->logger->error($ex->getMessage());
  185.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  186.         }
  187.     }
  188.     /**
  189.      * @Route("/industry/query", name="industry_query", methods={"POST"})
  190.      */
  191.     public function fetchIndustryMeteomaticData(Request $request): JsonResponse
  192.     {
  193.         try {
  194.             $params json_decode($request->getContent(), true);
  195.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  196.             // check user credentials and expiry
  197.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  198.             if ($response['success'] !== true) {
  199.                 return $this->json($response);
  200.             }
  201.             $user $response['user'];
  202.             // Varify user allowed permissions
  203.             $reslult $this->publicUserPermissionService->publicUserPermissionCheck($user$params['parameters'], $this->translator);
  204.             if ($reslult['success'] !== true) {
  205.                 return $this->json($reslult);
  206.             }
  207.             if (!isset($params['format'])) {
  208.                 throw new \InvalidArgumentException('Missing "format" parameter');
  209.             }
  210.             if ($params['format'] == "json") {
  211.                 if (
  212.                     !isset($params['startdate']) ||
  213.                     !isset($params['enddate']) ||
  214.                     !isset($params['resolution']) ||
  215.                     !isset($params['parameters']) ||
  216.                     !isset($params['lat']) ||
  217.                     !isset($params['lon']) ||
  218.                     !isset($params['format'])
  219.                 ) {
  220.                     throw new \Exception('Missing required parameters');
  221.                 }
  222.                 // date_default_timezone_set('UTC');
  223.                 //$hour = $params['hour'];
  224.                 $format $params['format'];
  225.                 $startDate = new DateTime($params['startdate']);
  226.                 $endDate = new DateTime($params['enddate']);
  227.                 $resolution $params['resolution'];
  228.                 $parameters $params['parameters'];
  229.                 $model $params['model'];
  230.                 $lat $params['lat'];
  231.                 $lon $params['lon'];
  232.                 $response $this->meteomaticApiService->timeSeriesQuery(
  233.                     $startDate,
  234.                     $endDate,
  235.                     $resolution,
  236.                     $parameters,
  237.                     $model,
  238.                     $lat,
  239.                     $lon,
  240.                     $format,
  241.                     $this->translator
  242.                 );
  243.             } else {
  244.                 $mandatoryParams = ['version''request''layers''crs''bbox''format''width''height''tiled'];
  245.                 foreach ($mandatoryParams as $param) {
  246.                     if (!isset($params[$param]) || empty($params[$param])) {
  247.                         $missingParams[] = $param;
  248.                     }
  249.                 }
  250.                 if (!empty($missingParams)) {
  251.                     // Throw an exception with a message that includes the missing parameters
  252.                     $parameterList implode(", "$missingParams);
  253.                     throw new \InvalidArgumentException(sprintf($this->translator->trans("missing_or_empty_mandatory_parameter: %s"), $parameterList));
  254.                 }
  255.                 header('Content-Type: image/png');
  256.                 $result $this->meteomaticsWeatherService->getWeatherMap($params['version'], $params['request'], $params['layers'], $params['crs'], $params['bbox'], $params['format'], $params['width'], $params['height'], $params['tiled']);
  257.                 echo $result;
  258.                 exit;
  259.             }
  260.             // You can return or process the $data as needed
  261.             // For demonstration purposes, we will just return a JSON response
  262.             return $this->json($response);
  263.         } catch (\Exception $ex) {
  264.             $this->logger->error($ex->getMessage());
  265.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  266.         }
  267.     }
  268.     /**
  269.      * @Route("/governorate", methods={"GET"})
  270.      */
  271.     public function getGovernorateAction(Request $request)
  272.     {
  273.         try {
  274.             // check user credentials and expiry
  275.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  276.             if ($response['success'] !== true) {
  277.                 return $this->json($response);
  278.             }
  279.             $result $this->ewsNotificationModel->getGovernorates();
  280.             return $this->json($result);
  281.         } catch (\Exception $ex) {
  282.             $this->logger->error($ex->getMessage());
  283.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  284.         }
  285.     }
  286.     /**
  287.      * @Route("/municipality", methods={"GET"})
  288.      */
  289.     public function getMunicipalityAction(Request $request)
  290.     {
  291.         try {
  292.             // check user credentials and expiry
  293.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  294.             if ($response['success'] !== true) {
  295.                 return $this->json($response);
  296.             }
  297.             $result $this->ewsNotificationModel->getMunicipality();
  298.             return $this->json($result);
  299.         } catch (\Exception $ex) {
  300.             $this->logger->error($ex->getMessage());
  301.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  302.         }
  303.     }
  304. }