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.type = ITEM_UPDATE_LOADED;
|
||||||
request.payload = newItem;
|
request.payload = newItem;
|
||||||
request.changes = ItemFieldChanges(true);
|
request.changes = ItemFieldChanges(true);
|
||||||
|
request.changes.value = false;
|
||||||
itemAddRequests.push_back(request);
|
itemAddRequests.push_back(request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <QtMqtt/QMqttClient>
|
#include <QtMqtt/QMqttClient>
|
||||||
|
|
||||||
#include "mqttclient.h"
|
#include "mqttclient.h"
|
||||||
|
#include "programmode.h"
|
||||||
|
|
||||||
MqttItem::MqttItem(QString name, uint8_t value, QObject *parent)
|
MqttItem::MqttItem(QString name, uint8_t value, QObject *parent)
|
||||||
: Item(0, name, value, parent),
|
: Item(0, name, value, parent),
|
||||||
|
|
@ -15,8 +16,9 @@ MqttItem::MqttItem(QString name, uint8_t value, QObject *parent)
|
||||||
{
|
{
|
||||||
hashId();
|
hashId();
|
||||||
std::shared_ptr<MqttClient> workClient = client.lock();
|
std::shared_ptr<MqttClient> workClient = client.lock();
|
||||||
assert(workClient);
|
assert(workClient || programMode == PROGRAM_MODE_UI_ONLY);
|
||||||
|
|
||||||
|
if(workClient)
|
||||||
connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged);
|
connect(workClient->getClient().get(), &QMqttClient::stateChanged, this, &MqttItem::onClientStateChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -83,13 +83,17 @@ void MqttClient::unsubscribe(MqttClient::Subscription* subscription)
|
||||||
|
|
||||||
void MqttClient::unsubscribe(QString topic)
|
void MqttClient::unsubscribe(QString topic)
|
||||||
{
|
{
|
||||||
assert(!subscriptions.contains(topic));
|
|
||||||
MqttClient::Subscription* sub = subscriptions[topic];
|
MqttClient::Subscription* sub = subscriptions[topic];
|
||||||
|
if(!sub)
|
||||||
|
{
|
||||||
|
qWarning()<<"MqttClient: Trying to unsubscribe from unkown topic:"<<topic;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(--sub->ref > 0)
|
if(--sub->ref > 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
qDebug()<<"MqttClient: unsubscibeing"<<sub->subscription->topic();
|
qDebug()<<"MqttClient: unsubscibeing"<<sub->subscription->topic().filter();
|
||||||
client->unsubscribe(sub->subscription->topic());
|
client->unsubscribe(sub->subscription->topic());
|
||||||
subscriptions.erase(topic);
|
subscriptions.erase(topic);
|
||||||
delete sub;
|
delete sub;
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,6 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state)
|
||||||
{
|
{
|
||||||
for(SensorSubscription& sensor : sensors)
|
for(SensorSubscription& sensor : sensors)
|
||||||
{
|
{
|
||||||
qDebug()<<"MQTT subscribeing to"<<client->getBaseTopic() + "/" + sensor.topic;
|
|
||||||
sensor.subscription = client->subscribe(client->getBaseTopic() + "/" + sensor.topic);
|
sensor.subscription = client->subscribe(client->getBaseTopic() + "/" + sensor.topic);
|
||||||
connect(sensor.subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttSensorSource::onMessageReceived);
|
connect(sensor.subscription->subscription, &QMqttSubscription::messageReceived, this, &MqttSensorSource::onMessageReceived);
|
||||||
}
|
}
|
||||||
|
|
@ -58,7 +57,7 @@ void MqttSensorSource::onClientStateChanged(QMqttClient::ClientState state)
|
||||||
{
|
{
|
||||||
if(sensor.subscription)
|
if(sensor.subscription)
|
||||||
{
|
{
|
||||||
client->unsubscribe(sensor.topic);
|
client->unsubscribe(client->getBaseTopic() + "/" + sensor.topic);
|
||||||
sensor.subscription = nullptr;
|
sensor.subscription = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -167,6 +166,6 @@ void MqttSensorSource::store(QJsonObject& json)
|
||||||
MqttSensorSource::~MqttSensorSource()
|
MqttSensorSource::~MqttSensorSource()
|
||||||
{
|
{
|
||||||
for(SensorSubscription& sub : sensors)
|
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