From 03ed3a878bb2a848311d66adcee8eb4a48f0b9ae Mon Sep 17 00:00:00 2001 From: jdg Date: Thu, 13 Jan 2022 21:04:20 +0000 Subject: [PATCH] Reading prices from api.esios.ree.es --- backend/symfony/README.md | 4 + .../src/DataProvider/PriceNowProvider.php | 175 ++++++++++++++++++ .../src/DataProvider/PriceTodayProvider.php | 45 ----- .../Entity/{PriceToday.php => PriceNow.php} | 2 +- backend/symfony/src/Entity/Prices.php | 38 ++-- 5 files changed, 206 insertions(+), 58 deletions(-) create mode 100644 backend/symfony/src/DataProvider/PriceNowProvider.php delete mode 100644 backend/symfony/src/DataProvider/PriceTodayProvider.php rename backend/symfony/src/Entity/{PriceToday.php => PriceNow.php} (97%) diff --git a/backend/symfony/README.md b/backend/symfony/README.md index 7962041..4bbcd42 100644 --- a/backend/symfony/README.md +++ b/backend/symfony/README.md @@ -10,3 +10,7 @@ Created src\entity\prices.php > php bin/console doctrine:schema:update --dump-sql > php bin/console doctrine:schema:update --force + +Modified src\entity\prices.php +> php bin/console make:migration +> php bin/console doctrine:migrations:migrate \ No newline at end of file diff --git a/backend/symfony/src/DataProvider/PriceNowProvider.php b/backend/symfony/src/DataProvider/PriceNowProvider.php new file mode 100644 index 0000000..8fb88a0 --- /dev/null +++ b/backend/symfony/src/DataProvider/PriceNowProvider.php @@ -0,0 +1,175 @@ +doctrine = $doctrine; + } + + private function getPriceToday(): Prices { + $em = $this->doctrine->getManager(); + return $em->getRepository(Prices::class)->findOneBy(array( + 'day' => new \DateTime('now') + )); + } + private function setPriceToday(Prices $price):bool { + $em = $this->doctrine->getManager(); + $em->persist($price); + $em->flush(); + return true; + } + + + private function getPriceHour(string $hh, Prices $price):float + { + switch($hh) { + case '00': return $priceToday->h00; + case '01': return $priceToday->h01; + case '02': return $priceToday->h02; + case '03': return $priceToday->h03; + case '04': return $priceToday->h04; + case '05': return $priceToday->h05; + case '06': return $priceToday->h06; + case '07': return $priceToday->h07; + case '08': return $priceToday->h08; + case '09': return $priceToday->h09; + case '10': return $priceToday->h10; + case '11': return $priceToday->h11; + case '12': return $priceToday->h12; + case '13': return $priceToday->h13; + case '14': return $priceToday->h14; + case '15': return $priceToday->h15; + case '16': return $priceToday->h16; + case '17': return $priceToday->h17; + case '18': return $priceToday->h18; + case '19': return $priceToday->h19; + case '20': return $priceToday->h20; + case '21': return $priceToday->h21; + case '22': return $priceToday->h22; + case '23': return $priceToday->h23; + } + + return -1; + } + + + private function setPriceHour(string $hh, Prices $price):void + { + switch($hh) { + case '00': $priceToday->h00 = $p; break; + case '01': $priceToday->h01 = $p; break; + case '02': $priceToday->h02 = $p; break; + case '03': $priceToday->h03 = $p; break; + case '04': $priceToday->h04 = $p; break; + case '05': $priceToday->h05 = $p; break; + case '06': $priceToday->h06 = $p; break; + case '07': $priceToday->h07 = $p; break; + case '08': $priceToday->h08 = $p; break; + case '09': $priceToday->h09 = $p; break; + case '10': $priceToday->h10 = $p; break; + case '11': $priceToday->h11 = $p; break; + case '12': $priceToday->h12 = $p; break; + case '13': $priceToday->h13 = $p; break; + case '14': $priceToday->h14 = $p; break; + case '15': $priceToday->h15 = $p; break; + case '16': $priceToday->h16 = $p; break; + case '17': $priceToday->h17 = $p; break; + case '18': $priceToday->h18 = $p; break; + case '19': $priceToday->h19 = $p; break; + case '20': $priceToday->h20 = $p; break; + case '21': $priceToday->h21 = $p; break; + case '22': $priceToday->h22 = $p; break; + case '23': $priceToday->h23 = $p; break; + } + } + + + public function getCollection(string $resourceClass, string $operationName = null) : iterable + { + // Get the today's price (from cache) + /* + $em = $this->doctrine->getManager(); + $dql = "SELECT hour_start, hour_end, price FROM prices WHERE day = CURRENT_DATE()"; + $query = $em->getConnection()->prepare($dql); + $prices = $query->executeQuery()->fetchAllAssociative(); + */ + $priceToday = $this->getPriceToday(); + + echo print_r($priceToday,true); + exit; + + if ( $priceToday->id<=0 ) { + // Get the today's price (from internet) + $url = 'https://api.esios.ree.es/archives/70/download_json?locale=es&date=2022-01-12'; + $data = file_get_contents($url); + $json = json_decode($data, true); + + if ( !array_key_exists('PVPC', $json) ) + throw new Exception('Unable to get the prices for today'); + + $ddMMyyy = explode('/',$pvpc['Dia'])[0]; + $priceToday->day = new \DateTime($ddMMyyy[2].'/'.$ddMMyyy[1].'/'.$ddMMyyy[0].' 00:00:00'); + foreach( $json['PVPC'] as $pvpc ) { + $hh = explode('-',$pvpc['Hora'])[0]; + $p = str_replace(',','.',$pvpc['PCB']) / 1000; + + $this->setPriceHour($hh, $priceNow); +/* + switch($hh) { + case '00': $priceToday->h00 = $p; break; + case '01': $priceToday->h01 = $p; break; + case '02': $priceToday->h02 = $p; break; + case '03': $priceToday->h03 = $p; break; + case '04': $priceToday->h04 = $p; break; + case '05': $priceToday->h05 = $p; break; + case '06': $priceToday->h06 = $p; break; + case '07': $priceToday->h07 = $p; break; + case '08': $priceToday->h08 = $p; break; + case '09': $priceToday->h09 = $p; break; + case '10': $priceToday->h10 = $p; break; + case '11': $priceToday->h11 = $p; break; + case '12': $priceToday->h12 = $p; break; + case '13': $priceToday->h13 = $p; break; + case '14': $priceToday->h14 = $p; break; + case '15': $priceToday->h15 = $p; break; + case '16': $priceToday->h16 = $p; break; + case '17': $priceToday->h17 = $p; break; + case '18': $priceToday->h18 = $p; break; + case '19': $priceToday->h19 = $p; break; + case '20': $priceToday->h20 = $p; break; + case '21': $priceToday->h21 = $p; break; + case '22': $priceToday->h22 = $p; break; + case '23': $priceToday->h23 = $p; break; + } + */ + //echo "\n $dd $hh = $p"; + } + $this->setPriceToday($priceToday); + } + + $priceNow = new PriceNow; + $priceNow->id = $priceToday->id; + $priceNow->min = min($priceToday->h00,$priceToday->h01,$priceToday->h02,$priceToday->h03,$priceToday->h04,$priceToday->h05,$priceToday->h06,$priceToday->h07,$priceToday->h08,$priceToday->h09,$priceToday->h10,$priceToday->h11,$priceToday->h12,$priceToday->h13,$priceToday->h14,$priceToday->h15,$priceToday->h16,$priceToday->h17,$priceToday->h18,$priceToday->h19,$priceToday->h20,$priceToday->h21,$priceToday->h22,$priceToday->h23); + $priceNow->max = max($priceToday->h00,$priceToday->h01,$priceToday->h02,$priceToday->h03,$priceToday->h04,$priceToday->h05,$priceToday->h06,$priceToday->h07,$priceToday->h08,$priceToday->h09,$priceToday->h10,$priceToday->h11,$priceToday->h12,$priceToday->h13,$priceToday->h14,$priceToday->h15,$priceToday->h16,$priceToday->h17,$priceToday->h18,$priceToday->h19,$priceToday->h20,$priceToday->h21,$priceToday->h22,$priceToday->h23); + $priceNow->avg = ($priceToday->h00+$priceToday->h01+$priceToday->h02+$priceToday->h03+$priceToday->h04+$priceToday->h05+$priceToday->h06+$priceToday->h07+$priceToday->h08+$priceToday->h09+$priceToday->h10+$priceToday->h11+$priceToday->h12+$priceToday->h13+$priceToday->h14+$priceToday->h15+$priceToday->h16+$priceToday->h17+$priceToday->h18+$priceToday->h19+$priceToday->h20+$priceToday->h21+$priceToday->h22+$priceToday->h23)/24; + $priceNow->now = $this->getPriceHour( Date('H'), $priceNow); + + return [$priceNow]; + } + + public function supports(string $resourceClass, string $operationName = null, array $context = []): bool + { + return $resourceClass === PriceNow::class; + } +} \ No newline at end of file diff --git a/backend/symfony/src/DataProvider/PriceTodayProvider.php b/backend/symfony/src/DataProvider/PriceTodayProvider.php deleted file mode 100644 index d710462..0000000 --- a/backend/symfony/src/DataProvider/PriceTodayProvider.php +++ /dev/null @@ -1,45 +0,0 @@ -doctrine = $doctrine; - } - - public function getCollection(string $resourceClass, string $operationName = null) : iterable - { - // Get the today's price (from cache) - $em = $this->doctrine->getManager(); - $dql = "SELECT hour_start, hour_end, price FROM prices WHERE day = CURRENT_DATE()"; - $query = $em->getConnection()->prepare($dql); - $prices = $query->executeQuery()->fetchAllAssociative(); // \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY - - if ( count($prices)<=0 ) { - // Get the today's price (from internet) - } - - $priceToday = new PriceToday(); - $priceToday->id = 1; - $priceToday->min = 0.01; - $priceToday->max = 0.10; - $priceToday->avg = 0.05; - $priceToday->now = 0.03; - return [$priceToday]; - } - - public function supports(string $resourceClass, string $operationName = null, array $context = []): bool - { - return $resourceClass === PriceToday::class; - } -} \ No newline at end of file diff --git a/backend/symfony/src/Entity/PriceToday.php b/backend/symfony/src/Entity/PriceNow.php similarity index 97% rename from backend/symfony/src/Entity/PriceToday.php rename to backend/symfony/src/Entity/PriceNow.php index fbe4bd2..4a4760c 100644 --- a/backend/symfony/src/Entity/PriceToday.php +++ b/backend/symfony/src/Entity/PriceNow.php @@ -22,7 +22,7 @@ use ApiPlatform\Core\Action\NotFoundAction; * ) * */ -class PriceToday +class PriceNow { /** * @ApiProperty(identifier=true) diff --git a/backend/symfony/src/Entity/Prices.php b/backend/symfony/src/Entity/Prices.php index 7debde5..36d7ab8 100644 --- a/backend/symfony/src/Entity/Prices.php +++ b/backend/symfony/src/Entity/Prices.php @@ -30,21 +30,35 @@ class Prices */ public ?\DateTime $day = null; - /** - * @ORM\Column(type="integer") - */ - public ?int $hourStart = null; - - /** - * @ORM\Column(type="integer") - */ - public ?int $hourEnd = null; - /** * @ORM\Column(type="decimal", precision="8", scale="6") - * @Assert\NotBlank() */ - public float $price = 0.0; + public ?float $h00 = null; + public ?float $h01 = null; + public ?float $h02 = null; + public ?float $h03 = null; + public ?float $h04 = null; + public ?float $h05 = null; + public ?float $h06 = null; + public ?float $h07 = null; + public ?float $h08 = null; + public ?float $h09 = null; + public ?float $h10 = null; + public ?float $h11 = null; + public ?float $h12 = null; + public ?float $h13 = null; + public ?float $h14 = null; + public ?float $h15 = null; + public ?float $h16 = null; + public ?float $h17 = null; + public ?float $h18 = null; + public ?float $h19 = null; + public ?float $h20 = null; + public ?float $h21 = null; + public ?float $h22 = null; + public ?float $h23 = null; + + /******** METHODS ********/