Skip to content

Commit

Permalink
fixed bug when non recent change was published if topic was updated a…
Browse files Browse the repository at this point in the history
…s part of poll group read
  • Loading branch information
BlackZork committed Dec 30, 2024
1 parent b1005c9 commit e6c1626
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libmodmqttsrv/mqttclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ MqttClient::processRegisterValues(const std::string& pModbusNetworkName, const M

for (std::shared_ptr<MqttObject>& obj: *affectedObjects) {
AvailableFlag oldAvail = obj->getAvailableFlag();
bool hadValue = obj->hasValue();
obj->updateRegisterValues(pModbusNetworkName, pSlaveData);
AvailableFlag newAvail = obj->getAvailableFlag();

Expand All @@ -163,8 +164,12 @@ MqttClient::processRegisterValues(const std::string& pModbusNetworkName, const M
publishState(*obj, true);
} else {
// delete retained message
if (oldAvail == AvailableFlag::NotSet)
if (oldAvail == AvailableFlag::NotSet) {
mMqttImpl->publish(obj->getStateTopic().c_str(), 0, NULL, true);
// remember initial payload for comparsion with subsequent modbus data updates
if (!obj->getRetain())
obj->setLastPublishedPayload(MqttPayload::generate(*obj));
}
if (obj->getPublishMode() == PublishMode::EVERY_POLL)
publishState(*obj, true);
}
Expand Down
2 changes: 2 additions & 0 deletions libmodmqttsrv/mqttobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ class MqttObject {
void setAvailableValue(const MqttValue& pValue) { mAvailability.setAvailableValue(pValue); }
AvailableFlag getAvailableFlag() const { return mIsAvailable; }

bool hasValue() const { return mState.hasAllValues(); }

void setLastPublishedPayload(const std::string& pVal) { mLastPublishedPayload = pVal; }
const std::string& getLastPublishedPayload() const { return mLastPublishedPayload; }

Expand Down
44 changes: 44 additions & 0 deletions unittests/mqtt_publish_retain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,47 @@ TestConfig config(R"(
}
}

TEST_CASE ("When retain topic is a part of poll group") {

TestConfig config(R"(
modbus:
networks:
- name: tcptest
address: localhost
port: 501
slaves:
- address: 1
poll_groups:
- register: 2
register_type: holding
count: 2
mqtt:
client_id: mqtt_test
refresh: 50ms
broker:
host: localhost
objects:
- topic: test_sensor
retain: false
state:
register: tcptest.1.2
- topic: other_sensor
state:
register: tcptest.1.3
)");

SECTION("its value should be ignored if related register value is not changed") {
MockedModMqttServerThread server(config.toString());
server.setModbusRegisterValue("tcptest", 1, 2, modmqttd::RegisterType::HOLDING, 2);
server.setModbusRegisterValue("tcptest", 1, 3, modmqttd::RegisterType::HOLDING, 3);

server.start();
server.waitForPublish("other_sensor/state");

//this caused test_sensor value change due to null -> 3
server.setModbusRegisterValue("tcptest", 1, 3, modmqttd::RegisterType::HOLDING, 33);
server.waitForPublish("other_sensor/state");

server.requirePublishCount("test_sensor/state", 1);
}
}

0 comments on commit e6c1626

Please sign in to comment.