diff --git a/src/CHANGES.md b/src/CHANGES.md index 97c0c595..776af866 100644 --- a/src/CHANGES.md +++ b/src/CHANGES.md @@ -1,5 +1,10 @@ # Development Changes +## 0.8.146 - 2024-09-23 +* fix reset ticker #1754 +* disable MqTT second and minute ticker on network loss +* converted many `std::bind` to its lambda pendant + ## 0.8.145 - 2024-09-22 * fix NTP related issues #1748 #1752 * fix MqTT discovery total #1715 diff --git a/src/app.cpp b/src/app.cpp index 5a992f74..7c6a8aef 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -17,10 +17,6 @@ app::app() : ah::Scheduler {} , mSunrise {0} , mSunset {0} - , idTickMqttSecond {MAX_NUM_TICKER} - , idTickMqttMinute {MAX_NUM_TICKER} - , idTickMidnight {MAX_NUM_TICKER} - , idTickReboot {MAX_NUM_TICKER} { memset(mVersion, 0, sizeof(char) * 12); memset(mVersionModules, 0, sizeof(char) * 12); @@ -86,7 +82,7 @@ void app::setup() { mMqttEnabled = (mConfig->mqtt.broker[0] > 0); if (mMqttEnabled) { mMqtt.setup(this, &mConfig->mqtt, mConfig->sys.deviceName, mVersion, &mSys, &mTimestamp, &mUptime); - mMqtt.setSubscriptionCb(std::bind(&app::mqttSubRxCb, this, std::placeholders::_1)); + mMqtt.setSubscriptionCb([this](JsonObject obj) { mqttSubRxCb(obj); }); mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); }); } #endif @@ -156,20 +152,25 @@ void app::loop(void) { } //----------------------------------------------------------------------------- -void app::onNetwork(bool gotIp) { - mNetworkConnected = gotIp; - if(gotIp) { +void app::onNetwork(bool connected) { + mNetworkConnected = connected; + #if defined(ENABLE_MQTT) + if (mMqttEnabled) { + resetTickerByName("mqttS"); + resetTickerByName("mqttM"); + } + #endif + + if(connected) { mNetwork->updateNtpTime(); - if(!mConfig->inst.startWithoutTime) // already set in regularTickers - every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); + + resetTickerByName("tSend"); + every([this]() { tickSend(); }, mConfig->inst.sendInterval, "tSend"); #if defined(ENABLE_MQTT) if (mMqttEnabled) { - if(MAX_NUM_TICKER == idTickMqttSecond) - idTickMqttSecond = everySec(std::bind(&PubMqttType::tickerSecond, &mMqtt), "mqttS"); - - if(MAX_NUM_TICKER == idTickMqttMinute) - idTickMqttMinute = everyMin(std::bind(&PubMqttType::tickerMinute, &mMqtt), "mqttM"); + everySec([this]() { mMqtt.tickerSecond(); }, "mqttS"); + everyMin([this]() { mMqtt.tickerMinute(); }, "mqttM"); } #endif /*ENABLE_MQTT*/ } @@ -178,33 +179,33 @@ void app::onNetwork(bool gotIp) { //----------------------------------------------------------------------------- void app::regularTickers(void) { DPRINTLN(DBG_DEBUG, F("regularTickers")); - everySec(std::bind(&WebType::tickSecond, &mWeb), "webSc"); + everySec([this]() { mWeb.tickSecond(); }, "webSc"); everySec([this]() { mProtection->tickSecond(); }, "prot"); - everySec([this]() {mNetwork->tickNetworkLoop(); }, "net"); + everySec([this]() { mNetwork->tickNetworkLoop(); }, "net"); if(mConfig->inst.startWithoutTime) - every(std::bind(&app::tickSend, this), mConfig->inst.sendInterval, "tSend"); + every([this]() { tickSend(); }, mConfig->inst.sendInterval, "tSend"); every([this]() { mNetwork->updateNtpTime(); }, mConfig->ntp.interval * 60, "ntp"); if (mConfig->inst.rstValsNotAvail) - everyMin(std::bind(&app::tickMinute, this), "tMin"); + everyMin([this]() { tickMinute(); }, "tMin"); // Plugins #if defined(PLUGIN_DISPLAY) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) - everySec(std::bind(&DisplayType::tickerSecond, &mDisplay), "disp"); + everySec([this]() { mDisplay.tickerSecond(); }, "disp"); #endif - every(std::bind(&PubSerialType::tick, &mPubSerial), 5, "uart"); + every([this]() { mPubSerial.tick(); }, 5, "uart"); //everySec([this]() { mImprov.tickSerial(); }, "impro"); #if defined(ENABLE_HISTORY) - everySec(std::bind(&HistoryType::tickerSecond, &mHistory), "hist"); + everySec([this]() { mHistory.tickerSecond(); }, "hist"); #endif /*ENABLE_HISTORY*/ #if defined(ENABLE_SIMULATOR) - every(std::bind(&SimulatorType::tick, &mSimulator), 5, "sim"); + every([this]() {mSimulator->tick(); }, 5, "sim"); #endif /*ENABLE_SIMULATOR*/ } @@ -219,13 +220,13 @@ void app::onNtpUpdate(uint32_t utcTimestamp) { uint32_t localTime = gTimezone.toLocal(mTimestamp); uint32_t midTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time - resetById(idTickMidnight); - idTickMidnight = onceAt(std::bind(&app::tickMidnight, this), midTrig, "midNi"); + resetTickerByName("midNi"); + onceAt([this]() { tickMidnight(); }, midTrig, "midNi"); if (mConfig->sys.schedReboot) { - resetById(idTickReboot); uint32_t rebootTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86410); // reboot 10 secs after midnght - idTickReboot = onceAt(std::bind(&app::tickReboot, this), rebootTrig, "midRe"); + resetTickerByName("midRe"); + onceAt([this]() { tickReboot(); }, rebootTrig, "midRe"); } if ((0 == mSunrise) && (0.0 != mConfig->sun.lat) && (0.0 != mConfig->sun.lon)) { @@ -246,11 +247,11 @@ void app::tickCalcSunrise(void) { tickIVCommunication(); uint32_t nxtTrig = mSunset + mConfig->sun.offsetSecEvening + 60; // set next trigger to communication stop, +60 for safety that it is certain past communication stop - onceAt(std::bind(&app::tickCalcSunrise, this), nxtTrig, "Sunri"); + onceAt([this]() { tickCalcSunrise(); }, nxtTrig, "Sunri"); if (mMqttEnabled) { tickSun(); nxtTrig = mSunrise + mConfig->sun.offsetSecMorning + 1; // one second safety to trigger correctly - onceAt(std::bind(&app::tickSunrise, this), nxtTrig, "mqSr"); // trigger on sunrise to update 'dis_night_comm' + onceAt([this]() { tickSunrise(); }, nxtTrig, "mqSr"); // trigger on sunrise to update 'dis_night_comm' } } @@ -288,10 +289,10 @@ void app::tickIVCommunication(void) { } if(restartTick) // at least one inverter - onceAt(std::bind(&app::tickIVCommunication, this), nxtTrig, "ivCom"); + onceAt([this]() { tickIVCommunication(); }, nxtTrig, "ivCom"); if (zeroValues) // at least one inverter - once(std::bind(&app::tickZeroValues, this), mConfig->inst.sendInterval, "tZero"); + once([this]() { tickZeroValues(); }, mConfig->inst.sendInterval, "tZero"); } //----------------------------------------------------------------------------- @@ -299,7 +300,7 @@ void app::tickSun(void) { // only used and enabled by MQTT (see setup()) #if defined(ENABLE_MQTT) if (!mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSecMorning, mConfig->sun.offsetSecEvening)) - once(std::bind(&app::tickSun, this), 1, "mqSun"); // MQTT not connected, retry + once([this]() { tickSun(); }, 1, "mqSun"); // MQTT not connected, retry #endif } @@ -308,7 +309,7 @@ void app::tickSunrise(void) { // only used and enabled by MQTT (see setup()) #if defined(ENABLE_MQTT) if (!mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSecMorning, mConfig->sun.offsetSecEvening, true)) - once(std::bind(&app::tickSun, this), 1, "mqSun"); // MQTT not connected, retry + once([this]() { tickSun(); }, 1, "mqSun"); // MQTT not connected, retry #endif } @@ -336,7 +337,8 @@ void app::tickMinute(void) { void app::tickMidnight(void) { uint32_t localTime = gTimezone.toLocal(mTimestamp); uint32_t nxtTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time - onceAt(std::bind(&app::tickMidnight, this), nxtTrig, "mid2"); + resetTickerByName("midNi"); + onceAt([this]() { tickMidnight(); }, nxtTrig, "midNi"); Inverter<> *iv; for (uint8_t id = 0; id < mSys.getNumInverters(); id++) { @@ -379,7 +381,7 @@ void app::tickSend(void) { } if(mAllIvNotAvail != notAvail) - once(std::bind(&app::notAvailChanged, this), 1, "avail"); + once([this]() { notAvailChanged(); }, 1, "avail"); mAllIvNotAvail = notAvail; updateLed(); diff --git a/src/app.h b/src/app.h index 5e460bb5..d954df35 100644 --- a/src/app.h +++ b/src/app.h @@ -89,7 +89,7 @@ class app : public IApp, public ah::Scheduler { void setup(void); void loop(void) override; - void onNetwork(bool gotIp); + void onNetwork(bool connected); void regularTickers(void); void handleIntr(void) { @@ -464,9 +464,6 @@ class app : public IApp, public ah::Scheduler { #endif bool mMqttEnabled = false; - uint8_t idTickMqttSecond, idTickMqttMinute; - uint8_t idTickMidnight, idTickReboot; - // sun int32_t mCalculatedTimezoneOffset = 0; uint32_t mSunrise = 0, mSunset = 0; diff --git a/src/defines.h b/src/defines.h index 025f6e30..3095b7a1 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 145 +#define VERSION_PATCH 146 //------------------------------------- typedef struct { uint8_t ch; diff --git a/src/utils/scheduler.h b/src/utils/scheduler.h index 1f5ef7d7..13208942 100644 --- a/src/utils/scheduler.h +++ b/src/utils/scheduler.h @@ -87,12 +87,18 @@ namespace ah { mTimestamp = ts; } - bool resetById(uint8_t id) { - id = (id % (MAX_NUM_TICKER - 1)); - if (mTickerInUse[id] == false) - return false; - mTicker[id].timeout = mTicker[id].reload; - return true; + bool resetTickerByName(const char* name) { + for (uint8_t id = 0; id < MAX_NUM_TICKER; id++) { + if (mTickerInUse[id]) { + if(strncmp(name, mTicker[id].name, strlen(name)) == 0) { + mTicker[id].timeout = mTicker[id].reload; + mTickerInUse[id] = false; + return true; + } + } + } + + return false; } uint32_t getUptime(void) {