src/Controller/DeviceApiController.php line 1081

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. use DateTime;
  9. use App\Model\WeatherStationModel;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use App\Service\NCMWeatherAPIService;
  12. use App\Service\BassicAuthService;
  13. use App\Service\MeteomaticsWeatherService;
  14. use App\Service\UserPermission;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use App\Model\EwsNotificationModel;
  17. use App\Model\DeviceModel;
  18. use App\Model\ReportLogModel;
  19. use Pimcore\Log\ApplicationLogger;
  20. use Symfony\Component\Templating\EngineInterface;
  21. use App\Service\EmailService;
  22. use App\Service\MeteomaticApiService;
  23. use App\Service\PublicUserPermissionService;
  24. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  25. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  26. use Knp\Component\Pager\PaginatorInterface;
  27. use App\Model\WeatherForecastCityModel;
  28. use App\Service\CustomNotificationService;
  29. /**
  30.  * User controller is responsible for handling various actions related to user management.
  31.  *
  32.  * @Route("/api/ncm/device")
  33.  */
  34. class DeviceApiController extends FrontendController
  35. {
  36.     private $weatherStationModel;
  37.     private $ewsNotificationModel;
  38.     private $deviceModel;
  39.     private $reportLogModel;
  40.     var $emailService null;
  41.     public function __construct(
  42.         protected TranslatorInterface $translator,
  43.         private ApplicationLogger $logger,
  44.         private EngineInterface $templating,
  45.         private NCMWeatherAPIService $ncmWeatherApiService,
  46.         private MeteomaticApiService $meteomaticApiService,
  47.         private MeteomaticsWeatherService $meteomaticsWeatherService,
  48.         private BassicAuthService $bassicAuthService,
  49.         private UserPermission $userPermission,
  50.         private TokenStorageInterface $tokenStorageInterface,
  51.         private JWTTokenManagerInterface $jwtManager,
  52.         private PublicUserPermissionService $publicUserPermissionService,
  53.         private CustomNotificationService $customNotificationService
  54.     ) {
  55.         header('Content-Type: application/json; charset=UTF-8');
  56.         header("Access-Control-Allow-Origin: *");
  57.         $this->ewsNotificationModel = new EwsNotificationModel();
  58.         $this->weatherStationModel = new WeatherStationModel();
  59.         $this->deviceModel = new DeviceModel();
  60.         $this->reportLogModel = new ReportLogModel();
  61.         $this->templating $templating;
  62.         $this->emailService = new EmailService();
  63.     }
  64.     /**
  65.      * @Route("/regions", methods={"POST"})
  66.      */
  67.     public function regions(Request $request): JsonResponse
  68.     {
  69.         try {
  70.             $params json_decode($request->getContent(), true);
  71.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  72.             // check user credentials and expiry
  73.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  74.             if ($response['success'] !== true) {
  75.                 return $this->json($response);
  76.             }
  77.             $search = (isset($params['search']) && !empty($params['search'])) ? $params['search'] : null;
  78.             $result $this->ncmWeatherApiService->getRegions($search, isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  79.             return $this->json($result);
  80.         } catch (\Exception $ex) {
  81.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  82.         }
  83.     }
  84.     /**
  85.      * @Route("/route-query", methods={"POST"})
  86.      */
  87.     public function routeQuery(Request $request): JsonResponse
  88.     {
  89.         $params json_decode($request->getContent(), true);
  90.         $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  91.         // check basic Authorization
  92.         $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  93.         if ($response['success'] !== true) {
  94.             return $this->json($response);
  95.         }
  96.         // validate required parms
  97.         if (
  98.             !isset($params['date']) || !isset($params['parameters']) || !isset($params['location'])
  99.         ) {
  100.             throw new \Exception('Missing required parameters');
  101.         }
  102.         $locations $params['location'];
  103.         $parameters $params['parameters'];
  104.         // Join the locations and parameters strings with ',' for the URL
  105.         $parameterString $parameters;
  106.         $locationString implode('+'$locations);
  107.         // Create a DateTime object
  108.         $date = new DateTime($params['date']);
  109.         $dateStrings = [];
  110.         // Format the DateTime object as a string and add it to the array
  111.         for ($i 0$i count($locations); $i++) {
  112.             $dateStrings[] = $date->format(DateTime::ISO8601);
  113.         }
  114.         // Join the date strings with ',' for the URL
  115.         $dateString implode(','$dateStrings);
  116.         $response $this->meteomaticApiService->getRouteQuery(
  117.             $dateString,
  118.             $parameterString,
  119.             $locationString
  120.         );
  121.         // For demonstration purposes, we will just return a JSON response
  122.         return $this->json($response);
  123.     }
  124.     /**
  125.      * @Route("/municipality", methods={"POST"})
  126.      */
  127.     public function getMunicipalityAction(Request $request): JsonResponse
  128.     {
  129.         try {
  130.             $params json_decode($request->getContent(), true);
  131.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  132.             // check user credentials and expiry
  133.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  134.             if ($response['success'] !== true) {
  135.                 return $this->json($response);
  136.             }
  137.             if (!isset($params['governorate_id'])) {
  138.                 throw new \Exception('Missing required governorate id');
  139.             }
  140.             $governorateId $params['governorate_id'];
  141.             $result $this->ewsNotificationModel->getMunicipality($governorateId$params['lang']);
  142.             return $this->json($result);
  143.         } catch (\Exception $ex) {
  144.             $this->logger->error($ex->getMessage());
  145.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  146.         }
  147.     }
  148.     /**
  149.      * @Route("/governorates", methods={"POST"})
  150.      */
  151.     public function getGovernorateByRegionAction(Request $request): JsonResponse
  152.     {
  153.         try {
  154.             $params json_decode($request->getContent(), true);
  155.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  156.             // check user credentials and expiry
  157.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  158.             if ($response['success'] !== true) {
  159.                 return $this->json($response);
  160.             }
  161.             // if (!isset($params['region_id'])) {
  162.             //     throw new \Exception('Missing required region id');
  163.             // }
  164.             // $regionId = $params['region_id'];
  165.             $result $this->ewsNotificationModel->getGovernoratesByRegion($params);
  166.             return $this->json($result);
  167.         } catch (\Exception $ex) {
  168.             $this->logger->error($ex->getMessage());
  169.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  170.         }
  171.     }
  172.     /**
  173.      * @Route("/tidal-amplitude", name="tidal-amplitude", methods={"POST"})
  174.      */
  175.     public function tidalAmplitudeData(Request $request): JsonResponse
  176.     {
  177.         try {
  178.             $params json_decode($request->getContent(), true);
  179.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  180.             // check user credentials and expiry
  181.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  182.             if ($response['success'] !== true) {
  183.                 return $this->json($response);
  184.             }
  185.             $requiredParameters = ['hour''parameter''date''coordinates''model''format'];
  186.             foreach ($requiredParameters as $param) {
  187.                 if (!isset($params[$param])) {
  188.                     $missingParams[] = $param;
  189.                 }
  190.             }
  191.             if (!empty($missingParams)) {
  192.                 // Throw an exception with a message that includes the missing parameters
  193.                 $parameterList implode(", "$missingParams);
  194.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  195.             }
  196.             $hour $params['hour'];
  197.             $parameter $params['parameter'];
  198.             $date $params['date'];
  199.             $coordinates $params['coordinates'];
  200.             $model $params['model'];
  201.             $format $params['format'];
  202.             $response $this->meteomaticsWeatherService->getTidalAmplitudes(
  203.                 $hour,
  204.                 $parameter,
  205.                 $date,
  206.                 $coordinates,
  207.                 $model,
  208.                 $format
  209.             );
  210.             // You can return or process the $data as needed
  211.             // For demonstration purposes, we will just return a JSON response
  212.             return $this->json($response);
  213.         } catch (\Exception $ex) {
  214.             $this->logger->error($ex->getMessage());
  215.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  216.         }
  217.     }
  218.     /**
  219.      * @Route("/high-low-tide-times", name="high_low_tide_times", methods={"POST"})
  220.      */
  221.     public function highLowTideTimesData(Request $request): JsonResponse
  222.     {
  223.         try {
  224.             $params json_decode($request->getContent(), true);
  225.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  226.             // check user credentials and expiry
  227.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  228.             if ($response['success'] !== true) {
  229.                 return $this->json($response);
  230.             }
  231.             $requiredParameters = ['hour''date''coordinates''model''format'];
  232.             foreach ($requiredParameters as $param) {
  233.                 if (!isset($params[$param])) {
  234.                     $missingParams[] = $param;
  235.                 }
  236.             }
  237.             if (!empty($missingParams)) {
  238.                 // Throw an exception with a message that includes the missing parameters
  239.                 $parameterList implode(", "$missingParams);
  240.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  241.             }
  242.             $hour $params['hour'];
  243.             $date $params['date'];
  244.             $coordinates $params['coordinates'];
  245.             $model $params['model'];
  246.             $format $params['format'];
  247.             $response $this->meteomaticsWeatherService->getHighLowTideTimes(
  248.                 $hour,
  249.                 $date,
  250.                 $coordinates,
  251.                 $model,
  252.                 $format
  253.             );
  254.             // You can return or process the $data as needed
  255.             // For demonstration purposes, we will just return a JSON response
  256.             return $this->json($response);
  257.         } catch (\Exception $ex) {
  258.             $this->logger->error($ex->getMessage());
  259.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  260.         }
  261.     }
  262.     /**
  263.      * @Route("/significant-wave-height", name="significant_wave_height", methods={"POST"})
  264.      */
  265.     public function significantWaveHeightData(Request $request): JsonResponse
  266.     {
  267.         try {
  268.             $params json_decode($request->getContent(), true);
  269.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  270.             // check user credentials and expiry
  271.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  272.             if ($response['success'] !== true) {
  273.                 return $this->json($response);
  274.             }
  275.             $requiredParameters = ['hour''date''coordinates''format'];
  276.             foreach ($requiredParameters as $param) {
  277.                 if (!isset($params[$param])) {
  278.                     $missingParams[] = $param;
  279.                 }
  280.             }
  281.             if (!empty($missingParams)) {
  282.                 // Throw an exception with a message that includes the missing parameters
  283.                 $parameterList implode(", "$missingParams);
  284.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  285.             }
  286.             $hour $params['hour'];
  287.             $date $params['date'];
  288.             $coordinates $params['coordinates'];
  289.             $format $params['format'];
  290.             $response $this->meteomaticsWeatherService->getSignificantWaveHeight(
  291.                 $hour,
  292.                 $date,
  293.                 $coordinates,
  294.                 $format
  295.             );
  296.             // You can return or process the $data as needed
  297.             // For demonstration purposes, we will just return a JSON response
  298.             return $this->json($response);
  299.         } catch (\Exception $ex) {
  300.             $this->logger->error($ex->getMessage());
  301.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  302.         }
  303.     }
  304.     /**
  305.      * @Route("/surge-amplitude", name="surge_amplitude", methods={"POST"})
  306.      */
  307.     public function surgeAmplitudeData(Request $request): JsonResponse
  308.     {
  309.         try {
  310.             $params json_decode($request->getContent(), true);
  311.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  312.             // check user credentials and expiry
  313.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  314.             if ($response['success'] !== true) {
  315.                 return $this->json($response);
  316.             }
  317.             $requiredParameters = ['hour''date''coordinates''model''format'];
  318.             foreach ($requiredParameters as $param) {
  319.                 if (!isset($params[$param])) {
  320.                     $missingParams[] = $param;
  321.                 }
  322.             }
  323.             if (!empty($missingParams)) {
  324.                 // Throw an exception with a message that includes the missing parameters
  325.                 $parameterList implode(", "$missingParams);
  326.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  327.             }
  328.             $hour $params['hour'];
  329.             $date $params['date'];
  330.             $coordinates $params['coordinates'];
  331.             $model $params['model'];
  332.             $format $params['format'];
  333.             $response $this->meteomaticsWeatherService->getSurgeAmplitude(
  334.                 $hour,
  335.                 $date,
  336.                 $coordinates,
  337.                 $model,
  338.                 $format
  339.             );
  340.             // You can return or process the $data as needed
  341.             // For demonstration purposes, we will just return a JSON response
  342.             return $this->json($response);
  343.         } catch (\Exception $ex) {
  344.             $this->logger->error($ex->getMessage());
  345.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  346.         }
  347.     }
  348.     /**
  349.      * @Route("/fog", name="fog", methods={"POST"})
  350.      */
  351.     public function fogData(Request $request): JsonResponse
  352.     {
  353.         try {
  354.             $params json_decode($request->getContent(), true);
  355.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  356.             // check user credentials and expiry
  357.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  358.             if ($response['success'] !== true) {
  359.                 return $this->json($response);
  360.             }
  361.             $requiredParameters = ['hour''date''coordinates''format'];
  362.             foreach ($requiredParameters as $param) {
  363.                 if (!isset($params[$param])) {
  364.                     $missingParams[] = $param;
  365.                 }
  366.             }
  367.             if (!empty($missingParams)) {
  368.                 // Throw an exception with a message that includes the missing parameters
  369.                 $parameterList implode(", "$missingParams);
  370.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  371.             }
  372.             $hour $params['hour'];
  373.             $date $params['date'];
  374.             $coordinates $params['coordinates'];
  375.             $format $params['format'];
  376.             $response $this->meteomaticsWeatherService->getFog(
  377.                 $coordinates,
  378.                 $date
  379.             );
  380.             // You can return or process the $data as needed
  381.             // For demonstration purposes, we will just return a JSON response
  382.             return $this->json($response);
  383.         } catch (\Exception $ex) {
  384.             $this->logger->error($ex->getMessage());
  385.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  386.         }
  387.     }
  388.     /**
  389.      * @Route("/search-ews-notification",  name = "search-ews-notification", methods={"POST"})
  390.      */
  391.     public function searchEwsNotificationAction(Request $requestPaginatorInterface $paginator): JsonResponse
  392.     {
  393.         try {
  394.             // check user credentials and expiry
  395.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  396.             if ($response['success'] !== true) {
  397.                 return $this->json($response);
  398.             }
  399.             $params json_decode($request->getContent(), true);
  400.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  401.             $lang = (isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  402.             $result $this->ewsNotificationModel->searchEwsNotification($params$lang$paginator null$this->translator);
  403.             return $this->json($result);
  404.         } catch (\Exception $ex) {
  405.             $this->logger->error($ex->getMessage());
  406.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  407.         }
  408.     }
  409.     /**
  410.      * @Route("/translations/{locale?}", name="translations", methods={"GET"})
  411.      */
  412.     public function frontendDashboardTranslation(Request $request$locale null)
  413.     {
  414.         try {
  415.             $response = [];
  416.             $db \Pimcore\Db::get();
  417.             if ($locale) {
  418.                 // Fetch translations for the provided locale
  419.                 $selectedLocalities $db->fetchAllAssociative("
  420.                 SELECT * FROM translations_messages 
  421.                 WHERE `key` LIKE 'publicapp_%' 
  422.                 AND `language` = :locale 
  423.                 ORDER BY `creationDate` ASC
  424.             ", ['locale' => $locale]);
  425.                 foreach ($selectedLocalities as $value) {
  426.                     $keyParts explode('_'$value['key'], 3);
  427.                     $category $keyParts[1];
  428.                     $key $keyParts[2];
  429.                     if (!isset($response[$category])) {
  430.                         $response[$category] = [];
  431.                     }
  432.                     $response[$category][$key] = $value['text'];
  433.                 }
  434.                 return $this->json($response);
  435.             } else {
  436.                 // Fetch translations for both 'ar' and 'en'
  437.                 $selectedLocalities $db->fetchAllAssociative("
  438.                 SELECT * FROM translations_messages 
  439.                 WHERE `key` LIKE 'publicapp_%' 
  440.                 AND `language` IN ('ar', 'en') 
  441.                 ORDER BY `creationDate` ASC
  442.             ");
  443.                 foreach ($selectedLocalities as $value) {
  444.                     $keyParts explode('_'$value['key'], 3);
  445.                     $category $keyParts[1];
  446.                     $key $keyParts[2];
  447.                     $language $value['language'];
  448.                     if (!isset($response[$language])) {
  449.                         $response[$language] = [];
  450.                     }
  451.                     if (!isset($response[$language][$category])) {
  452.                         $response[$language][$category] = [];
  453.                     }
  454.                     $response[$language][$category][$key] = $value['text'];
  455.                 }
  456.                 return $this->json($response);
  457.             }
  458.         } catch (\Exception $ex) {
  459.             $this->logger->error($ex->getMessage());
  460.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  461.         }
  462.     }
  463.     /**
  464.      * @Route("/list-ewsnotification", methods={"POST"})
  465.      */
  466.     public function listEwsNotificationAction(Request $requestPaginatorInterface $paginator): JsonResponse
  467.     {
  468.         try {
  469.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  470.             if ($response['success'] !== true) {
  471.                 return $this->json($response);
  472.             }
  473.             $params json_decode($request->getContent(), true);
  474.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  475.             $params['unpublished'] = false;
  476.             $params['status'] = 'active';
  477.             $result $this->ewsNotificationModel->notificationListing($params$paginator null$this->translator);
  478.             return $this->json($result);
  479.         } catch (\Exception $ex) {
  480.             $this->logger->error($ex->getMessage());
  481.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  482.         }
  483.     }
  484.     /**
  485.      * @Route("/query", methods={"POST"})
  486.      */
  487.     public function fetchMeteomaticData(Request $request): JsonResponse
  488.     {
  489.         $params json_decode($request->getContent(), true);
  490.         $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  491.         // check user credentials and expiry
  492.         $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  493.         if ($response['success'] !== true) {
  494.             return $this->json($response);
  495.         }
  496.         if (!isset($params['format'])) {
  497.             throw new \InvalidArgumentException('Missing "format" parameter');
  498.         }
  499.         if ($params['format'] == "json") {
  500.             if (
  501.                 !isset($params['startdate']) ||
  502.                 !isset($params['enddate']) ||
  503.                 !isset($params['resolution']) ||
  504.                 !isset($params['parameters']) ||
  505.                 !isset($params['lat']) ||
  506.                 !isset($params['lon']) ||
  507.                 !isset($params['format'])
  508.             ) {
  509.                 throw new \Exception('Missing required parameters');
  510.             }
  511.             // date_default_timezone_set('UTC');
  512.             //$hour = $params['hour'];
  513.             $format $params['format'];
  514.             $startDate $params['startdate'];
  515.             $endDate $params['enddate'];
  516.             $resolution $params['resolution'];
  517.             $parameters $params['parameters'];
  518.             $model $params['model'];
  519.             $lat $params['lat'];
  520.             $lon $params['lon'];
  521.             $response $this->meteomaticApiService->timeSeriesQuery(
  522.                 $startDate,
  523.                 $endDate,
  524.                 $resolution,
  525.                 $parameters,
  526.                 $model,
  527.                 $lat,
  528.                 $lon,
  529.                 $format,
  530.                 $this->translator
  531.             );
  532.         } else {
  533.             $mandatoryParams = ['version''request''layers''crs''bbox''format''width''height''tiled'];
  534.             foreach ($mandatoryParams as $param) {
  535.                 if (!isset($params[$param]) || empty($params[$param])) {
  536.                     $missingParams[] = $param;
  537.                 }
  538.             }
  539.             if (!empty($missingParams)) {
  540.                 // Throw an exception with a message that includes the missing parameters
  541.                 $parameterList implode(", "$missingParams);
  542.                 throw new \InvalidArgumentException(sprintf($this->translator->trans("missing_or_empty_mandatory_parameter: %s"), $parameterList));
  543.             }
  544.             header('Content-Type: image/png');
  545.             $result $this->meteomaticsWeatherService->getWeatherMap($params['version'], $params['request'], $params['layers'], $params['crs'], $params['bbox'], $params['format'], $params['width'], $params['height'], $params['tiled']);
  546.             echo $result;
  547.             exit;
  548.         }
  549.         // You can return or process the $data as needed
  550.         // For demonstration purposes, we will just return a JSON response
  551.         return $this->json($response);
  552.     }
  553.     /**
  554.      * @Route("/public-query", methods={"POST"})
  555.      */
  556.     public function publicMetarQuery(Request $request): JsonResponse
  557.     {
  558.         try {
  559.             $params json_decode($request->getContent(), true);
  560.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  561.             //check user credentials and expiry
  562.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  563.             if ($response['success'] !== true) {
  564.                 return $this->json($response);
  565.             }
  566.             if ($response['success'] !== true) {
  567.                 return $this->json($response);
  568.             }
  569.             if (
  570.                 !isset($params['startdate']) ||
  571.                 !isset($params['enddate']) ||
  572.                 !isset($params['resolution']) ||
  573.                 !isset($params['parameters']) ||
  574.                 !isset($params['coordinate']) ||
  575.                 !isset($params['format'])
  576.             ) {
  577.                 throw new \Exception('Missing required parameters');
  578.             }
  579.             // date_default_timezone_set('UTC');
  580.             //$hour = $params['hour'];
  581.             $format $params['format'];
  582.             $startDate $params['startdate'];
  583.             $endDate $params['enddate'];
  584.             $resolution $params['resolution'];
  585.             $parametersArray $params['parameters'];
  586.             $coordinate $params['coordinate'];
  587.             $parameters implode(','$parametersArray);
  588.             $model '';
  589.             if (isset($params['model']) && !empty($params['model'])) {
  590.                 $model $params['model'];
  591.             }
  592.             $source '';
  593.             if (isset($params['source']) && !empty($params['source'])) {
  594.                 $source $params['source'];
  595.             }
  596.             $onInvalid '';
  597.             if (isset($params['on_invalid']) && !empty($params['on_invalid'])) {
  598.                 $onInvalid $params['on_invalid'];
  599.             }
  600.             // Convert dates to the desired format (assuming start time is 00:00:00 and end time is 23:59:59)
  601.             $startDate = new DateTime($params['startdate']);
  602.             $endDate = new DateTime($params['enddate']);
  603.             $startDateStr $startDate->format(DateTime::ISO8601);
  604.             $endDateStr $endDate->format(DateTime::ISO8601);
  605.             $dateRange $startDateStr "--" $endDateStr ':' $resolution;
  606.             $response $this->meteomaticApiService->publicRouteQuery(
  607.                 $startDate,
  608.                 $endDate,
  609.                 $resolution,
  610.                 $parametersArray,
  611.                 $coordinate,
  612.                 $format,
  613.                 $model,
  614.                 $source,
  615.                 $onInvalid,
  616.             );
  617.             // You can return or process the $data as needed
  618.             // For demonstration purposes, we will just return a JSON response
  619.             return $this->json($response);
  620.         } catch (\Exception $ex) {
  621.             $this->logger->error($ex->getMessage());
  622.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  623.         }
  624.     }
  625.     /**
  626.      * @Route("/get-metar-data", methods={"POST"})
  627.      */ 
  628.     public function getMetarData(Request $request): Response
  629.     {
  630.         try {
  631.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  632.             if ($response['success'] !== true) {
  633.                 return $this->json($response);
  634.             }
  635.             $params json_decode($request->getContent(), true);
  636.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  637.             $requiredParameters = ['startDate''endDate''metar''duration'];
  638.             foreach ($requiredParameters as $param) {
  639.                 if (!isset($params[$param])) {
  640.                     $missingParams[] = $param;
  641.                 }
  642.             }
  643.             if (!empty($missingParams)) {
  644.                 // Throw an exception with a message that includes the missing parameters
  645.                 $parameterList implode(", "$missingParams);
  646.                 return $this->json(['success' => false'message' => sprintf($this->translator->trans("missing_required_parameters: %s"), $parameterList)]);
  647.             }
  648.             $startDate $params['startDate'];
  649.             $endDate $params['endDate'];
  650.             $metar $params['metar'];
  651.             $duration $params['duration'];
  652.             $parameters $params['parameter'] ?? null;
  653.             $genExcel $request->get('gen_excel'false);
  654.             $metarData $this->meteomaticsWeatherService->getMetarData($startDate$endDate$metar$duration$this->translator$parameters$genExcel);
  655.             return $this->json(['success' => true'message' => $this->translator->trans("excel_file_downloaded_successfully"), 'data' => $metarData]);
  656.         } catch (\Exception $ex) {
  657.             $this->logger->error($ex->getMessage());
  658.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  659.         }
  660.     }
  661.     /**
  662.      * @Route("/get-airport-stations", methods={"POST"})
  663.      */
  664.     public function getAirportStations(Request $request): JsonResponse
  665.     {
  666.         try {
  667.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  668.             if ($response['success'] !== true) {
  669.                 return $this->json($response);
  670.             }
  671.             $params json_decode($request->getContent(), true);
  672.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  673.             $result $this->ncmWeatherApiService->getAirportStations($params['ccode'] ?? null);
  674.             return $this->json(['success' => true'data' => $result]);
  675.         } catch (\Exception $ex) {
  676.             $this->logger->error($ex->getMessage());
  677.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  678.         }
  679.     }
  680.     /**
  681.      * @Route("/get-forecast-cities", methods={"POST"})
  682.      */
  683.     public function getForecastCities(Request $request): Response
  684.     {
  685.         try {
  686.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  687.             if ($response['success'] !== true) {
  688.                 return $this->json($response);
  689.             }
  690.             $params json_decode($request->getContent(), true);
  691.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  692.             $forecastCityModel = new WeatherForecastCityModel();
  693.             $cities $forecastCityModel->getWeatherForecastCities();
  694.             return $this->json(['success' => true'data' => $cities]);
  695.         } catch (\Exception $ex) {
  696.             $this->logger->error($ex->getMessage());
  697.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  698.         }
  699.     }
  700.     /**
  701.      * @Route("/momra-generate-token", methods={"POST"})
  702.      */
  703.     public function getMomraGenerateToken(Request $request): JsonResponse
  704.     {
  705.         try {
  706.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  707.             if ($response['success'] !== true) {
  708.                 return $this->json($response);
  709.             }
  710.             $result $this->ncmWeatherApiService->generateMomraToken();
  711.             return $this->json($result);
  712.         } catch (\Exception $ex) {
  713.             $this->logger->error($ex->getMessage());
  714.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  715.         }
  716.     }
  717.     /**
  718.      * @Route("/list-weather-station", methods={"POST"})
  719.      */
  720.     public function listWeatherStation(Request $requestPaginatorInterface $paginator): JsonResponse
  721.     {
  722.         try {
  723.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  724.             if ($response['success'] !== true) {
  725.                 return $this->json($response);
  726.             }
  727.             $params  json_decode($request->getContent(), true);
  728.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  729.             $search = (isset($params['search']) && !empty($params['search'])) ? $params['search'] : null;
  730.             $id = (isset($params['id']) && !empty($params['id'])) ? $params['id'] : null;
  731.             $result $this->weatherStationModel->getWeatherStations($paginator nullnullnull$this->translator$search$id);
  732.             return $this->json($result);
  733.         } catch (\Exception $ex) {
  734.             $this->logger->error($ex->getMessage());
  735.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  736.         }
  737.     }
  738.     /**
  739.      * @Route("/get-station-data", methods={"GET"})
  740.      */
  741.     public function getWeatherStationDataAction(Request $request)
  742.     {
  743.         try {
  744.             $params json_decode($request->getContent(), true);
  745.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  746.             // check user credentials and expiry
  747.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  748.             if ($response['success'] !== true) {
  749.                 return $this->json($response);
  750.             }
  751.             $typeName $request->get('type_name');
  752.             $parameters $request->get('parameters');
  753.             $dateTime $request->get('date_time');
  754.             $bBox $request->get('b_box');
  755.             if (!$typeName) {
  756.                 throw new \InvalidArgumentException("Missing mandatory parameter: type_name");
  757.             }
  758.             if (!$parameters) {
  759.                 throw new \InvalidArgumentException("Missing mandatory parameter: parameters");
  760.             }
  761.             if (!$dateTime) {
  762.                 throw new \InvalidArgumentException("Missing mandatory parameter: date_time");
  763.             }
  764.             if (!$bBox) {
  765.                 throw new \InvalidArgumentException("Missing mandatory parameter: b_box");
  766.             }
  767.             $result $this->meteomaticsWeatherService->getWeatherStationData($typeName$parameters$dateTime$bBox);
  768.             return $result;
  769.         } catch (\Exception $ex) {
  770.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  771.         }
  772.     }
  773.     /**
  774.      * @Route("/get-push-alert-notification", methods={"POST"})
  775.      */
  776.     public function getPushAlertNotification(Request $request): Response
  777.     {
  778.         try {
  779.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  780.             if ($response['success'] !== true) {
  781.                 return $this->json($response);
  782.             }
  783.             $params json_decode($request->getContent(), true);
  784.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  785.             $result  $this->deviceModel->getPushAlertNotification($params$this->translator);
  786.             return $this->json(['success' => true'data' => $result]);
  787.         } catch (\Exception $ex) {
  788.             $this->logger->error($ex->getMessage());
  789.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  790.         }
  791.     }
  792.     /**
  793.      * @Route("/manned-alerts", methods={"POST"})
  794.      */
  795.     public function mannedAlerts(Request $requestPaginatorInterface $paginator): JsonResponse
  796.     {
  797.         try {
  798.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  799.             if ($response['success'] !== true) {
  800.                 return $this->json($response);
  801.             }
  802.             $params json_decode($request->getContent(), true);
  803.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  804.             if (!isset($params['from_date']) || !isset($params['to_date'])) {
  805.                 // throw new \Exception('Date range is required');
  806.                 return $this->json(['success' => false'message' =>  $this->translator->trans('date_range_is_required')]);
  807.             }
  808.             $result $this->customNotificationService->getMannedAlerts($params$paginator null);
  809.             return $this->json($result);
  810.         } catch (\Exception $ex) {
  811.             $this->logger->error($ex->getMessage());
  812.             return $this->json(['success' => false'message' =>  $this->translator->trans(USER_ERROR_MESSAGE)]);
  813.         }
  814.     }
  815.     /**
  816.      * @Route("/get-phenomena-list", methods={"POST"})
  817.      */
  818.     public function getPhenomenaList(Request $request): JsonResponse
  819.     {
  820.         try {
  821.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  822.             if ($response['success'] !== true) {
  823.                 return $this->json($response);
  824.             }
  825.             $params json_decode($request->getContent(), true);
  826.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  827.             $result $this->ewsNotificationModel->getPhenomenaList();
  828.             return $this->json($result);
  829.         } catch (\Exception $ex) {
  830.             $this->logger->error($ex->getMessage());
  831.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  832.         }
  833.     }
  834.     /**
  835.      * @Route("/get-today-weather-report", methods={"POST"})
  836.      */
  837.     public function getTodayWeatherReport(Request $request): JsonResponse
  838.     {
  839.         try {
  840.             // check user credentials and expiry
  841.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  842.             if ($response['success'] !== true) {
  843.                 return $this->json($response);
  844.             }
  845.             // get request params
  846.             $params json_decode($request->getContent(), true);
  847.             // set locale
  848.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  849.             // get today weather report data
  850.             $result $this->reportLogModel->getTodayWeatherReportData($request$this->translator);
  851.             return $this->json($result);
  852.         } catch (\Exception $ex) {
  853.             $this->logger->error($ex->getMessage());
  854.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  855.         }
  856.     }
  857.     /**
  858.      * @Route("/get-all-weather-report", methods={"POST"})
  859.      */
  860.     public function getAllWeatherReport(Request $request): JsonResponse
  861.     {
  862.         try {
  863.             // check user credentials and expiry
  864.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  865.             if ($response['success'] !== true) {
  866.                 return $this->json($response);
  867.             }
  868.             // get request params
  869.             $params json_decode($request->getContent(), true);
  870.             // set locale
  871.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  872.             // get today weather report data
  873.             $result $this->reportLogModel->getAllWeatherReportData($request$this->translator);
  874.             return $this->json($result);
  875.         } catch (\Exception $ex) {
  876.             $this->logger->error($ex->getMessage());
  877.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  878.         }
  879.     }
  880.     /**
  881.      * @Route("/get-mobile-assets-list", methods={"POST"})
  882.      */
  883.     public function getMobileAssetsList(Request $request): JsonResponse
  884.     {
  885.         try {
  886.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  887.             if ($response['success'] !== true) {
  888.                 return $this->json($response);
  889.             }
  890.             $data = [];
  891.             $list = new \Pimcore\Model\Asset\Listing();
  892.             $list->setCondition("path LIKE ?", ["/mobile-assets%"]);
  893.             $list->load();
  894.             foreach ($list as $key => $value) {
  895.                 if ($value->getType() == "image") {
  896.                     $data[] = BASE_URL $value->getPath() . $value->getFilename();
  897.                 } elseif ($value->getType() == 'video') {
  898.                     $data[] = BASE_URL $value->getPath() . $value->getFilename();
  899.                 }
  900.             }
  901.             $result = ["success" => true"data" => $data];
  902.             return $this->json($result);
  903.         } catch (\Exception $ex) {
  904.             $this->logger->error($ex->getMessage());
  905.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  906.         }
  907.     }
  908.     /**
  909.      * @Route("/kingdom-weather-description", methods={"POST"})
  910.      */
  911.     public function kingdomWeatherDirect(Request $request): JsonResponse
  912.     {
  913.         try {
  914.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  915.             if ($response['success'] !== true) {
  916.                 return $this->json($response);
  917.             }
  918.             // get request params
  919.             $params json_decode($request->getContent(), true);
  920.             // set locale
  921.             $this->translator->setlocale(isset($params["lang"]) ? $params["lang"] : DEFAULT_LOCALE);
  922.             return $this->json($this->ncmWeatherApiService->getKingdomWeatherData());
  923.         } catch (\Exception $ex) {
  924.             $this->logger->error($ex->getMessage());
  925.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  926.         }
  927.     }
  928.     /**
  929.      * @Route("/get-hijri-date", name="api_ncm_device_get_hijri_date", methods={"POST"})
  930.      */
  931.     public function getHijriDatetime(Request $request): Response
  932.     {
  933.         $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  934.         if ($response['success'] !== true) {
  935.             return $this->json($response);
  936.         }
  937.         $params json_decode($request->getContent(), true);
  938.         if (isset($params['location']) && isset($params['rawOffset'])) {
  939.             $offset $params['rawOffset'] / 3600;
  940.             $locations $params['location'];
  941.             $parameters = ["sunset:sql"];
  942.             // Join the locations and parameters strings with ',' for the URL
  943.             $parameterString $parameters;
  944.             $locationString implode('+'$locations);
  945.             // Create a DateTime object
  946.             $date = new DateTime();
  947.             $dateStrings = [];
  948.             // Format the DateTime object as a string and add it to the array
  949.             for ($i 0$i count($locations); $i++) {
  950.                 $dateStrings[] = $date->format(DateTime::ISO8601);
  951.             }
  952.             // Join the date strings with ',' for the URL
  953.             $dateString implode(','$dateStrings);
  954.             $response $this->meteomaticApiService->getRouteQuery(
  955.                 $dateString,
  956.                 $parameterString,
  957.                 $locationString
  958.             );
  959.             $sunsetTime "";
  960.             if ($response['success'] == true) {
  961.                 $sunsetTime $response['data']['data'][0]['parameters'][0]['value'];
  962.                 $response \App\Lib\Utility::getHijriDatetime($sunsetTime$offset);
  963.             }
  964.         } else {
  965.             $response \App\Lib\Utility::getHijriDatetime();
  966.         }
  967.         return $this->json(["success" => true"date" => $response]);
  968.     }
  969.     /**
  970.      * @Route("/get-isolines", methods={"GET"})
  971.      */
  972.     public function getIsolinesAction(Request $request): JsonResponse
  973.     {
  974.         try {
  975.             $response $this->publicUserPermissionService->isAuthorized($request$this->translator);
  976.             if ($response['success'] !== true) {
  977.                 return $this->json($response);
  978.             }
  979.             // Get access token
  980.             $tokenResult $this->meteomaticApiService->getToken();
  981.             if (!isset($tokenResult['access_token'])) {
  982.                 throw new \RuntimeException("Failed to retrieve access token");
  983.             }
  984.             $accessToken $tokenResult['access_token'];
  985.             $measure $request->get('measure');
  986.             $dateTime $request->get('date_time');
  987.             $accessToken $accessToken;
  988.             if (!$measure) {
  989.                 throw new \InvalidArgumentException("Missing mandatory parameter: measure");
  990.             }
  991.             if (!$dateTime) {
  992.                 throw new \InvalidArgumentException("Missing mandatory parameter: date_time");
  993.             }
  994.             if (!$accessToken) {
  995.                 throw new \InvalidArgumentException("Missing mandatory parameter: access_token");
  996.             }
  997.             $result $this->meteomaticsWeatherService->getIsolines($measure$dateTime$accessToken);
  998.             return $this->json($result);
  999.         } catch (\Exception $ex) {
  1000.             return $this->json(['success' => false'message' => $ex->getMessage()]);
  1001.         }
  1002.     }
  1003. }