From 3529c0adb9ddd925cbd7d5ab0fa59802657767f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=CC=81s=CC=8C=20Holeczy?= Date: Thu, 30 Sep 2021 22:32:35 +0200 Subject: [PATCH] Allow setting angle and azimuth --- README.md | 9 ++- src/Adapter/PvgisAdapter.php | 38 +++--------- src/Adapter/UrlBuilder.php | 54 +++++++++++++++++ src/Enum/CardinalDirection.php | 17 ++++++ .../InvalidResponseFormatException.php | 17 +----- .../PvgisResponseFailureException.php | 13 +---- src/Model/Request.php | 58 +++++++++++++++++++ src/PVGIS.php | 22 ++++--- 8 files changed, 160 insertions(+), 68 deletions(-) create mode 100644 src/Adapter/UrlBuilder.php create mode 100644 src/Enum/CardinalDirection.php create mode 100644 src/Model/Request.php diff --git a/README.md b/README.md index 7f28079..d42ebc4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ composer require holicz/pvgis ``` # Requirements -* PHP >= 7.4 +* PHP ^7.4|^8.0 # Usage @@ -19,7 +19,12 @@ $latitude = '50.0898689'; $longitude = '14.4000936'; $pvgis = new PVGIS(new PvgisAdapter()); -$electricityProduction = $pvgis->getElectricityProduction($latitude, $longitude); +$electricityProduction = $pvgis->getElectricityProduction( + $latitude, + $longitude, + 35, // Solar panels angle (not required) + CardinalDirection::SOUTH // Solar panels azimuth (not required) +); // Yearly sum of production $electricityProduction->getYearlyProduction(); diff --git a/src/Adapter/PvgisAdapter.php b/src/Adapter/PvgisAdapter.php index 2994baa..60120a3 100644 --- a/src/Adapter/PvgisAdapter.php +++ b/src/Adapter/PvgisAdapter.php @@ -8,37 +8,20 @@ use holicz\PVGIS\Exception\InvalidResponseFormatException; use holicz\PVGIS\Exception\PvgisResponseFailureException; use holicz\PVGIS\Model\ElectricityProduction; -use Safe\Exceptions\FilesystemException; -use Safe\Exceptions\StringsException; +use holicz\PVGIS\Model\Request; + +use function file_get_contents; final class PvgisAdapter { - private const API_URL = 'https://re.jrc.ec.europa.eu/api/pvcalc?peakpower=1&pvtechchoice=crystSi&mountingplace=building&loss=10&outputformat=json&angle=35&lat=%s&lon=%s'; - - /** - * Multiplier to change the PVGIS result in order to get desired production - */ - private float $multiplier; - - public function __construct(float $multiplier = 1.0) - { - $this->multiplier = $multiplier; - } - /** - * @param string $latitude - * @param string $longitude - * - * @return ElectricityProduction - * - * @throws FilesystemException * @throws InvalidResponseFormatException * @throws PvgisResponseFailureException - * @throws StringsException */ - public function getPvgisData(string $latitude, string $longitude): ElectricityProduction + public function getPvgisData(Request $request): ElectricityProduction { - $response = \Safe\file_get_contents(\Safe\sprintf(self::API_URL, $latitude, $longitude)); + $urlBuilder = new UrlBuilder($request); + $response = file_get_contents($urlBuilder->build()); if (!$response) { throw new PvgisResponseFailureException(); @@ -48,21 +31,16 @@ public function getPvgisData(string $latitude, string $longitude): ElectricityPr } /** - * @param string $response - * - * @return ElectricityProduction - * * @throws InvalidResponseFormatException - * @throws StringsException */ private function parsePvgisResponse(string $response): ElectricityProduction { try { $response = json_decode($response, true, 512, JSON_THROW_ON_ERROR); - $electricityProduction = new ElectricityProduction($this->multiplier * $response['outputs']['totals']['fixed']['E_y']); + $electricityProduction = new ElectricityProduction($response['outputs']['totals']['fixed']['E_y']); foreach ($response['outputs']['monthly']['fixed'] as $month) { - $electricityProduction->addMonthlyProduction($month['month'], $this->multiplier * $month['E_m']); + $electricityProduction->addMonthlyProduction($month['month'], $month['E_m']); } } catch (Exception $e) { throw new InvalidResponseFormatException(); diff --git a/src/Adapter/UrlBuilder.php b/src/Adapter/UrlBuilder.php new file mode 100644 index 0000000..2227a67 --- /dev/null +++ b/src/Adapter/UrlBuilder.php @@ -0,0 +1,54 @@ +request = $request; + $this->url = 'https://re.jrc.ec.europa.eu/api/pvcalc?peakpower=1&pvtechchoice=crystSi&mountingplace=building&loss=10&outputformat=json'; + } + + public function build(): string + { + $this->buildGPSCoordinates(); + $this->buildAngle(); + $this->buildAzimuth(); + + return $this->url; + } + + private function buildGPSCoordinates(): void + { + $this->url .= sprintf('&lat=%s&lon=%s', $this->request->getLatitude(), $this->request->getLongitude()); + } + + private function buildAngle(): void + { + if ($this->request->getAngle() === null) { + return; + } + + $this->url .= sprintf('&angle=%d', $this->request->getAngle()); + } + + private function buildAzimuth(): void + { + if ($this->request->getAzimuth() === null) { + return; + } + + $this->url .= sprintf('&aspect=%d', $this->request->getAzimuth()); + } +} diff --git a/src/Enum/CardinalDirection.php b/src/Enum/CardinalDirection.php new file mode 100644 index 0000000..5bf521e --- /dev/null +++ b/src/Enum/CardinalDirection.php @@ -0,0 +1,17 @@ +latitude = $latitude; + $this->longitude = $longitude; + $this->angle = $angle; + $this->azimuth = $azimuth; + } + + public function getLatitude(): string + { + return $this->latitude; + } + + public function getLongitude(): string + { + return $this->longitude; + } + + public function setAngle(?int $angle): void + { + $this->angle = $angle; + } + + public function getAngle(): ?int + { + return $this->angle; + } + + public function setAzimuth(?int $azimuth): void + { + $this->azimuth = $azimuth; + } + + public function getAzimuth(): ?int + { + return $this->azimuth; + } +} diff --git a/src/PVGIS.php b/src/PVGIS.php index 4fc9c62..ad33281 100644 --- a/src/PVGIS.php +++ b/src/PVGIS.php @@ -6,8 +6,7 @@ use holicz\PVGIS\Adapter\PvgisAdapter; use holicz\PVGIS\Model\ElectricityProduction; -use Safe\Exceptions\FilesystemException; -use Safe\Exceptions\StringsException; +use holicz\PVGIS\Model\Request; final class PVGIS { @@ -19,18 +18,17 @@ public function __construct(PvgisAdapter $pvgisAdapter) } /** - * @param string $latitude - * @param string $longitude - * - * @return ElectricityProduction - * * @throws Exception\InvalidResponseFormatException * @throws Exception\PvgisResponseFailureException - * @throws FilesystemException - * @throws StringsException */ - public function getElectricityProduction(string $latitude, string $longitude): ElectricityProduction - { - return $this->pvgisAdapter->getPvgisData($latitude, $longitude); + public function getElectricityProduction( + string $latitude, + string $longitude, + ?int $angle = null, + ?int $azimuth = null + ): ElectricityProduction { + $request = new Request($latitude, $longitude, $angle, $azimuth); + + return $this->pvgisAdapter->getPvgisData($request); } }