From 5323ab26311e001b41ff51052fc0d4dd108542ea Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 10 Feb 2022 17:38:01 +0100 Subject: [PATCH] Add discoveryType thing class property Some plugins may support network discovery, but may not be able to clearly identify a device and return a list of discovery results that may be the device, but also may not. As this breaks some app flows they will be marked as "weak" discovery and can be excluded from said setup wizards. NOTE: This commit does not bump the API version even though it should to avoid merge conflicts with other branches that do bump the version. --- libnymea-core/jsonrpc/integrationshandler.cpp | 1 + libnymea/integrations/pluginmetadata.cpp | 17 ++++++++++++++++- libnymea/types/thingclass.cpp | 14 +++++++++++--- libnymea/types/thingclass.h | 15 +++++++++++++-- tests/auto/api.json | 5 +++++ 5 files changed, 46 insertions(+), 6 deletions(-) diff --git a/libnymea-core/jsonrpc/integrationshandler.cpp b/libnymea-core/jsonrpc/integrationshandler.cpp index abcbe0da..5c0f6fed 100644 --- a/libnymea-core/jsonrpc/integrationshandler.cpp +++ b/libnymea-core/jsonrpc/integrationshandler.cpp @@ -58,6 +58,7 @@ IntegrationsHandler::IntegrationsHandler(ThingManager *thingManager, QObject *pa registerEnum(); registerEnum(); registerFlag(); + registerEnum(); registerEnum(); registerEnum(); registerEnum(); diff --git a/libnymea/integrations/pluginmetadata.cpp b/libnymea/integrations/pluginmetadata.cpp index c13665ea..a0458cb2 100644 --- a/libnymea/integrations/pluginmetadata.cpp +++ b/libnymea/integrations/pluginmetadata.cpp @@ -203,7 +203,8 @@ void PluginMetadata::parse(const QJsonObject &jsonObject) /*! Returns a list of all valid JSON properties a ThingClass JSON definition can have. */ QStringList thingClassProperties = QStringList() << "id" << "name" << "displayName" << "createMethods" << "setupMethod" << "interfaces" << "providedInterfaces" << "browsable" << "discoveryParamTypes" - << "paramTypes" << "settingsTypes" << "stateTypes" << "actionTypes" << "eventTypes" << "browserItemActionTypes"; + << "paramTypes" << "settingsTypes" << "stateTypes" << "actionTypes" << "eventTypes" << "browserItemActionTypes" + << "discoveryType"; QStringList mandatoryThingClassProperties = QStringList() << "id" << "name" << "displayName"; QPair verificationResult = verifyFields(thingClassProperties, mandatoryThingClassProperties, thingClassObject); @@ -260,6 +261,20 @@ void PluginMetadata::parse(const QJsonObject &jsonObject) } thingClass.setCreateMethods(createMethods); + if (thingClassObject.contains("discoveryType")) { + QString discoveryTypeString = thingClassObject.value("discoveryType").toString(); + if (discoveryTypeString == "precise") { + thingClass.setDiscoveryType(ThingClass::DiscoveryTypePrecise); + } else if (discoveryTypeString == "weak") { + thingClass.setDiscoveryType(ThingClass::DiscoveryTypeWeak); + } else { + m_validationErrors.append("Unknown discoveryType \"" + discoveryTypeString + "\" in thingClass \"" + thingClass.name() + "\"."); + hasError = true; + } + } else { + thingClass.setDiscoveryType(ThingClass::DiscoveryTypePrecise); + } + // Read params QPair > paramTypesVerification = parseParamTypes(thingClassObject.value("paramTypes").toArray()); if (!paramTypesVerification.first) { diff --git a/libnymea/types/thingclass.cpp b/libnymea/types/thingclass.cpp index a4ae297e..c7cc3ba8 100644 --- a/libnymea/types/thingclass.cpp +++ b/libnymea/types/thingclass.cpp @@ -79,9 +79,7 @@ ThingClass::ThingClass(const PluginId &pluginId, const VendorId &vendorId, const ThingClassId &id): m_id(id), m_vendorId(vendorId), - m_pluginId(pluginId), - m_createMethods(CreateMethodUser), - m_setupMethod(SetupMethodJustAdd) + m_pluginId(pluginId) { } @@ -342,6 +340,16 @@ void ThingClass::setSetupMethod(ThingClass::SetupMethod setupMethod) m_setupMethod = setupMethod; } +ThingClass::DiscoveryType ThingClass::discoveryType() const +{ + return m_discoveryType; +} + +void ThingClass::setDiscoveryType(DiscoveryType discoveryType) +{ + m_discoveryType = discoveryType; +} + /*! Returns the \l{Interfaces for DeviceClasses}{interfaces} of this \l{DeviceClass}.*/ QStringList ThingClass::interfaces() const { diff --git a/libnymea/types/thingclass.h b/libnymea/types/thingclass.h index 14382e49..a8301a42 100644 --- a/libnymea/types/thingclass.h +++ b/libnymea/types/thingclass.h @@ -55,6 +55,7 @@ class LIBNYMEA_EXPORT ThingClass Q_PROPERTY(bool browsable READ browsable) Q_PROPERTY(SetupMethod setupMethod READ setupMethod) Q_PROPERTY(CreateMethods createMethods READ createMethods) + Q_PROPERTY(DiscoveryType discoveryType READ discoveryType) Q_PROPERTY(StateTypes stateTypes READ stateTypes) Q_PROPERTY(EventTypes eventTypes READ eventTypes) Q_PROPERTY(ActionTypes actionTypes READ actionTypes) @@ -83,6 +84,12 @@ public: }; Q_ENUM(SetupMethod) + enum DiscoveryType { + DiscoveryTypePrecise, + DiscoveryTypeWeak + }; + Q_ENUM(DiscoveryType) + ThingClass(const PluginId &pluginId = PluginId(), const VendorId &vendorId = VendorId(), const ThingClassId &id = ThingClassId()); ThingClassId id() const; @@ -134,6 +141,9 @@ public: SetupMethod setupMethod() const; void setSetupMethod(SetupMethod setupMethod); + DiscoveryType discoveryType() const; + void setDiscoveryType(DiscoveryType discoveryType); + QStringList interfaces() const; void setInterfaces(const QStringList &interfaces); @@ -156,8 +166,9 @@ private: ParamTypes m_paramTypes; ParamTypes m_settingsTypes; ParamTypes m_discoveryParamTypes; - CreateMethods m_createMethods; - SetupMethod m_setupMethod; + CreateMethods m_createMethods = CreateMethodUser; + SetupMethod m_setupMethod = SetupMethodJustAdd; + DiscoveryType m_discoveryType = DiscoveryTypePrecise; QStringList m_interfaces; QStringList m_providedInterfaces; }; diff --git a/tests/auto/api.json b/tests/auto/api.json index 4c3eafb4..f21df2a0 100644 --- a/tests/auto/api.json +++ b/tests/auto/api.json @@ -47,6 +47,10 @@ "CreateMethodAuto", "CreateMethodDiscovery" ], + "DiscoveryType": [ + "DiscoveryTypePrecise", + "DiscoveryTypeWeak" + ], "IOType": [ "IOTypeNone", "IOTypeDigitalInput", @@ -2620,6 +2624,7 @@ "r:browserItemActionTypes": "$ref:ActionTypes", "r:createMethods": "$ref:CreateMethods", "r:discoveryParamTypes": "$ref:ParamTypes", + "r:discoveryType": "$ref:DiscoveryType", "r:displayName": "String", "r:eventTypes": "$ref:EventTypes", "r:id": "Uuid",