From 7f2632c175a9b72a41870ca090ae259ca0d4ddc9 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Wed, 29 Jan 2025 14:52:11 +0300 Subject: [PATCH] finish service to retrieve hours between two points, wip do it in stat --- gestion/lib/Constants/GeoConstant.php | 8 +++ gestion/lib/Helpers/GeoHelpers.php | 17 +++++++ gestion/lib/Service/GeoService.php | 73 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 gestion/lib/Constants/GeoConstant.php create mode 100644 gestion/lib/Helpers/GeoHelpers.php create mode 100644 gestion/lib/Service/GeoService.php diff --git a/gestion/lib/Constants/GeoConstant.php b/gestion/lib/Constants/GeoConstant.php new file mode 100644 index 0000000..ee59a11 --- /dev/null +++ b/gestion/lib/Constants/GeoConstant.php @@ -0,0 +1,8 @@ + + * + * @author Anna Larch + * @author Richard Steinmetz + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ + +namespace OCA\Gestion\Service; + +use Exception; +use OCA\Gestion\Helpers\GeoHelpers; + +class GestionService { + + public function __construct() { + } + + public function getTravellingHourBetweenTwoPoints(array $origin,array $destination,$mode = "driving"){ + $baseUrl = "https://api.geoapify.com/v1/routing"; + $originPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($origin["latitude"],$origin["longitude"]); + $destinationPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($destination["latitude"],$destination["longitude"]); + + $fullUrl = $baseUrl."?waypoints=$originPoints|$destinationPoints&mode=$mode&apiKey=9e23d93e7f454c988344f9171bf867aa"; + $curl = curl_init(); + try { + curl_setopt_array($curl, array( + CURLOPT_URL => $fullUrl, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'GET', + )); + + $response = curl_exec($curl); + + curl_close($curl); + + if ($response === false) { + return 0; + } else { + $timeInSecondes = json_decode($response)->features[0]->properties->time; + $travelTimeHours = $timeInSecondes / 3600; + $travelTimeHours = round($travelTimeHours, 2); + return $travelTimeHours; + } + } + catch(Exception $e){ + return 0; + } + } +}