Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EdfColorDay Bridge] add new bridge #4370

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions bridges/EdfColorDayBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class EdfColorDayBridge extends BridgeAbstract
{
const NAME = 'EDF tempo color';
// pull info from this site for now because EDF do not provide correct opendata
const URI = 'https://www.services-rte.com/cms/open_data/v1/tempo';
const DESCRIPTION = 'Get EDF color of today and tomorrow of tempo contract';
const MAINTAINER = 'floviolleau';
const PARAMETERS = [
[
'contract' => [
'name' => 'Choisir un contrat',
'type' => 'list',
// we can add later more option prices like EJP
'values' => [
'Tempo' => 'tempo'
],
]
]
];
const CACHE_TIMEOUT = 7200; // 2h

/**
* @param simple_html_dom $html
* @param string $contractUri
* @return void
*/
private function tempo(string $json): void
{
$jsonDecoded = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out Json::decode


$values = [
$this->formatFrenchDate('now') => date('Y-m-d'),
'Demain ' . $this->formatFrenchDate('tomorrow') => date('Y-m-d', strtotime('+1 day'))
];

foreach ($values as $key => $value) {
$i++;
$item = [];

$text = $key . ' : ' . $this->getDisplayableColor($jsonDecoded['values'][$value]);
$item['uri'] = self::URI . $contractUri;
$item['title'] = $text;
$item['author'] = self::MAINTAINER;
$item['content'] = $text;
$item['uid'] = hash('sha256', $item['title']);

$this->items[] = $item;
}
}

private function formatFrenchDate(string $datetime): string
{
// Set the locale to French
setlocale(LC_TIME, 'fr_FR.UTF-8');

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change locale. Can you find another way?

// Create a DateTime object for the desired date
$now = new DateTime($datetime);

// Format the date
return strftime('%A %d %B %Y', $now->getTimestamp());
}

private function getDisplayableColor(string $color): string
{
$displayableColor = null;
switch ($color) {
case 'BLUE':
$displayableColor = '🟦 TEMPO_BLEU';
break;
case 'WHITE':
$displayableColor = '⬜ TEMPO_BLANC';
break;
case 'RED':
$displayableColor = '🟥 TEMPO_ROUGE';
break;
default:
$displayableColor = '⬛ NON_DEFINI';
break;
}

return $displayableColor;
}

private function getTempoYear(): string
{
$month = date('n'); // Current month as a number (1-12)
$year = date('Y'); // Current year

// Assuming the tempo year starts in September
if ($month >= 9) {
return $year . '-' . ($year + 1); // e.g., 2024-2025
}

return ($year - 1) . '-' . $year; // e.g., 2023-2024
}

public function collectData()
{
$contract = $this->getKey('contract');

$header = [
'Content-type: application/json',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. This is the content type of the data in the http request. There is none body in request.

You probably meant to use Accept?

See https://stackoverflow.com/questions/35722586/http-headers-accept-and-content-type-in-a-rest-context

];
$opts = [
CURLOPT_HTTPGET => 1,
];

$json = getContents(self::URI . '?season=' . $this->getTempoYear(), $header, $opts);

if ($contract === 'Tempo') {
$this->tempo($json);
}
}
}
Loading