! * \brief Updates the will message of the client */
| 753 | * \brief Updates the will message of the client |
| 754 | */ |
| 755 | void MQTTClient::updateWillMessage() { |
| 756 | // Search for the will topic |
| 757 | const auto& topics = children<const MQTTTopic>(AbstractAspect::ChildIndexFlag::Recursive); |
| 758 | const MQTTTopic* willTopic = nullptr; |
| 759 | for (const auto* topic : topics) { |
| 760 | if (topic->topicName() == m_MQTTWill.willTopic) { |
| 761 | willTopic = topic; |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // if the will topic is found we can update the will message |
| 767 | if (willTopic != nullptr) { |
| 768 | // To update the will message we have to disconnect first, then after setting everything connect again |
| 769 | if (m_MQTTWill.enabled && (m_client->state() == QMqttClient::ClientState::Connected)) { |
| 770 | // Disconnect only once (disconnecting may take a while) |
| 771 | if (!m_disconnectForWill) { |
| 772 | // qDebug() << "Disconnecting from host in order to update will message"; |
| 773 | m_client->disconnectFromHost(); |
| 774 | m_disconnectForWill = true; |
| 775 | } |
| 776 | // Try to update again |
| 777 | updateWillMessage(); |
| 778 | } |
| 779 | // If client is disconnected we can update the settings |
| 780 | else if (m_MQTTWill.enabled && (m_client->state() == QMqttClient::ClientState::Disconnected) && m_disconnectForWill) { |
| 781 | m_client->setWillQoS(m_MQTTWill.willQoS); |
| 782 | m_client->setWillRetain(m_MQTTWill.willRetain); |
| 783 | m_client->setWillTopic(m_MQTTWill.willTopic); |
| 784 | |
| 785 | // Set the will message according to m_willMessageType |
| 786 | switch (m_MQTTWill.willMessageType) { |
| 787 | case WillMessageType::OwnMessage: |
| 788 | m_client->setWillMessage(m_MQTTWill.willOwnMessage.toUtf8()); |
| 789 | // qDebug()<<"Will own message" << m_MQTTWill.willOwnMessage; |
| 790 | break; |
| 791 | case WillMessageType::Statistics: { |
| 792 | // Statistics is only possible if the data stored in the MQTTTopic is of type integer or numeric |
| 793 | // check the column mode of the last column in the topic for this. |
| 794 | // TODO: check this logic again - why last column only? |
| 795 | const auto* col = willTopic->child<Column>(willTopic->childCount<Column>() - 1); |
| 796 | auto mode = col->columnMode(); |
| 797 | if (mode == AbstractColumn::ColumnMode::Integer || mode == AbstractColumn::ColumnMode::Double) |
| 798 | m_client->setWillMessage(this->statistics(willTopic).toUtf8()); |
| 799 | else |
| 800 | m_client->setWillMessage(QByteArray()); // empty message |
| 801 | // qDebug() << "Will statistics message: "<< QString(m_client->willMessage()); |
| 802 | break; |
| 803 | } |
| 804 | case WillMessageType::LastMessage: |
| 805 | m_client->setWillMessage(m_MQTTWill.willLastMessage.toUtf8()); |
| 806 | // qDebug()<<"Will last message:\n" << m_MQTTWill.willLastMessage; |
| 807 | break; |
| 808 | default: |
| 809 | break; |
| 810 | } |
| 811 | m_disconnectForWill = false; |
| 812 | // Reconnect with the updated message |
no test coverage detected