state json refactored

This commit is contained in:
Patrick Haßel 2025-01-07 11:10:05 +01:00
parent 8418821e77
commit 60c9adf677
6 changed files with 21 additions and 13 deletions

View File

@ -36,10 +36,13 @@ public:
dallas.loop(); dallas.loop();
test.loop(); test.loop();
testraum.loop(); testraum.loop();
if (test.isDue() || testraum.isDue()) { const auto tt = test.isDue();
const auto tr = testraum.isDue();
if (tt || tr) {
JsonDocument json; JsonDocument json;
json["timestamp"] = time(nullptr);
toJson(json); toJson(json);
if (mqttPublish(WiFiClass::getHostname(), json)) { if (mqttPublish(WiFiClass::getHostname(), "state", json)) {
test.markSent(); test.markSent();
testraum.markSent(); testraum.markSent();
} }

View File

@ -46,9 +46,11 @@ void mqttLoop() {
} }
} }
bool mqttPublish(const char *topic, const JsonDocument& json) { bool mqttPublish(const char *topic0, const char *topic1, const JsonDocument& json) {
char topic[256];
snprintf(topic, sizeof(topic), "%s/%s", topic0, topic1);
char buffer[512]; char buffer[512];
serializeJson(json, buffer); serializeJson(json, buffer);
info(buffer);
return mqtt.publish(topic, buffer); return mqtt.publish(topic, buffer);
} }

View File

@ -5,6 +5,6 @@
void mqttLoop(); void mqttLoop();
bool mqttPublish(const char *topic, const JsonDocument& json); bool mqttPublish(const char *topic0, const char *topic1, const JsonDocument& json);
#endif #endif

View File

@ -27,8 +27,8 @@ public:
) : Sensor(name), ) : Sensor(name),
sensor(pin, DHT_TYPE_22), sensor(pin, DHT_TYPE_22),
temperature(name, "temperature", temperatureThreshold, maxAgeSeconds, overdueSeconds), temperature(name, "temperature", temperatureThreshold, maxAgeSeconds, overdueSeconds),
humidityRelative(name, "humidityRelative", humidityRelativeThreshold, maxAgeSeconds, overdueSeconds), humidityRelative(name, "humidity/relative", humidityRelativeThreshold, maxAgeSeconds, overdueSeconds),
humidityAbsolute(name, "humidityAbsolute", humidityAbsoluteThreshold, maxAgeSeconds, overdueSeconds) { humidityAbsolute(name, "humidity/absolute", humidityAbsoluteThreshold, maxAgeSeconds, overdueSeconds) {
// //
} }
@ -44,13 +44,16 @@ public:
} }
void toJson(JsonDocument& json) override { void toJson(JsonDocument& json) override {
json[name][temperature.getName()] = temperature.getCurrentValue(); json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
json[name][humidityRelative.getName()] = humidityRelative.getCurrentValue(); json[String(name) + "/" + humidityRelative.getName()] = humidityRelative.getCurrentValue();
json[name][humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue(); json[String(name) + "/" + humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue();
} }
bool isDue() override { bool isDue() override {
return temperature.isDue() || humidityRelative.isDue() || humidityAbsolute.isDue(); const auto tp = temperature.isDue();
const auto hr = humidityRelative.isDue();
const auto ha = humidityAbsolute.isDue();
return tp || hr || ha;
} }
void markSent() override { void markSent() override {

View File

@ -38,7 +38,7 @@ public:
} }
void toJson(JsonDocument& json) override { void toJson(JsonDocument& json) override {
json[name][temperature.getName()] = temperature.getCurrentValue(); json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
} }
bool isDue() override { bool isDue() override {

View File

@ -37,7 +37,7 @@ public:
} }
void toJson(JsonDocument& json) override { void toJson(JsonDocument& json) override {
json[name][temperature.getName()] = temperature.getCurrentValue(); json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
} }
bool isDue() override { bool isDue() override {