state json refactored
This commit is contained in:
parent
8418821e77
commit
60c9adf677
@ -36,10 +36,13 @@ public:
|
||||
dallas.loop();
|
||||
test.loop();
|
||||
testraum.loop();
|
||||
if (test.isDue() || testraum.isDue()) {
|
||||
const auto tt = test.isDue();
|
||||
const auto tr = testraum.isDue();
|
||||
if (tt || tr) {
|
||||
JsonDocument json;
|
||||
json["timestamp"] = time(nullptr);
|
||||
toJson(json);
|
||||
if (mqttPublish(WiFiClass::getHostname(), json)) {
|
||||
if (mqttPublish(WiFiClass::getHostname(), "state", json)) {
|
||||
test.markSent();
|
||||
testraum.markSent();
|
||||
}
|
||||
|
||||
@ -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];
|
||||
serializeJson(json, buffer);
|
||||
info(buffer);
|
||||
return mqtt.publish(topic, buffer);
|
||||
}
|
||||
|
||||
@ -5,6 +5,6 @@
|
||||
|
||||
void mqttLoop();
|
||||
|
||||
bool mqttPublish(const char *topic, const JsonDocument& json);
|
||||
bool mqttPublish(const char *topic0, const char *topic1, const JsonDocument& json);
|
||||
|
||||
#endif
|
||||
|
||||
@ -27,8 +27,8 @@ public:
|
||||
) : Sensor(name),
|
||||
sensor(pin, DHT_TYPE_22),
|
||||
temperature(name, "temperature", temperatureThreshold, maxAgeSeconds, overdueSeconds),
|
||||
humidityRelative(name, "humidityRelative", humidityRelativeThreshold, maxAgeSeconds, overdueSeconds),
|
||||
humidityAbsolute(name, "humidityAbsolute", humidityAbsoluteThreshold, maxAgeSeconds, overdueSeconds) {
|
||||
humidityRelative(name, "humidity/relative", humidityRelativeThreshold, maxAgeSeconds, overdueSeconds),
|
||||
humidityAbsolute(name, "humidity/absolute", humidityAbsoluteThreshold, maxAgeSeconds, overdueSeconds) {
|
||||
//
|
||||
}
|
||||
|
||||
@ -44,13 +44,16 @@ public:
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name][temperature.getName()] = temperature.getCurrentValue();
|
||||
json[name][humidityRelative.getName()] = humidityRelative.getCurrentValue();
|
||||
json[name][humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue();
|
||||
json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
|
||||
json[String(name) + "/" + humidityRelative.getName()] = humidityRelative.getCurrentValue();
|
||||
json[String(name) + "/" + humidityAbsolute.getName()] = humidityAbsolute.getCurrentValue();
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@ -38,7 +38,7 @@ public:
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name][temperature.getName()] = temperature.getCurrentValue();
|
||||
json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
|
||||
}
|
||||
|
||||
bool isDue() override {
|
||||
|
||||
@ -37,7 +37,7 @@ public:
|
||||
}
|
||||
|
||||
void toJson(JsonDocument& json) override {
|
||||
json[name][temperature.getName()] = temperature.getCurrentValue();
|
||||
json[String(name) + "/" + temperature.getName()] = temperature.getCurrentValue();
|
||||
}
|
||||
|
||||
bool isDue() override {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user