Skip to content

Commit

Permalink
feat(filter): reenable filters for new protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
tspopp committed Nov 20, 2024
1 parent 3d1e751 commit c86e828
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 90 deletions.
1 change: 1 addition & 0 deletions AquaMQTT/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
SeparateDefinitionBlocks: Always
17 changes: 14 additions & 3 deletions AquaMQTT/include/task/MQTTTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@

#include "SimpleKalmanFilter.h"
#include "config/Configuration.h"
#include "message/MessageConstants.h"
#include "message/IMainMessage.h"

#include "message/MessageConstants.h"

namespace aquamqtt
{
Expand Down Expand Up @@ -53,10 +52,15 @@ class MQTTTask
uint8_t* mLastProcessedMainMessage;

SimpleKalmanFilter mEvaporatorLowerAirTempFilter;
float mEvaporatorLowerAirTempFiltered;
SimpleKalmanFilter mEvaporatorUpperAirTempFilter;
float mEvaporatorUpperAirTempFiltered;
SimpleKalmanFilter mAirTempFilter;
float mAirTempFiltered;
SimpleKalmanFilter mHotWaterTempFilter;
float mHotWaterTempFiltered;
SimpleKalmanFilter mCompressorTempFilter;
float mCompressorTempFiltered;

// helper to avoid code duplication
void publishFloat(const char* subtopic, const char* topic, float value, bool retained = false);
Expand All @@ -69,11 +73,18 @@ class MQTTTask
const char* topic,
unsigned long value,
bool retained = false);
void applyTemperatureFilter(message::ProtocolVersion& version);

void enableDiscovery();
template <typename T>
void publishDiscovery(uint16_t identifier, const char* haCategory, T enumClass);

void publishFiltered(
std::unique_ptr<aquamqtt::message::IMainMessage>& message,
aquamqtt::message::MAIN_ATTR_FLOAT attribute,
SimpleKalmanFilter& filter,
float& mFilteredValue,
const char* topic,
bool fullUpdate);
};
} // namespace aquamqtt

Expand Down
164 changes: 77 additions & 87 deletions AquaMQTT/src/task/MQTTTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ MQTTTask::MQTTTask()
, mLastProcessedEnergyMessage(nullptr)
, mLastProcessedMainMessage(nullptr)
, mHotWaterTempFilter(config::KALMAN_MEA_E, config::KALMAN_EST_E, config::KALMAN_Q)
, mHotWaterTempFiltered(0.0)
, mAirTempFilter(config::KALMAN_MEA_E, config::KALMAN_EST_E, config::KALMAN_Q)
, mAirTempFiltered(0.0)
, mEvaporatorLowerAirTempFilter(config::KALMAN_MEA_E, config::KALMAN_EST_E, config::KALMAN_Q)
, mEvaporatorLowerAirTempFiltered(0.0)
, mEvaporatorUpperAirTempFilter(config::KALMAN_MEA_E, config::KALMAN_EST_E, config::KALMAN_Q)
, mEvaporatorUpperAirTempFiltered(0.0)
, mCompressorTempFilter(config::KALMAN_MEA_E, config::KALMAN_EST_E, config::KALMAN_Q)
, mCompressorTempFiltered(0.0)
{
}

Expand Down Expand Up @@ -553,8 +558,6 @@ void MQTTTask::updateStats()

