the MQTT power meter can now process the messages published at the respective topics as JSON and extract a power value using a JSON path (same as in HTTP+JSON power meter). additionally, selecting a unit for the power value as well as an option to invert the value's sign was added as well, similar to the HTTPS+JSON power meter.
38 lines
1009 B
C++
38 lines
1009 B
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#pragma once
|
|
|
|
#include "Configuration.h"
|
|
#include "PowerMeterProvider.h"
|
|
#include <espMqttClient.h>
|
|
#include <vector>
|
|
#include <mutex>
|
|
#include <array>
|
|
|
|
class PowerMeterMqtt : public PowerMeterProvider {
|
|
public:
|
|
explicit PowerMeterMqtt(PowerMeterMqttConfig const& cfg)
|
|
: _cfg(cfg) { }
|
|
|
|
~PowerMeterMqtt();
|
|
|
|
bool init() final;
|
|
void loop() final { }
|
|
float getPowerTotal() const final;
|
|
void doMqttPublish() const final;
|
|
|
|
private:
|
|
using MsgProperties = espMqttClientTypes::MessageProperties;
|
|
void onMessage(MsgProperties const& properties, char const* topic,
|
|
uint8_t const* payload, size_t len, size_t index,
|
|
size_t total, float* targetVariable, PowerMeterMqttValue const* cfg);
|
|
|
|
PowerMeterMqttConfig const _cfg;
|
|
|
|
using power_values_t = std::array<float, POWERMETER_MQTT_MAX_VALUES>;
|
|
power_values_t _powerValues;
|
|
|
|
std::vector<String> _mqttSubscriptions;
|
|
|
|
mutable std::mutex _mutex;
|
|
};
|