From 3774a4adc9effb5bfc0cfc8d540895f73304672e Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Thu, 24 Jun 2021 11:28:13 +0200 Subject: [PATCH] Add some convenience methods to access states/stateTypes ba name --- libnymea/integrations/thing.cpp | 5 +++++ libnymea/integrations/thing.h | 1 + libnymea/types/thingclass.cpp | 36 ++++++++++++++++++++++++++++++--- libnymea/types/thingclass.h | 9 ++++++--- nymea.pro | 2 +- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/libnymea/integrations/thing.cpp b/libnymea/integrations/thing.cpp index 4e13c075..abfbf97f 100644 --- a/libnymea/integrations/thing.cpp +++ b/libnymea/integrations/thing.cpp @@ -331,6 +331,11 @@ QVariant Thing::stateValue(const StateTypeId &stateTypeId) const return QVariant(); } +QVariant Thing::stateValue(const QString &stateName) const +{ + return stateValue(m_thingClass.stateTypes().findByName(stateName).id()); +} + /*! For convenience, this finds the \l{State} matching the given \a stateTypeId in this thing and sets the current value to \a value. */ void Thing::setStateValue(const StateTypeId &stateTypeId, const QVariant &value) { diff --git a/libnymea/integrations/thing.h b/libnymea/integrations/thing.h index 6dd02a97..220720e0 100644 --- a/libnymea/integrations/thing.h +++ b/libnymea/integrations/thing.h @@ -133,6 +133,7 @@ public: void setStates(const States &states); Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId) const; + Q_INVOKABLE QVariant stateValue(const QString &stateName) const; Q_INVOKABLE void setStateValue(const StateTypeId &stateTypeId, const QVariant &value); Q_INVOKABLE State state(const StateTypeId &stateTypeId) const; diff --git a/libnymea/types/thingclass.cpp b/libnymea/types/thingclass.cpp index 48fb9988..33d114ae 100644 --- a/libnymea/types/thingclass.cpp +++ b/libnymea/types/thingclass.cpp @@ -160,7 +160,7 @@ void ThingClass::setStateTypes(const StateTypes &stateTypes) } /*! Returns true if this DeviceClass has a \l{StateType} with the given \a stateTypeId. */ -bool ThingClass::hasStateType(const StateTypeId &stateTypeId) +bool ThingClass::hasStateType(const StateTypeId &stateTypeId) const { foreach (const StateType &stateType, m_stateTypes) { if (stateType.id() == stateTypeId) { @@ -170,6 +170,16 @@ bool ThingClass::hasStateType(const StateTypeId &stateTypeId) return false; } +bool ThingClass::hasStateType(const QString &stateTypeName) const +{ + foreach (const StateType &stateType, m_stateTypes) { + if (stateType.name() == stateTypeName) { + return true; + } + } + return false; +} + /*! Returns the eventTypes of this DeviceClass. \{Device}{Devices} created from this \l{DeviceClass} must have their events matching to this template. */ EventTypes ThingClass::eventTypes() const @@ -185,7 +195,7 @@ void ThingClass::setEventTypes(const EventTypes &eventTypes) } /*! Returns true if this DeviceClass has a \l{EventType} with the given \a eventTypeId. */ -bool ThingClass::hasEventType(const EventTypeId &eventTypeId) +bool ThingClass::hasEventType(const EventTypeId &eventTypeId) const { foreach (const EventType &eventType, m_eventTypes) { if (eventType.id() == eventTypeId) { @@ -195,6 +205,16 @@ bool ThingClass::hasEventType(const EventTypeId &eventTypeId) return false; } +bool ThingClass::hasEventType(const QString &eventTypeName) const +{ + foreach (const EventType &eventType, m_eventTypes) { + if (eventType.name() == eventTypeName) { + return true; + } + } + return false; +} + /*! Returns the actionTypes of this DeviceClass. \{Device}{Devices} created from this \l{DeviceClass} must have their actions matching to this template. */ ActionTypes ThingClass::actionTypes() const @@ -210,7 +230,7 @@ void ThingClass::setActionTypes(const ActionTypes &actionTypes) } /*! Returns true if this DeviceClass has a \l{ActionType} with the given \a actionTypeId. */ -bool ThingClass::hasActionType(const ActionTypeId &actionTypeId) +bool ThingClass::hasActionType(const ActionTypeId &actionTypeId) const { foreach (const ActionType &actionType, m_actionTypes) { if (actionType.id() == actionTypeId) { @@ -220,6 +240,16 @@ bool ThingClass::hasActionType(const ActionTypeId &actionTypeId) return false; } +bool ThingClass::hasActionType(const QString &actionTypeName) const +{ + foreach (const ActionType &actionType, m_actionTypes) { + if (actionType.name() == actionTypeName) { + return true; + } + } + return false; +} + /*! Returns the browserItemActionTypes of this DeviceClass. \{Device}{Devices} created from this \l{DeviceClass} may set those actions to their browser items. */ ActionTypes ThingClass::browserItemActionTypes() const diff --git a/libnymea/types/thingclass.h b/libnymea/types/thingclass.h index 2597b7de..66ce7a09 100644 --- a/libnymea/types/thingclass.h +++ b/libnymea/types/thingclass.h @@ -98,15 +98,18 @@ public: StateTypes stateTypes() const; StateType getStateType(const StateTypeId &stateTypeId); void setStateTypes(const StateTypes &stateTypes); - bool hasStateType(const StateTypeId &stateTypeId); + bool hasStateType(const StateTypeId &stateTypeId) const; + bool hasStateType(const QString &stateTypeName) const; EventTypes eventTypes() const; void setEventTypes(const EventTypes &eventTypes); - bool hasEventType(const EventTypeId &eventTypeId); + bool hasEventType(const EventTypeId &eventTypeId) const; + bool hasEventType(const QString &eventTypeName) const; ActionTypes actionTypes() const; void setActionTypes(const ActionTypes &actionTypes); - bool hasActionType(const ActionTypeId &actionTypeId); + bool hasActionType(const ActionTypeId &actionTypeId) const; + bool hasActionType(const QString &actionTypeName) const; bool browsable() const; void setBrowsable(bool browsable); diff --git a/nymea.pro b/nymea.pro index f51820af..44b245e1 100644 --- a/nymea.pro +++ b/nymea.pro @@ -8,7 +8,7 @@ JSON_PROTOCOL_VERSION_MAJOR=5 JSON_PROTOCOL_VERSION_MINOR=5 JSON_PROTOCOL_VERSION="$${JSON_PROTOCOL_VERSION_MAJOR}.$${JSON_PROTOCOL_VERSION_MINOR}" LIBNYMEA_API_VERSION_MAJOR=7 -LIBNYMEA_API_VERSION_MINOR=0 +LIBNYMEA_API_VERSION_MINOR=1 LIBNYMEA_API_VERSION_PATCH=0 LIBNYMEA_API_VERSION="$${LIBNYMEA_API_VERSION_MAJOR}.$${LIBNYMEA_API_VERSION_MINOR}.$${LIBNYMEA_API_VERSION_PATCH}"