From 3ca74503d94d419d0ff2984012e97912b6626c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Mon, 15 Apr 2024 13:50:00 +0200 Subject: [PATCH] generic Cache --- lib/patrix/Patrix.cpp | 3 -- lib/patrix/data.cpp | 45 ---------------------- lib/patrix/data.h | 64 ++++++++++++++++++++++++++++++- lib/patrix/sensors/DallasSensor.h | 15 ++++---- src/Fermenter.cpp | 44 ++++++++++++++++++++- 5 files changed, 113 insertions(+), 58 deletions(-) delete mode 100644 lib/patrix/data.cpp diff --git a/lib/patrix/Patrix.cpp b/lib/patrix/Patrix.cpp index 31adc27..9875380 100644 --- a/lib/patrix/Patrix.cpp +++ b/lib/patrix/Patrix.cpp @@ -3,8 +3,6 @@ #include #include #include "mqtt.h" -#include "log.h" -#include "data.h" #include "config.h" void setup() { @@ -20,6 +18,5 @@ void loop() { configLoop(); wifiLoop(); mqttLoop(); - dataLoop(); patrixLoop(); } \ No newline at end of file diff --git a/lib/patrix/data.cpp b/lib/patrix/data.cpp deleted file mode 100644 index 6205816..0000000 --- a/lib/patrix/data.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "data.h" -#include "ArduinoJson.h" -#include "mqtt.h" -#include "wifi.h" - -#define ENTRY_COUNT 1500 - -struct Entry { - const char *name = ""; - time_t timestamp = 0; - double value = NAN; -}; - -Entry data[ENTRY_COUNT]; - -Entry *dataRead = data; - -Entry *dataWrite = data; - -size_t dataCount = 0; - -bool dataAdd(const char *name, const time_t timestamp, const double value) { - if (dataCount >= ENTRY_COUNT) { - return false; - } - dataWrite->name = name; - dataWrite->timestamp = timestamp; - dataWrite->value = value; - dataWrite = (dataWrite - data + 1) % ENTRY_COUNT + data; - dataCount++; - return true; -} - -void dataLoop() { - if (dataCount == 0 || !isTimeSet()) { - return; - } - JsonDocument json; - json["timestamp"] = correctTime(dataRead->timestamp); - json["value"] = dataRead->value; - if (mqttPublishData(dataRead->name, json)) { - dataRead = (dataRead - data + 1) % ENTRY_COUNT + data; - dataCount--; - } -} \ No newline at end of file diff --git a/lib/patrix/data.h b/lib/patrix/data.h index bdd75a9..49061fb 100644 --- a/lib/patrix/data.h +++ b/lib/patrix/data.h @@ -2,9 +2,69 @@ #define SENSOR3_DATA_H #include "base.h" +#include -bool dataAdd(const char *name, const time_t timestamp, const double value); +struct IData { -void dataLoop(); + virtual void toJson(JsonObject &json) const = 0; + +}; + +template +using ExtendsIData = std::enable_if_t>; + +template +class Cache { + +private: + + struct Entry { + time_t timestamp = 0; + T data; + }; + + const char *name; + + Entry buffer[size]; + + Entry *bufferRead = buffer; + + Entry *bufferWrite = buffer; + + size_t dataCount = 0; + +public: + + template> + explicit Cache(const char *name) : name(name) { + // - + } + + bool add(const time_t timestamp, const T &data) { + if (dataCount >= size) { + return false; + } + bufferWrite->timestamp = timestamp; + bufferWrite->data = data; + bufferWrite = (bufferWrite - buffer + 1) % size + buffer; + dataCount++; + return true; + } + + void loop() { + if (dataCount == 0 || !isTimeSet()) { + return; + } + JsonDocument json; + json["timestamp"] = correctTime(bufferRead->timestamp); + JsonObject data = json["data"].to(); + bufferRead->data.toJson(data); + if (mqttPublishData(name, json)) { + bufferRead = (bufferRead - buffer + 1) % size + buffer; + dataCount--; + } + } + +}; #endif diff --git a/lib/patrix/sensors/DallasSensor.h b/lib/patrix/sensors/DallasSensor.h index 34d9040..fbe197d 100644 --- a/lib/patrix/sensors/DallasSensor.h +++ b/lib/patrix/sensors/DallasSensor.h @@ -47,7 +47,7 @@ public: // - } - void loop() { + bool loop() { const time_t now = getTime(); if (now - lastTimestamp > timeoutSec) { lastTimestamp = 0; @@ -61,7 +61,6 @@ public: if (doPublish) { lastSentValue = value; lastSentMillis = millisNow; - dataAdd(name, timestamp, value); } lastValue = value; lastTimestamp = timestamp; @@ -69,26 +68,28 @@ public: lastValidValue = value; lastValidTimestamp = timestamp; } + return doPublish; } + return false; } - const char *getName() const { + [[nodiscard]] const char *getName() const { return name; } - double getLastValue() const { + [[nodiscard]] double getLastValue() const { return lastValue; } - time_t getLastTimestamp() const { + [[nodiscard]] time_t getLastTimestamp() const { return lastTimestamp; } - double getLastValidValue() const { + [[nodiscard]] double getLastValidValue() const { return lastValidValue; } - time_t getLastValidTimestamp() const { + [[nodiscard]] time_t getLastValidTimestamp() const { return lastValidTimestamp; } diff --git a/src/Fermenter.cpp b/src/Fermenter.cpp index c74fa97..926f9b6 100644 --- a/src/Fermenter.cpp +++ b/src/Fermenter.cpp @@ -42,6 +42,37 @@ double temperatureCurrent = NAN; double heaterPWM = 0; +struct Data : IData { + double temperature; + double target; + double p; + double i; + double d; + + Data() { + temperature = NAN; + target = NAN; + p = NAN; + i = NAN; + d = NAN; + } + + Data(double temperature, double target, double p, double i, double d) : temperature(temperature), target(target), p(p), i(i), d(d) { + // - + } + + void toJson(JsonObject &json) const override { + json["temperature"] = temperature; + json["target"] = target; + json["p"] = p; + json["i"] = i; + json["d"] = d; + } + +}; + +Cache cache("data"); + void writeDecimal(int *digit, double value); void displayLoop(); @@ -59,7 +90,18 @@ void patrixSetup() { void patrixLoop() { dallas.loop(); - sensor.loop(); + + if (sensor.loop()) { + Data data = { + temperatureCurrent, + temperatureTarget, + proportional, + integral, + derivative, + }; + cache.add(sensor.getLastTimestamp(), data); + } + cache.loop(); displayLoop();