mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-29 08:46: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)
|
while (!Serial)
|
||||||
yield();
|
yield();
|
||||||
|
|
||||||
|
addListener(EVERY_SEC, std::bind(&app::uptimeTick, this));
|
||||||
|
addListener(EVERY_12H, std::bind(&app::ntpUpdateTick, this));
|
||||||
|
|
||||||
resetSystem();
|
resetSystem();
|
||||||
mSettings.setup();
|
mSettings.setup();
|
||||||
mSettings.getPtr(mConfig);
|
mSettings.getPtr(mConfig);
|
||||||
DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false")));
|
DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false")));
|
||||||
|
|
||||||
mWifi = new ahoywifi(mConfig);
|
mWifi = new ahoywifi(mConfig);
|
||||||
|
mWifi->setup(timeout, mSettings.getValid());
|
||||||
|
|
||||||
mSys = new HmSystemType();
|
mSys = new HmSystemType();
|
||||||
mSys->enableDebug();
|
mSys->enableDebug();
|
||||||
mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
|
mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
|
||||||
mSys->addInverters(&mConfig->inst);
|
mSys->addInverters(&mConfig->inst);
|
||||||
|
|
||||||
mWifi->setup(timeout, mSettings.getValid());
|
|
||||||
|
|
||||||
mPayload.setup(mSys);
|
mPayload.setup(mSys);
|
||||||
mPayload.enableSerialDebug(mConfig->serial.debug);
|
mPayload.enableSerialDebug(mConfig->serial.debug);
|
||||||
#ifndef AP_ONLY
|
#if !defined(AP_ONLY)
|
||||||
if (mConfig->mqtt.broker[0] > 0) {
|
if (mConfig->mqtt.broker[0] > 0) {
|
||||||
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset);
|
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset);
|
||||||
mPayload.addListener(std::bind(&PubMqttType::payloadEventListener, &mMqtt, std::placeholders::_1));
|
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
|
#endif
|
||||||
setupLed();
|
setupLed();
|
||||||
|
@ -46,8 +48,9 @@ void app::setup(uint32_t timeout) {
|
||||||
mWeb = new web(this, mConfig, &mStat, mVersion);
|
mWeb = new web(this, mConfig, &mStat, mVersion);
|
||||||
mWeb->setup();
|
mWeb->setup();
|
||||||
mWeb->setProtection(strlen(mConfig->sys.adminPwd) != 0);
|
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();
|
ah::Scheduler::loop();
|
||||||
|
|
||||||
bool apActive = mWifi->loop();
|
|
||||||
mWeb->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) {
|
if (mFlagSendDiscoveryConfig) {
|
||||||
mFlagSendDiscoveryConfig = false;
|
mFlagSendDiscoveryConfig = false;
|
||||||
mMqtt.sendMqttDiscoveryConfig(mConfig->mqtt.topic);
|
mMqtt.sendMqttDiscoveryConfig(mConfig->mqtt.topic);
|
||||||
|
@ -230,13 +207,9 @@ void app::resetSystem(void) {
|
||||||
|
|
||||||
mShouldReboot = false;
|
mShouldReboot = false;
|
||||||
mUptimeSecs = 0;
|
mUptimeSecs = 0;
|
||||||
mPrevMillis = 0;
|
|
||||||
mUpdateNtp = false;
|
mUpdateNtp = false;
|
||||||
mFlagSendDiscoveryConfig = false;
|
mFlagSendDiscoveryConfig = false;
|
||||||
|
|
||||||
mNtpRefreshTicker = 0;
|
|
||||||
mNtpRefreshInterval = NTP_REFRESH_INTERVAL; // [ms]
|
|
||||||
|
|
||||||
#ifdef AP_ONLY
|
#ifdef AP_ONLY
|
||||||
mUtcTimestamp = 1;
|
mUtcTimestamp = 1;
|
||||||
#else
|
#else
|
||||||
|
|
27
src/app.h
27
src/app.h
|
@ -115,9 +115,7 @@ class app : public ah::Scheduler {
|
||||||
if(0 == newTime)
|
if(0 == newTime)
|
||||||
mUpdateNtp = true;
|
mUpdateNtp = true;
|
||||||
else
|
else
|
||||||
{
|
|
||||||
mUtcTimestamp = newTime;
|
mUtcTimestamp = newTime;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline uint32_t getSunrise(void) {
|
inline uint32_t getSunrise(void) {
|
||||||
|
@ -147,6 +145,28 @@ class app : public ah::Scheduler {
|
||||||
void setupLed(void);
|
void setupLed(void);
|
||||||
void updateLed(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) {
|
void stats(void) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
|
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
|
@ -168,10 +188,7 @@ class app : public ah::Scheduler {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t mUptimeSecs;
|
uint32_t mUptimeSecs;
|
||||||
uint32_t mPrevMillis;
|
|
||||||
uint8_t mHeapStatCnt;
|
uint8_t mHeapStatCnt;
|
||||||
uint32_t mNtpRefreshTicker;
|
|
||||||
uint32_t mNtpRefreshInterval;
|
|
||||||
|
|
||||||
uint32_t mUtcTimestamp;
|
uint32_t mUtcTimestamp;
|
||||||
bool mUpdateNtp;
|
bool mUpdateNtp;
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 5
|
#define VERSION_MINOR 5
|
||||||
#define VERSION_PATCH 38
|
#define VERSION_PATCH 39
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -34,7 +34,7 @@ class PubMqtt {
|
||||||
~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) {
|
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;
|
mAddressSet = true;
|
||||||
|
|
||||||
mCfg_mqtt = cfg_mqtt;
|
mCfg_mqtt = cfg_mqtt;
|
||||||
|
@ -59,8 +59,7 @@ class PubMqtt {
|
||||||
mClient->loop();
|
mClient->loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
void tickerSecond(int i) {
|
void tickerSecond() {
|
||||||
DPRINTLN(DBG_INFO, "tickerSecond");
|
|
||||||
if(mAddressSet) {
|
if(mAddressSet) {
|
||||||
if(!mClient->connected())
|
if(!mClient->connected())
|
||||||
reconnect();
|
reconnect();
|
||||||
|
|
|
@ -21,7 +21,7 @@ class PubSerial {
|
||||||
mUtcTimestamp = utcTs;
|
mUtcTimestamp = utcTs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tickerMinute(int i) {
|
void tickerMinute() {
|
||||||
DPRINTLN(DBG_INFO, "tickerMinute");
|
DPRINTLN(DBG_INFO, "tickerMinute");
|
||||||
if(++mTick >= mCfg->serial.interval) {
|
if(++mTick >= mCfg->serial.interval) {
|
||||||
mTick = 0;
|
mTick = 0;
|
||||||
|
|
|
@ -20,11 +20,11 @@ class Handler {
|
||||||
mList.push_back(f);
|
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) {
|
for(typename std::list<TYPE>::iterator it = mList.begin(); it != mList.end(); ++it) {
|
||||||
(*it);
|
(*it)();
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::list<TYPE> mList;
|
std::list<TYPE> mList;
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
enum {EVERY_SEC = 1, EVERY_MIN, EVERY_HR, EVERY_12H, EVERY_DAY};
|
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 {
|
namespace ah {
|
||||||
class Scheduler {
|
class Scheduler {
|
||||||
|
@ -61,7 +61,7 @@ class Scheduler {
|
||||||
|
|
||||||
virtual void notify(std::list<SchedulerCb> *lType) {
|
virtual void notify(std::list<SchedulerCb> *lType) {
|
||||||
for(std::list<SchedulerCb>::iterator it = lType->begin(); it != lType->end(); ++it) {
|
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) {
|
if(0 != mLogoutTimeout) {
|
||||||
mLogoutTimeout -= 1;
|
mLogoutTimeout -= 1;
|
||||||
if(0 == mLogoutTimeout)
|
if(0 == mLogoutTimeout)
|
||||||
|
|
|
@ -29,7 +29,7 @@ class web {
|
||||||
|
|
||||||
void setup(void);
|
void setup(void);
|
||||||
void loop(void);
|
void loop(void);
|
||||||
void tickSecond(void);
|
void tickSecond();
|
||||||
|
|
||||||
void setProtection(bool protect);
|
void setProtection(bool protect);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue