Browse Source

fix build

added scheduler (not finished now, draft)
pull/430/head
lumapu 2 years ago
parent
commit
d0db3ece88
  1. 25
      src/app.cpp
  2. 6
      src/app.h
  3. 2
      src/defines.h
  4. 37
      src/publisher/pubMqtt.h
  5. 3
      src/publisher/pubSerial.h
  6. 81
      src/utils/scheduler.h
  7. 9
      src/web/webApi.cpp

25
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

6
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<MAX_NUM_INVERTERS> HmSystemType;
typedef Payload<HmSystemType> PayloadType;
typedef mqtt<HmSystemType> MqttType;
typedef PubMqtt<HmSystemType> PubMqttType;
typedef PubSerial<HmSystemType> PubSerialType;
class ahoywifi;
@ -197,7 +197,7 @@ class app : public ah::Scheduler {
uint32_t mRxTicker;
// mqtt
MqttType mMqtt;
PubMqttType mMqtt;
bool mMqttActive;
// sun

2
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 {

37
src/web/mqtt.h → 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 <ESP8266WiFi.h>
@ -21,9 +21,9 @@
#include "../hm/hmSystem.h"
template<class HMSYSTEM>
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<HMSYSTEM>::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
setCallback(std::bind(&PubMqtt<HMSYSTEM>::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<uint8_t> mSendList;
};
#endif /*__MQTT_H_*/
#endif /*__PUB_MQTT_H__*/

3
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) {

81
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 <memory>
#include <functional>
#include <list>
enum {EVERY_SEC = 1, EVERY_MIN, EVERY_HR, EVERY_12H, EVERY_DAY};
typedef std::function<void(int)> 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<SchedulerCb> *lType) {
for(std::list<SchedulerCb>::iterator it = lType->begin(); it != lType->end(); ++it) {
(*it)(1);
}
}
protected:
std::list<SchedulerCb> mListSecond;
std::list<SchedulerCb> mListMinute;
std::list<SchedulerCb> mListHour;
std::list<SchedulerCb> mList12h;
std::list<SchedulerCb> mListDay;
private:
uint32_t mPrevMillis;
uint8_t mSeconds, mMinutes, mHours;
};
}
#endif /*__SCHEDULER_H__*/

9
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

Loading…
Cancel
Save