2024-09-11 16:37:00 +08:00
# include "Mqttclient.h"
2023-04-27 19:48:15 +08:00
MqttClient : : MqttClient ( QObject * parent ) : QObject ( parent )
{
}
void MqttClient : : onMQTT_Connected ( )
{
qDebug ( ) < < " doConnected " < < endl ;
2024-11-15 11:44:54 +08:00
emit DoConnect_sig ( ) ;
2023-04-27 19:48:15 +08:00
}
void MqttClient : : onMQTT_disconnected ( )
{
qDebug ( ) < < " doDisconnected " < < endl ;
}
void MqttClient : : onMQTT_Connacked ( quint8 ack )
{
//todo: should emit on server suback
/*
QString ackStatus ;
switch ( ack ) {
case QMQTT : : CONNACK_ACCEPT :
ui - > pushButtonPusblish - > setEnabled ( true ) ;
ui - > pushButtonSubscribe - > setEnabled ( true ) ;
ackStatus = " Connection Accepted " ;
break ;
case QMQTT : : CONNACK_PROTO_VER :
ackStatus = " Connection Refused: unacceptable protocol version " ;
break ;
case QMQTT : : CONNACK_INVALID_ID :
ackStatus = " Connection Refused: identifier rejected " ;
break ;
case QMQTT : : CONNACK_SERVER :
ackStatus = " Connection Refused: server unavailable " ;
break ;
case QMQTT : : CONNACK_CREDENTIALS :
ackStatus = " Connection Refused: bad user name or password " ;
break ;
case QMQTT : : CONNACK_AUTH :
ackStatus = " Connection Refused: not authorized " ;
break ;
}
log ( tr ( " connacked: %1, %2 " ) . arg ( ack ) . arg ( ackStatus ) ) ;
*/
}
void MqttClient : : onMQTT_error ( QMQTT : : ClientError err )
{
//todo: should emit on server suback
2024-11-15 11:44:54 +08:00
QString errInfo ;
QTimer * timer ;
2023-04-27 19:48:15 +08:00
switch ( err ) {
// 0 The connection was refused by the peer (or timed out).
case QAbstractSocket : : ConnectionRefusedError :
errInfo = tr ( " Connection Refused " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
// 1 The remote host closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent.
case QAbstractSocket : : RemoteHostClosedError :
errInfo = tr ( " Remote Host Closed " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
// 2 The host address was not found.
case QAbstractSocket : : HostNotFoundError :
errInfo = tr ( " Host Not Found Error " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
// 3 The socket operation failed because the application lacked the required privileges.
case QAbstractSocket : : SocketAccessError :
errInfo = tr ( " Socket Access Error " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
// 4 The local system ran out of resources (e.g., too many sockets).
case QAbstractSocket : : SocketResourceError :
errInfo = tr ( " Socket Resource Error " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
// 5 The socket operation timed out.
case QAbstractSocket : : SocketTimeoutError :
errInfo = tr ( " Socket Timeout Error " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
default :
errInfo = tr ( " Socket Error " ) ;
2024-11-15 11:44:54 +08:00
break ;
2023-04-27 19:48:15 +08:00
}
2024-11-15 11:44:54 +08:00
qDebug ( ) < < errInfo < < endl ;
2023-04-27 19:48:15 +08:00
}
void MqttClient : : onMQTT_Published ( const QMQTT : : Message & message )
{
qDebug ( ) < < " onMQTT_Published " < < endl ;
}
void MqttClient : : onMQTT_Pubacked ( quint8 type , quint16 msgid )
{
}
void MqttClient : : onMQTT_Received ( const QMQTT : : Message & message )
{
QString mes = QString ( message . id ( ) ) + " " + QString ( message . qos ( ) ) + " " + message . topic ( ) + " " + message . payload ( ) + " " ;
emit Recevive_sig ( message . topic ( ) , message . payload ( ) ) ;
}
void MqttClient : : onMQTT_subscribed ( const QString & topic )
{
qDebug ( ) < < " onMQTT_subscribed " < < topic < < endl ;
}
void MqttClient : : onMQTT_subacked ( quint16 msgid , quint8 qos )
{
}
void MqttClient : : onMQTT_unsubscribed ( const QString & topic )
{
2024-11-15 11:44:54 +08:00
m_client - > unsubscribe ( topic ) ;
2023-04-27 19:48:15 +08:00
}
void MqttClient : : onMQTT_unsubacked ( quint16 msgid )
{
}
void MqttClient : : Push ( const QMQTT : : Message & message )
{
qDebug ( ) < < " onMQTT_Published " < < endl ;
m_client - > publish ( message ) ;
}
void MqttClient : : subscribed ( QString strTopic )
{
qDebug ( ) < < " subscribed " < < endl ;
m_client - > subscribe ( strTopic ) ;
}
void MqttClient : : ConnectMQTT ( QString strIP )
{
m_client = new QMQTT : : Client ( QHostAddress ( strIP ) , 1883 ) ;
2023-10-19 14:15:31 +08:00
m_client - > setUsername ( " chaos " ) ;
m_client - > setPassword ( " HSD272*#xkd " ) ;
2023-04-27 19:48:15 +08:00
m_client - > connectToHost ( ) ;
2024-11-15 11:44:54 +08:00
m_client - > setAutoReconnect ( true ) ;
m_client - > setAutoReconnectInterval ( 60 ) ;
2023-04-27 19:48:15 +08:00
connect ( m_client , SIGNAL ( connected ( ) ) , this , SLOT ( onMQTT_Connected ( ) ) ) ;
//todo: should emit on server suback
//connect(_client, SIGNAL(connacked(quint8)), this, SLOT(onMQTT_Connacked(quint8)));
connect ( m_client , SIGNAL ( error ( QMQTT : : ClientError ) ) , this , SLOT ( onMQTT_error ( QMQTT : : ClientError ) ) ) ;
//slots changes
//API: void published(const QMQTT::Message& message);
connect ( m_client , SIGNAL ( published ( const QMQTT : : Message & ) ) , this , SLOT ( onMQTT_Published ( const QMQTT : : Message & ) ) ) ;
//todo: should emit on server suback
//connect(_client, SIGNAL(pubacked(quint8, quint16)), this, SLOT(onMQTT_Pubacked(quint8, quint16)));
connect ( m_client , SIGNAL ( received ( const QMQTT : : Message & ) ) , this , SLOT ( onMQTT_Received ( const QMQTT : : Message & ) ) ) ;
connect ( m_client , SIGNAL ( subscribed ( const QString & ) ) , this , SLOT ( onMQTT_subscribed ( const QString & ) ) ) ;
}