From aa893f7ede914b597b35c6d12473376806ab2ac6 Mon Sep 17 00:00:00 2001 From: Gunnar Skjold Date: Sat, 18 Dec 2021 15:17:34 +0100 Subject: [PATCH] Fixed bug in price fetch --- src/entsoe/EntsoeA44Parser.cpp | 2 +- src/entsoe/EntsoeA44Parser.h | 2 ++ src/entsoe/EntsoeApi.cpp | 41 +++++++++++----------------------- src/entsoe/EntsoeApi.h | 3 --- 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/entsoe/EntsoeA44Parser.cpp b/src/entsoe/EntsoeA44Parser.cpp index c82ed277..c01504df 100644 --- a/src/entsoe/EntsoeA44Parser.cpp +++ b/src/entsoe/EntsoeA44Parser.cpp @@ -2,7 +2,7 @@ #include "HardwareSerial.h" EntsoeA44Parser::EntsoeA44Parser() { - for(int i = 0; i < 24; i++) points[i] = 0.0; + for(int i = 0; i < 24; i++) points[i] = ENTSOE_NO_VALUE; } char* EntsoeA44Parser::getCurrency() { diff --git a/src/entsoe/EntsoeA44Parser.h b/src/entsoe/EntsoeA44Parser.h index 28750896..4d860214 100644 --- a/src/entsoe/EntsoeA44Parser.h +++ b/src/entsoe/EntsoeA44Parser.h @@ -9,6 +9,8 @@ #define DOCPOS_POSITION 3 #define DOCPOS_AMOUNT 4 +#define ENTSOE_NO_VALUE -127 + class EntsoeA44Parser: public Stream { public: EntsoeA44Parser(); diff --git a/src/entsoe/EntsoeApi.cpp b/src/entsoe/EntsoeApi.cpp index 3f32d223..ff0c0618 100644 --- a/src/entsoe/EntsoeApi.cpp +++ b/src/entsoe/EntsoeApi.cpp @@ -93,14 +93,13 @@ bool EntsoeApi::loop() { tmElements_t tm; breakTime(epoch, tm); if(tm.Year > 50) { // Make sure we are in 2021 or later (years after 1970) - uint64_t curDeviceMillis = millis64(); uint32_t curDayMillis = (((((tm.Hour * 60) + tm.Minute) * 60) + tm.Second) * 1000); - midnightMillis = curDeviceMillis + (SECS_PER_DAY * 1000) - curDayMillis; - printI("Setting midnight millis " + String((uint32_t) midnightMillis)); + midnightMillis = now + (SECS_PER_DAY * 1000) - curDayMillis + 1000; // Adding 1s to ensure we have passed midnight + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Setting midnight millis %lu\n", midnightMillis); } } else if(now > midnightMillis) { - printI("Rotating price objects"); + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Rotating price objects at %lu\n", now); delete today; today = tomorrow; tomorrow = NULL; @@ -121,21 +120,21 @@ bool EntsoeApi::loop() { d2.Year+1970, d2.Month, d2.Day, 23, 00, config->area, config->area); - printI("Fetching prices for today"); - printD(url); + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Fetching prices for today\n"); + if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url); EntsoeA44Parser* a44 = new EntsoeA44Parser(); - if(retrieve(url, a44)) { + if(retrieve(url, a44) && a44->getPoint(0) != ENTSOE_NO_VALUE) { today = a44; ret = true; - } else { + } else if(a44 != NULL) { delete a44; today = NULL; } } if(tomorrow == NULL - && midnightMillis - now < 39600000 - && (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 60000) + && midnightMillis - now < 39600000 // Fetch 11hrs before midnight (13:00 CE(S)T) + && (lastTomorrowFetch == 0 || now - lastTomorrowFetch > 300000) // Retry every 5min ) { lastTomorrowFetch = now; time_t e1 = time(nullptr); @@ -151,13 +150,13 @@ bool EntsoeApi::loop() { d2.Year+1970, d2.Month, d2.Day, 23, 00, config->area, config->area); - printI("Fetching prices for tomorrow"); - printD(url); + if(debugger->isActive(RemoteDebug::INFO)) debugger->printf("(EntsoeApi) Fetching prices for tomorrow\n"); + if(debugger->isActive(RemoteDebug::DEBUG)) debugger->printf("(EntsoeApi) url: %s\n", url); EntsoeA44Parser* a44 = new EntsoeA44Parser(); - if(retrieve(url, a44)) { + if(retrieve(url, a44) && a44->getPoint(0) != ENTSOE_NO_VALUE) { tomorrow = a44; ret = true; - } else { + } else if(a44 != NULL) { delete a44; tomorrow = NULL; } @@ -262,20 +261,6 @@ void EntsoeApi::printD(String fmt, ...) { va_end(args); } -void EntsoeApi::printI(String fmt, ...) { - va_list args; - va_start(args, fmt); - if(debugger->isActive(RemoteDebug::INFO)) debugger->printf(String("(EntsoeApi)" + fmt + "\n").c_str(), args); - va_end(args); -} - -void EntsoeApi::printW(String fmt, ...) { - va_list args; - va_start(args, fmt); - if(debugger->isActive(RemoteDebug::WARNING)) debugger->printf(String("(EntsoeApi)" + fmt + "\n").c_str(), args); - va_end(args); -} - void EntsoeApi::printE(String fmt, ...) { va_list args; va_start(args, fmt); diff --git a/src/entsoe/EntsoeApi.h b/src/entsoe/EntsoeApi.h index ca94ca2c..6830efd2 100644 --- a/src/entsoe/EntsoeApi.h +++ b/src/entsoe/EntsoeApi.h @@ -7,7 +7,6 @@ #include "EntsoeA44Parser.h" #include "AmsConfiguration.h" -#define ENTSOE_NO_VALUE -127 #define ENTSOE_DEFAULT_MULTIPLIER 1.00 #define SSL_BUF_SIZE 512 @@ -41,8 +40,6 @@ private: float getCurrencyMultiplier(const char* from, const char* to); void printD(String fmt, ...); - void printI(String fmt, ...); - void printW(String fmt, ...); void printE(String fmt, ...); }; #endif