From 1c07249b2ce076371c61a64480c4376e478e2264 Mon Sep 17 00:00:00 2001 From: Ralf Bauer Date: Fri, 19 May 2023 11:07:49 +0200 Subject: [PATCH 01/78] 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 --- include/WebApi.h | 2 + include/WebApi_database.h | 27 ++++++++++ src/MqttHandleInverterTotal.cpp | 4 ++ src/WebApi.cpp | 2 + src/WebApi_database.cpp | 91 +++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 include/WebApi_database.h create mode 100644 src/WebApi_database.cpp diff --git a/include/WebApi.h b/include/WebApi.h index ed1f386b2..bcad724f1 100644 --- a/include/WebApi.h +++ b/include/WebApi.h @@ -20,6 +20,7 @@ #include "WebApi_webapp.h" #include "WebApi_ws_console.h" #include "WebApi_ws_live.h" +#include "WebApi_database.h" #include class WebApiClass { @@ -56,6 +57,7 @@ class WebApiClass { WebApiWebappClass _webApiWebapp; WebApiWsConsoleClass _webApiWsConsole; WebApiWsLiveClass _webApiWsLive; + WebApiDatabaseClass _webApiWsDatabase; }; extern WebApiClass WebApi; \ No newline at end of file diff --git a/include/WebApi_database.h b/include/WebApi_database.h new file mode 100644 index 000000000..27b7cbd8f --- /dev/null +++ b/include/WebApi_database.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include +#include + +#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; +}; diff --git a/src/MqttHandleInverterTotal.cpp b/src/MqttHandleInverterTotal.cpp index a0c3a38c2..add8ca68a 100644 --- a/src/MqttHandleInverterTotal.cpp +++ b/src/MqttHandleInverterTotal.cpp @@ -6,8 +6,10 @@ #include "Configuration.h" #include "MqttSettings.h" #include +#include "WebApi_database.h" MqttHandleInverterTotalClass MqttHandleInverterTotal; +WebApiDatabaseClass database; void MqttHandleInverterTotalClass::init() { @@ -73,5 +75,7 @@ void MqttHandleInverterTotalClass::loop() MqttSettings.publish("dc/is_valid", String(totalReachable)); _lastPublish.set(Configuration.get().Mqtt_PublishInterval * 1000); + + database.write(totalAcYieldTotal); // write value to database } } diff --git a/src/WebApi.cpp b/src/WebApi.cpp index fca1b0880..9f1eda20b 100644 --- a/src/WebApi.cpp +++ b/src/WebApi.cpp @@ -36,6 +36,7 @@ void WebApiClass::init() _webApiWebapp.init(&_server); _webApiWsConsole.init(&_server); _webApiWsLive.init(&_server); + _webApiWsDatabase.init(&_server); _server.begin(); } @@ -60,6 +61,7 @@ void WebApiClass::loop() _webApiWebapp.loop(); _webApiWsConsole.loop(); _webApiWsLive.loop(); + _webApiWsDatabase.loop(); } bool WebApiClass::checkCredentials(AsyncWebServerRequest* request) diff --git a/src/WebApi_database.cpp b/src/WebApi_database.cpp new file mode 100644 index 000000000..129f1dfd7 --- /dev/null +++ b/src/WebApi_database.cpp @@ -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 +#include + +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); + } +} From abc4b8300b0b04ebd91520ec10861f0d8598064e Mon Sep 17 00:00:00 2001 From: Ralf Bauer Date: Sun, 21 May 2023 10:26:47 +0200 Subject: [PATCH 02/78] Added code for JS and HTML (as comment at the end of the file). This code is working and has to be integrated into the UI. --- src/WebApi_database.cpp | 92 ++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/src/WebApi_database.cpp b/src/WebApi_database.cpp index 129f1dfd7..67559a8ff 100644 --- a/src/WebApi_database.cpp +++ b/src/WebApi_database.cpp @@ -27,18 +27,23 @@ bool WebApiDatabaseClass::write(float energy) static uint8_t old_hour = 255; static float old_energy = 0.0; - //LittleFS.remove(DATABASE_FILENAME); + // 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); + if (!getLocalTime(&timeinfo, 5)) // get cuurent time + return (false); + if (timeinfo.tm_hour == old_hour) // must be new hour + return (false); + if (old_hour == 255) { // don't write to database after reboot + old_hour = timeinfo.tm_hour; + return (false); + } + if (energy <= old_energy) // enery must have increased + return (false); struct Data d; - d.tm_hour = old_hour = timeinfo.tm_hour; + d.tm_hour = old_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; @@ -46,11 +51,11 @@ bool WebApiDatabaseClass::write(float energy) File f = LittleFS.open(DATABASE_FILENAME, "a"); if (!f) { - return(false); + return (false); } f.write((const uint8_t*)&d, sizeof(Data)); f.close(); - return(true); + return (true); } void WebApiDatabaseClass::onDatabase(AsyncWebServerRequest* request) @@ -70,8 +75,8 @@ void WebApiDatabaseClass::onDatabase(AsyncWebServerRequest* request) AsyncJsonResponse* response = new AsyncJsonResponse(true, 40000U); JsonArray root = response->getRoot(); - while (f.read((uint8_t*)&d, sizeof(Data))) { - JsonArray nested = root.createNestedArray(); + while (f.read((uint8_t*)&d, sizeof(Data))) { // read from database + JsonArray nested = root.createNestedArray(); // create new nested array and copy data to array nested.add(d.tm_year); nested.add(d.tm_mon); nested.add(d.tm_mday); @@ -79,13 +84,70 @@ void WebApiDatabaseClass::onDatabase(AsyncWebServerRequest* request) 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); } } + + + +/* JS +const energy = JSON.parse('[[23,5,19,6,175.4089966],[23,5,19,10,175.7649994],[23,5,19,10,175.8939972],[23,5,19,10,175.904007],[23,5,19,10,175.9149933],[23,5,19,11,175.9669952],[23,5,19,11,175.973999],[23,5,19,12,176.1159973],[23,5,19,13,176.2900085],[23,5,19,14,176.4179993],[23,5,19,15,176.5429993],[23,5,19,16,176.6100006],[23,5,19,17,176.7269897],[23,5,19,18,176.8370056],[23,5,19,19,176.9060059],[23,5,19,20,176.9360046],[23,5,19,21,176.9459991],[23,5,20,5,176.9459991],[23,5,20,6,176.9470062],[23,5,20,7,176.9629974],[23,5,20,8,177.0270081],[23,5,20,9,177.0899963],[23,5,20,10,177.2389984],[23,5,20,11,177.4759979],[23,5,20,12,177.7310028],[23,5,20,13,178.0749817],[23,5,20,14,178.3970032],[23,5,20,15,178.7460022],[23,5,20,16,179.0829926],[23,5,20,17,179.3110046],[23,5,20,18,179.4849854],[23,5,20,19,179.5549927],[23,5,20,20,179.5899963]]') + +var end = new Date() +var start = new Date() +var interval = 1 +start.setDate(end.getDate() - interval) +start.setHours(start.getHours() - 1) +console.log(start.toLocaleString()) +console.log(end.toLocaleString()) + +google.charts.load('current', { + packages: ['corechart', 'bar'] +}); +google.charts.setOnLoadCallback(drawBasic); + +function drawBasic() { + var data = new google.visualization.DataTable(); + data.addColumn('datetime', 'Time'); + data.addColumn('number', 'Energy'); + var old_energy = energy[0][4] + energy.forEach((x) => { + var d = new Date(x[0] + 2000, x[1] - 1, x[2], x[3]) + if ((d >= start) && (d <= end)) { + data.addRow([d, (x[4] - old_energy) * 1000]) + } + old_energy = x[4] + }) + + var date_formatter = new google.visualization.DateFormat({ + pattern: "dd.MM.YY HH:mm" + }); + date_formatter.format(data, 0); + + var options = { + height: 500, + legend: { + position: 'none' + }, + hAxis: { + format: 'dd.MM.YY HH:mm', + slantedText: true + }, + vAxis: { + format: '# Wh' + } + } + + var chart = new google.visualization.ColumnChart(document.getElementById('chart_div')); + chart.draw(data, options); +} +*/ + +/* HTML + +
+*/ From a785074fa8ac6486d82f1c6574a2632d3f38fed5 Mon Sep 17 00:00:00 2001 From: Ralf Bauer Date: Sun, 21 May 2023 15:18:22 +0200 Subject: [PATCH 03/78] added new card to LiveView page with chart for last 25 hours --- webapp/index.html | 1 + webapp/src/components/DatabaseChart.vue | 84 +++++++++++++++++++++++++ webapp/src/locales/de.json | 3 + webapp/src/locales/en.json | 3 + webapp/src/locales/fr.json | 3 + webapp/src/views/HomeView.vue | 3 + 6 files changed, 97 insertions(+) create mode 100644 webapp/src/components/DatabaseChart.vue diff --git a/webapp/index.html b/webapp/index.html index 36236f188..9c0466a8a 100644 --- a/webapp/index.html +++ b/webapp/index.html @@ -4,6 +4,7 @@ + OpenDTU diff --git a/webapp/src/components/DatabaseChart.vue b/webapp/src/components/DatabaseChart.vue new file mode 100644 index 000000000..67ade66c4 --- /dev/null +++ b/webapp/src/components/DatabaseChart.vue @@ -0,0 +1,84 @@ + + + diff --git a/webapp/src/locales/de.json b/webapp/src/locales/de.json index 871b32403..a06a06742 100644 --- a/webapp/src/locales/de.json +++ b/webapp/src/locales/de.json @@ -289,6 +289,9 @@ "Value": "Wert", "Unit": "Einheit" }, + "databasechart": { + "LastDay": "Letzte 25 Stunden", + }, "invertertotalinfo": { "TotalYieldTotal": "Gesamtertrag Insgesamt", "TotalYieldDay": "Gesamtertrag Heute", diff --git a/webapp/src/locales/en.json b/webapp/src/locales/en.json index 130d948da..20c40041b 100644 --- a/webapp/src/locales/en.json +++ b/webapp/src/locales/en.json @@ -289,6 +289,9 @@ "Value": "Value", "Unit": "Unit" }, + "databasechart": { + "LastDay": "last 25 hours", + }, "invertertotalinfo": { "TotalYieldTotal": "Total Yield Total", "TotalYieldDay": "Total Yield Day", diff --git a/webapp/src/locales/fr.json b/webapp/src/locales/fr.json index eca44fae4..c056ff853 100644 --- a/webapp/src/locales/fr.json +++ b/webapp/src/locales/fr.json @@ -289,6 +289,9 @@ "Value": "Valeur", "Unit": "Unité" }, + "databasechart": { + "LastDay": "25 dernières heures", + }, "invertertotalinfo": { "TotalYieldTotal": "Rendement total", "TotalYieldDay": "Rendement du jour", diff --git a/webapp/src/views/HomeView.vue b/webapp/src/views/HomeView.vue index 08cf1cb77..19ac09583 100644 --- a/webapp/src/views/HomeView.vue +++ b/webapp/src/views/HomeView.vue @@ -109,6 +109,7 @@ + @@ -323,6 +324,7 @@ import DevInfo from '@/components/DevInfo.vue'; import EventLog from '@/components/EventLog.vue'; import HintView from '@/components/HintView.vue'; import InverterChannelInfo from "@/components/InverterChannelInfo.vue"; +import DatabaseChart from "@/components/DatabaseChart.vue"; import InverterTotalInfo from '@/components/InverterTotalInfo.vue'; import type { DevInfoStatus } from '@/types/DevInfoStatus'; import type { EventlogItems } from '@/types/EventlogStatus'; @@ -353,6 +355,7 @@ export default defineComponent({ EventLog, HintView, InverterChannelInfo, + DatabaseChart, InverterTotalInfo, BIconArrowCounterclockwise, BIconCheckCircleFill, From c26b10a719c5a75a5eba0d90ab92f871b2f7c153 Mon Sep 17 00:00:00 2001 From: Ralf Bauer Date: Sun, 21 May 2023 15:49:11 +0200 Subject: [PATCH 04/78] fixed formatting of the DatabaseChart --- webapp/src/components/DatabaseChart.vue | 1 + webapp/src/views/HomeView.vue | 82 ++++++++++++++----------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/webapp/src/components/DatabaseChart.vue b/webapp/src/components/DatabaseChart.vue index 67ade66c4..ffb2e20c1 100644 --- a/webapp/src/components/DatabaseChart.vue +++ b/webapp/src/components/DatabaseChart.vue @@ -63,6 +63,7 @@ export default defineComponent({ var options = { height: 600, + chartArea:{ top:25, width:'85%', height:'80%' }, legend: { position: 'none' }, diff --git a/webapp/src/views/HomeView.vue b/webapp/src/views/HomeView.vue index 19ac09583..127478a7c 100644 --- a/webapp/src/views/HomeView.vue +++ b/webapp/src/views/HomeView.vue @@ -18,15 +18,14 @@
+ 'col-sm-9 col-md-10': inverterData.length > 1, + 'col-sm-12 col-md-12': inverterData.length == 1 + }">
-
{{ $t('home.CurrentLimit') }}{{ $n(inverter.limit_relative / 100, 'percent') }}
- {{ $t('home.DataAge') }} {{ $t('home.Seconds', {'val': $n(inverter.data_age) }) }} + {{ $t('home.DataAge') }} {{ $t('home.Seconds', { 'val': $n(inverter.data_age) }) }} @@ -56,7 +55,8 @@