void MQTTTask::updateMainStatus(bool fullUpdate, message::ProtocolVersion& version)
{
applyTemperatureFilter(version);

std::unique_ptr<message::IMainMessage> message;
if (version == ProtocolVersion::PROTOCOL_NEXT)
{
Expand All @@ -569,54 +572,45 @@ void MQTTTask::updateMainStatus(bool fullUpdate, message::ProtocolVersion& versi
fullUpdate ? nullptr : mLastProcessedMainMessage);
}

if (message->hasAttr(MAIN_ATTR_FLOAT::WATER_TEMPERATURE))
{
if (fullUpdate || message->hasChanged(MAIN_ATTR_FLOAT::WATER_TEMPERATURE))
{
publishFloat(MAIN_SUBTOPIC, MAIN_HOT_WATER_TEMP, message->getAttr(MAIN_ATTR_FLOAT::WATER_TEMPERATURE));
}
}

if (message->hasAttr(MAIN_ATTR_FLOAT::AIR_TEMPERATURE))
{
if (fullUpdate || message->hasChanged(MAIN_ATTR_FLOAT::AIR_TEMPERATURE))
{
publishFloat(MAIN_SUBTOPIC, MAIN_SUPPLY_AIR_TEMP, message->getAttr(MAIN_ATTR_FLOAT::AIR_TEMPERATURE));
}
}

if (message->hasAttr(MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE))
{
if (fullUpdate || message->hasChanged(MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE))
{
publishFloat(
MAIN_SUBTOPIC,
MAIN_EVAPORATOR_AIR_TEMP_LOWER,
message->getAttr(MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE));
}
}

if (message->hasAttr(MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE))
{
if (fullUpdate || message->hasChanged(MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE))
{
publishFloat(
MAIN_SUBTOPIC,
MAIN_EVAPORATOR_AIR_TEMP_UPPER,
message->getAttr(MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE));
}
}

if (message->hasAttr(MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE))
{
if (fullUpdate || message->hasChanged(MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE))
{
publishFloat(
MAIN_SUBTOPIC,
MAIN_COMPRESSOR_OUTLET_TEMP,
message->getAttr(MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE));
}
}
publishFiltered(
message,
MAIN_ATTR_FLOAT::WATER_TEMPERATURE,
mHotWaterTempFilter,
mHotWaterTempFiltered,
MAIN_HOT_WATER_TEMP,
fullUpdate);

publishFiltered(
message,
MAIN_ATTR_FLOAT::AIR_TEMPERATURE,
mAirTempFilter,
mAirTempFiltered,
MAIN_SUPPLY_AIR_TEMP,
fullUpdate);

publishFiltered(
message,
MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE,
mEvaporatorLowerAirTempFilter,
mEvaporatorLowerAirTempFiltered,
MAIN_EVAPORATOR_AIR_TEMP_LOWER,
fullUpdate);

publishFiltered(
message,
MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE,
mEvaporatorUpperAirTempFilter,
mEvaporatorUpperAirTempFiltered,
MAIN_EVAPORATOR_AIR_TEMP_UPPER,
fullUpdate);

publishFiltered(
message,
MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE,
mCompressorTempFilter,
mCompressorTempFiltered,
MAIN_COMPRESSOR_OUTLET_TEMP,
fullUpdate);

if (message->hasAttr(MAIN_ATTR_FLOAT::FAN_SPEED_PWM))
{
Expand Down Expand Up @@ -1430,43 +1424,6 @@ void MQTTTask::publishul(
mMQTTClient.publish(reinterpret_cast<char*>(mTopicBuffer), reinterpret_cast<char*>(mPayloadBuffer), retained, 0);
}

void MQTTTask::applyTemperatureFilter(message::ProtocolVersion& version)
{
if (config::MQTT_FILTER_TEMPERATURE_NOISE)
{
std::unique_ptr<message::IMainMessage> message;
if (version == ProtocolVersion::PROTOCOL_NEXT)
{
message = std::make_unique<message::next::MainStatusMessage>(
mTransferBuffer, nullptr);
}
else
{
message = std::make_unique<message::legacy::MainStatusMessage>(
mTransferBuffer, nullptr);
}

message->setAttr(
MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE,
mEvaporatorLowerAirTempFilter.updateEstimate(
message->getAttr(MAIN_ATTR_FLOAT::EVAPORATOR_LOWER_TEMPERATURE)));
message->setAttr(
MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE,
mEvaporatorUpperAirTempFilter.updateEstimate(
message->getAttr(MAIN_ATTR_FLOAT::EVAPORATOR_UPPER_TEMPERATURE)));
message->setAttr(
MAIN_ATTR_FLOAT::AIR_TEMPERATURE,
mAirTempFilter.updateEstimate(message->getAttr(MAIN_ATTR_FLOAT::AIR_TEMPERATURE)));
message->setAttr(
MAIN_ATTR_FLOAT::WATER_TEMPERATURE,
mHotWaterTempFilter.updateEstimate(message->getAttr(MAIN_ATTR_FLOAT::WATER_TEMPERATURE)));
message->setAttr(
MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE,
mCompressorTempFilter.updateEstimate(
message->getAttr(MAIN_ATTR_FLOAT::COMPRESSOR_OUTLET_TEMPERATURE)));
}
}

// FIXME: discovery shall only include supported attributes, given by the protocol NEXT or LEGACY
void MQTTTask::enableDiscovery()
{
Expand Down Expand Up @@ -1510,4 +1467,37 @@ void MQTTTask::publishDiscovery(uint16_t identifier, const char* haCategory, T)
}
}

void MQTTTask::publishFiltered(
std::unique_ptr<IMainMessage>& message,
MAIN_ATTR_FLOAT attribute,
SimpleKalmanFilter& filter,
float& mFilteredValue,
const char* topic,
bool fullUpdate)
{

if (message->hasAttr(attribute))
{
float hotWaterTempRaw = message->getAttr(attribute);
auto previousFilteredValue = mFilteredValue;
mFilteredValue = filter.updateEstimate(hotWaterTempRaw);

if (fullUpdate)
{
publishFloat(
MAIN_SUBTOPIC,
topic,
config::MQTT_FILTER_TEMPERATURE_NOISE ? mFilteredValue : hotWaterTempRaw);
}
else if (!config::MQTT_FILTER_TEMPERATURE_NOISE && message->hasChanged(attribute))
{
publishFloat(MAIN_SUBTOPIC, topic, hotWaterTempRaw);
}
else if (config::MQTT_FILTER_TEMPERATURE_NOISE && (std::fabs(previousFilteredValue - mFilteredValue) >= 0.1))
{
publishFloat(MAIN_SUBTOPIC, topic, mFilteredValue);
}
}
}

} // namespace aquamqtt

0 comments on commit c86e828

Please sign in to comment.