Fix mqtt items asserting on client instances
This commit is contained in:
parent
b0792d32db
commit
d69bfb58b9
4 changed files with 13 additions and 7 deletions
|
|
@ -26,6 +26,7 @@ void ItemLoaderSource::refresh()
|
|||
request.type = ITEM_UPDATE_LOADED;
|
||||
request.payload = newItem;
|
||||
request.changes = ItemFieldChanges(true);
|
||||
request.changes.value = false;
|
||||
itemAddRequests.push_back(request);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include <QtMqtt/QMqttClient>
|
||||
|
||||
#include "mqttclient.h"
|
||||
#include "programmode.h"
|
||||
|
||||
MqttItem::MqttItem(QString name, uint8_t value, QObject *parent)
|
||||
: Item(0, name, value, parent),
|
||||
|
|
@ -15,9 +16,10 @@ MqttItem::MqttItem(QString name, uint8_t value, QObject *parent)
|
|||
{
|
||||
hashId();
|
||||
std::shared_ptr<MqttClient> workClient = client.lock();
|
||||
assert(workClient);
|
||||
assert(workClient || programMode == PROGRAM_MODE_UI_ONLY);
|
||||
|
||||
connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged);
|
||||
if(workClient)
|
||||
connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged);
|
||||
}
|
||||
|
||||
MqttItem::~MqttItem()
|
||||
|
|
|
|||
|
|
@ -83,13 +83,17 @@ void MqttClient::unsubscribe(MqttClient::Subscription* subscription)
|
|||
|
||||
void MqttClient::unsubscribe(QString topic)
|
||||
{
|
||||
assert(!subscriptions.contains(topic));
|
||||
MqttClient::Subscription* sub = subscriptions[topic];
|
||||
if(!sub)
|
||||
{
|
||||
qWarning()<<"MqttClient: Trying to unsubscribe from unkown topic:"<<topic;
|
||||
return;
|
||||
}
|
||||
|
||||
if(--sub->ref > 0)
|
||||
return;
|
||||
|
||||
qDebug()<<"MqttClient: unsubscibeing"<<sub->subscription->topic();
|
||||
qDebug()<<"MqttClient: unsubscibeing"<<sub->subscription->topic().filter();
|
||||
client->unsubscribe(sub->subscription->topic());
|
||||
subscriptions.erase(topic);
|
||||
delete sub;
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state)
|
|||
{
|
||||
for(SensorSubscription& sensor : sensors)
|
||||
{
|
||||
qDebug()<<"MQTT subscribeing to"<<client->getBaseTopic() + "/" + sensor.topic;
|
||||
sensor.subscription = client->subscribe(client->getBaseTopic() + "/" + sensor.topic);
|
||||
connect(sensor.subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttSensorSource::onMessageReceived);
|
||||
}
|
||||
|
|
@ -58,7 +57,7 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state)
|
|||
{
|
||||
if(sensor.subscription)
|
||||
{
|
||||
client->unsubscribe(sensor.topic);
|
||||
client->unsubscribe(client->getBaseTopic() + "/" + sensor.topic);
|
||||
sensor.subscription = nullptr;
|
||||
}
|
||||
}
|
||||
|
|
@ -167,6 +166,6 @@ void MqttSensorSource::store(QJsonObject& json)
|
|||
MqttSensorSource::~MqttSensorSource()
|
||||
{
|
||||
for(SensorSubscription& sub : sensors)
|
||||
client->unsubscribe(sub.topic);
|
||||
client->unsubscribe(client->getBaseTopic() + "/" + sub.topic);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue