mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
feat: publish MQTT field changes with periodic refresh
Track 89 settings and state fields with partial structure snapshots and a fixed-capacity map of publication timestamps. Publish changed or expired values in bounded passes, updating snapshots only after successful queue acceptance and keeping failed fields eligible for retry. Refresh fields after reconnecting and affected temperatures after unit or Equitherm mode changes. Keep the status heartbeat independent, cache DHW limits for MQTT and API consumers, and clear non-finite retained readings. Default the forced publication interval to 60 seconds and accept 15 to 3600 seconds in the API and UI. Set Home Assistant state expiration to twice this interval and refresh discovery when the interval changes. Remove network connection and dedicated fault/diagnostic availability publications. Use existing active states and RSSI for HA availability.
This commit is contained in:
1 parent
a629589122
commit
d40b7cb85a
8 files changed
+1028
-164
No files matched your search
+3
-1
@@ -116,7 +116,7 @@ struct MqttSettings {
|
|||||||
char user[33] = DEFAULT_MQTT_USER;
|
char user[33] = DEFAULT_MQTT_USER;
|
||||||
char password[33] = DEFAULT_MQTT_PASSWORD;
|
char password[33] = DEFAULT_MQTT_PASSWORD;
|
||||||
char prefix[33] = DEFAULT_MQTT_PREFIX;
|
char prefix[33] = DEFAULT_MQTT_PREFIX;
|
||||||
unsigned short interval = 5;
|
unsigned short interval = 60;
|
||||||
bool homeAssistantDiscovery = DEFAULT_HA_DISCOVERY_ENABLED;
|
bool homeAssistantDiscovery = DEFAULT_HA_DISCOVERY_ENABLED;
|
||||||
char homeAssistantDiscoveryPrefix[33] = DEFAULT_HA_DISCOVERY_PREFIX;
|
char homeAssistantDiscoveryPrefix[33] = DEFAULT_HA_DISCOVERY_PREFIX;
|
||||||
};
|
};
|
||||||
@@ -285,6 +285,8 @@ struct Variables {
|
|||||||
float targetTemp = 0.0f;
|
float targetTemp = 0.0f;
|
||||||
float currentTemp = 0.0f;
|
float currentTemp = 0.0f;
|
||||||
float returnTemp = 0.0f;
|
float returnTemp = 0.0f;
|
||||||
|
uint8_t minTemp = 0;
|
||||||
|
uint8_t maxTemp = 0;
|
||||||
} dhw;
|
} dhw;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstring>
|
||||||
|
#include <string_view>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
template <typename Structure>
|
||||||
|
class StructSnapshot {
|
||||||
|
public:
|
||||||
|
explicit StructSnapshot(const Structure& source) : source(source), snapshot(source) {}
|
||||||
|
|
||||||
|
// Compare the live field with the corresponding field in the private snapshot.
|
||||||
|
template <typename Field>
|
||||||
|
bool equals(const Field* field, float tolerance = 0.0001f) const {
|
||||||
|
const auto index = offset(field);
|
||||||
|
if (index >= sizeof(Structure) || sizeof(Field) > sizeof(Structure) - index) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto* previous = reinterpret_cast<const Field*>(
|
||||||
|
reinterpret_cast<const unsigned char*>(&snapshot) + index
|
||||||
|
);
|
||||||
|
|
||||||
|
if constexpr (std::is_array_v<Field>) {
|
||||||
|
return text(*field) == text(*previous);
|
||||||
|
|
||||||
|
} else if constexpr (std::is_same_v<Field, float>) {
|
||||||
|
return !isFloatDifferent(*field, *previous, tolerance);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return *field == *previous;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Field>
|
||||||
|
void set(const Field* field, const Field* value = nullptr) {
|
||||||
|
static_assert(std::is_trivially_copyable_v<Field>, "Field must be trivially copyable");
|
||||||
|
const auto index = offset(field);
|
||||||
|
if (index >= sizeof(Structure) || sizeof(Field) > sizeof(Structure) - index) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::memcpy(reinterpret_cast<unsigned char*>(&snapshot) + index, value != nullptr ? value : field, sizeof(Field));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const Structure& source;
|
||||||
|
Structure snapshot;
|
||||||
|
|
||||||
|
// A fixed-size settings buffer may have no terminator or have unused trailing bytes.
|
||||||
|
template <std::size_t N>
|
||||||
|
static std::string_view text(const char (&value)[N]) {
|
||||||
|
const std::string_view buffer(value, N);
|
||||||
|
return buffer.substr(0, buffer.find('\0'));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t offset(const void* field) const {
|
||||||
|
const auto base = reinterpret_cast<std::uintptr_t>(&source);
|
||||||
|
const auto address = reinterpret_cast<std::uintptr_t>(field);
|
||||||
|
if (field == nullptr || address < base) {
|
||||||
|
return sizeof(Structure);
|
||||||
|
}
|
||||||
|
|
||||||
|
return address - base;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <initializer_list>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
template <std::size_t Capacity>
|
||||||
|
class StructTimestampMap {
|
||||||
|
public:
|
||||||
|
// Adding a key can move entries; use the reference before the next insertion.
|
||||||
|
unsigned long& get(const void* field) {
|
||||||
|
const auto end = times.begin() + count;
|
||||||
|
const auto entry = std::lower_bound(times.begin(), end, field, [](const auto& item, const void* ptr) {
|
||||||
|
return reinterpret_cast<std::uintptr_t>(item.first) < reinterpret_cast<std::uintptr_t>(ptr);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (entry != end && entry->first == field) {
|
||||||
|
return entry->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(count < times.size());
|
||||||
|
std::move_backward(entry, end, end + 1);
|
||||||
|
*entry = {field, 0};
|
||||||
|
++count;
|
||||||
|
return entry->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset(std::initializer_list<const void*> fields) {
|
||||||
|
for (const void* field : fields) {
|
||||||
|
get(field) = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetAll() {
|
||||||
|
for (std::size_t index = 0; index < count; ++index) {
|
||||||
|
times[index].second = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<std::pair<const void*, unsigned long>, Capacity> times{};
|
||||||
|
std::size_t count = 0;
|
||||||
|
};
|
||||||
+3
-3
@@ -1303,7 +1303,7 @@ SettingResult fromJson(JsonVariantConst src, MqttSettings& dst) {
|
|||||||
if (src[S_INTERVAL].is<unsigned short>()) {
|
if (src[S_INTERVAL].is<unsigned short>()) {
|
||||||
const auto value = src[S_INTERVAL].as<unsigned short>();
|
const auto value = src[S_INTERVAL].as<unsigned short>();
|
||||||
|
|
||||||
if (value >= 3 && value <= 60) {
|
if (value >= 15 && value <= 3600) {
|
||||||
result.accepted = true;
|
result.accepted = true;
|
||||||
if (value != dst.interval) {
|
if (value != dst.interval) {
|
||||||
dst.interval = value;
|
dst.interval = value;
|
||||||
@@ -3566,8 +3566,8 @@ JsonVariant toJson(const Variables& src, JsonVariant dst) {
|
|||||||
mDhw[S_TARGET_TEMP] = roundf((src.master.dhw.targetTemp == 0 ? 0 : convertTemp(src.master.dhw.targetTemp, UnitSystem::METRIC, settings.system.unitSystem)), 2);
|
mDhw[S_TARGET_TEMP] = roundf((src.master.dhw.targetTemp == 0 ? 0 : convertTemp(src.master.dhw.targetTemp, UnitSystem::METRIC, settings.system.unitSystem)), 2);
|
||||||
mDhw[S_CURRENT_TEMP] = roundf(convertTemp(src.master.dhw.currentTemp, UnitSystem::METRIC, settings.system.unitSystem), 2);
|
mDhw[S_CURRENT_TEMP] = roundf(convertTemp(src.master.dhw.currentTemp, UnitSystem::METRIC, settings.system.unitSystem), 2);
|
||||||
mDhw[S_RETURN_TEMP] = roundf(convertTemp(src.master.dhw.returnTemp, UnitSystem::METRIC, settings.system.unitSystem), 2);
|
mDhw[S_RETURN_TEMP] = roundf(convertTemp(src.master.dhw.returnTemp, UnitSystem::METRIC, settings.system.unitSystem), 2);
|
||||||
mDhw[S_MIN_TEMP] = convertTemp(settings.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem);
|
mDhw[S_MIN_TEMP] = convertTemp(src.master.dhw.minTemp, UnitSystem::METRIC, settings.system.unitSystem);
|
||||||
mDhw[S_MAX_TEMP] = convertTemp(settings.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem);
|
mDhw[S_MAX_TEMP] = convertTemp(src.master.dhw.maxTemp, UnitSystem::METRIC, settings.system.unitSystem);
|
||||||
|
|
||||||
master[S_NETWORK][S_CONNECTED] = isNetworkConnected();
|
master[S_NETWORK][S_CONNECTED] = isNetworkConnected();
|
||||||
master[S_NETWORK][S_RSSI] = src.network.rssi;
|
master[S_NETWORK][S_RSSI] = src.network.rssi;
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
#include <app.h>
|
#include <app.h>
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
std::unique_ptr<HA::Discovery> haDiscovery;
|
std::unique_ptr<HA::Discovery> haDiscovery;
|
||||||
|
|
||||||
bool publishHaEntity(HaEntity entityId, bool remove) {
|
bool publishHaEntity(HaEntity entityId, bool remove) {
|
||||||
std::unique_ptr<HA::Component> entity;
|
std::unique_ptr<HA::Component> entity;
|
||||||
const UnitSystem unit = settings.system.unitSystem;
|
const UnitSystem unit = settings.system.unitSystem;
|
||||||
const uint32_t stateExpiration = std::max<uint32_t>(60u, 2u * settings.mqtt.interval);
|
const uint32_t stateExpiration = 2u * settings.mqtt.interval;
|
||||||
|
|
||||||
switch (entityId) {
|
switch (entityId) {
|
||||||
case HA_ENTITY_HEATING_TURBO: {
|
case HA_ENTITY_HEATING_TURBO: {
|
||||||
@@ -498,7 +496,7 @@ bool publishHaEntity(HaEntity entityId, bool remove) {
|
|||||||
auto value = std::make_unique<HA::Value>("fault_code", "Fault code");
|
auto value = std::make_unique<HA::Value>("fault_code", "Fault code");
|
||||||
if (!remove) {
|
if (!remove) {
|
||||||
value->stateTopic = "~/state/slave/fault/code";
|
value->stateTopic = "~/state/slave/fault/code";
|
||||||
value->availabilityTopic = "~/state/slave/fault/available";
|
value->availabilityTopic = "~/state/slave/fault/active";
|
||||||
value->category = HA::Category::DIAGNOSTIC;
|
value->category = HA::Category::DIAGNOSTIC;
|
||||||
value->icon = "mdi:cog-box";
|
value->icon = "mdi:cog-box";
|
||||||
value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}";
|
value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}";
|
||||||
@@ -515,7 +513,7 @@ bool publishHaEntity(HaEntity entityId, bool remove) {
|
|||||||
auto value = std::make_unique<HA::Value>("diagnostic_code", "Diagnostic code");
|
auto value = std::make_unique<HA::Value>("diagnostic_code", "Diagnostic code");
|
||||||
if (!remove) {
|
if (!remove) {
|
||||||
value->stateTopic = "~/state/slave/diag/code";
|
value->stateTopic = "~/state/slave/diag/code";
|
||||||
value->availabilityTopic = "~/state/slave/diag/available";
|
value->availabilityTopic = "~/state/slave/diag/active";
|
||||||
value->category = HA::Category::DIAGNOSTIC;
|
value->category = HA::Category::DIAGNOSTIC;
|
||||||
value->icon = "mdi:information-box";
|
value->icon = "mdi:information-box";
|
||||||
value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}";
|
value->valueTemplate = "{{ '%02d (0x%02X)' | format(value | int, value | int) }}";
|
||||||
@@ -532,8 +530,8 @@ bool publishHaEntity(HaEntity entityId, bool remove) {
|
|||||||
auto gauge = std::make_unique<HA::Gauge>("rssi", "RSSI");
|
auto gauge = std::make_unique<HA::Gauge>("rssi", "RSSI");
|
||||||
if (!remove) {
|
if (!remove) {
|
||||||
gauge->stateTopic = "~/state/master/network/rssi";
|
gauge->stateTopic = "~/state/master/network/rssi";
|
||||||
gauge->availabilityTopic = "~/state/master/network/connection_type";
|
gauge->availabilityTopic = gauge->stateTopic;
|
||||||
gauge->availabilityTemplate = "{{ 'online' if value == 'wifi' else 'offline' }}";
|
gauge->availabilityTemplate = "{{ 'online' if value | int(0) < 0 else 'offline' }}";
|
||||||
gauge->category = HA::Category::DIAGNOSTIC;
|
gauge->category = HA::Category::DIAGNOSTIC;
|
||||||
gauge->deviceClass = "signal_strength";
|
gauge->deviceClass = "signal_strength";
|
||||||
gauge->icon = "mdi:signal";
|
gauge->icon = "mdi:signal";
|
||||||
|
|||||||
+896
-152
File diff suppressed because it is too large.
Load diff
@@ -342,6 +342,8 @@ static void updateMasterState() {
|
|||||||
settings.dhw.enabled &&
|
settings.dhw.enabled &&
|
||||||
!vars.master.dhw.overheat;
|
!vars.master.dhw.overheat;
|
||||||
vars.master.dhw.targetTemp = settings.dhw.target;
|
vars.master.dhw.targetTemp = settings.dhw.target;
|
||||||
|
vars.master.dhw.minTemp = settings.dhw.minTemp;
|
||||||
|
vars.master.dhw.maxTemp = settings.dhw.maxTemp;
|
||||||
|
|
||||||
// CH2 settings
|
// CH2 settings
|
||||||
vars.master.ch2.enabled =
|
vars.master.ch2.enabled =
|
||||||
|
|||||||
@@ -840,7 +840,7 @@
|
|||||||
|
|
||||||
<label>
|
<label>
|
||||||
<span data-i18n>settings.mqtt.interval</span>
|
<span data-i18n>settings.mqtt.interval</span>
|
||||||
<input type="number" inputmode="numeric" name="interval" min="3" max="60" step="1" required>
|
<input type="number" inputmode="numeric" name="interval" min="15" max="3600" step="1" required>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user