diff --git a/src/app.cpp b/src/app.cpp index 940c9ca7..20e47e7b 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -18,27 +18,29 @@ void app::setup(uint32_t timeout) { while (!Serial) yield(); + addListener(EVERY_SEC, std::bind(&app::uptimeTick, this)); + addListener(EVERY_12H, std::bind(&app::ntpUpdateTick, this)); + resetSystem(); mSettings.setup(); mSettings.getPtr(mConfig); DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false"))); mWifi = new ahoywifi(mConfig); + mWifi->setup(timeout, mSettings.getValid()); mSys = new HmSystemType(); mSys->enableDebug(); mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs); mSys->addInverters(&mConfig->inst); - mWifi->setup(timeout, mSettings.getValid()); - mPayload.setup(mSys); mPayload.enableSerialDebug(mConfig->serial.debug); -#ifndef AP_ONLY +#if !defined(AP_ONLY) if (mConfig->mqtt.broker[0] > 0) { mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset); mPayload.addListener(std::bind(&PubMqttType::payloadEventListener, &mMqtt, std::placeholders::_1)); - addListener(EVERY_SEC, std::bind(&PubMqttType::tickerSecond, &mMqtt, std::placeholders::_1)); + addListener(EVERY_SEC, std::bind(&PubMqttType::tickerSecond, &mMqtt)); } #endif setupLed(); @@ -46,8 +48,9 @@ void app::setup(uint32_t timeout) { mWeb = new web(this, mConfig, &mStat, mVersion); mWeb->setup(); mWeb->setProtection(strlen(mConfig->sys.adminPwd) != 0); + addListener(EVERY_SEC, std::bind(&web::tickSecond, mWeb)); - //addListener(EVERY_MIN, std::bind(&PubSerialType::tickerMinute, &mPubSerial, std::placeholders::_1)); + //addListener(EVERY_MIN, std::bind(&PubSerialType::tickerMinute, &mPubSerial)); } //----------------------------------------------------------------------------- @@ -56,34 +59,8 @@ void app::loop(void) { ah::Scheduler::loop(); - bool apActive = mWifi->loop(); mWeb->loop(); - if (millis() - mPrevMillis >= 1000) { - mPrevMillis += 1000; - mUptimeSecs++; - if (0 != mUtcTimestamp) - mUtcTimestamp++; - - mWeb->tickSecond(); - - if (mShouldReboot) { - DPRINTLN(DBG_INFO, F("Rebooting...")); - ESP.restart(); - } - } - - if (ah::checkTicker(&mNtpRefreshTicker, mNtpRefreshInterval)) { - if (!apActive) - mUpdateNtp = true; - } - - if (mUpdateNtp) { - mUpdateNtp = false; - mUtcTimestamp = mWifi->getNtpTime(); - DPRINTLN(DBG_INFO, F("[NTP]: ") + getDateTimeStr(mUtcTimestamp) + F(" UTC")); - } - if (mFlagSendDiscoveryConfig) { mFlagSendDiscoveryConfig = false; mMqtt.sendMqttDiscoveryConfig(mConfig->mqtt.topic); @@ -230,13 +207,9 @@ void app::resetSystem(void) { mShouldReboot = false; mUptimeSecs = 0; - mPrevMillis = 0; mUpdateNtp = false; mFlagSendDiscoveryConfig = false; - mNtpRefreshTicker = 0; - mNtpRefreshInterval = NTP_REFRESH_INTERVAL; // [ms] - #ifdef AP_ONLY mUtcTimestamp = 1; #else diff --git a/src/app.h b/src/app.h index f8894f87..e614d89a 100644 --- a/src/app.h +++ b/src/app.h @@ -115,9 +115,7 @@ class app : public ah::Scheduler { if(0 == newTime) mUpdateNtp = true; else - { mUtcTimestamp = newTime; - } } inline uint32_t getSunrise(void) { @@ -147,6 +145,28 @@ class app : public ah::Scheduler { void setupLed(void); void updateLed(void); + void uptimeTick(void) { + mUptimeSecs++; + if (0 != mUtcTimestamp) + mUtcTimestamp++; + + if (mShouldReboot) { + DPRINTLN(DBG_INFO, F("Rebooting...")); + ESP.restart(); + } + + if (mUpdateNtp) { + mUpdateNtp = false; + mUtcTimestamp = mWifi->getNtpTime(); + DPRINTLN(DBG_INFO, F("[NTP]: ") + getDateTimeStr(mUtcTimestamp) + F(" UTC")); + } + } + + void ntpUpdateTick(void) { + if (!mWifi->getApActive()) + mUpdateNtp = true; + } + void stats(void) { DPRINTLN(DBG_VERBOSE, F("main.h:stats")); #ifdef ESP8266 @@ -168,10 +188,7 @@ class app : public ah::Scheduler { } uint32_t mUptimeSecs; - uint32_t mPrevMillis; uint8_t mHeapStatCnt; - uint32_t mNtpRefreshTicker; - uint32_t mNtpRefreshInterval; uint32_t mUtcTimestamp; bool mUpdateNtp; diff --git a/src/defines.h b/src/defines.h index 46ecfe7f..1c2e5d23 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 38 +#define VERSION_PATCH 39 //------------------------------------- typedef struct { diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index e3f211fb..f3ff4eb1 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -34,7 +34,7 @@ class PubMqtt { ~PubMqtt() { } void setup(cfgMqtt_t *cfg_mqtt, const char *devName, const char *version, HMSYSTEM *sys, uint32_t *utcTs, uint32_t *sunrise, uint32_t *sunset) { - DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup")); + DPRINTLN(DBG_VERBOSE, F("PubMqtt.h:setup")); mAddressSet = true; mCfg_mqtt = cfg_mqtt; @@ -59,8 +59,7 @@ class PubMqtt { mClient->loop(); } - void tickerSecond(int i) { - DPRINTLN(DBG_INFO, "tickerSecond"); + void tickerSecond() { if(mAddressSet) { if(!mClient->connected()) reconnect(); diff --git a/src/publisher/pubSerial.h b/src/publisher/pubSerial.h index 066f2a06..ef4fb88f 100644 --- a/src/publisher/pubSerial.h +++ b/src/publisher/pubSerial.h @@ -21,7 +21,7 @@ class PubSerial { mUtcTimestamp = utcTs; } - void tickerMinute(int i) { + void tickerMinute() { DPRINTLN(DBG_INFO, "tickerMinute"); if(++mTick >= mCfg->serial.interval) { mTick = 0; diff --git a/src/utils/handler.h b/src/utils/handler.h index 10dcfef5..51d64c0d 100644 --- a/src/utils/handler.h +++ b/src/utils/handler.h @@ -20,11 +20,11 @@ class Handler { mList.push_back(f); } - virtual void notify(void) { + /*virtual void notify(void) { for(typename std::list::iterator it = mList.begin(); it != mList.end(); ++it) { - (*it); + (*it)(); } - } + }*/ protected: std::list mList; diff --git a/src/utils/scheduler.h b/src/utils/scheduler.h index 2553f18f..ff732b58 100644 --- a/src/utils/scheduler.h +++ b/src/utils/scheduler.h @@ -12,7 +12,7 @@ #include enum {EVERY_SEC = 1, EVERY_MIN, EVERY_HR, EVERY_12H, EVERY_DAY}; -typedef std::function SchedulerCb; +typedef std::function SchedulerCb; namespace ah { class Scheduler { @@ -61,7 +61,7 @@ class Scheduler { virtual void notify(std::list *lType) { for(std::list::iterator it = lType->begin(); it != lType->end(); ++it) { - (*it)(1); + (*it)(); } } diff --git a/src/web/web.cpp b/src/web/web.cpp index d762751b..45be992e 100644 --- a/src/web/web.cpp +++ b/src/web/web.cpp @@ -104,7 +104,7 @@ void web::loop(void) { } //----------------------------------------------------------------------------- -void web::tickSecond(void) { +void web::tickSecond() { if(0 != mLogoutTimeout) { mLogoutTimeout -= 1; if(0 == mLogoutTimeout) diff --git a/src/web/web.h b/src/web/web.h index 582b5421..2c74faf5 100644 --- a/src/web/web.h +++ b/src/web/web.h @@ -29,7 +29,7 @@ class web { void setup(void); void loop(void); - void tickSecond(void); + void tickSecond(); void setProtection(bool protect);