Fix mqtt item expose detection only working on local instances
This commit is contained in:
parent
45676b3384
commit
58ba22b267
24 changed files with 340 additions and 415 deletions
|
|
@ -26,6 +26,20 @@ void MqttClient::start(const QJsonObject& settings)
|
|||
client->connectToHost();
|
||||
}
|
||||
|
||||
void MqttClient::onDevicesMessageReceived(const QMqttMessage& message)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(message.payload());
|
||||
if(!doc.isArray())
|
||||
return;
|
||||
qDebug()<<"MqttClient got devices array";
|
||||
devices = doc.array();
|
||||
}
|
||||
|
||||
QJsonArray MqttClient::getDevicesArray()
|
||||
{
|
||||
return devices;
|
||||
}
|
||||
|
||||
void MqttClient::onClientError(QMqttClient::ClientError error)
|
||||
{
|
||||
qWarning()<<"MQTT Client error:"<<error;
|
||||
|
|
@ -34,11 +48,19 @@ void MqttClient::onClientError(QMqttClient::ClientError error)
|
|||
void MqttClient::onClientStateChanged(QMqttClient::ClientState state)
|
||||
{
|
||||
if(state == QMqttClient::ClientState::Connected)
|
||||
{
|
||||
devicesSubscription = subscribe(getBaseTopic() + "/bridge/devices");
|
||||
connect(devicesSubscription->subscription, &QMqttSubscription::messageReceived, this, &MqttClient::onDevicesMessageReceived);
|
||||
qInfo()<<"Connected to MQTT broker at "<<client->hostname()<<client->port();
|
||||
}
|
||||
else if (state == QMqttClient::ClientState::Disconnected)
|
||||
{
|
||||
qWarning()<<"Lost connection to MQTT broker";
|
||||
}
|
||||
else if(state == QMqttClient::ClientState::Connecting)
|
||||
{
|
||||
qInfo()<<"Connecting to MQTT broker at "<<client->hostname()<<client->port();
|
||||
}
|
||||
}
|
||||
|
||||
void MqttClient::store(QJsonObject& json)
|
||||
|
|
@ -107,9 +129,11 @@ QString MqttClient::getBaseTopic()
|
|||
|
||||
MqttClient::~MqttClient()
|
||||
{
|
||||
if(devicesSubscription)
|
||||
unsubscribe(devicesSubscription);
|
||||
for(const std::pair<QString, Subscription*> sub : subscriptions)
|
||||
{
|
||||
qWarning()<<sub.first<<"not unregistered at exit!";
|
||||
client->unsubscribe(sub.second->subscription->topic());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue