<?php
namespace App\Service;
use GuzzleHttp\Client;
use DateTime;
use App\Service\RedisCache;
use GuzzleHttp;
class MateomaticsAgricultureService
{
private $apiBaseUrl = MATEOMATICS_API_URL;
private $username;
private $password;
private $redisCache;
public function __construct(RedisCache $redisCache)
{
$this->username = MATEOMATICS_API_USERNAME;
$this->password = MATEOMATICS_API_PASSWORD;
$this->redisCache = $redisCache;
}
public function getEvapotranspiration(array $coordinates, string $date, string $interval = '1h', $translator)
{
try {
if ($coordinates) {
$latitude1 = $coordinates[0][0];
$longitude1 = $coordinates[0][1];
$latitude2 = $coordinates[1][0];
$longitude2 = $coordinates[1][1];
}
// Validate the input parameters
if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/', $latitude1)) {
return ["success" => false, "message" => $translator->trans("invalid_latitude")];
}
if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/', $longitude1)) {
return ["success" => false, "message" => $translator->trans("invalid_longitude")];
}
// Validate the input parameters
if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/', $latitude2)) {
return ["success" => false, "message" => $translator->trans("invalid_latitude")];
}
if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/', $longitude2)) {
return ["success" => false, "message" => $translator->trans("invalid_longitude")];
}
if (empty($date)) {
return ["success" => false, "message" => $translator->trans("invalid_dates")];
}
$date = date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
// Create a Redis key for the cache
$cacheKey = sprintf('evapotranspiration_%s_%s', md5(implode('_', array_map(function ($coordinate) {
return implode('_', $coordinate);
}, $coordinates))), ($date));
// Try to get the data from Redis cache
$cachedData = $this->redisCache->get($cacheKey);
if ($cachedData !== null) {
// Return the cached data if available
return $cachedData;
}
// If cache is empty, get the data from the API
$client = new Client(['verify' => false]);
$url = sprintf(
'%s/%s/evapotranspiration_%s:mm/%s,%s_%s,%s/json?use_decluttered=true',
$this->apiBaseUrl,
$date,
$interval,
$latitude1,
$longitude1,
$latitude2,
$longitude2,
);
$response = $client->request('GET', $url, [
'auth' => [$this->username, $this->password],
]);
$statusCode = $response->getStatusCode();
$data = json_decode($response->getBody(), true);
// Set the data to Redis cache
$this->redisCache->set($cacheKey, $data);
return $data;
} catch (GuzzleHttp\Exception\RequestException $e) {
return $e->getMessage();
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function getGrowingDegreeDays(array $coordinates, string $date, string $interval = '1H', string $days = '1')
{
try {
if ($coordinates) {
$latitude = $coordinates[0][0];
$longitude = $coordinates[0][1];
}
// Validate the input parameters
if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/', $latitude)) {
throw new \InvalidArgumentException('Invalid latitude');
}
if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/', $longitude)) {
throw new \InvalidArgumentException('Invalid longitude');
}
if (empty($date)) {
throw new \InvalidArgumentException('Invalid dates');
}
$date = date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
// Create a Redis key for the cache
$cacheKey = sprintf('growing_degree_days_accumulated_%s_%s', md5(implode('_', array_map(function ($coordinate) {
return implode('_', $coordinate);
}, $coordinates))), ($date));
// Try to get the data from Redis cache
$cachedData = $this->redisCache->get($cacheKey);
if ($cachedData !== null) {
// Return the cached data if available
return $cachedData;
}
// If cache is empty, get the data from the API
$client = new Client(['verify' => false]);
$url = sprintf(
'%s/%sP%sD:P%sD/growing_degree_days_accumulated:gdd/%s,%s/json?use_decluttered=true',
$this->apiBaseUrl,
$date,
$interval,
$days,
$latitude,
$longitude,
);
$response = $client->request('GET', $url, [
'auth' => [$this->username, $this->password],
]);
$statusCode = $response->getStatusCode();
$data = json_decode($response->getBody(), true);
// Set the data to Redis cache
$this->redisCache->set($cacheKey, $data);
return $data;
} catch (GuzzleHttp\Exception\RequestException $e) {
return $e->getMessage();
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function getGrassLandTemperatureSum(array $coordinates, string $date, string $interval = '1H', string $days = '1', $translator)
{
try {
if ($coordinates) {
$latitude = $coordinates[0][0];
$longitude = $coordinates[0][1];
}
// Validate the input parameters
if (!preg_match('/^[-]?[0-9]{1,2}\.[0-9]+/', $latitude)) {
return ["success" => false, "message" => $translator->trans("invalid_latitude")];
}
if (!preg_match('/^[-]?[0-9]{1,3}\.[0-9]+/', $longitude)) {
return ["success" => false, "message" => $translator->trans("invalid_longitude")];
}
if (empty($date)) {
return ["success" => false, "message" => $translator->trans("invalid_dates")];
}
$date = date('Y-m-d\TH:i:s.v\Z', (strtotime($date)));
// Create a Redis key for the cache
$cacheKey = sprintf('growing_degree_days_accumulated_%s_%s', md5(implode('_', array_map(function ($coordinate) {
return implode('_', $coordinate);
}, $coordinates))), ($date));
// Try to get the data from Redis cache
$cachedData = $this->redisCache->get($cacheKey);
if ($cachedData !== null) {
// Return the cached data if available
return $cachedData;
}
// If cache is empty, get the data from the API
$client = new Client(['verify' => false]);
$url = sprintf(
'%s/%sP%sD:PT%s/grass_land_temperature_sum:C,t_2m:C/%s,%s/json?use_decluttered=true',
$this->apiBaseUrl,
$date,
$days,
$interval,
$latitude,
$longitude
);
$response = $client->request('GET', $url, [
'auth' => [$this->username, $this->password],
]);
$statusCode = $response->getStatusCode();
$data = json_decode($response->getBody(), true);
// Set the data to Redis cache
$this->redisCache->set($cacheKey, $data);
return $data;
} catch (GuzzleHttp\Exception\RequestException $e) {
return $e->getMessage();
} catch (\Exception $e) {
return $e->getMessage();
}
}
}