Skip to content

Commit

Permalink
Only ignore messages with 'retain' flag if not on VRM broker
Browse files Browse the repository at this point in the history
Disable the "ignore retained messages" feature for VRM connections
until VRM switches over to FlashMQ.

Also disable the "suppress-republish" keepalive for VRM
connections until VRM switches over to FlashMQ.
  • Loading branch information
chriadam committed Jun 28, 2023
1 parent 9dcdc02 commit 9f5651e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions inc/veutil/qt/ve_qitems_mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ private Q_SLOTS:
QMqttClient::ClientError mError;
QMqttClient::ProtocolVersion mProtocolVersion;
bool mReceivedMessage;
bool mIsVrmBroker;
};

#ifdef MQTT_WEBSOCKETS_ENABLED
Expand Down
11 changes: 8 additions & 3 deletions src/qt/ve_qitems_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ VeQItemMqttProducer::VeQItemMqttProducer(
mAutoReconnectMaxAttempts(sizeof(mReconnectAttemptIntervals)/sizeof(mReconnectAttemptIntervals[0])),
mError(QMqttClient::NoError),
mProtocolVersion(QMqttClient::MQTT_3_1_1),
mReceivedMessage(false)
mReceivedMessage(false),
mIsVrmBroker(false)
{
// Create a sanitized clientId. MQTT v3.1 spec states that the clientId must be
// between 1 and 23 characters, and some brokers support [a-z][A-Z][0-9] only.
Expand Down Expand Up @@ -117,6 +118,8 @@ void VeQItemMqttProducer::open(
const QUrl &url,
QMqttClient::ProtocolVersion protocolVersion)
{
mIsVrmBroker = url.toString().startsWith(QStringLiteral("wss://webmqtt"))
&& url.toString().endsWith(QStringLiteral(".victronenergy.com/mqtt"));
mAutoReconnectAttemptCounter = 0;
setError(QMqttClient::NoError);

Expand Down Expand Up @@ -363,10 +366,12 @@ void VeQItemMqttProducer::onSubscriptionMessageReceived(const QMqttMessage &mess
const QString keepaliveTopic = notificationPrefix + QStringLiteral("/keepalive");
if (topicName.compare(keepaliveTopic, Qt::CaseInsensitive) == 0) {
// ignore keepalive topic.
} else if (message.retain()) {
} else if (!mIsVrmBroker && message.retain()) {
// ignore retained messages, as for internet brokers (VRM)
// nothing will "unpublish" the topic for a device which goes offline.
// see issue #313 in gui-v2.
// for now, only enable this for non-VRM (i.e. local) brokers,
// as VRM is not yet using FlashMQ.
} else {
// we have a topic message which we need to expose via VeQItem.
const QString path = topicName.mid(notificationPrefix.size() + 1);
Expand Down Expand Up @@ -420,7 +425,7 @@ void VeQItemMqttProducer::doKeepAlive(bool suppressRepublish)
if (mMqttConnection
&& mMqttConnection->state() == QMqttClient::Connected
&& !mPortalId.isEmpty()) {
if (!suppressRepublish) {
if (mIsVrmBroker || !suppressRepublish) {
mMqttConnection->publish(QMqttTopicName(QStringLiteral("R/%1/keepalive").arg(mPortalId)),
QByteArray());
mKeepAliveTimer->start();
Expand Down

0 comments on commit 9f5651e

Please sign in to comment.