From 5e4ccca6637808c3be7e1d762641baf1514f5a18 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sun, 16 Jan 2022 10:37:08 +0100 Subject: [PATCH] Fetch prices between 13:30 and 14:00 --- src/entsoe/EntsoeApi.cpp | 16 +++++++++++++--- src/entsoe/EntsoeApi.h | 1 + 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/entsoe/EntsoeApi.cpp b/src/entsoe/EntsoeApi.cpp index 8930c187..02eba76f 100644 --- a/src/entsoe/EntsoeApi.cpp +++ b/src/entsoe/EntsoeApi.cpp @@ -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); diff --git a/src/entsoe/EntsoeApi.h b/src/entsoe/EntsoeApi.h index df5ef496..c4ad9407 100644 --- a/src/entsoe/EntsoeApi.h +++ b/src/entsoe/EntsoeApi.h @@ -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;