diff --git a/software/src/modules/day_ahead_prices/day_ahead_prices.cpp b/software/src/modules/day_ahead_prices/day_ahead_prices.cpp index 31942c291..8cd615fbe 100644 --- a/software/src/modules/day_ahead_prices/day_ahead_prices.cpp +++ b/software/src/modules/day_ahead_prices/day_ahead_prices.cpp @@ -27,8 +27,6 @@ #include "tools.h" #include -extern "C" esp_err_t esp_crt_bundle_attach(void *conf); - static constexpr auto CHECK_INTERVAL = 1_m; static constexpr auto PRICE_UPDATE_INTERVAL = 15_m; @@ -134,7 +132,7 @@ void DayAheadPrices::register_urls() api.addState("day_ahead_prices/prices", &prices); task_scheduler.scheduleWhenClockSynced([this]() { - task_scheduler.scheduleWithFixedDelay([this]() { + this->task_id = task_scheduler.scheduleWithFixedDelay([this]() { this->update(); }, CHECK_INTERVAL); }); @@ -170,6 +168,18 @@ void DayAheadPrices::update_price() #endif } +void DayAheadPrices::retry_update(millis_t delay) +{ + // Cancel current task + task_scheduler.cancel(task_id); + + // And schedule a new one that will run after the given delay, + // but with the standard interval afterwards again + this->task_id = task_scheduler.scheduleWithFixedDelay([this]() { + this->update(); + }, delay, CHECK_INTERVAL); +} + void DayAheadPrices::update() { if (config.get("enable")->asBool() == false) { @@ -181,6 +191,7 @@ void DayAheadPrices::update() } if (!network.connected) { + retry_update(1_s); return; } @@ -191,6 +202,7 @@ void DayAheadPrices::update() // Only update if clock is synced struct timeval tv_now; if (!rtc.clock_synced(&tv_now)) { + retry_update(1_s); return; } @@ -203,8 +215,6 @@ void DayAheadPrices::update() return; } - last_update_begin = now_us(); - if(json_buffer == nullptr) { json_buffer = (char *)heap_caps_calloc_prefer(DAY_AHEAD_PRICE_MAX_JSON_LENGTH, sizeof(char), 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL); } else { diff --git a/software/src/modules/day_ahead_prices/day_ahead_prices.h b/software/src/modules/day_ahead_prices/day_ahead_prices.h index 01dd93e2f..ef7d2b8ef 100644 --- a/software/src/modules/day_ahead_prices/day_ahead_prices.h +++ b/software/src/modules/day_ahead_prices/day_ahead_prices.h @@ -50,6 +50,7 @@ class DayAheadPrices final : public IModule { private: void update(); + void retry_update(millis_t delay); void update_price(); String get_api_url_with_path(); int get_max_price_values(); @@ -62,6 +63,7 @@ class DayAheadPrices final : public IModule uint32_t json_buffer_position; bool current_price_available = false; AsyncHTTPSClient https_client; + uint64_t task_id; DAPDownloadState download_state = DAP_DOWNLOAD_STATE_OK;