From d0db3ece885a87802633035721d69a826f475002 Mon Sep 17 00:00:00 2001 From: lumapu Date: Wed, 16 Nov 2022 12:32:12 +0100 Subject: [PATCH] fix build added scheduler (not finished now, draft) --- src/app.cpp | 25 +++----- src/app.h | 6 +- src/defines.h | 2 +- src/{web/mqtt.h => publisher/pubMqtt.h} | 37 +++++------ src/publisher/pubSerial.h | 3 +- src/utils/scheduler.h | 81 +++++++++++++++++++++++++ src/web/webApi.cpp | 9 +-- 7 files changed, 121 insertions(+), 42 deletions(-) rename src/{web/mqtt.h => publisher/pubMqtt.h} (96%) create mode 100644 src/utils/scheduler.h diff --git a/src/app.cpp b/src/app.cpp index a298e109..940c9ca7 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -35,9 +35,11 @@ void app::setup(uint32_t timeout) { mPayload.setup(mSys); mPayload.enableSerialDebug(mConfig->serial.debug); #ifndef AP_ONLY - setupMqtt(); - if(mMqttActive) - mPayload.addListener(std::bind(&MqttType::payloadEventListener, &mMqtt, std::placeholders::_1)); + 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)); + } #endif setupLed(); @@ -45,13 +47,15 @@ void app::setup(uint32_t timeout) { mWeb->setup(); mWeb->setProtection(strlen(mConfig->sys.adminPwd) != 0); - addListener(EVERY_MIN, std::bind(&PubSerialType::tickerMinute, &mPubSerial)); + //addListener(EVERY_MIN, std::bind(&PubSerialType::tickerMinute, &mPubSerial, std::placeholders::_1)); } //----------------------------------------------------------------------------- void app::loop(void) { DPRINTLN(DBG_VERBOSE, F("app::loop")); + ah::Scheduler::loop(); + bool apActive = mWifi->loop(); mWeb->loop(); @@ -114,8 +118,7 @@ void app::loop(void) { mPayload.process(true, mConfig->nrf.maxRetransPerPyld, &mStat); } - if (mMqttActive) - mMqtt.loop(); + mMqtt.loop(); if (ah::checkTicker(&mTicker, 1000)) { if (mUtcTimestamp > 946684800 && mConfig->sun.lat && mConfig->sun.lon && (mUtcTimestamp + mCalculatedTimezoneOffset) / 86400 != (mLatestSunTimestamp + mCalculatedTimezoneOffset) / 86400) { // update on reboot or midnight @@ -243,7 +246,6 @@ void app::resetSystem(void) { mHeapStatCnt = 0; mSendTicker = 0xffff; - mMqttActive = false; mTicker = 0; mRxTicker = 0; @@ -255,15 +257,6 @@ void app::resetSystem(void) { memset(&mStat, 0, sizeof(statistics_t)); } -//----------------------------------------------------------------------------- -void app::setupMqtt(void) { - if (mConfig->mqtt.broker[0] > 0) - mMqttActive = true; - - if(mMqttActive) - mMqtt.setup(this, &mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp); -} - //----------------------------------------------------------------------------- void app::setupLed(void) { /** LED connection diagram diff --git a/src/app.h b/src/app.h index 5bbf6e04..f8894f87 100644 --- a/src/app.h +++ b/src/app.h @@ -22,9 +22,9 @@ #include "hm/hmSystem.h" #include "hm/payload.h" #include "wifi/ahoywifi.h" -#include "web/mqtt.h" #include "web/web.h" +#include "publisher/pubMqtt.h" #include "publisher/pubSerial.h" // convert degrees and radians for sun calculation @@ -35,7 +35,7 @@ typedef HmSystem HmSystemType; typedef Payload PayloadType; -typedef mqtt MqttType; +typedef PubMqtt PubMqttType; typedef PubSerial PubSerialType; class ahoywifi; @@ -197,7 +197,7 @@ class app : public ah::Scheduler { uint32_t mRxTicker; // mqtt - MqttType mMqtt; + PubMqttType mMqtt; bool mMqttActive; // sun diff --git a/src/defines.h b/src/defines.h index 3d123d61..46ecfe7f 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 5 -#define VERSION_PATCH 37 +#define VERSION_PATCH 38 //------------------------------------- typedef struct { diff --git a/src/web/mqtt.h b/src/publisher/pubMqtt.h similarity index 96% rename from src/web/mqtt.h rename to src/publisher/pubMqtt.h index 56c46d82..e3f211fb 100644 --- a/src/web/mqtt.h +++ b/src/publisher/pubMqtt.h @@ -3,8 +3,8 @@ // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //----------------------------------------------------------------------------- -#ifndef __MQTT_H__ -#define __MQTT_H__ +#ifndef __PUB_MQTT_H__ +#define __PUB_MQTT_H__ #ifdef ESP8266 #include @@ -21,9 +21,9 @@ #include "../hm/hmSystem.h" template -class mqtt { +class PubMqtt { public: - mqtt() { + PubMqtt() { mClient = new PubSubClient(mEspClient); mAddressSet = false; @@ -31,22 +31,23 @@ class mqtt { mTxCnt = 0; } - ~mqtt() { } + ~PubMqtt() { } - void setup(app *app, cfgMqtt_t *cfg_mqtt, const char *devName, const char *version, HMSYSTEM *sys, uint32_t *utcTs) { + 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")); mAddressSet = true; - mApp = app; mCfg_mqtt = cfg_mqtt; mDevName = devName; mSys = sys; mUtcTimestamp = utcTs; + mSunrise = sunrise; + mSunset = sunset; mClient->setServer(mCfg_mqtt->broker, mCfg_mqtt->port); mClient->setBufferSize(MQTT_MAX_PACKET_SIZE); - setCallback(std::bind(&mqtt::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); + setCallback(std::bind(&PubMqtt::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); sendMsg("version", version); sendMsg("device", devName); @@ -54,13 +55,17 @@ class mqtt { } void loop() { + if(mAddressSet) + mClient->loop(); + } + + void tickerSecond(int i) { + DPRINTLN(DBG_INFO, "tickerSecond"); if(mAddressSet) { if(!mClient->connected()) reconnect(); - mClient->loop(); - - sendIvData(); } + sendIvData(); } void setCallback(MQTT_CALLBACK_SIGNATURE) { @@ -224,10 +229,8 @@ class mqtt { sendMsg("wifi_rssi", String(WiFi.RSSI()).c_str()); - String sunrise = String(mApp->getSunrise()); - String sunset = String(mApp->getSunset()); - sendMsg("sunrise", sunrise.c_str()); - sendMsg("sunset", sunset.c_str()); + sendMsg("sunrise", String(*mSunrise).c_str()); + sendMsg("sunset", String(*mSunset).c_str()); while(!mSendList.empty()) { memset(total, 0, sizeof(float) * 4); @@ -430,7 +433,7 @@ class mqtt { DPRINTLN(DBG_INFO, F("app::cbMqtt finished")); } - app *mApp; + uint32_t *mSunrise, *mSunset; WiFiClient mEspClient; PubSubClient *mClient; HMSYSTEM *mSys; @@ -444,4 +447,4 @@ class mqtt { std::queue mSendList; }; -#endif /*__MQTT_H_*/ +#endif /*__PUB_MQTT_H__*/ diff --git a/src/publisher/pubSerial.h b/src/publisher/pubSerial.h index 1954409b..066f2a06 100644 --- a/src/publisher/pubSerial.h +++ b/src/publisher/pubSerial.h @@ -21,7 +21,8 @@ class PubSerial { mUtcTimestamp = utcTs; } - void tickerMinute(void) { + void tickerMinute(int i) { + DPRINTLN(DBG_INFO, "tickerMinute"); if(++mTick >= mCfg->serial.interval) { mTick = 0; if (mCfg->serial.showIv) { diff --git a/src/utils/scheduler.h b/src/utils/scheduler.h new file mode 100644 index 00000000..2553f18f --- /dev/null +++ b/src/utils/scheduler.h @@ -0,0 +1,81 @@ +//----------------------------------------------------------------------------- +// 2022 Ahoy, https://ahoydtu.de +// Lukas Pusch, lukas@lpusch.de +// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ +//----------------------------------------------------------------------------- + +#ifndef __SCHEDULER_H__ +#define __SCHEDULER_H__ + +#include +#include +#include + +enum {EVERY_SEC = 1, EVERY_MIN, EVERY_HR, EVERY_12H, EVERY_DAY}; +typedef std::function SchedulerCb; + +namespace ah { +class Scheduler { + public: + Scheduler() {} + + void setup() { + mPrevMillis = 0; + mSeconds = 0; + mMinutes = 0; + mHours = 0; + } + + void loop() { + if (millis() - mPrevMillis >= 1000) { + mPrevMillis += 1000; + notify(&mListSecond); + if(++mSeconds >= 60) { + mSeconds = 0; + notify(&mListMinute); + if(++mMinutes >= 60) { + mMinutes = 0; + notify(&mListHour); + if(++mHours >= 24) { + mHours = 0; + notify(&mListDay); + notify(&mList12h); + } + else if(mHours == 12) + notify(&mList12h); + } + } + } + } + + void addListener(uint8_t every, SchedulerCb cb) { + switch(every) { + case EVERY_SEC: mListSecond.push_back(cb); break; + case EVERY_MIN: mListMinute.push_back(cb); break; + case EVERY_HR: mListHour.push_back(cb); break; + case EVERY_12H: mList12h.push_back(cb); break; + case EVERY_DAY: mListDay.push_back(cb); break; + default: break; + } + } + + virtual void notify(std::list *lType) { + for(std::list::iterator it = lType->begin(); it != lType->end(); ++it) { + (*it)(1); + } + } + + protected: + std::list mListSecond; + std::list mListMinute; + std::list mListHour; + std::list mList12h; + std::list mListDay; + + private: + uint32_t mPrevMillis; + uint8_t mSeconds, mMinutes, mHours; +}; +} + +#endif /*__SCHEDULER_H__*/ diff --git a/src/web/webApi.cpp b/src/web/webApi.cpp index 5596e96c..f06b8bb0 100644 --- a/src/web/webApi.cpp +++ b/src/web/webApi.cpp @@ -157,17 +157,18 @@ void webApi::getSysInfo(JsonObject obj) { obj[F("hostname")] = WiFi.getHostname(); obj[F("sdk_version")] = ESP.getSdkVersion(); obj[F("cpu_freq")] = ESP.getCpuFreqMHz(); - +#if defined(ESP32) obj[F("heap_total")] = ESP.getHeapSize(); obj[F("heap_used")] = ESP.getHeapSize() - ESP.getFreeHeap(); + obj[F("chip_revision")] = ESP.getChipRevision(); + obj[F("chip_model")] = ESP.getChipModel(); + obj[F("chip_cores")] = ESP.getChipCores(); +#endif obj[F("sketch_total")] = ESP.getFreeSketchSpace(); obj[F("sketch_used")] = ESP.getSketchSize(); //obj[F("littlefs_total")] = LittleFS.totalBytes(); //obj[F("littlefs_used")] = LittleFS.usedBytes(); - obj[F("chip_revision")] = ESP.getChipRevision(); - obj[F("chip_model")] = ESP.getChipModel(); - obj[F("chip_cores")] = ESP.getChipCores(); #if defined(ESP32) obj[F("esp_type")] = F("ESP32"); #else