diff --git a/src/ESPDash.cpp b/src/ESPDash.cpp index f7b52b41..1936024f 100644 --- a/src/ESPDash.cpp +++ b/src/ESPDash.cpp @@ -64,6 +64,8 @@ ESPDash::ESPDash(AsyncWebServer* server, const char* uri, bool enable_default_st // client side commands parsing if (json["command"] == "get:layout") { _asyncAccessInProgress = true; + if (_beforeUpdateCallback) + _beforeUpdateCallback(false); generateLayoutJSON(client, false); _asyncAccessInProgress = false; } else if (json["command"] == "ping") { @@ -487,6 +489,8 @@ void ESPDash::sendUpdates(bool force) { if (!hasClient()) { return; } + if (_beforeUpdateCallback) + _beforeUpdateCallback(!force); generateLayoutJSON(nullptr, !force); } @@ -495,6 +499,8 @@ void ESPDash::refreshCard(Card *card) { if (!hasClient()) { return; } + if (_beforeUpdateCallback) + _beforeUpdateCallback(true); generateLayoutJSON(nullptr, true, card); } diff --git a/src/ESPDash.h b/src/ESPDash.h index ab54d14f..e63876ab 100644 --- a/src/ESPDash.h +++ b/src/ESPDash.h @@ -68,6 +68,11 @@ class Statistic; // ESPDASH Class class ESPDash{ + public: + // changes_only: true (equivalent to sendUpdates(false)) - when sending updates to the client + // changes_only: false (equivalent to sendUpdates(true)) - when sending the entire layout to the client or when forcing a full update + typedef std::function BeforeUpdateCallback; + private: AsyncWebServer* _server = nullptr; AsyncWebSocket* _ws = nullptr; @@ -80,6 +85,7 @@ class ESPDash{ char username[64]; char password[64]; uint32_t _idCounter = 0; + BeforeUpdateCallback _beforeUpdateCallback = nullptr; volatile bool _asyncAccessInProgress = false; @@ -129,6 +135,12 @@ class ESPDash{ // in which case you should not modify them bool isAsyncAccessInProgress() { return _asyncAccessInProgress; } + // Register a callback that will be called before some updates will be sent to the client. + // This callback can be used for example to refresh some card values that never change after only when a full layout is request (i.e. on page reload). + // This allows to avoid spending time refreshing cards that never change, but still allows them to be refreshed hen the user refresh the dashboard. + // If called from the async_http task, isAsyncAccessInProgress() will return true while in this callback. + void onBeforeUpdate(BeforeUpdateCallback callback) { _beforeUpdateCallback = callback; } + ~ESPDash(); };