mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 18:23:03 +05:00
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.
72 lines
2.1 KiB
C++
72 lines
2.1 KiB
C++
#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;
|
|
}
|
|
};
|