Skip to content

Commit

Permalink
fixed timesync listening on wrong topic and added messageId counter f…
Browse files Browse the repository at this point in the history
…or timesync reply
  • Loading branch information
vaterlangen committed Oct 4, 2024
1 parent 69e2d78 commit 50ee059
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion include/ZendureBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ZendureBattery : public BatteryProvider {
void shutdown() const;

protected:
void timesync() const;
void timesync();
static String parseVersion(uint32_t version);
uint16_t calcOutputLimit(uint16_t limit) const;

Expand All @@ -145,6 +145,8 @@ class ZendureBattery : public BatteryProvider {
uint32_t _rateSunCalcMs;
uint64_t _nextSunCalc;

uint32_t _messageCounter = 0;

String _deviceId;

String _baseTopic;
Expand Down
6 changes: 3 additions & 3 deletions src/ZendureBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool ZendureBattery::init(bool verboseLogging)

// subscribe for timesync messages
_topicTimesync = _baseTopic + "time-sync";
MqttSettings.subscribe(_topicReport, 0/*QoS*/,
MqttSettings.subscribe(_topicTimesync, 0/*QoS*/,
std::bind(&ZendureBattery::onMqttMessageTimesync,
this, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4,
Expand Down Expand Up @@ -302,13 +302,13 @@ void ZendureBattery::shutdown() const
}
}

void ZendureBattery::timesync() const
void ZendureBattery::timesync()
{
struct tm timeinfo;
if (!_baseTopic.isEmpty() && getLocalTime(&timeinfo, 5)) {
char timeStringBuff[50];
strftime(timeStringBuff, sizeof(timeStringBuff), "%s", &timeinfo);
MqttSettings.publishGeneric("iot" + _baseTopic + "time-sync/reply","{\"zoneOffset\": \"+00:00\", \"messageId\": 123, \"timestamp\": " + String(timeStringBuff) + "}", false, 0);
MqttSettings.publishGeneric("iot" + _baseTopic + "time-sync/reply","{\"zoneOffset\": \"+00:00\", \"messageId\": " + String(++_messageCounter) + ", \"timestamp\": " + String(timeStringBuff) + "}", false, 0);
MessageOutput.printf("ZendureBattery: Timesync Reply\r\n");
}
}
Expand Down

0 comments on commit 50ee059

Please sign in to comment.