mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-11 07:56:39 +02:00
* main in app integriert, Zwischenstand
This commit is contained in:
parent
c6e3cd39ba
commit
ccb9fb537d
7 changed files with 759 additions and 603 deletions
|
@ -6,12 +6,24 @@
|
|||
#ifndef __APP_H__
|
||||
#define __APP_H__
|
||||
|
||||
#include "main.h"
|
||||
#include "dbg.h"
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
|
||||
// NTP
|
||||
#include <WiFiUdp.h>
|
||||
#include <TimeLib.h>
|
||||
#include <DNSServer.h>
|
||||
|
||||
#include <RF24.h>
|
||||
#include <RF24_config.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include "eep.h"
|
||||
#include "defines.h"
|
||||
#include "crc.h"
|
||||
|
||||
#include "CircularBuffer.h"
|
||||
#include "hmSystem.h"
|
||||
|
@ -48,8 +60,41 @@ typedef struct {
|
|||
bool requested;
|
||||
} invPayload_t;
|
||||
|
||||
const byte mDnsPort = 53;
|
||||
|
||||
class app : public Main {
|
||||
/* NTP TIMESERVER CONFIG */
|
||||
#define NTP_SERVER_NAME "pool.ntp.org"
|
||||
#define NTP_LOCAL_PORT 8888
|
||||
#define NTP_PACKET_SIZE 48
|
||||
#define TIMEZONE 1 // Central European time +1
|
||||
|
||||
|
||||
typedef struct {
|
||||
char version[12];
|
||||
char deviceName[DEVNAME_LEN];
|
||||
|
||||
// wifi
|
||||
char stationSsid[SSID_LEN];
|
||||
char stationPwd[PWD_LEN];
|
||||
bool apActive;
|
||||
|
||||
// nrf24
|
||||
uint16_t sendInterval;
|
||||
uint8_t maxRetransPerPyld;
|
||||
|
||||
} config_t;
|
||||
|
||||
|
||||
#define SAVE_SSID 0x00000001
|
||||
#define SAVE_PWD 0x00000002
|
||||
#define SAVE_DEVICE_NAME 0x00000004
|
||||
#define SAVE_INVERTERS 0x00000008
|
||||
#define SAVE_INV_SEND_INTERVAL 0x00000010
|
||||
#define SAVE_INV_RETRY 0x00000020
|
||||
|
||||
#define CHK_MSK(v, m) ((m & v) == m)
|
||||
|
||||
class app {
|
||||
public:
|
||||
app();
|
||||
~app();
|
||||
|
@ -63,24 +108,7 @@ class app : public Main {
|
|||
uint8_t getIrqPin(void) {
|
||||
return mSys->Radio.pinIrq;
|
||||
}
|
||||
|
||||
private:
|
||||
bool buildPayload(uint8_t id);
|
||||
void processPayload(bool retransmit);
|
||||
|
||||
void showFavicon(void);
|
||||
void showSetup(void);
|
||||
void showSave(void);
|
||||
void showStatistics(void);
|
||||
void showHoymiles(void);
|
||||
void showLiveData(void);
|
||||
void showJSON(void);
|
||||
void webapi(void);
|
||||
|
||||
|
||||
void sendMqttDiscoveryConfig(void);
|
||||
const char* getFieldDeviceClass(uint8_t fieldId);
|
||||
const char* getFieldStateClass(uint8_t fieldId);
|
||||
HmSystemType *mSys;
|
||||
|
||||
uint64_t Serial2u64(const char *val) {
|
||||
char tmp[3] = {0};
|
||||
|
@ -96,10 +124,160 @@ class app : public Main {
|
|||
}
|
||||
return ret;
|
||||
}
|
||||
void saveValues(uint32_t saveMask);
|
||||
|
||||
String getDateTimeStr(time_t t) {
|
||||
char str[20] = {0};
|
||||
if(0 == t)
|
||||
sprintf(str, "n/a");
|
||||
else
|
||||
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
|
||||
return String(str);
|
||||
}
|
||||
|
||||
inline uint32_t getUptime(void) {
|
||||
return mUptimeSecs;
|
||||
}
|
||||
|
||||
inline uint32_t getTimestamp(void) {
|
||||
return mTimestamp;
|
||||
}
|
||||
|
||||
void eraseSettings(bool all = false) {
|
||||
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
|
||||
uint8_t buf[64] = {0};
|
||||
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
|
||||
uint16_t end;
|
||||
do {
|
||||
end = addr + 64;
|
||||
if(end > (ADDR_SETTINGS_CRC + 2))
|
||||
end = (ADDR_SETTINGS_CRC + 2);
|
||||
DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
|
||||
mEep->write(addr, buf, (end-addr));
|
||||
addr = end;
|
||||
} while(addr < (ADDR_SETTINGS_CRC + 2));
|
||||
mEep->commit();
|
||||
}
|
||||
|
||||
ESP8266WebServer *mWeb;
|
||||
config_t config;
|
||||
|
||||
private:
|
||||
void MainLoop(void);
|
||||
void Mainsetup(uint32_t timeout);
|
||||
bool getConfig(void);
|
||||
void setupAp(const char *ssid, const char *pwd);
|
||||
bool setupStation(uint32_t timeout);
|
||||
|
||||
|
||||
time_t getNtpTime(void);
|
||||
void sendNTPpacket(IPAddress& address);
|
||||
time_t offsetDayLightSaving (uint32_t local_t);
|
||||
|
||||
uint32_t mUptimeTicker;
|
||||
uint16_t mUptimeInterval;
|
||||
uint32_t mUptimeSecs;
|
||||
uint8_t mHeapStatCnt;
|
||||
|
||||
DNSServer *mDns;
|
||||
|
||||
WiFiUDP *mUdp; // for time server
|
||||
|
||||
|
||||
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
|
||||
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
|
||||
uint8_t buf[32];
|
||||
uint16_t crc = 0xffff;
|
||||
uint8_t len;
|
||||
|
||||
while(length > 0) {
|
||||
len = (length < 32) ? length : 32;
|
||||
mEep->read(start, buf, len);
|
||||
crc = crc16(buf, len, crc);
|
||||
start += len;
|
||||
length -= len;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
void updateCrc(void) {
|
||||
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
|
||||
uint16_t crc;
|
||||
|
||||
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
|
||||
DPRINTLN(DBG_DEBUG, F("new Wifi CRC: ") + String(crc, HEX));
|
||||
mEep->write(ADDR_WIFI_CRC, crc);
|
||||
|
||||
crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
|
||||
DPRINTLN(DBG_DEBUG, F("new Settings CRC: ") + String(crc, HEX));
|
||||
mEep->write(ADDR_SETTINGS_CRC, crc);
|
||||
|
||||
mEep->commit();
|
||||
}
|
||||
|
||||
bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
|
||||
DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
|
||||
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
|
||||
uint16_t crcRd, crcCheck;
|
||||
crcCheck = buildEEpCrc(start, length);
|
||||
mEep->read(crcPos, &crcRd);
|
||||
DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
|
||||
return (crcCheck == crcRd);
|
||||
}
|
||||
|
||||
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
|
||||
//DPRINTLN(DBG_VERBOSE, F("c"));
|
||||
uint32_t mil = millis();
|
||||
if(mil >= *ticker) {
|
||||
*ticker = mil + interval;
|
||||
return true;
|
||||
}
|
||||
else if(mil < (*ticker - interval)) {
|
||||
*ticker = mil + interval;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void stats(void) {
|
||||
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
|
||||
uint32_t free;
|
||||
uint16_t max;
|
||||
uint8_t frag;
|
||||
ESP.getHeapStats(&free, &max, &frag);
|
||||
DPRINT(DBG_VERBOSE, F("free: ") + String(free));
|
||||
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
|
||||
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
|
||||
}
|
||||
|
||||
bool mWifiSettingsValid;
|
||||
bool mSettingsValid;
|
||||
bool mStActive;
|
||||
|
||||
eep *mEep;
|
||||
uint32_t mTimestamp;
|
||||
uint32_t mLimit;
|
||||
uint32_t mNextTryTs;
|
||||
uint32_t mApLastTick;
|
||||
|
||||
|
||||
bool buildPayload(uint8_t id);
|
||||
void processPayload(bool retransmit);
|
||||
|
||||
void showStatistics(void);
|
||||
void showHoymiles(void);
|
||||
void showLiveData(void);
|
||||
void showJSON(void);
|
||||
void webapi(void);
|
||||
|
||||
|
||||
void sendMqttDiscoveryConfig(void);
|
||||
const char* getFieldDeviceClass(uint8_t fieldId);
|
||||
const char* getFieldStateClass(uint8_t fieldId);
|
||||
|
||||
bool mShowRebootRequest;
|
||||
|
||||
HmSystemType *mSys;
|
||||
|
||||
uint16_t mSendTicker;
|
||||
uint8_t mSendLastIvId;
|
||||
|
@ -109,7 +287,6 @@ class app : public Main {
|
|||
uint32_t mRxSuccess;
|
||||
uint32_t mFrameCnt;
|
||||
uint8_t mLastPacketId;
|
||||
uint8_t mMaxRetransPerPyld;
|
||||
|
||||
// timer
|
||||
uint32_t mTicker;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue