Show MQTT errors in messagebox

This commit is contained in:
Alex Spataru 2021-02-27 23:11:04 -05:00
parent 48e8dc24b5
commit 92323f546d
4 changed files with 189 additions and 90 deletions

View File

@ -59,11 +59,11 @@ Control {
text: qsTr("Version") + ":"
} ComboBox {
Layout.fillWidth: true
model: Cpp_MQTT_Publisher.mqttVersions
currentIndex: Cpp_MQTT_Publisher.mqttVersion
model: Cpp_MQTT_Client.mqttVersions
currentIndex: Cpp_MQTT_Client.mqttVersion
onCurrentIndexChanged: {
if (Cpp_MQTT_Publisher.mqttVersion !== currentIndex)
Cpp_MQTT_Publisher.mqttVersion = currentIndex
if (Cpp_MQTT_Client.mqttVersion !== currentIndex)
Cpp_MQTT_Client.mqttVersion = currentIndex
}
}
@ -74,11 +74,11 @@ Control {
text: qsTr("Mode") + ":"
} ComboBox {
Layout.fillWidth: true
model: Cpp_MQTT_Publisher.clientModes
currentIndex: Cpp_MQTT_Publisher.clientMode
model: Cpp_MQTT_Client.clientModes
currentIndex: Cpp_MQTT_Client.clientMode
onCurrentIndexChanged: {
if (Cpp_MQTT_Publisher.clientMode !== currentIndex)
Cpp_MQTT_Publisher.clientMode = currentIndex
if (Cpp_MQTT_Client.clientMode !== currentIndex)
Cpp_MQTT_Client.clientMode = currentIndex
}
}
@ -87,21 +87,21 @@ Control {
//
Label {
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
text: qsTr("Host") + ":"
} TextField {
id: _host
Layout.fillWidth: true
text: Cpp_MQTT_Publisher.host
placeholderText: Cpp_MQTT_Publisher.defaultHost
text: Cpp_MQTT_Client.host
placeholderText: Cpp_MQTT_Client.defaultHost
onTextChanged: {
if (Cpp_MQTT_Publisher.host !== text)
Cpp_MQTT_Publisher.host = text
if (Cpp_MQTT_Client.host !== text)
Cpp_MQTT_Client.host = text
}
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
}
@ -110,17 +110,17 @@ Control {
//
Label {
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
text: qsTr("Port") + ":"
} TextField {
id: _port
Layout.fillWidth: true
text: Cpp_MQTT_Publisher.port
placeholderText: Cpp_MQTT_Publisher.defaultPort
text: Cpp_MQTT_Client.port
placeholderText: Cpp_MQTT_Client.defaultPort
onTextChanged: {
if (Cpp_MQTT_Publisher.port !== text)
Cpp_MQTT_Publisher.port = text
if (Cpp_MQTT_Client.port !== text)
Cpp_MQTT_Client.port = text
}
validator: IntValidator {
@ -129,7 +129,7 @@ Control {
}
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
}
@ -138,21 +138,21 @@ Control {
//
Label {
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
text: qsTr("Topic") + ":"
} TextField {
id: _topic
Layout.fillWidth: true
text: Cpp_MQTT_Publisher.topic
text: Cpp_MQTT_Client.topic
placeholderText: qsTr("MQTT topic")
onTextChanged: {
if (Cpp_MQTT_Publisher.topic !== text)
Cpp_MQTT_Publisher.topic = text
if (Cpp_MQTT_Client.topic !== text)
Cpp_MQTT_Client.topic = text
}
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
}
@ -161,21 +161,21 @@ Control {
//
Label {
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
text: qsTr("User") + ":"
} TextField {
id: _user
Layout.fillWidth: true
text: Cpp_MQTT_Publisher.username
text: Cpp_MQTT_Client.username
placeholderText: qsTr("MQTT username")
onTextChanged: {
if (Cpp_MQTT_Publisher.username !== text)
Cpp_MQTT_Publisher.username = text
if (Cpp_MQTT_Client.username !== text)
Cpp_MQTT_Client.username = text
}
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
}
@ -184,7 +184,7 @@ Control {
//
Label {
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
text: qsTr("Password") + ":"
} RowLayout {
@ -195,15 +195,15 @@ Control {
id: _password
Layout.fillWidth: true
echoMode: TextField.PasswordEchoOnEdit
text: Cpp_MQTT_Publisher.password
text: Cpp_MQTT_Client.password
placeholderText: qsTr("MQTT password")
onTextChanged: {
if (Cpp_MQTT_Publisher.password !== text)
Cpp_MQTT_Publisher.password = text
if (Cpp_MQTT_Client.password !== text)
Cpp_MQTT_Client.password = text
}
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
}
@ -233,7 +233,7 @@ Control {
Label {
text: qsTr("DNS lookup") + ": "
opacity: enabled ? 1 : 0.5
enabled: !Cpp_MQTT_Publisher.isConnectedToHost
enabled: !Cpp_MQTT_Client.isConnectedToHost
Behavior on opacity {NumberAnimation{}}
} RowLayout {
Layout.fillWidth: true
@ -244,10 +244,10 @@ Control {
Layout.fillWidth: true
opacity: enabled ? 1 : 0.5
Layout.alignment: Qt.AlignVCenter
onAccepted: Cpp_MQTT_Publisher.lookup(text)
onAccepted: Cpp_MQTT_Client.lookup(text)
placeholderText: qsTr("Enter address (e.g. google.com)")
enabled: !Cpp_MQTT_Publisher.isConnectedToHost &&
!Cpp_MQTT_Publisher.lookupActive
enabled: !Cpp_MQTT_Client.isConnectedToHost &&
!Cpp_MQTT_Client.lookupActive
Behavior on opacity {NumberAnimation{}}
}
@ -258,10 +258,10 @@ Control {
Layout.maximumWidth: height
Layout.alignment: Qt.AlignVCenter
icon.source: "qrc:/icons/search.svg"
onClicked: Cpp_MQTT_Publisher.lookup(_addrLookup.text)
onClicked: Cpp_MQTT_Client.lookup(_addrLookup.text)
enabled: _addrLookup.text.length > 0 &&
!Cpp_MQTT_Publisher.isConnectedToHost &&
!Cpp_MQTT_Publisher.lookupActive
!Cpp_MQTT_Client.isConnectedToHost &&
!Cpp_MQTT_Client.lookupActive
Behavior on opacity {NumberAnimation{}}
}
@ -283,8 +283,8 @@ Control {
font.bold: true
Layout.fillWidth: true
icon.color: palette.buttonText
checked: Cpp_MQTT_Publisher.isConnectedToHost
onClicked: Cpp_MQTT_Publisher.toggleConnection()
checked: Cpp_MQTT_Client.isConnectedToHost
onClicked: Cpp_MQTT_Client.toggleConnection()
palette.buttonText: checked ? "#d72d60" : "#2eed5c"
text: (checked ? qsTr("Disconnect") : qsTr("Connect")) + " "
icon.source: checked ? "qrc:/icons/disconnect.svg" : "qrc:/icons/connect.svg"

View File

@ -27,45 +27,45 @@
using namespace MQTT;
static Publisher *INSTANCE = nullptr;
static Client *INSTANCE = nullptr;
Publisher::Publisher()
Client::Client()
{
m_lookupActive = false;
m_clientMode = MQTTClientMode::ClientPublisher;
connect(&m_client, &QMQTT::Client::connected, this, &Publisher::connectedChanged);
connect(&m_client, &QMQTT::Client::disconnected, this, &Publisher::connectedChanged);
connect(&m_client, &QMQTT::Client::error, this, &Publisher::onError);
connect(&m_client, &QMQTT::Client::connected, this, &Client::connectedChanged);
connect(&m_client, &QMQTT::Client::disconnected, this, &Client::connectedChanged);
connect(&m_client, &QMQTT::Client::error, this, &Client::onError);
setPort(defaultPort());
setHost(defaultHost());
}
Publisher::~Publisher()
Client::~Client()
{
disconnectFromHost();
}
Publisher *Publisher::getInstance()
Client *Client::getInstance()
{
if (!INSTANCE)
INSTANCE = new Publisher;
INSTANCE = new Client;
return INSTANCE;
}
quint16 Publisher::port() const
quint16 Client::port() const
{
return m_client.port();
}
QString Publisher::topic() const
QString Client::topic() const
{
return m_topic;
}
int Publisher::mqttVersion() const
int Client::mqttVersion() const
{
switch (m_client.version())
{
@ -81,47 +81,47 @@ int Publisher::mqttVersion() const
}
}
int Publisher::clientMode() const
int Client::clientMode() const
{
return m_clientMode;
}
QString Publisher::username() const
QString Client::username() const
{
return m_client.username();
}
QString Publisher::password() const
QString Client::password() const
{
return QString::fromUtf8(m_client.password());
}
QString Publisher::host() const
QString Client::host() const
{
return m_client.host().toString();
}
bool Publisher::lookupActive() const
bool Client::lookupActive() const
{
return m_lookupActive;
}
bool Publisher::isConnectedToHost() const
bool Client::isConnectedToHost() const
{
return m_client.isConnectedToHost();
}
QStringList Publisher::clientModes() const
QStringList Client::clientModes() const
{
return QStringList { tr("Publisher"), tr("Suscriber") };
}
QStringList Publisher::mqttVersions() const
QStringList Client::mqttVersions() const
{
return QStringList { "MQTT 3.1.0", "MQTT 3.1.1" };
}
void Publisher::connectToHost()
void Client::connectToHost()
{
m_client.connectToHost();
}
@ -130,7 +130,7 @@ void Publisher::connectToHost()
* Connects/disconnects the application from the current MQTT broker. This function is
* used as a convenience for the connect/disconnect button.
*/
void Publisher::toggleConnection()
void Client::toggleConnection()
{
if (isConnectedToHost())
disconnectFromHost();
@ -138,7 +138,7 @@ void Publisher::toggleConnection()
connectToHost();
}
void Publisher::disconnectFromHost()
void Client::disconnectFromHost()
{
m_client.disconnectFromHost();
}
@ -146,50 +146,50 @@ void Publisher::disconnectFromHost()
/**
* Performs a DNS lookup for the given @a host name
*/
void Publisher::lookup(const QString &host)
void Client::lookup(const QString &host)
{
m_lookupActive = true;
emit lookupActiveChanged();
QHostInfo::lookupHost(host.simplified(), this, &Publisher::lookupFinished);
QHostInfo::lookupHost(host.simplified(), this, &Client::lookupFinished);
}
void Publisher::setPort(const quint16 port)
void Client::setPort(const quint16 port)
{
m_client.setPort(port);
emit portChanged();
}
void Publisher::setHost(const QString &host)
void Client::setHost(const QString &host)
{
m_client.setHost(QHostAddress(host));
emit hostChanged();
}
void Publisher::setClientMode(const int mode)
void Client::setClientMode(const int mode)
{
m_clientMode = (MQTTClientMode)mode;
emit clientModeChanged();
}
void Publisher::setTopic(const QString &topic)
void Client::setTopic(const QString &topic)
{
m_topic = topic;
emit topicChanged();
}
void Publisher::setUsername(const QString &username)
void Client::setUsername(const QString &username)
{
m_client.setUsername(username);
emit usernameChanged();
}
void Publisher::setPassword(const QString &password)
void Client::setPassword(const QString &password)
{
m_client.setPassword(password.toUtf8());
emit passwordChanged();
}
void Publisher::setMqttVersion(const int versionIndex)
void Client::setMqttVersion(const int versionIndex)
{
switch (versionIndex)
{
@ -206,19 +206,22 @@ void Publisher::setMqttVersion(const int versionIndex)
emit mqttVersionChanged();
}
void Publisher::sendData()
void Client::sendData()
{
// Sort JFI list from oldest to most recent
JFI_SortList(&m_jfiList);
// Send data in CSV format
// Clear JFI list
m_jfiList.clear();
}
/**
* Sets the host IP address when the lookup finishes.
* If the lookup fails, the error code/string shall be shown to the user in a messagebox.
*/
void Publisher::lookupFinished(const QHostInfo &info)
void Client::lookupFinished(const QHostInfo &info)
{
m_lookupActive = false;
emit lookupActiveChanged();
@ -236,12 +239,109 @@ void Publisher::lookupFinished(const QHostInfo &info)
Misc::Utilities::showMessageBox(tr("IP address lookup error"), info.errorString());
}
void Publisher::onError(const QMQTT::ClientError error)
void Client::onError(const QMQTT::ClientError error)
{
qDebug() << error;
QString str;
switch (error)
{
case QMQTT::UnknownError:
str = tr("Unknown error");
break;
case QMQTT::SocketConnectionRefusedError:
str = tr("Connection refused");
break;
case QMQTT::SocketRemoteHostClosedError:
str = tr("Remote host closed the connection");
break;
case QMQTT::SocketHostNotFoundError:
str = tr("Host not found");
break;
case QMQTT::SocketAccessError:
str = tr("Socket access error");
break;
case QMQTT::SocketResourceError:
str = tr("Socket resource error");
break;
case QMQTT::SocketTimeoutError:
str = tr("Socket timeout");
break;
case QMQTT::SocketDatagramTooLargeError:
str = tr("Socket datagram too large");
break;
case QMQTT::SocketNetworkError:
str = tr("Network error");
break;
case QMQTT::SocketAddressInUseError:
str = tr("Address in use");
break;
case QMQTT::SocketAddressNotAvailableError:
str = tr("Address not available");
break;
case QMQTT::SocketUnsupportedSocketOperationError:
str = tr("Unsupported socket operation");
break;
case QMQTT::SocketUnfinishedSocketOperationError:
str = tr("Unfinished socket operation");
break;
case QMQTT::SocketProxyAuthenticationRequiredError:
str = tr("Proxy authentication required");
break;
case QMQTT::SocketSslHandshakeFailedError:
str = tr("SSL handshake failed");
break;
case QMQTT::SocketProxyConnectionRefusedError:
str = tr("Proxy connection refused");
break;
case QMQTT::SocketProxyConnectionClosedError:
str = tr("Proxy connection closed");
break;
case QMQTT::SocketProxyConnectionTimeoutError:
str = tr("Proxy connection timeout");
break;
case QMQTT::SocketProxyNotFoundError:
str = tr("Proxy not found");
break;
case QMQTT::SocketProxyProtocolError:
str = tr("Proxy protocol error");
break;
case QMQTT::SocketOperationError:
str = tr("Operation error");
break;
case QMQTT::SocketSslInternalError:
str = tr("SSL internal error");
break;
case QMQTT::SocketSslInvalidUserDataError:
str = tr("Invalid SSL user data");
break;
case QMQTT::SocketTemporaryError:
str = tr("Socket temprary error");
break;
case QMQTT::MqttUnacceptableProtocolVersionError:
str = tr("Unacceptable MQTT protocol");
break;
case QMQTT::MqttIdentifierRejectedError:
str = tr("MQTT identifier rejected");
break;
case QMQTT::MqttServerUnavailableError:
str = tr("MQTT server unavailable");
break;
case QMQTT::MqttBadUserNameOrPasswordError:
str = tr("Bad MQTT username or password");
break;
case QMQTT::MqttNotAuthorizedError:
str = tr("MQTT authorization error");
break;
case QMQTT::MqttNoPingResponse:
str = tr("MQTT no ping response");
break;
}
if (!str.isEmpty())
Misc::Utilities::showMessageBox(tr("MQTT client error"), str);
}
void Publisher::registerJsonFrame(const JFI_Object &frameInfo)
void Client::registerJsonFrame(const JFI_Object &frameInfo)
{
m_jfiList.append(frameInfo);
}

View File

@ -20,8 +20,8 @@
* THE SOFTWARE.
*/
#ifndef MQTT_PUBLISHER_H
#define MQTT_PUBLISHER_H
#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H
#include <QObject>
#include <QHostInfo>
@ -41,7 +41,7 @@ enum MQTTClientMode
ClientSubscriber = 1
};
class Publisher : public QObject
class Client : public QObject
{
// clang-format off
Q_OBJECT
@ -105,7 +105,7 @@ signals:
void lookupActiveChanged();
public:
static Publisher *getInstance();
static Client *getInstance();
quint16 port() const;
QString host() const;
@ -120,7 +120,6 @@ public:
QStringList mqttVersions() const;
quint16 defaultPort() const { return 1883; }
QString defaultHost() const { return "127.0.0.1"; }
public slots:
@ -137,8 +136,8 @@ public slots:
void setMqttVersion(const int versionIndex);
private:
Publisher();
~Publisher();
Client();
~Client();
private slots:
void sendData();

View File

@ -150,7 +150,7 @@ void ModuleManager::initializeQmlInterface()
auto ioNetwork = IO::DataSources::Network::getInstance();
auto jsonGenerator = JSON::Generator::getInstance();
auto utilities = Misc::Utilities::getInstance();
auto mqttPublisher = MQTT::Publisher::getInstance();
auto mqttPublisher = MQTT::Client::getInstance();
LOG_INFO() << "Finished initializing C++ modules";
// Retranslate the QML interface automagically
@ -173,7 +173,7 @@ void ModuleManager::initializeQmlInterface()
c->setContextProperty("Cpp_IO_Serial", ioSerial);
c->setContextProperty("Cpp_IO_Network", ioNetwork);
c->setContextProperty("Cpp_JSON_Generator", jsonGenerator);
c->setContextProperty("Cpp_MQTT_Publisher", mqttPublisher);
c->setContextProperty("Cpp_MQTT_Client", mqttPublisher);
// Register app info with QML
c->setContextProperty("Cpp_AppName", qApp->applicationName());
@ -211,7 +211,7 @@ void ModuleManager::stopOperations()
CSV::Player::getInstance()->closeFile();
IO::Manager::getInstance()->disconnectDevice();
Misc::TimerEvents::getInstance()->stopTimers();
MQTT::Publisher::getInstance()->disconnectFromHost();
MQTT::Client::getInstance()->disconnectFromHost();
LOG_INFO() << "Application modules stopped";
}