diff --git a/src/patrix/Node.h b/src/patrix/Node.h index a365d08..c812578 100644 --- a/src/patrix/Node.h +++ b/src/patrix/Node.h @@ -50,12 +50,17 @@ private: auto sensorIndex = 0; Sensor *sensor; while ((sensor = getSensor(sensorIndex++)) != nullptr) { - auto valueIndex = 0; - Value *value; - while ((value = sensor->getValue(valueIndex++)) != nullptr) { - if (value->loop()) { - cache.put(timestamp, cacheIndex++, value->getCurrentValue()); - } + sensor->loopBeforeValues(); + loopValues(timestamp, cacheIndex, sensor); + } + } + + void loopValues(const time_t timestamp, int& cacheIndex, Sensor *sensor) { + auto valueIndex = 0; + Value *value; + while ((value = sensor->getValue(valueIndex++)) != nullptr) { + if (value->loop()) { + cache.put(timestamp, cacheIndex++, value->getCurrentValue()); } } } diff --git a/src/patrix/sensor/Value.h b/src/patrix/sensor/Value.h index 9606004..8434bdd 100644 --- a/src/patrix/sensor/Value.h +++ b/src/patrix/sensor/Value.h @@ -52,15 +52,16 @@ public: update(NAN); } + const auto now = time(nullptr); const auto dueToNAN = isnan(currentValue) != isnan(sentValue); const auto dueToThreshold = abs(sentValue - currentValue) >= threshold; - const auto dueToTime = sentInterval != 0 && sentInterval != time(nullptr) / overdueSeconds; + const auto dueToTime = sentInterval != 0 && sentInterval != now / overdueSeconds; const auto changed = dueToNAN || dueToThreshold || dueToTime; if (changed) { mqttPublish(name + "/retain", String(currentValue), RETAIN); websocketSendAll(toJson(false)); sentValue = currentValue; - sentInterval = time(nullptr) / overdueSeconds; + sentInterval = now / overdueSeconds; } return changed; }