mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-29 00:36:11 +02:00
moved elements to scheduler
This commit is contained in:
parent
d0db3ece88
commit
4fe03d641b
9 changed files with 41 additions and 52 deletions
43
src/app.cpp
43
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
|
||||
|
|
27
src/app.h
27
src/app.h
|
@ -115,10 +115,8 @@ class app : public ah::Scheduler {
|
|||
if(0 == newTime)
|
||||
mUpdateNtp = true;
|
||||
else
|
||||
{
|
||||
mUtcTimestamp = newTime;
|
||||
}
|
||||
}
|
||||
|
||||
inline uint32_t getSunrise(void) {
|
||||
return mSunrise;
|
||||
|
@ -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;
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
//-------------------------------------
|
||||
#define VERSION_MAJOR 0
|
||||
#define VERSION_MINOR 5
|
||||
#define VERSION_PATCH 38
|
||||
#define VERSION_PATCH 39
|
||||
|
||||
//-------------------------------------
|
||||
typedef struct {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -20,11 +20,11 @@ class Handler {
|
|||
mList.push_back(f);
|
||||
}
|
||||
|
||||
virtual void notify(void) {
|
||||
/*virtual void notify(void) {
|
||||
for(typename std::list<TYPE>::iterator it = mList.begin(); it != mList.end(); ++it) {
|
||||
(*it);
|
||||
}
|
||||
(*it)();
|
||||
}
|
||||
}*/
|
||||
|
||||
protected:
|
||||
std::list<TYPE> mList;
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include <list>
|
||||
|
||||
enum {EVERY_SEC = 1, EVERY_MIN, EVERY_HR, EVERY_12H, EVERY_DAY};
|
||||
typedef std::function<void(int)> SchedulerCb;
|
||||
typedef std::function<void()> SchedulerCb;
|
||||
|
||||
namespace ah {
|
||||
class Scheduler {
|
||||
|
@ -61,7 +61,7 @@ class Scheduler {
|
|||
|
||||
virtual void notify(std::list<SchedulerCb> *lType) {
|
||||
for(std::list<SchedulerCb>::iterator it = lType->begin(); it != lType->end(); ++it) {
|
||||
(*it)(1);
|
||||
(*it)();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -104,7 +104,7 @@ void web::loop(void) {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
void web::tickSecond(void) {
|
||||
void web::tickSecond() {
|
||||
if(0 != mLogoutTimeout) {
|
||||
mLogoutTimeout -= 1;
|
||||
if(0 == mLogoutTimeout)
|
||||
|
|
|
@ -29,7 +29,7 @@ class web {
|
|||
|
||||
void setup(void);
|
||||
void loop(void);
|
||||
void tickSecond(void);
|
||||
void tickSecond();
|
||||
|
||||
void setProtection(bool protect);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue