Skip to content

Commit

Permalink
0.8.146
Browse files Browse the repository at this point in the history
* fix reset ticker #1754
* disable MqTT second and minute ticker on network loss
* converted many `std::bind` to its lambda pendant
  • Loading branch information
lumapu committed Sep 23, 2024
1 parent 2a99097 commit 8b1d734
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
5 changes: 5 additions & 0 deletions src/CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
72 changes: 37 additions & 35 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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*/
}
Expand All @@ -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*/
}

Expand All @@ -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)) {
Expand All @@ -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'
}
}

Expand Down Expand Up @@ -288,18 +289,18 @@ 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");
}

//-----------------------------------------------------------------------------
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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 12 additions & 6 deletions src/utils/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8b1d734

Please sign in to comment.