generic Cache

This commit is contained in:
Patrick Haßel 2024-04-15 13:50:00 +02:00
parent 9933a70ae3
commit 3ca74503d9
5 changed files with 113 additions and 58 deletions

View File

@ -3,8 +3,6 @@
#include <wifi.h>
#include <console.h>
#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();
}

View File

@ -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--;
}
}

View File

@ -2,9 +2,69 @@
#define SENSOR3_DATA_H
#include "base.h"
#include <ArduinoJson.h>
bool dataAdd(const char *name, const time_t timestamp, const double value);
struct IData {
void dataLoop();
virtual void toJson(JsonObject &json) const = 0;
};
template<typename Data>
using ExtendsIData = std::enable_if_t<std::is_base_of_v<IData, Data>>;
template<typename T, int size>
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<typename U = T, typename = ExtendsIData<U>>
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<JsonObject>();
bufferRead->data.toJson(data);
if (mqttPublishData(name, json)) {
bufferRead = (bufferRead - buffer + 1) % size + buffer;
dataCount--;
}
}
};
#endif

View File

@ -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;
}

View File

@ -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<Data, 500> 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();