Fetch prices between 13:30 and 14:00

This commit is contained in:
Gunnar Skjold 2022-01-16 10:37:08 +01:00
parent 6b0ec39759
commit 5e4ccca663
2 changed files with 14 additions and 3 deletions

View File

@ -18,6 +18,8 @@ EntsoeApi::EntsoeApi(RemoteDebug* Debug) {
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
tz = new Timezone(CEST, CET);
tomorrowFetchMillis = random(3600000,3780000); // Random between 13:30 and 14:00
}
void EntsoeApi::setup(EntsoeConfig& config) {
@ -140,9 +142,11 @@ bool EntsoeApi::loop() {
}
}
// Prices for next day are published at 13:00 CE(S)T, but to avoid heavy server traffic at that time, we will
// fetch 1 hr after that (with some random delay) and retry every 15 minutes
if(tomorrow == NULL
&& midnightMillis - now < 39600000 // Fetch 11hrs before midnight (13:00 CE(S)T)
&& (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 300000) // Retry every 5min
&& midnightMillis - now < tomorrowFetchMillis
&& (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 900000)
) {
lastTomorrowFetch = now;
time_t e1 = time(nullptr);
@ -261,10 +265,16 @@ float EntsoeApi::getCurrencyMultiplier(const char* from, const char* to) {
return 1.00;
uint64_t now = millis64();
if(lastCurrencyFetch == 0 || now - lastCurrencyFetch > (SECS_PER_HOUR * 1000)) {
if(lastCurrencyFetch == 0 || lastCurrencyFetch < midnightMillis) {
char url[256];
DnbCurrParser p;
#if defined(ESP32)
esp_task_wdt_reset();
#elif defined(ESP8266)
ESP.wdtFeed();
#endif
snprintf(url, sizeof(url), "https://data.norges-bank.no/api/data/EXR/M.%s.NOK.SP?lastNObservations=1", from);
if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Retrieving %s to NOK conversion\n", from);
if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url);

View File

@ -35,6 +35,7 @@ private:
WiFiClientSecure client;
HTTPClient https;
uint32_t tomorrowFetchMillis = 3600000; // Number of ms before midnight. Default fetch 10hrs before midnight (14:00 CE(S)T)
uint64_t midnightMillis = 0;
uint64_t lastTodayFetch = 0;
uint64_t lastTomorrowFetch = 0;