pws2mqtt-qt/mqtt.h
2024-09-10 20:34:16 +02:00

192 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef MQTT_H
#define MQTT_H
#include <cstring>
#include <cstdio>
#include <QString>
#include <QHash>
#include <QtMqtt>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttMessage>
#include <QtMqtt/QMqttSubscription>
#define MQTT_TOPIC "pws2mqtt"
/*{128, "Unspecified error: The Server does not wish to reveal the reason for the failure, or none of the other Reason Codes apply."},
{129, "Malformed Packet; Data within the CONNECT packet could not be correctly parsed."},
{130, "Protocol Error: Data in the CONNECT packet does not conform to this specification."},
{131, "Implementation specific error: The CONNECT is valid but is not accepted by this Server."},
{132, "Unsupported Protocol Version: The Server does not support the version of the MQTT protocol requested by the Client."},
{133, "Client Identifier not valid: The Client Identifier is a valid string but is not allowed by the Server."},
{134, "Bad User Name or Password: The Server does not accept the User Name or Password specified by the Client."},
{135, "Not authorized: The Client is not authorized to connect."},
{136, "Server unavailable: The MQTT Server is not available."},
{137, "Server busy: The Server is busy. Try again later."},
{138, "Banned: This Client has been banned by administrative action. Contact the server administrator."},
{140, "Bad authentication method: The authentication method is not supported or does not match the authentication method currently in use."},
{144, "Topic Name invalid: The Will Topic Name is not malformed, but is not accepted by this Server."},
{149, "Packet too large: The CONNECT packet exceeded the maximum permissible size."},
{151, "Quota exceeded: An implementation or administrative imposed limit has been exceeded."},
{153, "Payload format invalid: The Will Payload does not match the specified Payload Format Indicator."},
{154, "Retain not supported: The Server does not support retained messages, and Will Retain was set to 1."},
{155, "QoS not supported: The Server does not support the QoS set in Will QoS."},
{156, "Use another server: The Client should temporarily use another server."},
{157, "Server moved: The Client should permanently use another server."},
{159, "Connection rate exceeded: The connection rate limit has been exceeded."},
QHash <int, QString> mosq_error
{
{MOSQ_ERR_ERRNO, "System return error"},
{MOSQ_ERR_INVAL, "Input parameters were invalid."},
{MOSQ_ERR_NOMEM, "Out of memory condition occurred."},
{MOSQ_ERR_PAYLOAD_SIZE, "Payloadlen is too large."},
{MOSQ_ERR_MALFORMED_UTF8, "The topic is not valid UTF-8."},
{MOSQ_ERR_NOT_SUPPORTED, "Properties is not NULL and the client is not using MQTT v5"},
{MOSQ_ERR_NO_CONN, "if the client isnt connected to a broker."},
{MOSQ_ERR_DUPLICATE_PROPERTY, "A property is duplicated where it is forbidden."},
{MOSQ_ERR_PROTOCOL, "Any property is invalid for use with DISCONNECT."},
{MOSQ_ERR_QOS_NOT_SUPPORTED, "The QoS is greater than that supported by the broker."},
{MOSQ_ERR_OVERSIZE_PACKET, "The resulting packet would be larger than supported by the broker."},
{MOSQ_ERR_CONN_LOST, "The connection to the broker was lost."},
{MOSQ_ERR_PROTOCOL, "There is a protocol error communicating with the broker."},
};
*/
/*class MqttSub : public QMqttSubscription
{
Q_OBJECT;
public:
explicit MqttSub();
~MqttSub();
public slots:
void updateStatus({ , "SubscriptionState state);
};*/
class MqttClient : public QObject
{
Q_OBJECT
public:
MqttClient ();
~MqttClient();
QString topic = "pws2mqtt/wh2650a";
QString host = "127.0.0.1";
QString friendlyName = "WH2650A"; // friendly name of the device in mqtt
QString macAddress = "0x483fda53cbcb"; // lan mac address as lan identity TODO get from device
QString type = "Meteo";
int qos = 0;
QString server = "127.0.0.1"; //"127.0.0.1";
int serverPort = 1883;
int keepAlive = 60;
int protocolVersion = 4;
int retryConnectionDelay = 10; // in seconds
int retrySubscribeDelay = 10; // in seconds
QMqttClient *qmqttClient;
QHash <qint32, QString> messagesList;
QMqttSubscription *subscription = nullptr;
QMqttSubscription::SubscriptionState subscriptionState;
QMqttClient::ClientState clientState;
qint32 send_message(QString message);
QHash <int , QString> messageStatus =
{
{0, "Unknown : The message status is unknown."},
{1, "Published : The client received a message for one of its subscriptions. This applies to QoS levels 1 and 2."},
{2, "Acknowledged A message has been acknowledged. This applies to QoS 1 and states that the message handling has been finished from the client side."},
{3, "Received : A message has been received. This applies to QoS 2."},
{4, "Released : A message has been released. This applies to QoS 2. For a publisher the message handling has been finished."},
{5, "Completed : A message has been completed. This applies to QoS 2 and states that the message handling has been finished from the client side."}
};
QHash <int, QString> connectReasonCode =
{
{0, "SubscriptionQoSLevel0 : subscription with QoS level 0 has been created."},
{1 , "SubscriptionQoSLevel1 0x01 A subscription with QoS level 1 has been created."},
{2 , "SubscriptionQoSLevel2 0x02 A subscription with QoS level 2 has been created."},
{16 , "NoMatchingSubscriber 0x10 The message has been accepted by the server, but there are no subscribers to receive this message. A broker may send this reason code instead of Success."},
{17 , "NoSubscriptionExisted (since Qt 5.15) 0x11 No matching Topic Filter is being used by the Client."},
{24 , "ContinueAuthentication (since Qt 5.15) 0x18 Continue the authentication with another step."},
{25 , "ReAuthenticate (since Qt 5.15) 0x19 Initiate a re-authentication."},
{128 , "UnspecifiedError 0x80 An unspecified error occurred."},
{129 , "MalformedPacket 0x81 The packet sent to the server is invalid."},
{130 , "ProtocolError 0x82 A protocol error has occurred. In most cases, this will cause the server to disconnect the client."},
{131 , "ImplementationSpecificError 0x83 The packet is valid, but the recipient rejects it."},
{132 , "UnsupportedProtocolVersion 0x84 The requested protocol version is not supported by the server."},
{133 , "InvalidClientId 0x85 The client ID is invalid."},
{134 , "InvalidUserNameOrPassword 0x86 The username or password specified is invalid."},
{135 , "NotAuthorized 0x87 The client is not authorized for the specified action."},
{136 , "ServerNotAvailable 0x88 The server to connect to is not available."},
{137 , "ServerBusy 0x89 The server to connect to is not available. The client is asked to try at a later time."},
{138 , "ClientBanned 0x8A The client has been banned from the server."},
{139 , "InvalidAuthenticationMethod 0x8C The authentication method specified is invalid."},
{143 , "InvalidTopicFilter 0x8F The topic filter specified is invalid."},
{144 , "InvalidTopicName 0x90 The topic name specified is invalid."},
{145 , "MessageIdInUse 0x91 The message ID used in the previous packet is already in use."},
{146 , "MessageIdNotFound 0x92 The message ID used in the previous packet has not been found."},
{149 , "PacketTooLarge 0x95 The packet received is too large. See also QMqttServerConnectionProperties::maximumPacketSize()."},
{151 , "QuotaExceeded 0x97 An administratively imposed limit has been exceeded."},
{153 , "InvalidPayloadFormat 0x99 The payload format is invalid. See also QMqttPublishProperties::payloadFormatIndicator()."},
{154 , "RetainNotSupported 0x9A The server does not support retained messages. See also QMqttServerConnectionProperties::retainAvailable()."},
{155 , "QoSNotSupported 0x9B The QoS level requested is not supported. See also QMqttServerConnectionProperties::maximumQoS()."},
{156 , "UseAnotherServer 0x9C The server the client tries to connect to is not available. See also QMqttServerConnectionProperties::serverReference()."},
{157 , "ServerMoved 0x9D The server the client tries to connect to has moved to a new address. See also QMqttServerConnectionProperties::serverReference()."},
{158 , "SharedSubscriptionsNotSupported 0x9E Shared subscriptions are not supported. See also QMqttServerConnectionProperties::sharedSubscriptionSupported()."},
{159 , "ExceededConnectionRate 0x9F The connection rate limit has been exceeded."},
{161 , "SubscriptionIdsNotSupported 0xA1 Subscription IDs are not supported. See also QMqttServerConnectionProperties::subscriptionIdentifierSupported()."},
{162 , "WildCardSubscriptionsNotSupported 0xA2 Subscriptions using wildcards are not supported by the server. See also QMqttServerConnectionProperties::wildcardSupported()."},
};
QHash <int, QString> qmqttErrors =
{
{1, "InvalidProtocolVersion : The broker does not accept a connection using the specified protocol version."},
{2, "IdRejected ; The client ID is malformed. This might be related to its length."},
{3, "ServerUnavailable :The network connection has been established, but the service is unavailable on the broker side."},
{4, "QMqttClient::Bad Username Or Password : The data in the username or password is malformed."},
{5, "NotAuthorized : The client is not authorized to connect."},
{256, "TransportInvalid : The underlying transport caused an error. For example, the connection might have been interrupted unexpectedly."},
{257, "ProtocolViolation : The client encountered a protocol violation, and therefore closed the connection."},
{258, "UnknownError : An unknown error occurred."},
{259, "Mqtt5SpecificError : The error is related to MQTT protocol level 5. A reason code might provide more details."}
};
QHash <int, QString> clientStateString
{
{0, "Disconnected : The client is disconnected from the broker."},
{1, "Connecting : A connection request has been made, but the broker has not approved the connection yet."},
{2, "Connected : The client is connected to the broker."}
};
QHash <int, QString> subscriptionStateString
{
{0, "Unsubscribed : The topic has been unsubscribed from."},
{1, "SubscriptionPending : A request for a subscription has been sent, but is has not been confirmed by the broker yet."},
{2, "Subscribed : The subscription was successful and messages will be received."},
{3, "UnsubscriptionPending : A request to unsubscribe from a topic has been sent, but it has not been confirmed by the broker yet."},
{4, "Error : An error occured."}
};
public slots:
void on_message(const QByteArray &message, const QMqttTopicName &topic);
void on_connect();
void on_disconnect();
void on_error(QMqttClient::ClientError error);
void on_messageSentStatusChanged(qint32 id, QMqtt::MessageStatus s, const QMqttMessageStatusProperties &properties);
void on_messageSent(int mid);
void updateStatus(QMqttSubscription::SubscriptionState state);
void on_stateChanged(QMqttClient::ClientState state);
// void updateMessage();
};
#endif //MQTT_H