Browse Source

first draft of scheduler

pull/430/head
lumapu 2 years ago
parent
commit
fd9f454cfb
  1. 50
      src/app.cpp
  2. 21
      src/app.h
  3. 4
      src/hm/payload.h
  4. 59
      src/publisher/pubSerial.h
  5. 5
      src/web/mqtt.h

50
src/app.cpp

@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
@ -21,17 +21,17 @@ void app::setup(uint32_t timeout) {
resetSystem();
mSettings.setup();
mSettings.getPtr(mConfig);
DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false")));
mWifi = new ahoywifi(mConfig);
mSys = new HmSystemType();
mSys->enableDebug();
mShouldReboot = false;
mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
mSys->addInverters(&mConfig->inst);
mWifi->setup(timeout, mSettings.getValid());
mSys->setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs);
mSys->addInverters(&mConfig->inst);
mPayload.setup(mSys);
mPayload.enableSerialDebug(mConfig->serial.debug);
#ifndef AP_ONLY
@ -41,12 +41,11 @@ void app::setup(uint32_t timeout) {
#endif
setupLed();
mWeb = new web(this, mConfig, &mStat, mVersion);
mWeb->setup();
mWeb->setProtection(strlen(mConfig->sys.adminPwd) != 0);
mWebInst = new web(this, mConfig, &mStat, mVersion);
mWebInst->setup();
mWebInst->setProtection(strlen(mConfig->sys.adminPwd) != 0);
DPRINTLN(DBG_INFO, F("Settings valid: ") + String((mSettings.getValid()) ? F("true") : F("false")));
addListener(EVERY_MIN, std::bind(&PubSerialType::tickerMinute, &mPubSerial));
}
//-----------------------------------------------------------------------------
@ -54,7 +53,7 @@ void app::loop(void) {
DPRINTLN(DBG_VERBOSE, F("app::loop"));
bool apActive = mWifi->loop();
mWebInst->loop();
mWeb->loop();
if (millis() - mPrevMillis >= 1000) {
mPrevMillis += 1000;
@ -62,7 +61,7 @@ void app::loop(void) {
if (0 != mUtcTimestamp)
mUtcTimestamp++;
mWebInst->tickSecond();
mWeb->tickSecond();
if (mShouldReboot) {
DPRINTLN(DBG_INFO, F("Rebooting..."));
@ -98,12 +97,10 @@ void app::loop(void) {
packet_t *p = mSys->BufCtrl.getBack();
if (mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) {
// process buffer only on first occurrence
if (mConfig->serial.debug) {
DPRINT(DBG_INFO, "RX " + String(len) + "B Ch" + String(p->rxCh) + " | ");
mSys->Radio.dumpBuf(NULL, p->packet, len);
}
mStat.frmCnt++;
if (0 != len)
@ -129,30 +126,7 @@ void app::loop(void) {
mLatestSunTimestamp = mUtcTimestamp;
}
if (mConfig->serial.showIv) {
if (++mSerialTicker >= mConfig->serial.interval) {
mSerialTicker = 0;
char topic[30], val[10];
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL != iv) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
if (iv->isAvailable(mUtcTimestamp, rec)) {
DPRINTLN(DBG_INFO, "Inverter: " + String(id));
for (uint8_t i = 0; i < rec->length; i++) {
if (0.0f != iv->getValue(i, rec)) {
snprintf(topic, 30, "%s/ch%d/%s", iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(val, 10, "%.3f %s", iv->getValue(i, rec), iv->getUnit(i, rec));
DPRINTLN(DBG_INFO, String(topic) + ": " + String(val));
}
yield();
}
DPRINTLN(DBG_INFO, "");
}
}
}
}
}
if (++mSendTicker >= mConfig->nrf.sendInterval) {
mSendTicker = 0;
@ -251,6 +225,7 @@ void app::getAvailNetworks(JsonObject obj) {
void app::resetSystem(void) {
snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
mShouldReboot = false;
mUptimeSecs = 0;
mPrevMillis = 0;
mUpdateNtp = false;
@ -268,7 +243,6 @@ void app::resetSystem(void) {
mHeapStatCnt = 0;
mSendTicker = 0xffff;
mSerialTicker = 0xffff;
mMqttActive = false;
mTicker = 0;

21
src/app.h

@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
@ -16,6 +16,7 @@
#include "defines.h"
#include "utils/crc.h"
#include "utils/ahoyTimer.h"
#include "utils/scheduler.h"
#include "hm/CircularBuffer.h"
#include "hm/hmSystem.h"
@ -24,6 +25,8 @@
#include "web/mqtt.h"
#include "web/web.h"
#include "publisher/pubSerial.h"
// convert degrees and radians for sun calculation
#define SIN(x) (sin(radians(x)))
#define COS(x) (cos(radians(x)))
@ -31,15 +34,16 @@
#define ACOS(x) (degrees(acos(x)))
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
typedef Payload<HmSystemType> PayloadType;
typedef mqtt<HmSystemType> MqttType;
typedef payload<HmSystemType> PayloadType;
typedef PubSerial<HmSystemType> PubSerialType;
class ahoywifi;
class web;
class app {
class app : public ah::Scheduler {
public:
app() {}
app() : ah::Scheduler() {}
~app() {}
void setup(uint32_t timeout);
@ -175,9 +179,11 @@ class app {
bool mShowRebootRequest;
ahoywifi *mWifi;
web *mWebInst;
char mVersion[12];
web *mWeb;
PayloadType mPayload;
PubSerialType mPubSerial;
char mVersion[12];
settings mSettings;
settings_t *mConfig;
@ -194,9 +200,6 @@ class app {
MqttType mMqtt;
bool mMqttActive;
// serial
uint16_t mSerialTicker;
// sun
int32_t mCalculatedTimezoneOffset;
uint32_t mSunrise, mSunset;

4
src/hm/payload.h

@ -29,9 +29,9 @@ typedef std::function<void(uint8_t)> payloadListenerType;
template<class HMSYSTEM>
class payload : public Handler<payloadListenerType> {
class Payload : public Handler<payloadListenerType> {
public:
payload() : Handler() {}
Payload() : Handler() {}
void setup(HMSYSTEM *sys) {
mSys = sys;

59
src/publisher/pubSerial.h

@ -0,0 +1,59 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __PUB_SERIAL_H__
#define __PUB_SERIAL_H__
#include "../utils/dbg.h"
#include "../config/settings.h"
#include "../hm/hmSystem.h"
template<class HMSYSTEM>
class PubSerial {
public:
PubSerial() {}
void setup(settings_t *cfg, HMSYSTEM *sys, uint32_t *utcTs) {
mCfg = cfg;
mSys = sys;
mUtcTimestamp = utcTs;
}
void tickerMinute(void) {
if(++mTick >= mCfg->serial.interval) {
mTick = 0;
if (mCfg->serial.showIv) {
char topic[30], val[10];
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL != iv) {
record_t<> *rec = iv->getRecordStruct(RealTimeRunData_Debug);
if (iv->isAvailable(*mUtcTimestamp, rec)) {
DPRINTLN(DBG_INFO, F("Inverter: ") + String(id));
for (uint8_t i = 0; i < rec->length; i++) {
if (0.0f != iv->getValue(i, rec)) {
snprintf(topic, 30, "%s/ch%d/%s", iv->config->name, rec->assign[i].ch, iv->getFieldName(i, rec));
snprintf(val, 10, "%.3f %s", iv->getValue(i, rec), iv->getUnit(i, rec));
DPRINTLN(DBG_INFO, String(topic) + ": " + String(val));
}
yield();
}
DPRINTLN(DBG_INFO, "");
}
}
}
}
}
}
private:
settings_t *mCfg;
HMSYSTEM *mSys;
uint8_t mTick;
uint32_t *mUtcTimestamp;
};
#endif /*__PUB_SERIAL_H__*/

5
src/web/mqtt.h

@ -12,11 +12,6 @@
#include <WiFi.h>
#endif
#if defined(ESP32) && defined(F)
#undef F
#define F(sl) (sl)
#endif
#include "../utils/dbg.h"
#include "../utils/ahoyTimer.h"
#include "../config/config.h"

Loading…
Cancel
Save