You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
6.3 KiB

//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __APP_H__
#define __APP_H__
#include "utils/dbg.h"
#include <Arduino.h>
#include <RF24.h>
#include <RF24_config.h>
#include <ArduinoJson.h>
#include "appInterface.h"
#include "config/settings.h"
#include "defines.h"
#include "utils/crc.h"
#include "utils/ahoyTimer.h"
#include "utils/scheduler.h"
#include "hm/CircularBuffer.h"
#include "hm/hmSystem.h"
#include "hm/payload.h"
#include "wifi/ahoywifi.h"
#include "web/web.h"
#include "web/RestApi.h"
3 years ago
#include "publisher/pubMqtt.h"
#include "publisher/pubSerial.h"
// convert degrees and radians for sun calculation
#define SIN(x) (sin(radians(x)))
#define COS(x) (cos(radians(x)))
#define ASIN(x) (degrees(asin(x)))
#define ACOS(x) (degrees(acos(x)))
typedef HmSystem<MAX_NUM_INVERTERS> HmSystemType;
typedef Payload<HmSystemType> PayloadType;
typedef Web<HmSystemType> WebType;
typedef RestApi<HmSystemType> RestApiType;
typedef PubMqtt<HmSystemType> PubMqttType;
typedef PubSerial<HmSystemType> PubSerialType;
// PLUGINS
#if defined(ENA_NOKIA) || defined(ENA_SSD1306) || defined(ENA_SH1106)
#include "plugins/MonochromeDisplay/MonochromeDisplay.h"
typedef MonochromeDisplay<HmSystemType> MonoDisplayType;
#endif
class app : public IApp, public ah::Scheduler {
public:
app();
~app() {}
void setup(void);
void loop(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
void resetPayload(Inverter<>* iv);
bool getWifiApActive(void);
uint32_t getUptime() {
return Scheduler::getUptime();
}
uint32_t getTimestamp() {
return Scheduler::getTimestamp();
}
bool saveSettings() {
mShowRebootRequest = true;
return mSettings.saveSettings();
}
bool eraseSettings(bool eraseWifi = false) {
return mSettings.eraseSettings(eraseWifi);
}
statistics_t *getStatistics() {
return &mStat;
}
void scanAvailNetworks() {
mWifi.scanAvailNetworks();
}
void getAvailNetworks(JsonObject obj) {
mWifi.getAvailNetworks(obj);
}
void setRebootFlag() {
once(std::bind(&app::tickReboot, this), 1);
}
const char *getVersion() {
return mVersion;
}
uint32_t getSunrise() {
return mSunrise;
}
uint32_t getSunset() {
return mSunset;
}
bool getSettingsValid() {
return mSettings.getValid();
}
bool getRebootRequestState() {
return mShowRebootRequest;
}
void setMqttDiscoveryFlag() {
once(std::bind(&PubMqttType::sendDiscoveryConfig, &mMqtt), 1);
}
void ivSendHighPrio(Inverter<> *iv) {
mPayload.ivSendHighPrio(iv);
}
bool getMqttIsConnected() {
return mMqtt.isConnected();
}
uint32_t getMqttTxCnt() {
return mMqtt.getTxCnt();
}
uint32_t getMqttRxCnt() {
return mMqtt.getRxCnt();
}
bool getProtection() {
return mWeb.getProtection();
}
uint8_t getIrqPin(void) {
return mConfig->nrf.pinIrq;
}
String getTimeStr(uint32_t offset = 0) {
char str[10];
if(0 == mTimestamp)
sprintf(str, "n/a");
else
sprintf(str, "%02d:%02d:%02d ", hour(mTimestamp + offset), minute(mTimestamp + offset), second(mTimestamp + offset));
return String(str);
}
uint32_t getTimezoneOffset() {
return mApi.getTimezoneOffset();
}
void getSchedulerInfo(uint8_t *max) {
getStat(max);
}
void setTimestamp(uint32_t newTime) {
DPRINTLN(DBG_DEBUG, F("setTimestamp: ") + String(newTime));
if(0 == newTime)
mWifi.getNtpTime();
else
Scheduler::setTimestamp(newTime);
}
HmSystemType *mSys;
private:
void resetSystem(void);
void payloadEventListener(uint8_t cmd) {
#if !defined(AP_ONLY)
mMqtt.payloadEventListener(cmd);
#endif
#if defined(ENA_NOKIA) || defined(ENA_SSD1306) || defined(ENA_SH1106)
mMonoDisplay.payloadEventListener(cmd);
#endif
}
void mqttSubRxCb(JsonObject obj);
void setupLed(void);
void updateLed(void);
void tickReboot(void) {
DPRINTLN(DBG_INFO, F("Rebooting..."));
ESP.restart();
}
void tickNtpUpdate(void);
void tickCalcSunrise(void);
void tickIVCommunication(void);
void tickSend(void);
/*void tickSerial(void) {
if(Serial.available() == 0)
return;
uint8_t buf[80];
uint8_t len = Serial.readBytes(buf, 80);
DPRINTLN(DBG_INFO, "got serial data, len: " + String(len));
for(uint8_t i = 0; i < len; i++) {
if((0 != i) && (i % 8 == 0))
DBGPRINTLN("");
DBGPRINT(String(buf[i], HEX) + " ");
}
DBGPRINTLN("");
}*/
bool mShowRebootRequest;
bool mIVCommunicationOn;
ahoywifi mWifi;
WebType mWeb;
RestApiType mApi;
PayloadType mPayload;
PubSerialType mPubSerial;
char mVersion[12];
settings mSettings;
settings_t *mConfig;
uint8_t mSendLastIvId;
uint8_t mSendTickerId;
statistics_t mStat;
3 years ago
// timer
uint32_t mRxTicker;
3 years ago
// mqtt
PubMqttType mMqtt;
bool mMqttActive;
// sun
int32_t mCalculatedTimezoneOffset;
uint32_t mSunrise, mSunset;
// plugins
#if defined(ENA_NOKIA) || defined(ENA_SSD1306) || defined(ENA_SH1106)
MonoDisplayType mMonoDisplay;
#endif
};
#endif /*__APP_H__*/