Value.maxAgeSeconds

This commit is contained in:
Patrick Haßel 2025-01-07 10:40:33 +01:00
parent 32401a6560
commit 8418821e77
8 changed files with 62 additions and 38 deletions

View File

@ -9,6 +9,10 @@
class NodeHeizung final : public Node { class NodeHeizung final : public Node {
const unsigned long MAX_AGE_SECONDS = 10;
const unsigned long OVERDUE_SECONDS = 60;
DHT22 keller; DHT22 keller;
Max6675Sensor abgas; Max6675Sensor abgas;
@ -37,19 +41,19 @@ class NodeHeizung final : public Node {
public: public:
NodeHeizung() : keller(13, "keller", 0.5, 2, 0.5, 60), NodeHeizung() : keller(13, "keller", 0.5, 2, 0.5, MAX_AGE_SECONDS, OVERDUE_SECONDS),
abgas(27, 26, 25, "heizung/abgas", 2, 60), abgas(27, 26, 25, "heizung/abgas", 2, MAX_AGE_SECONDS, OVERDUE_SECONDS),
dallas(14), dallas(14),
pufferVorlauf(dallas, 0xAA0121125E4A7528, "heizung/puffer/vorlauf", 1.0, 60), pufferVorlauf(dallas, 0xAA0121125E4A7528, "heizung/puffer/vorlauf", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
pufferRuecklauf(dallas, 0x3E0121126A163B28, "heizung/puffer/ruecklauf", 1.0, 60), pufferRuecklauf(dallas, 0x3E0121126A163B28, "heizung/puffer/ruecklauf", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
pufferEingang(dallas, 0x0201211240EE3128, "heizung/puffer/eingang", 1.0, 60), pufferEingang(dallas, 0x0201211240EE3128, "heizung/puffer/eingang", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
pufferSpeicher(dallas, 0x1001211233D3A428, "heizung/puffer/speicher", 1.0, 60), pufferSpeicher(dallas, 0x1001211233D3A428, "heizung/puffer/speicher", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
pufferAusgang(dallas, 0xE80121126FFD9328, "heizung/puffer/ausgang", 1.0, 60), pufferAusgang(dallas, 0xE80121126FFD9328, "heizung/puffer/ausgang", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
pufferZirkulation(dallas, 0x540121124A48FD28, "heizung/puffer/zirkulation", 1.0, 60), pufferZirkulation(dallas, 0x540121124A48FD28, "heizung/puffer/zirkulation", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
heizkreisVorlauf(dallas, 0x330121126F984728, "heizung/heizkreis/vorlauf", 1.0, 60), heizkreisVorlauf(dallas, 0x330121126F984728, "heizung/heizkreis/vorlauf", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
heizkreisRuecklauf(dallas, 0x4201211270337B28, "heizung/heizkreis/ruecklauf", 1.0, 60), heizkreisRuecklauf(dallas, 0x4201211270337B28, "heizung/heizkreis/ruecklauf", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
abwasser(dallas, 0x810121126EE53828, "abwasser", 1.0, 60), abwasser(dallas, 0x810121126EE53828, "abwasser", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
reserve(dallas, 0x0D0121126716CF28, "__RESERVE__", 1.0, 60) { reserve(dallas, 0x0D0121126716CF28, "__RESERVE__", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS) {
// //
}; };

View File

@ -10,6 +10,10 @@
class NodeTest final : public Node { class NodeTest final : public Node {
const unsigned long MAX_AGE_SECONDS = 10;
const unsigned long OVERDUE_SECONDS = 60;
Dallas dallas; Dallas dallas;
DallasSensor test; DallasSensor test;
@ -19,8 +23,8 @@ class NodeTest final : public Node {
public: public:
NodeTest() : dallas(27), NodeTest() : dallas(27),
test(dallas, 0xC90417C1C5C0FF28, "test/ds18b20", 1.0, 60), test(dallas, 0xC90417C1C5C0FF28, "test/ds18b20", 1.0, MAX_AGE_SECONDS, OVERDUE_SECONDS),
testraum(26, "test/dht22", 0.5, 2, 1, 605) { testraum(26, "test/dht22", 0.5, 2, 1, MAX_AGE_SECONDS, OVERDUE_SECONDS) {
// //
}; };

View File

@ -22,12 +22,13 @@ public:
const double temperatureThreshold, const double temperatureThreshold,
const double humidityRelativeThreshold, const double humidityRelativeThreshold,
const double humidityAbsoluteThreshold, const double humidityAbsoluteThreshold,
const unsigned long dueSeconds const unsigned long maxAgeSeconds,
const unsigned long overdueSeconds
) : Sensor(name), ) : Sensor(name),
sensor(pin, DHT_TYPE_22), sensor(pin, DHT_TYPE_22),
temperature("temperature", temperatureThreshold, dueSeconds), temperature(name, "temperature", temperatureThreshold, maxAgeSeconds, overdueSeconds),
humidityRelative("humidityRelative", humidityRelativeThreshold, dueSeconds), humidityRelative(name, "humidityRelative", humidityRelativeThreshold, maxAgeSeconds, overdueSeconds),
humidityAbsolute("humidityAbsolute", humidityAbsoluteThreshold, dueSeconds) { humidityAbsolute(name, "humidityAbsolute", humidityAbsoluteThreshold, maxAgeSeconds, overdueSeconds) {
// //
} }
@ -48,7 +49,7 @@ public:
json[name][humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue(); json[name][humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue();
} }
bool isDue() const override { bool isDue() override {
return temperature.isDue() || humidityRelative.isDue() || humidityAbsolute.isDue(); return temperature.isDue() || humidityRelative.isDue() || humidityAbsolute.isDue();
} }

View File

@ -5,7 +5,7 @@
#include "OneWire.h" #include "OneWire.h"
#include "DallasTemperature.h" #include "DallasTemperature.h"
#define DALLAS_TIMEOUT_MILLISECONDS 3000 #define DALLAS_INTERVAL_MILLISECONDS 2000
class Dallas { class Dallas {
@ -29,7 +29,7 @@ public:
} }
void loop() { void loop() {
if (lastMillis == 0 || millis() - lastMillis > DALLAS_TIMEOUT_MILLISECONDS) { if (lastMillis == 0 || millis() - lastMillis > DALLAS_INTERVAL_MILLISECONDS) {
if (converting) { if (converting) {
error("Dallas timeout!"); error("Dallas timeout!");
} }

View File

@ -21,11 +21,12 @@ public:
const uint64_t address, const uint64_t address,
const char *name, const char *name,
const double threshold, const double threshold,
const unsigned long dueSeconds const unsigned long maxAgeSeconds,
const unsigned long overdueSeconds
) : Sensor(name), ) : Sensor(name),
sensors(sensors), sensors(sensors),
address(address), address(address),
temperature("temperature", threshold, dueSeconds) { temperature(name, "temperature", threshold, maxAgeSeconds, overdueSeconds) {
// //
} }
@ -40,7 +41,7 @@ public:
json[name][temperature.getName()] = temperature.getCurrentValue(); json[name][temperature.getName()] = temperature.getCurrentValue();
} }
bool isDue() const override { bool isDue() override {
return temperature.isDue(); return temperature.isDue();
} }

View File

@ -22,10 +22,11 @@ public:
const int8_t pinCLK, const int8_t pinCLK,
const char *name, const char *name,
const double threshold, const double threshold,
const unsigned long dueSeconds const unsigned long maxAgeSeconds,
const unsigned long overdueSeconds
) : Sensor(name), ) : Sensor(name),
sensor(pinCLK, pinCS, pinMISO), sensor(pinCLK, pinCS, pinMISO),
temperature("temperature", threshold, dueSeconds) { temperature(name, "temperature", threshold, maxAgeSeconds, overdueSeconds) {
// //
} }
@ -39,7 +40,7 @@ public:
json[name][temperature.getName()] = temperature.getCurrentValue(); json[name][temperature.getName()] = temperature.getCurrentValue();
} }
bool isDue() const override { bool isDue() override {
return temperature.isDue(); return temperature.isDue();
} }

View File

@ -21,7 +21,7 @@ public:
virtual void toJson(JsonDocument& json) = 0; virtual void toJson(JsonDocument& json) = 0;
virtual bool isDue() const = 0; virtual bool isDue() = 0;
virtual void markSent() = 0; virtual void markSent() = 0;

View File

@ -1,49 +1,62 @@
#ifndef VALUE_H #ifndef VALUE_H
#define VALUE_H #define VALUE_H
#include <patrix/log.h>
class Value { class Value {
const char *parent;
const char *name; const char *name;
double threshold; double threshold;
unsigned long dueSeconds; unsigned long maxAgeMillis;
unsigned long overdueSeconds;
double currentValue = NAN; double currentValue = NAN;
unsigned long currentInterval = 0; unsigned long currentMillis = 0;
double sentValue = NAN; double sentValue = NAN;
unsigned long sentInterval = 0; time_t sentInterval = 0;
public: public:
Value( Value(
const char *parent,
const char *name, const char *name,
const double threshold, const double threshold,
const unsigned long dueSeconds const unsigned long maxAgeSeconds,
) : name(name), const unsigned long overdueSeconds
) : parent(parent),
name(name),
threshold(threshold), threshold(threshold),
dueSeconds(dueSeconds) { maxAgeMillis(maxAgeSeconds * 1000),
overdueSeconds(overdueSeconds) {
// //
} }
void update(const double value) { void update(const double value) {
currentValue = value; currentValue = value;
currentInterval = time(nullptr) / dueSeconds; currentMillis = millis();
} }
bool isDue() const { bool isDue() {
if (!isnan(currentValue) && millis() - currentMillis > maxAgeMillis) {
warn("Value too old: %s/%s", parent, name);
update(NAN);
}
const auto dueToNAN = isnan(currentValue) != isnan(sentValue); const auto dueToNAN = isnan(currentValue) != isnan(sentValue);
const auto dueToThreshold = abs(sentValue - currentValue) >= threshold; const auto dueToThreshold = abs(sentValue - currentValue) >= threshold;
const auto dueToTime = currentInterval != sentInterval; const auto dueToTime = sentInterval != time(nullptr) / overdueSeconds;
return dueToNAN || dueToThreshold || dueToTime; return dueToNAN || dueToThreshold || dueToTime;
} }
void markSent() { void markSent() {
sentValue = currentValue; sentValue = currentValue;
sentInterval = currentInterval; sentInterval = time(nullptr) / overdueSeconds;
} }
const char *getName() const { const char *getName() const {