Browse Source

0.8.146

* fix reset ticker #1754
* disable MqTT second and minute ticker on network loss
* converted many `std::bind` to its lambda pendant
pull/1759/head
lumapu 4 months ago
parent
commit
8b1d734587
  1. 5
      src/CHANGES.md
  2. 72
      src/app.cpp
  3. 5
      src/app.h
  4. 2
      src/defines.h
  5. 18
      src/utils/scheduler.h

5
src/CHANGES.md

@ -1,5 +1,10 @@
# Development Changes # 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 ## 0.8.145 - 2024-09-22
* fix NTP related issues #1748 #1752 * fix NTP related issues #1748 #1752
* fix MqTT discovery total #1715 * fix MqTT discovery total #1715

72
src/app.cpp

@ -17,10 +17,6 @@ app::app()
: ah::Scheduler {} : ah::Scheduler {}
, mSunrise {0} , mSunrise {0}
, mSunset {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(mVersion, 0, sizeof(char) * 12);
memset(mVersionModules, 0, sizeof(char) * 12); memset(mVersionModules, 0, sizeof(char) * 12);
@ -86,7 +82,7 @@ void app::setup() {
mMqttEnabled = (mConfig->mqtt.broker[0] > 0); mMqttEnabled = (mConfig->mqtt.broker[0] > 0);
if (mMqttEnabled) { if (mMqttEnabled) {
mMqtt.setup(this, &mConfig->mqtt, mConfig->sys.deviceName, mVersion, &mSys, &mTimestamp, &mUptime); 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); }); mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); });
} }
#endif #endif
@ -156,20 +152,25 @@ void app::loop(void) {
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::onNetwork(bool gotIp) { void app::onNetwork(bool connected) {
mNetworkConnected = gotIp; mNetworkConnected = connected;
if(gotIp) { #if defined(ENABLE_MQTT)
if (mMqttEnabled) {
resetTickerByName("mqttS");
resetTickerByName("mqttM");
}
#endif
if(connected) {
mNetwork->updateNtpTime(); 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 defined(ENABLE_MQTT)
if (mMqttEnabled) { if (mMqttEnabled) {
if(MAX_NUM_TICKER == idTickMqttSecond) everySec([this]() { mMqtt.tickerSecond(); }, "mqttS");
idTickMqttSecond = everySec(std::bind(&PubMqttType::tickerSecond, &mMqtt), "mqttS"); everyMin([this]() { mMqtt.tickerMinute(); }, "mqttM");
if(MAX_NUM_TICKER == idTickMqttMinute)
idTickMqttMinute = everyMin(std::bind(&PubMqttType::tickerMinute, &mMqtt), "mqttM");
} }
#endif /*ENABLE_MQTT*/ #endif /*ENABLE_MQTT*/
} }
@ -178,33 +179,33 @@ void app::onNetwork(bool gotIp) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::regularTickers(void) { void app::regularTickers(void) {
DPRINTLN(DBG_DEBUG, F("regularTickers")); DPRINTLN(DBG_DEBUG, F("regularTickers"));
everySec(std::bind(&WebType::tickSecond, &mWeb), "webSc"); everySec([this]() { mWeb.tickSecond(); }, "webSc");
everySec([this]() { mProtection->tickSecond(); }, "prot"); everySec([this]() { mProtection->tickSecond(); }, "prot");
everySec([this]() {mNetwork->tickNetworkLoop(); }, "net"); everySec([this]() { mNetwork->tickNetworkLoop(); }, "net");
if(mConfig->inst.startWithoutTime) 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"); every([this]() { mNetwork->updateNtpTime(); }, mConfig->ntp.interval * 60, "ntp");
if (mConfig->inst.rstValsNotAvail) if (mConfig->inst.rstValsNotAvail)
everyMin(std::bind(&app::tickMinute, this), "tMin"); everyMin([this]() { tickMinute(); }, "tMin");
// Plugins // Plugins
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type) if (DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
everySec(std::bind(&DisplayType::tickerSecond, &mDisplay), "disp"); everySec([this]() { mDisplay.tickerSecond(); }, "disp");
#endif #endif
every(std::bind(&PubSerialType::tick, &mPubSerial), 5, "uart"); every([this]() { mPubSerial.tick(); }, 5, "uart");
//everySec([this]() { mImprov.tickSerial(); }, "impro"); //everySec([this]() { mImprov.tickSerial(); }, "impro");
#if defined(ENABLE_HISTORY) #if defined(ENABLE_HISTORY)
everySec(std::bind(&HistoryType::tickerSecond, &mHistory), "hist"); everySec([this]() { mHistory.tickerSecond(); }, "hist");
#endif /*ENABLE_HISTORY*/ #endif /*ENABLE_HISTORY*/
#if defined(ENABLE_SIMULATOR) #if defined(ENABLE_SIMULATOR)
every(std::bind(&SimulatorType::tick, &mSimulator), 5, "sim"); every([this]() {mSimulator->tick(); }, 5, "sim");
#endif /*ENABLE_SIMULATOR*/ #endif /*ENABLE_SIMULATOR*/
} }
@ -219,13 +220,13 @@ void app::onNtpUpdate(uint32_t utcTimestamp) {
uint32_t localTime = gTimezone.toLocal(mTimestamp); uint32_t localTime = gTimezone.toLocal(mTimestamp);
uint32_t midTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time uint32_t midTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time
resetById(idTickMidnight); resetTickerByName("midNi");
idTickMidnight = onceAt(std::bind(&app::tickMidnight, this), midTrig, "midNi"); onceAt([this]() { tickMidnight(); }, midTrig, "midNi");
if (mConfig->sys.schedReboot) { if (mConfig->sys.schedReboot) {
resetById(idTickReboot);
uint32_t rebootTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86410); // reboot 10 secs after midnght 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)) { if ((0 == mSunrise) && (0.0 != mConfig->sun.lat) && (0.0 != mConfig->sun.lon)) {
@ -246,11 +247,11 @@ void app::tickCalcSunrise(void) {
tickIVCommunication(); 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 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) { if (mMqttEnabled) {
tickSun(); tickSun();
nxtTrig = mSunrise + mConfig->sun.offsetSecMorning + 1; // one second safety to trigger correctly 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 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 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()) // only used and enabled by MQTT (see setup())
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
if (!mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSecMorning, mConfig->sun.offsetSecEvening)) 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 #endif
} }
@ -308,7 +309,7 @@ void app::tickSunrise(void) {
// only used and enabled by MQTT (see setup()) // only used and enabled by MQTT (see setup())
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
if (!mMqtt.tickerSun(mSunrise, mSunset, mConfig->sun.offsetSecMorning, mConfig->sun.offsetSecEvening, true)) 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 #endif
} }
@ -336,7 +337,8 @@ void app::tickMinute(void) {
void app::tickMidnight(void) { void app::tickMidnight(void) {
uint32_t localTime = gTimezone.toLocal(mTimestamp); uint32_t localTime = gTimezone.toLocal(mTimestamp);
uint32_t nxtTrig = gTimezone.toUTC(localTime - (localTime % 86400) + 86400); // next midnight local time 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; Inverter<> *iv;
for (uint8_t id = 0; id < mSys.getNumInverters(); id++) { for (uint8_t id = 0; id < mSys.getNumInverters(); id++) {
@ -379,7 +381,7 @@ void app::tickSend(void) {
} }
if(mAllIvNotAvail != notAvail) if(mAllIvNotAvail != notAvail)
once(std::bind(&app::notAvailChanged, this), 1, "avail"); once([this]() { notAvailChanged(); }, 1, "avail");
mAllIvNotAvail = notAvail; mAllIvNotAvail = notAvail;
updateLed(); updateLed();

5
src/app.h

@ -89,7 +89,7 @@ class app : public IApp, public ah::Scheduler {
void setup(void); void setup(void);
void loop(void) override; void loop(void) override;
void onNetwork(bool gotIp); void onNetwork(bool connected);
void regularTickers(void); void regularTickers(void);
void handleIntr(void) { void handleIntr(void) {
@ -464,9 +464,6 @@ class app : public IApp, public ah::Scheduler {
#endif #endif
bool mMqttEnabled = false; bool mMqttEnabled = false;
uint8_t idTickMqttSecond, idTickMqttMinute;
uint8_t idTickMidnight, idTickReboot;
// sun // sun
int32_t mCalculatedTimezoneOffset = 0; int32_t mCalculatedTimezoneOffset = 0;
uint32_t mSunrise = 0, mSunset = 0; uint32_t mSunrise = 0, mSunset = 0;

2
src/defines.h

@ -13,7 +13,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 8 #define VERSION_MINOR 8
#define VERSION_PATCH 145 #define VERSION_PATCH 146
//------------------------------------- //-------------------------------------
typedef struct { typedef struct {
uint8_t ch; uint8_t ch;

18
src/utils/scheduler.h

@ -87,12 +87,18 @@ namespace ah {
mTimestamp = ts; mTimestamp = ts;
} }
bool resetById(uint8_t id) { bool resetTickerByName(const char* name) {
id = (id % (MAX_NUM_TICKER - 1)); for (uint8_t id = 0; id < MAX_NUM_TICKER; id++) {
if (mTickerInUse[id] == false) if (mTickerInUse[id]) {
return false; if(strncmp(name, mTicker[id].name, strlen(name)) == 0) {
mTicker[id].timeout = mTicker[id].reload; mTicker[id].timeout = mTicker[id].reload;
return true; mTickerInUse[id] = false;
return true;
}
}
}
return false;
} }
uint32_t getUptime(void) { uint32_t getUptime(void) {

Loading…
Cancel
Save