Add api to handle discovery errors better

pull/654/head
Michael Zanetti 2021-08-24 16:37:01 +02:00
parent 47ea7087c4
commit 0ba03ab8bb
2 changed files with 27 additions and 16 deletions

View File

@ -32,6 +32,7 @@
#include "engine.h" #include "engine.h"
#include <QMetaEnum>
#include <QLoggingCategory> #include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcThingManager) Q_DECLARE_LOGGING_CATEGORY(dcThingManager)
@ -73,7 +74,7 @@ QHash<int, QByteArray> ThingDiscovery::roleNames() const
return roles; return roles;
} }
void ThingDiscovery::discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams) int ThingDiscovery::discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams)
{ {
beginResetModel(); beginResetModel();
m_foundThings.clear(); m_foundThings.clear();
@ -82,32 +83,35 @@ void ThingDiscovery::discoverThings(const QUuid &thingClassId, const QVariantLis
if (!m_engine) { if (!m_engine) {
qCWarning(dcThingManager()) << "Cannot discover things. No Engine set"; qCWarning(dcThingManager()) << "Cannot discover things. No Engine set";
return; return -1;
} }
if (!m_engine->jsonRpcClient()->connected()) { if (!m_engine->jsonRpcClient()->connected()) {
qCWarning(dcThingManager()) << "Cannot discover things. Not connected."; qCWarning(dcThingManager()) << "Cannot discover things. Not connected.";
return; return -1;
} }
discoverThingsInternal(thingClassId, discoveryParams); int commandId = discoverThingsInternal(thingClassId, discoveryParams);
m_displayMessage.clear(); m_displayMessage.clear();
emit busyChanged(); emit busyChanged();
return commandId;
} }
void ThingDiscovery::discoverThingsByInterface(const QString &interfaceName) QList<int> ThingDiscovery::discoverThingsByInterface(const QString &interfaceName)
{ {
beginResetModel(); beginResetModel();
m_foundThings.clear(); m_foundThings.clear();
endResetModel(); endResetModel();
emit countChanged(); emit countChanged();
QList<int> pendingCommands;
if (!m_engine) { if (!m_engine) {
qCWarning(dcThingManager()) << "Cannot discover things. No Engine set"; qCWarning(dcThingManager()) << "Cannot discover things. No Engine set";
return; return pendingCommands;
} }
if (!m_engine->jsonRpcClient()->connected()) { if (!m_engine->jsonRpcClient()->connected()) {
qCWarning(dcThingManager()) << "Cannot discover things. Not connected."; qCWarning(dcThingManager()) << "Cannot discover things. Not connected.";
return; return pendingCommands;
} }
for (int i = 0; i < m_engine->thingManager()->thingClasses()->rowCount(); i++) { for (int i = 0; i < m_engine->thingManager()->thingClasses()->rowCount(); i++) {
@ -115,11 +119,12 @@ void ThingDiscovery::discoverThingsByInterface(const QString &interfaceName)
if (!thingClass->interfaces().contains(interfaceName)) { if (!thingClass->interfaces().contains(interfaceName)) {
continue; continue;
} }
discoverThingsInternal(thingClass->id()); pendingCommands.append(discoverThingsInternal(thingClass->id()));
} }
m_displayMessage.clear(); m_displayMessage.clear();
emit busyChanged(); emit busyChanged();
return pendingCommands;
} }
ThingDescriptor *ThingDiscovery::get(int index) const ThingDescriptor *ThingDiscovery::get(int index) const
@ -153,7 +158,7 @@ QString ThingDiscovery::displayMessage() const
return m_displayMessage; return m_displayMessage;
} }
void ThingDiscovery::discoverThingsInternal(const QUuid &thingClassId, const QVariantList &discoveryParams) int ThingDiscovery::discoverThingsInternal(const QUuid &thingClassId, const QVariantList &discoveryParams)
{ {
qCDebug(dcThingManager()) << "Starting thing discovery for thing class" << m_engine->thingManager()->thingClasses()->getThingClass(thingClassId)->name() << thingClassId; qCDebug(dcThingManager()) << "Starting thing discovery for thing class" << m_engine->thingManager()->thingClasses()->getThingClass(thingClassId)->name() << thingClassId;
QVariantMap params; QVariantMap params;
@ -163,6 +168,7 @@ void ThingDiscovery::discoverThingsInternal(const QUuid &thingClassId, const QVa
} }
int commandId = m_engine->jsonRpcClient()->sendCommand("Integrations.DiscoverThings", params, this, "discoverThingsResponse"); int commandId = m_engine->jsonRpcClient()->sendCommand("Integrations.DiscoverThings", params, this, "discoverThingsResponse");
m_pendingRequests.append(commandId); m_pendingRequests.append(commandId);
return commandId;
} }
void ThingDiscovery::discoverThingsResponse(int commandId, const QVariantMap &params) void ThingDiscovery::discoverThingsResponse(int commandId, const QVariantMap &params)
@ -199,6 +205,11 @@ void ThingDiscovery::discoverThingsResponse(int commandId, const QVariantMap &pa
// Note: in case of multiple discoveries we'll just overwrite the message... Not ideal but multiple error messages from different plugins // Note: in case of multiple discoveries we'll just overwrite the message... Not ideal but multiple error messages from different plugins
// wouldn't be of much use to the user anyways. // wouldn't be of much use to the user anyways.
m_displayMessage = params.value("displayMessage").toString(); m_displayMessage = params.value("displayMessage").toString();
QMetaEnum metaEnum = QMetaEnum::fromType<Thing::ThingError>();
Thing::ThingError thingError = static_cast<Thing::ThingError>(metaEnum.keyToValue(params.value("thingError").toByteArray()));
emit discoverThingsReply(commandId, thingError, m_displayMessage);
m_pendingRequests.removeAll(commandId); m_pendingRequests.removeAll(commandId);
if (m_pendingRequests.isEmpty()) { if (m_pendingRequests.isEmpty()) {
emit busyChanged(); emit busyChanged();

View File

@ -85,8 +85,8 @@ public:
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE void discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams = {}); Q_INVOKABLE int discoverThings(const QUuid &thingClassId, const QVariantList &discoveryParams = {});
Q_INVOKABLE void discoverThingsByInterface(const QString &interfaceName); Q_INVOKABLE QList<int> discoverThingsByInterface(const QString &interfaceName);
Q_INVOKABLE ThingDescriptor* get(int index) const; Q_INVOKABLE ThingDescriptor* get(int index) const;
@ -96,15 +96,15 @@ public:
bool busy() const; bool busy() const;
QString displayMessage() const; QString displayMessage() const;
private slots:
void discoverThingsInternal(const QUuid &thingClassId, const QVariantList &discoveryParams = {});
void discoverThingsResponse(int commandId, const QVariantMap &params);
signals: signals:
void busyChanged(); void busyChanged();
void countChanged(); void countChanged();
void engineChanged(); void engineChanged();
void discoverThingsReply(int commandId, Thing::ThingError thingError, const QString &displayMessage);
private slots:
int discoverThingsInternal(const QUuid &thingClassId, const QVariantList &discoveryParams = {});
void discoverThingsResponse(int commandId, const QVariantMap &params);
private: private:
Engine *m_engine = nullptr; Engine *m_engine = nullptr;