From 1e5d788914d168698221556c5c6e1ad1bc8c8ec9 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sun, 10 Mar 2024 23:00:31 +0100 Subject: [PATCH] optimizing ... --- src/app.h | 4 +-- src/appInterface.h | 4 +-- src/plugins/Display/Display_Mono.h | 14 ++++---- src/plugins/history.h | 55 +++++++++++++++++------------- src/web/RestApi.h | 11 +++--- src/web/html/history.html | 18 +++------- src/web/html/style.css | 19 +++-------- 7 files changed, 56 insertions(+), 69 deletions(-) diff --git a/src/app.h b/src/app.h index a6960d89..4069662f 100644 --- a/src/app.h +++ b/src/app.h @@ -314,9 +314,9 @@ class app : public IApp, public ah::Scheduler { #endif } - uint32_t getHistoryPeriode(uint8_t type) override { + uint32_t getHistoryPeriod(uint8_t type) override { #if defined(ENABLE_HISTORY) - return mHistory.getPeriode((HistoryStorageType)type); + return mHistory.getPeriod((HistoryStorageType)type); #else return 0; #endif diff --git a/src/appInterface.h b/src/appInterface.h index 8a18cf8d..99db7c34 100644 --- a/src/appInterface.h +++ b/src/appInterface.h @@ -67,11 +67,11 @@ class IApp { virtual bool isProtected(const char *clientIp, const char *token, bool askedFromWeb) const = 0; virtual uint16_t getHistoryValue(uint8_t type, uint16_t i) = 0; - virtual uint32_t getHistoryPeriode(uint8_t type) = 0; + virtual uint32_t getHistoryPeriod(uint8_t type) = 0; virtual uint16_t getHistoryMaxDay() = 0; virtual uint32_t getHistoryLastValueTs(uint8_t type) = 0; #if defined(ENABLE_HISTORY_LOAD_DATA) - virtual void addValueToHistory(uint8_t historyType, uint8_t valueType, uint32_t value) = 0; + virtual void addValueToHistory(uint8_t historyType, uint8_t valueType, uint32_t value) = 0; #endif virtual void* getRadioObj(bool nrf) = 0; }; diff --git a/src/plugins/Display/Display_Mono.h b/src/plugins/Display/Display_Mono.h index e855eeb9..ad463d7f 100644 --- a/src/plugins/Display/Display_Mono.h +++ b/src/plugins/Display/Display_Mono.h @@ -110,7 +110,7 @@ class DisplayMono { } // add new value to power graph and maintain state engine for period times - void addPowerGraphEntry(float val) { + /*void addPowerGraphEntry(float val) { if (nullptr == mPgData) // power graph not initialized return; @@ -163,7 +163,7 @@ class DisplayMono { mPgData[mPgLastPos] = std::max(mPgData[mPgLastPos], val); // update current datapoint to maximum of all seen values (= envelope curve) mPgMaxPwr = std::max(mPgMaxPwr, val); // update max value of stored data for scaling of y-axis } - } + }*/ // plot power graph to given display offset void plotPowerGraph(uint8_t xoff, uint8_t yoff) { @@ -296,15 +296,15 @@ class DisplayMono { uint8_t mPgWidth = 0; private: - float *mPgData = nullptr; + //float *mPgData = nullptr; uint8_t mPgHeight = 0; float mPgMaxPwr = 0.0; - uint32_t mPgStartTime = 0; - uint32_t mPgEndTime = 0; - uint32_t mPgPeriod = 0; // seconds + //uint32_t mPgStartTime = 0; + //uint32_t mPgEndTime = 0; + //uint32_t mPgPeriod = 0; // seconds uint8_t mPgLastPos = 0; uint32_t mPgLastTime = 0; - PowerGraphState mPgState = PowerGraphState::NO_TIME_SYNC; + //PowerGraphState mPgState = PowerGraphState::NO_TIME_SYNC; uint16_t mDispHeight = 0; uint8_t mLuminance = 0; diff --git a/src/plugins/history.h b/src/plugins/history.h index 2b5f0d32..1068b947 100644 --- a/src/plugins/history.h +++ b/src/plugins/history.h @@ -26,8 +26,8 @@ class HistoryData { private: struct storage_t { uint16_t refreshCycle = 0; - uint16_t loopCnt; - uint16_t listIdx; // index for next Element to write into WattArr + uint16_t loopCnt = 0; + uint16_t listIdx = 0; // index for next Element to write into WattArr // ring buffer for watt history std::array data; @@ -45,13 +45,12 @@ class HistoryData { mConfig = config; mTs = ts; - mCurPwr.reset(); mCurPwr.refreshCycle = mConfig->inst.sendInterval; - mCurPwrDay.reset(); mCurPwrDay.refreshCycle = mConfig->inst.sendInterval; - mYieldDay.reset(); + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) mYieldDay.refreshCycle = 60; - mLastValueTs = 0; + #endif + mLastValueTs = 0; mPgPeriod=0; mMaximumDay = 0; } @@ -60,7 +59,7 @@ class HistoryData { float curPwr = 0; //float maxPwr = 0; float yldDay = -0.1; - uint32_t ts = 0; + uint32_t ts = 0; for (uint8_t i = 0; i < mSys->getNumInverters(); i++) { Inverter<> *iv = mSys->getInverterByPos(i); @@ -70,14 +69,14 @@ class HistoryData { curPwr += iv->getChannelFieldValue(CH0, FLD_PAC, rec); //maxPwr += iv->getChannelFieldValue(CH0, FLD_MP, rec); yldDay += iv->getChannelFieldValue(CH0, FLD_YD, rec); - if (rec->ts > ts) - ts = rec->ts; + if (rec->ts > ts) + ts = rec->ts; } if ((++mCurPwr.loopCnt % mCurPwr.refreshCycle) == 0) { mCurPwr.loopCnt = 0; if (curPwr > 0) { - mLastValueTs = ts; + mLastValueTs = ts; addValue(&mCurPwr, roundf(curPwr)); if (curPwr > mMaximumDay) mMaximumDay = roundf(curPwr); @@ -94,18 +93,18 @@ class HistoryData { } } + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) if((++mYieldDay.loopCnt % mYieldDay.refreshCycle) == 0) { - mYieldDay.loopCnt = 0; - if (*mTs > mApp->getSunset()) - { + mYieldDay.loopCnt = 0; + if (*mTs > mApp->getSunset()) { if ((!mDayStored) && (yldDay > 0)) { addValue(&mYieldDay, roundf(yldDay)); mDayStored = true; } - } - else if (*mTs > mApp->getSunrise()) + } else if (*mTs > mApp->getSunrise()) mDayStored = false; - } + } + #endif } uint16_t valueAt(HistoryStorageType type, uint16_t i) { @@ -113,10 +112,12 @@ class HistoryData { uint16_t idx=i; DPRINTLN(DBG_VERBOSE, F("valueAt ") + String((uint8_t)type) + " i=" + String(i)); + idx = (s->listIdx + i) % HISTORY_DATA_ARR_LENGTH; switch (type) { + default: + [[fallthrough]]; case HistoryStorageType::POWER: s = &mCurPwr; - idx = (s->listIdx + i) % HISTORY_DATA_ARR_LENGTH; break; case HistoryStorageType::POWER_DAY: s = &mCurPwrDay; @@ -124,12 +125,10 @@ class HistoryData { break; case HistoryStorageType::YIELD: s = &mYieldDay; - idx = (s->listIdx + i) % HISTORY_DATA_ARR_LENGTH; break; } - if (s) + return s->data[idx]; - return 0; } uint16_t getMaximumDay() { @@ -150,8 +149,8 @@ class HistoryData { return mCurPwr.refreshCycle; break; case HistoryStorageType::POWER_DAY: - return mPgPeriod / HISTORY_DATA_ARR_LENGTH; - break; + return mPgPeriod / HISTORY_DATA_ARR_LENGTH; + break; case HistoryStorageType::YIELD: return (60 * 60 * 24); // 1 day break; @@ -159,21 +158,29 @@ class HistoryData { return 0; } + bool isDataValid(void) { + return ((0 != mPgStartTime) && (0 != mPgEndTime)); + } + #if defined(ENABLE_HISTORY_LOAD_DATA) /* For filling data from outside */ void addValue(HistoryStorageType historyType, uint8_t valueType, uint32_t value) { if (valueType<2) { storage_t *s=NULL; switch (historyType) { + default: + [[fallthrough]]; case HistoryStorageType::POWER: s = &mCurPwr; break; case HistoryStorageType::POWER_DAY: s = &mCurPwrDay; break; + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) case HistoryStorageType::YIELD: s = &mYieldDay; break; + #endif } if (s) { @@ -275,10 +282,12 @@ class HistoryData { storage_t mCurPwr; storage_t mCurPwrDay; + #if defined(ENABLE_HISTORY_YIELD_PER_DAY) storage_t mYieldDay; + #endif bool mDayStored = false; uint16_t mMaximumDay = 0; - uint32_t mLastValueTs = 0; + uint32_t mLastValueTs = 0; enum class PowerGraphState { NO_TIME_SYNC, IN_PERIOD, diff --git a/src/web/RestApi.h b/src/web/RestApi.h index 182e3a5e..dc1cadfc 100644 --- a/src/web/RestApi.h +++ b/src/web/RestApi.h @@ -50,9 +50,8 @@ class RestApi { #endif mConfig = config; #if defined(ENABLE_HISTORY_LOAD_DATA) - //Vart67: Debugging history graph (loading data into graph storage mSrv->on("/api/addYDHist", - HTTP_POST, std::bind(&RestApi::onApiPost, this, std::placeholders::_1), + HTTP_POST, std::bind(&RestApi::onApiPost, this, std::placeholders::_1), std::bind(&RestApi::onApiPostYDHist,this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5, std::placeholders::_6)); #endif mSrv->on("/api", HTTP_POST, std::bind(&RestApi::onApiPost, this, std::placeholders::_1)).onBody( @@ -913,7 +912,7 @@ class RestApi { void getPowerHistory(AsyncWebServerRequest *request, JsonObject obj) { getGeneric(request, obj.createNestedObject(F("generic"))); #if defined(ENABLE_HISTORY) - obj[F("refresh")] = mApp->getHistoryPeriode((uint8_t)HistoryStorageType::POWER); + obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER); uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER, fld); @@ -930,7 +929,7 @@ class RestApi { void getPowerHistoryDay(AsyncWebServerRequest *request, JsonObject obj){ getGeneric(request, obj.createNestedObject(F("generic"))); #if defined(ENABLE_HISTORY) - obj[F("refresh")] = mApp->getHistoryPeriode((uint8_t)HistoryStorageType::POWER_DAY); + obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::POWER_DAY); uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::POWER_DAY, fld); @@ -947,8 +946,8 @@ class RestApi { void getYieldDayHistory(AsyncWebServerRequest *request, JsonObject obj) { getGeneric(request, obj.createNestedObject(F("generic"))); - #if defined(ENABLE_HISTORY) - obj[F("refresh")] = mApp->getHistoryPeriode((uint8_t)HistoryStorageType::YIELD); + #if defined(ENABLE_HISTORY) && defined(ENABLE_HISTORY_YIELD_PER_DAY) + obj[F("refresh")] = mApp->getHistoryPeriod((uint8_t)HistoryStorageType::YIELD); uint16_t max = 0; for (uint16_t fld = 0; fld < HISTORY_DATA_ARR_LENGTH; fld++) { uint16_t value = mApp->getHistoryValue((uint8_t)HistoryStorageType::YIELD, fld); diff --git a/src/web/html/history.html b/src/web/html/history.html index ab0bc4a6..bf52c733 100644 --- a/src/web/html/history.html +++ b/src/web/html/history.html @@ -30,8 +30,7 @@ {#UPDATED} {#SECONDS}

- -

{#TOTAL_YIELD_PER_DAY}

@@ -39,7 +38,8 @@ {#MAXIMUM}: Wh

- + +

Insert data into Yield per day history

Insert data (*.json) i.e. from a saved "/api/yieldDayHistory" call @@ -50,7 +50,7 @@
- --> + {#HTML_FOOTER} @@ -151,19 +151,9 @@ } this.svg.appendChild(defLg); - let chartFrame = document.createElementNS(svgns, "rect"); - chartFrame.setAttribute("id", this.namePrefix + "_chartFrame"); - chartFrame.setAttribute("class", "chartFrame"); - chartFrame.setAttribute("x", "0"); - chartFrame.setAttribute("y", "0"); - chartFrame.setAttribute("width", String(this.datapoints * 2)); - chartFrame.setAttribute("height", String(this.mChartHight)); - this.svg.appendChild(chartFrame); - // Group chart content let chartContent = document.createElementNS(svgns, "g"); chartContent.setAttribute("id", this.namePrefix + "_svgChartContent"); - chartFrame.setAttribute("transform", "translate(29, 5)"); chartContent.setAttribute("transform", "translate(30, 5)"); // Graph values in a polyline diff --git a/src/web/html/style.css b/src/web/html/style.css index 9df9a4a7..556dfcdc 100644 --- a/src/web/html/style.css +++ b/src/web/html/style.css @@ -33,27 +33,16 @@ textarea { color: var(--fg2); } -svg.container { - background:var(--chart-cont); -} - -rect.chartFrame { - fill: var(--chart-bg); - stroke: gray; - stroke-width: 1px; -} - svg polyline { - fill-opacity: .5; - stroke-width: 1; + fill-opacity: .5; + stroke-width: 1; } svg text { - font-size: x-small; - fill: var(--chart-text); + font-size: x-small; + fill: var(--chart-text); } - div.chartDivContainer { padding: 1px; margin: 1px;