forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: implement MQTT-driven battery provider
this battery provider implementation subscribes to a user-configurable MQTT topic to retrieve the battery SoC value. the value is not re-published under a different topic. there is no card created in the web app's live view, since the SoC is already part of the totals at the top of the live view. that is the only info this battery provider implements. closes #293. relates to #581.
- Loading branch information
1 parent
7946dfc
commit 1c25a09
Showing
13 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "Battery.h" | ||
#include <espMqttClient.h> | ||
|
||
class MqttBattery : public BatteryProvider { | ||
public: | ||
MqttBattery() = default; | ||
|
||
bool init(bool verboseLogging) final; | ||
void deinit() final; | ||
void loop() final { return; } // this class is event-driven | ||
std::shared_ptr<BatteryStats> getStats() const final { return _stats; } | ||
|
||
private: | ||
bool _verboseLogging = false; | ||
String _socTopic; | ||
std::shared_ptr<MqttBatteryStats> _stats = std::make_shared<MqttBatteryStats>(); | ||
|
||
void onMqttMessage(espMqttClientTypes::MessageProperties const& properties, | ||
char const* topic, uint8_t const* payload, size_t len, size_t index, size_t total); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <functional> | ||
|
||
#include "Configuration.h" | ||
#include "MqttBattery.h" | ||
#include "MqttSettings.h" | ||
#include "MessageOutput.h" | ||
|
||
bool MqttBattery::init(bool verboseLogging) | ||
{ | ||
_verboseLogging = verboseLogging; | ||
|
||
auto const& config = Configuration.get(); | ||
_socTopic = config.Battery.MqttTopic; | ||
|
||
if (_socTopic.isEmpty()) { return false; } | ||
|
||
MqttSettings.subscribe(_socTopic, 0/*QoS*/, | ||
std::bind(&MqttBattery::onMqttMessage, | ||
this, std::placeholders::_1, std::placeholders::_2, | ||
std::placeholders::_3, std::placeholders::_4, | ||
std::placeholders::_5, std::placeholders::_6) | ||
); | ||
|
||
if (_verboseLogging) { | ||
MessageOutput.printf("MqttBattery: Subscribed to '%s'\r\n", | ||
_socTopic.c_str()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void MqttBattery::deinit() | ||
{ | ||
if (_socTopic.isEmpty()) { return; } | ||
MqttSettings.unsubscribe(_socTopic); | ||
} | ||
|
||
void MqttBattery::onMqttMessage(espMqttClientTypes::MessageProperties const& properties, | ||
char const* topic, uint8_t const* payload, size_t len, size_t index, size_t total) | ||
{ | ||
float soc = 0; | ||
std::string value(reinterpret_cast<const char*>(payload), len); | ||
|
||
try { | ||
soc = std::stof(value); | ||
} | ||
catch(std::invalid_argument const& e) { | ||
MessageOutput.printf("MqttBattery: Cannot parse payload '%s' in topic '%s' as float\r\n", | ||
value.c_str(), topic); | ||
return; | ||
} | ||
|
||
if (soc < 0 || soc > 100) { | ||
MessageOutput.printf("MqttBattery: Implausible SoC '%.2f' in topic '%s'\r\n", | ||
soc, topic); | ||
return; | ||
} | ||
|
||
_stats->setSoC(static_cast<uint8_t>(soc)); | ||
|
||
if (_verboseLogging) { | ||
MessageOutput.printf("MqttBattery: Updated SoC to %d from '%s'\r\n", | ||
static_cast<uint8_t>(soc), topic); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters