! * \brief Adds a MQTTSubscription to the MQTTClient *Used when the user unsubscribes from a topic of a MQTTSubscription * * \param topic, the name of the topic * \param QoS */
| 457 | * \param QoS |
| 458 | */ |
| 459 | void MQTTClient::addBeforeRemoveSubscription(const QString& topicName, quint8 QoS) { |
| 460 | // We can't add the subscription if it already exists |
| 461 | if (!m_subscriptions.contains(topicName)) { |
| 462 | // Subscribe to the topic |
| 463 | const QMqttTopicFilter filter{topicName}; |
| 464 | auto* temp = m_client->subscribe(filter, QoS); |
| 465 | if (temp) { |
| 466 | // Add the MQTTSubscription and other connected data |
| 467 | // qDebug()<<"Add subscription before remove: " << temp->topic() << " " << temp->qos(); |
| 468 | m_subscriptions.push_back(temp->topic().filter()); |
| 469 | m_subscribedTopicNameQoS[temp->topic().filter()] = temp->qos(); |
| 470 | |
| 471 | auto* newSubscription = new MQTTSubscription(temp->topic().filter()); |
| 472 | newSubscription->setMQTTClient(this); |
| 473 | |
| 474 | addChildFast(newSubscription); |
| 475 | m_MQTTSubscriptions.push_back(newSubscription); |
| 476 | |
| 477 | // Search for the subscription the topic belonged to |
| 478 | bool found = false; |
| 479 | MQTTSubscription* superiorSubscription = nullptr; |
| 480 | for (auto* subscription : m_MQTTSubscriptions) { |
| 481 | if (checkTopicContains(subscription->subscriptionName(), topicName) && topicName != subscription->subscriptionName()) { |
| 482 | found = true; |
| 483 | superiorSubscription = subscription; |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (found) { |
| 489 | // Search for topics belonging to the superior(old) subscription |
| 490 | // which are also contained by the new subscription |
| 491 | QVector<MQTTTopic*> topics = superiorSubscription->topics(); |
| 492 | // qDebug()<< topics.size(); |
| 493 | |
| 494 | QVector<MQTTTopic*> inferiorTopics; |
| 495 | for (auto* topic : topics) { |
| 496 | if (checkTopicContains(topicName, topic->topicName())) { |
| 497 | inferiorTopics.push_back(topic); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // Reparent these topics, in order to avoid data loss |
| 502 | for (auto* inferiorTopic : inferiorTopics) { |
| 503 | inferiorTopic->reparent(newSubscription); |
| 504 | } |
| 505 | } |
| 506 | connect(temp, &QMqttSubscription::messageReceived, this, &MQTTClient::MQTTSubscriptionMessageReceived); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /*! |
| 512 | * \brief Reparents the given MQTTTopic to the given MQTTSubscription |
nothing calls this directly
no test coverage detected