Skip to content

Commit

Permalink
Invalidate MQTT item values on disconnect / null
Browse files Browse the repository at this point in the history
The client now has the option whether to read the last-valid value
or the most-recent (even if invalid) value.

To support this, we need to ensure that we produce a
specified-as-invalid value whenever connection to the backend is
lost, or when the backend tells us that a specific device has been
disconnected (via a null payload message for a topic).
  • Loading branch information
chriadam committed Mar 24, 2023
1 parent 17bc7b1 commit 1e3165a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 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 @@ -72,6 +72,7 @@ class VeQItemMqttProducer : public VeQItemProducer
QMqttClient *mqttConnection() const;
void setCredentials(const QString &username, const QString &password);
bool publishValue(const QString &uid, const QVariant &value);
bool readValue(const QString &uid);
ConnectionState connectionState() const;
QMqttClient::ClientError error() const;

Expand Down
30 changes: 28 additions & 2 deletions src/qt/ve_qitems_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
VeQItemMqtt::VeQItemMqtt(VeQItemMqttProducer *producer)
: VeQItem(producer, nullptr)
{
connect(producer, &VeQItemMqttProducer::connectionStateChanged,
this, [this] {
const VeQItemMqttProducer::ConnectionState state = mqttProducer()->connectionState();
switch (state) {
case VeQItemMqttProducer::Ready:
// TODO: may not be required, since we subscribe to everything
// and send keepalive to read all values when Ready.
mqttProducer()->readValue(uniqueId());
break;
case VeQItemMqttProducer::Disconnected:
produceValue(QVariant(), VeQItem::Offline);
break;
default: break;
}
});
}

int VeQItemMqtt::setValue(QVariant const &value)
Expand Down Expand Up @@ -285,10 +300,10 @@ void VeQItemMqttProducer::parseMessage(const QString &path, const QByteArray &me
VeQItemMqtt *item = qobject_cast<VeQItemMqtt*>(mProducerRoot->itemGetOrCreate(path, true, true));

// if the payload is null/empty, then the device has been removed from the system.
// for now, simply ignore the message. In future, we might (after some timeout)
// do item->itemDelete() or similar.
// invalidate the value, setting the item's state to Offline.
if (message.size() == 0) {
Q_EMIT nullMessageReceived(path);
item->produceValue(QVariant(), VeQItem::Offline);
} else {
// otherwise, update the value in the item.
// TODO: text / min / max / default ??
Expand Down Expand Up @@ -368,6 +383,17 @@ bool VeQItemMqttProducer::publishValue(const QString &uid, const QVariant &value
return true;
}

bool VeQItemMqttProducer::readValue(const QString &uid)
{
if (!mMqttConnection) {
return false;
}

const QString topic = QStringLiteral("R/%1/%2").arg(mPortalId, uid.mid(5));
mMqttConnection->publish(topic, QByteArray());
return true;
}

//--

#ifdef MQTT_WEBSOCKETS_ENABLED
Expand Down

0 comments on commit 1e3165a

Please sign in to comment.