Skip to content

Commit

Permalink
sns: broken mqtt address publishing
Browse files Browse the repository at this point in the history
cannot declare constexpr objects like that with old compiler
fix #2594
  • Loading branch information
mcspr committed Oct 16, 2023
1 parent afce3dc commit 480c72d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
6 changes: 5 additions & 1 deletion code/espurna/config/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@
// Warning: this might wear out flash fast!
#endif

#ifndef SENSOR_PUBLISH_ADDRESSES
#define SENSOR_PUBLISH_ADDRESSES 0 // Publish sensor addresses
#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses
#endif

#ifndef SENSOR_ADDRESS_TOPIC
#define SENSOR_ADDRESS_TOPIC "address" // Topic to publish sensor addresses
#endif

// -----------------------------------------------------------------------------
// Magnitude offset correction
Expand Down
41 changes: 19 additions & 22 deletions code/espurna/sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1833,27 +1833,6 @@ void report(const Value& report) {
for (auto& handler : internal::report_handlers) {
handler(report);
}

#if MQTT_SUPPORT
{
mqttSend(report.topic.c_str(), report.repr.c_str());

#if SENSOR_PUBLISH_ADDRESSES
{
static constexpr auto AddressTopic = STRING_VIEW(SENSOR_ADDRESS_TOPIC);

String address_topic;
address_topic.reserve(report.topic.length() + AddressTopic.length());
address_topic.concat(AddressTopic.c_str(), AddressTopic.length());
address_topic += '/';
address_topic += report.topic;

mqttSend(address_topic.c_str(), magnitude.sensor->address(magnitude.slot).c_str());
}
#endif // SENSOR_PUBLISH_ADDRESSES

}
#endif // MQTT_SUPPORT
}

Info info(const Magnitude& magnitude) {
Expand Down Expand Up @@ -3652,6 +3631,22 @@ void setup() {
namespace mqtt {
namespace {

void report(const Value& report, const Magnitude& magnitude) {
mqttSend(report.topic.c_str(), report.repr.c_str());

#if SENSOR_PUBLISH_ADDRESSES
STRING_VIEW_INLINE(AddressTopic, SENSOR_ADDRESS_TOPIC);

String address_topic;
address_topic.reserve(1 + report.topic.length() + AddressTopic.length());
address_topic.concat(AddressTopic.data(), AddressTopic.length());
address_topic += '/';
address_topic += report.topic;

mqttSend(address_topic.c_str(), magnitude.sensor->address(magnitude.slot).c_str());
#endif
}

void callback(unsigned int type, StringView topic, StringView payload) {
if (!magnitude::count(MAGNITUDE_ENERGY)) {
return;
Expand Down Expand Up @@ -4031,10 +4026,12 @@ void loop() {
const auto report = magnitude::value(magnitude, value.filtered);
magnitude::report(report);

#if MQTT_SUPPORT
mqtt::report(report, magnitude);
#endif
#if THINGSPEAK_SUPPORT
tspkEnqueueMagnitude(index, report.repr);
#endif

#if DOMOTICZ_SUPPORT
domoticzSendMagnitude(index, report);
#endif
Expand Down
1 change: 1 addition & 0 deletions code/test/build/nondefault.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#define SENSOR_SUPPORT 1
#define SENSOR_PUBLISH_ADDRESSES 1
#define INFLUXDB_SUPPORT 1
#define KINGART_CURTAIN_SUPPORT 1
#define LLMNR_SUPPORT 1
Expand Down

0 comments on commit 480c72d

Please sign in to comment.