mirror of
https://github.com/lumapu/ahoy.git
synced 2025-07-27 05:07:18 +02:00
fix build
added scheduler (not finished now, draft)
This commit is contained in:
parent
453d8eb5ec
commit
d0db3ece88
7 changed files with 121 additions and 42 deletions
23
src/app.cpp
23
src/app.cpp
|
@ -35,9 +35,11 @@ void app::setup(uint32_t timeout) {
|
||||||
mPayload.setup(mSys);
|
mPayload.setup(mSys);
|
||||||
mPayload.enableSerialDebug(mConfig->serial.debug);
|
mPayload.enableSerialDebug(mConfig->serial.debug);
|
||||||
#ifndef AP_ONLY
|
#ifndef AP_ONLY
|
||||||
setupMqtt();
|
if (mConfig->mqtt.broker[0] > 0) {
|
||||||
if(mMqttActive)
|
mMqtt.setup(&mConfig->mqtt, mConfig->sys.deviceName, mVersion, mSys, &mUtcTimestamp, &mSunrise, &mSunset);
|
||||||
mPayload.addListener(std::bind(&MqttType::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));
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
setupLed();
|
setupLed();
|
||||||
|
|
||||||
|
@ -45,13 +47,15 @@ void app::setup(uint32_t timeout) {
|
||||||
mWeb->setup();
|
mWeb->setup();
|
||||||
mWeb->setProtection(strlen(mConfig->sys.adminPwd) != 0);
|
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) {
|
void app::loop(void) {
|
||||||
DPRINTLN(DBG_VERBOSE, F("app::loop"));
|
DPRINTLN(DBG_VERBOSE, F("app::loop"));
|
||||||
|
|
||||||
|
ah::Scheduler::loop();
|
||||||
|
|
||||||
bool apActive = mWifi->loop();
|
bool apActive = mWifi->loop();
|
||||||
mWeb->loop();
|
mWeb->loop();
|
||||||
|
|
||||||
|
@ -114,7 +118,6 @@ void app::loop(void) {
|
||||||
mPayload.process(true, mConfig->nrf.maxRetransPerPyld, &mStat);
|
mPayload.process(true, mConfig->nrf.maxRetransPerPyld, &mStat);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mMqttActive)
|
|
||||||
mMqtt.loop();
|
mMqtt.loop();
|
||||||
|
|
||||||
if (ah::checkTicker(&mTicker, 1000)) {
|
if (ah::checkTicker(&mTicker, 1000)) {
|
||||||
|
@ -243,7 +246,6 @@ void app::resetSystem(void) {
|
||||||
mHeapStatCnt = 0;
|
mHeapStatCnt = 0;
|
||||||
|
|
||||||
mSendTicker = 0xffff;
|
mSendTicker = 0xffff;
|
||||||
mMqttActive = false;
|
|
||||||
|
|
||||||
mTicker = 0;
|
mTicker = 0;
|
||||||
mRxTicker = 0;
|
mRxTicker = 0;
|
||||||
|
@ -255,15 +257,6 @@ void app::resetSystem(void) {
|
||||||
memset(&mStat, 0, sizeof(statistics_t));
|
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) {
|
void app::setupLed(void) {
|
||||||
/** LED connection diagram
|
/** LED connection diagram
|
||||||
|
|
|
@ -22,9 +22,9 @@
|
||||||
#include "hm/hmSystem.h"
|
#include "hm/hmSystem.h"
|
||||||
#include "hm/payload.h"
|
#include "hm/payload.h"
|
||||||
#include "wifi/ahoywifi.h"
|
#include "wifi/ahoywifi.h"
|
||||||
#include "web/mqtt.h"
|
|
||||||
#include "web/web.h"
|
#include "web/web.h"
|
||||||
|
|
||||||
|
#include "publisher/pubMqtt.h"
|
||||||
#include "publisher/pubSerial.h"
|
#include "publisher/pubSerial.h"
|
||||||
|
|
||||||
// convert degrees and radians for sun calculation
|
// convert degrees and radians for sun calculation
|
||||||
|
@ -35,7 +35,7 @@
|
||||||
|
|
||||||
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
|
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
|
||||||
typedef Payload<HmSystemType> PayloadType;
|
typedef Payload<HmSystemType> PayloadType;
|
||||||
typedef mqtt<HmSystemType> MqttType;
|
typedef PubMqtt<HmSystemType> PubMqttType;
|
||||||
typedef PubSerial<HmSystemType> PubSerialType;
|
typedef PubSerial<HmSystemType> PubSerialType;
|
||||||
|
|
||||||
class ahoywifi;
|
class ahoywifi;
|
||||||
|
@ -197,7 +197,7 @@ class app : public ah::Scheduler {
|
||||||
uint32_t mRxTicker;
|
uint32_t mRxTicker;
|
||||||
|
|
||||||
// mqtt
|
// mqtt
|
||||||
MqttType mMqtt;
|
PubMqttType mMqtt;
|
||||||
bool mMqttActive;
|
bool mMqttActive;
|
||||||
|
|
||||||
// sun
|
// sun
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
#define VERSION_MAJOR 0
|
#define VERSION_MAJOR 0
|
||||||
#define VERSION_MINOR 5
|
#define VERSION_MINOR 5
|
||||||
#define VERSION_PATCH 37
|
#define VERSION_PATCH 38
|
||||||
|
|
||||||
//-------------------------------------
|
//-------------------------------------
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
|
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef __MQTT_H__
|
#ifndef __PUB_MQTT_H__
|
||||||
#define __MQTT_H__
|
#define __PUB_MQTT_H__
|
||||||
|
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
@ -21,9 +21,9 @@
|
||||||
#include "../hm/hmSystem.h"
|
#include "../hm/hmSystem.h"
|
||||||
|
|
||||||
template<class HMSYSTEM>
|
template<class HMSYSTEM>
|
||||||
class mqtt {
|
class PubMqtt {
|
||||||
public:
|
public:
|
||||||
mqtt() {
|
PubMqtt() {
|
||||||
mClient = new PubSubClient(mEspClient);
|
mClient = new PubSubClient(mEspClient);
|
||||||
mAddressSet = false;
|
mAddressSet = false;
|
||||||
|
|
||||||
|
@ -31,22 +31,23 @@ class mqtt {
|
||||||
mTxCnt = 0;
|
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"));
|
DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup"));
|
||||||
mAddressSet = true;
|
mAddressSet = true;
|
||||||
|
|
||||||
mApp = app;
|
|
||||||
mCfg_mqtt = cfg_mqtt;
|
mCfg_mqtt = cfg_mqtt;
|
||||||
mDevName = devName;
|
mDevName = devName;
|
||||||
mSys = sys;
|
mSys = sys;
|
||||||
mUtcTimestamp = utcTs;
|
mUtcTimestamp = utcTs;
|
||||||
|
mSunrise = sunrise;
|
||||||
|
mSunset = sunset;
|
||||||
|
|
||||||
mClient->setServer(mCfg_mqtt->broker, mCfg_mqtt->port);
|
mClient->setServer(mCfg_mqtt->broker, mCfg_mqtt->port);
|
||||||
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
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("version", version);
|
||||||
sendMsg("device", devName);
|
sendMsg("device", devName);
|
||||||
|
@ -54,13 +55,17 @@ class mqtt {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
if(mAddressSet)
|
||||||
|
mClient->loop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void tickerSecond(int i) {
|
||||||
|
DPRINTLN(DBG_INFO, "tickerSecond");
|
||||||
if(mAddressSet) {
|
if(mAddressSet) {
|
||||||
if(!mClient->connected())
|
if(!mClient->connected())
|
||||||
reconnect();
|
reconnect();
|
||||||
mClient->loop();
|
|
||||||
|
|
||||||
sendIvData();
|
|
||||||
}
|
}
|
||||||
|
sendIvData();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCallback(MQTT_CALLBACK_SIGNATURE) {
|
void setCallback(MQTT_CALLBACK_SIGNATURE) {
|
||||||
|
@ -224,10 +229,8 @@ class mqtt {
|
||||||
|
|
||||||
sendMsg("wifi_rssi", String(WiFi.RSSI()).c_str());
|
sendMsg("wifi_rssi", String(WiFi.RSSI()).c_str());
|
||||||
|
|
||||||
String sunrise = String(mApp->getSunrise());
|
sendMsg("sunrise", String(*mSunrise).c_str());
|
||||||
String sunset = String(mApp->getSunset());
|
sendMsg("sunset", String(*mSunset).c_str());
|
||||||
sendMsg("sunrise", sunrise.c_str());
|
|
||||||
sendMsg("sunset", sunset.c_str());
|
|
||||||
|
|
||||||
while(!mSendList.empty()) {
|
while(!mSendList.empty()) {
|
||||||
memset(total, 0, sizeof(float) * 4);
|
memset(total, 0, sizeof(float) * 4);
|
||||||
|
@ -430,7 +433,7 @@ class mqtt {
|
||||||
DPRINTLN(DBG_INFO, F("app::cbMqtt finished"));
|
DPRINTLN(DBG_INFO, F("app::cbMqtt finished"));
|
||||||
}
|
}
|
||||||
|
|
||||||
app *mApp;
|
uint32_t *mSunrise, *mSunset;
|
||||||
WiFiClient mEspClient;
|
WiFiClient mEspClient;
|
||||||
PubSubClient *mClient;
|
PubSubClient *mClient;
|
||||||
HMSYSTEM *mSys;
|
HMSYSTEM *mSys;
|
||||||
|
@ -444,4 +447,4 @@ class mqtt {
|
||||||
std::queue<uint8_t> mSendList;
|
std::queue<uint8_t> mSendList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*__MQTT_H_*/
|
#endif /*__PUB_MQTT_H__*/
|
|
@ -21,7 +21,8 @@ class PubSerial {
|
||||||
mUtcTimestamp = utcTs;
|
mUtcTimestamp = utcTs;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tickerMinute(void) {
|
void tickerMinute(int i) {
|
||||||
|
DPRINTLN(DBG_INFO, "tickerMinute");
|
||||||
if(++mTick >= mCfg->serial.interval) {
|
if(++mTick >= mCfg->serial.interval) {
|
||||||
mTick = 0;
|
mTick = 0;
|
||||||
if (mCfg->serial.showIv) {
|
if (mCfg->serial.showIv) {
|
||||||
|
|
81
src/utils/scheduler.h
Normal file
81
src/utils/scheduler.h
Normal file
|
@ -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__*/
|
|
@ -157,17 +157,18 @@ void webApi::getSysInfo(JsonObject obj) {
|
||||||
obj[F("hostname")] = WiFi.getHostname();
|
obj[F("hostname")] = WiFi.getHostname();
|
||||||
obj[F("sdk_version")] = ESP.getSdkVersion();
|
obj[F("sdk_version")] = ESP.getSdkVersion();
|
||||||
obj[F("cpu_freq")] = ESP.getCpuFreqMHz();
|
obj[F("cpu_freq")] = ESP.getCpuFreqMHz();
|
||||||
|
#if defined(ESP32)
|
||||||
obj[F("heap_total")] = ESP.getHeapSize();
|
obj[F("heap_total")] = ESP.getHeapSize();
|
||||||
obj[F("heap_used")] = ESP.getHeapSize() - ESP.getFreeHeap();
|
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_total")] = ESP.getFreeSketchSpace();
|
||||||
obj[F("sketch_used")] = ESP.getSketchSize();
|
obj[F("sketch_used")] = ESP.getSketchSize();
|
||||||
//obj[F("littlefs_total")] = LittleFS.totalBytes();
|
//obj[F("littlefs_total")] = LittleFS.totalBytes();
|
||||||
//obj[F("littlefs_used")] = LittleFS.usedBytes();
|
//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)
|
#if defined(ESP32)
|
||||||
obj[F("esp_type")] = F("ESP32");
|
obj[F("esp_type")] = F("ESP32");
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue