Added database feature.
The database ist stored persistently on LittleFS. The AC total energy is written every hour to the database, together with a timestamp. Each entry to the database requires 8 bytes on the LittleFS partition. The database can be read with the API call /api/database Ralf Bauer
This commit is contained in:
parent
29e9da9126
commit
1c07249b2c
@ -20,6 +20,7 @@
|
|||||||
#include "WebApi_webapp.h"
|
#include "WebApi_webapp.h"
|
||||||
#include "WebApi_ws_console.h"
|
#include "WebApi_ws_console.h"
|
||||||
#include "WebApi_ws_live.h"
|
#include "WebApi_ws_live.h"
|
||||||
|
#include "WebApi_database.h"
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
class WebApiClass {
|
class WebApiClass {
|
||||||
@ -56,6 +57,7 @@ private:
|
|||||||
WebApiWebappClass _webApiWebapp;
|
WebApiWebappClass _webApiWebapp;
|
||||||
WebApiWsConsoleClass _webApiWsConsole;
|
WebApiWsConsoleClass _webApiWsConsole;
|
||||||
WebApiWsLiveClass _webApiWsLive;
|
WebApiWsLiveClass _webApiWsLive;
|
||||||
|
WebApiDatabaseClass _webApiWsDatabase;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern WebApiClass WebApi;
|
extern WebApiClass WebApi;
|
||||||
27
include/WebApi_database.h
Normal file
27
include/WebApi_database.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
|
#define DATABASE_FILENAME "/database.bin"
|
||||||
|
|
||||||
|
class WebApiDatabaseClass {
|
||||||
|
public:
|
||||||
|
void init(AsyncWebServer* server);
|
||||||
|
void loop();
|
||||||
|
bool write(float energy);
|
||||||
|
|
||||||
|
struct Data {
|
||||||
|
uint8_t tm_year;
|
||||||
|
uint8_t tm_mon;
|
||||||
|
uint8_t tm_mday;
|
||||||
|
uint8_t tm_hour;
|
||||||
|
float energy;
|
||||||
|
};
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onDatabase(AsyncWebServerRequest* request);
|
||||||
|
|
||||||
|
AsyncWebServer* _server;
|
||||||
|
};
|
||||||
@ -6,8 +6,10 @@
|
|||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include "MqttSettings.h"
|
#include "MqttSettings.h"
|
||||||
#include <Hoymiles.h>
|
#include <Hoymiles.h>
|
||||||
|
#include "WebApi_database.h"
|
||||||
|
|
||||||
MqttHandleInverterTotalClass MqttHandleInverterTotal;
|
MqttHandleInverterTotalClass MqttHandleInverterTotal;
|
||||||
|
WebApiDatabaseClass database;
|
||||||
|
|
||||||
void MqttHandleInverterTotalClass::init()
|
void MqttHandleInverterTotalClass::init()
|
||||||
{
|
{
|
||||||
@ -73,5 +75,7 @@ void MqttHandleInverterTotalClass::loop()
|
|||||||
MqttSettings.publish("dc/is_valid", String(totalReachable));
|
MqttSettings.publish("dc/is_valid", String(totalReachable));
|
||||||
|
|
||||||
_lastPublish.set(Configuration.get().Mqtt_PublishInterval * 1000);
|
_lastPublish.set(Configuration.get().Mqtt_PublishInterval * 1000);
|
||||||
|
|
||||||
|
database.write(totalAcYieldTotal); // write value to database
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,6 +36,7 @@ void WebApiClass::init()
|
|||||||
_webApiWebapp.init(&_server);
|
_webApiWebapp.init(&_server);
|
||||||
_webApiWsConsole.init(&_server);
|
_webApiWsConsole.init(&_server);
|
||||||
_webApiWsLive.init(&_server);
|
_webApiWsLive.init(&_server);
|
||||||
|
_webApiWsDatabase.init(&_server);
|
||||||
|
|
||||||
_server.begin();
|
_server.begin();
|
||||||
}
|
}
|
||||||
@ -60,6 +61,7 @@ void WebApiClass::loop()
|
|||||||
_webApiWebapp.loop();
|
_webApiWebapp.loop();
|
||||||
_webApiWsConsole.loop();
|
_webApiWsConsole.loop();
|
||||||
_webApiWsLive.loop();
|
_webApiWsLive.loop();
|
||||||
|
_webApiWsDatabase.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WebApiClass::checkCredentials(AsyncWebServerRequest* request)
|
bool WebApiClass::checkCredentials(AsyncWebServerRequest* request)
|
||||||
|
|||||||
91
src/WebApi_database.cpp
Normal file
91
src/WebApi_database.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2023 Ralf Bauer and others
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "WebApi_database.h"
|
||||||
|
#include "MessageOutput.h"
|
||||||
|
#include "WebApi.h"
|
||||||
|
#include "defaults.h"
|
||||||
|
#include <AsyncJson.h>
|
||||||
|
#include <LittleFS.h>
|
||||||
|
|
||||||
|
void WebApiDatabaseClass::init(AsyncWebServer* server)
|
||||||
|
{
|
||||||
|
using std::placeholders::_1;
|
||||||
|
|
||||||
|
_server = server;
|
||||||
|
_server->on("/api/database", HTTP_GET, std::bind(&WebApiDatabaseClass::onDatabase, this, _1));
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiDatabaseClass::loop()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool WebApiDatabaseClass::write(float energy)
|
||||||
|
{
|
||||||
|
static uint8_t old_hour = 255;
|
||||||
|
static float old_energy = 0.0;
|
||||||
|
|
||||||
|
//LittleFS.remove(DATABASE_FILENAME);
|
||||||
|
|
||||||
|
struct tm timeinfo;
|
||||||
|
if (!getLocalTime(&timeinfo, 5))
|
||||||
|
return(false);
|
||||||
|
if (timeinfo.tm_hour == old_hour)
|
||||||
|
return(false);
|
||||||
|
if (energy <= old_energy)
|
||||||
|
return(false);
|
||||||
|
|
||||||
|
struct Data d;
|
||||||
|
d.tm_hour = old_hour = timeinfo.tm_hour;
|
||||||
|
d.tm_year = timeinfo.tm_year - 100; // year counting from 2000
|
||||||
|
d.tm_mon = timeinfo.tm_mon + 1;
|
||||||
|
d.tm_mday = timeinfo.tm_mday;
|
||||||
|
d.energy = old_energy = energy;
|
||||||
|
|
||||||
|
File f = LittleFS.open(DATABASE_FILENAME, "a");
|
||||||
|
if (!f) {
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
f.write((const uint8_t*)&d, sizeof(Data));
|
||||||
|
f.close();
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WebApiDatabaseClass::onDatabase(AsyncWebServerRequest* request)
|
||||||
|
{
|
||||||
|
if (!WebApi.checkCredentialsReadonly(request)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
File f = LittleFS.open(DATABASE_FILENAME, "r", false);
|
||||||
|
if (!f) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Data d;
|
||||||
|
|
||||||
|
AsyncJsonResponse* response = new AsyncJsonResponse(true, 40000U);
|
||||||
|
JsonArray root = response->getRoot();
|
||||||
|
|
||||||
|
while (f.read((uint8_t*)&d, sizeof(Data))) {
|
||||||
|
JsonArray nested = root.createNestedArray();
|
||||||
|
nested.add(d.tm_year);
|
||||||
|
nested.add(d.tm_mon);
|
||||||
|
nested.add(d.tm_mday);
|
||||||
|
nested.add(d.tm_hour);
|
||||||
|
nested.add(d.energy);
|
||||||
|
}
|
||||||
|
f.close();
|
||||||
|
|
||||||
|
response->setLength();
|
||||||
|
request->send(response);
|
||||||
|
|
||||||
|
} catch (std::bad_alloc& bad_alloc) {
|
||||||
|
MessageOutput.printf("Call to /api/database temporarely out of resources. Reason: \"%s\".\r\n", bad_alloc.what());
|
||||||
|
|
||||||
|
WebApi.sendTooManyRequests(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user