3500/Mqttclient.cpp

165 lines
4.8 KiB
C++

#include "Mqttclient.h"
MqttClient::MqttClient(QObject *parent) : QObject(parent)
{
}
void MqttClient::onMQTT_Connected()
{
qDebug()<< "doConnected" <<endl;
emit DoConnect_sig();
}
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
QString errInfo;
QTimer *timer;
switch(err) {
// 0 The connection was refused by the peer (or timed out).
case QAbstractSocket::ConnectionRefusedError:
errInfo = tr("Connection Refused");
break;
// 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");
break;
// 2 The host address was not found.
case QAbstractSocket::HostNotFoundError:
errInfo = tr("Host Not Found Error");
break;
// 3 The socket operation failed because the application lacked the required privileges.
case QAbstractSocket::SocketAccessError:
errInfo = tr("Socket Access Error");
break;
// 4 The local system ran out of resources (e.g., too many sockets).
case QAbstractSocket::SocketResourceError:
errInfo = tr("Socket Resource Error");
break;
// 5 The socket operation timed out.
case QAbstractSocket::SocketTimeoutError:
errInfo = tr("Socket Timeout Error");
break;
default:
errInfo = tr("Socket Error");
break;
}
qDebug()<< errInfo <<endl;
}
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)
{
m_client->unsubscribe(topic);
}
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);
m_client->setUsername("chaos");
m_client->setPassword("HSD272*#xkd");
m_client->connectToHost();
m_client->setAutoReconnect(true);
m_client->setAutoReconnectInterval(60);
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 &)));
}