Reading prices from api.esios.ree.es

This commit is contained in:
José David Guillén 2022-01-13 21:04:20 +00:00
parent 1fd7941cf2
commit 03ed3a878b
5 changed files with 206 additions and 58 deletions

View File

@ -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

View File

@ -0,0 +1,175 @@
<?php
namespace App\DataProvider;
use Doctrine\Persistence\ManagerRegistry;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\PriceNow;
use App\Entity\Prices;
class PriceNowProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->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;
}
}

View File

@ -1,45 +0,0 @@
<?php
namespace App\DataProvider;
use Doctrine\Persistence\ManagerRegistry;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\PriceToday;
use App\Entity\Prices;
class PriceTodayProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->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;
}
}

View File

@ -22,7 +22,7 @@ use ApiPlatform\Core\Action\NotFoundAction;
* )
*
*/
class PriceToday
class PriceNow
{
/**
* @ApiProperty(identifier=true)

View File

@ -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 ********/