Merge PR #436: Add some convenience methods to access states/stateTypes by name

This commit is contained in:
Jenkins nymea 2021-07-06 17:18:30 +02:00
commit cdedaca2b7
4 changed files with 45 additions and 6 deletions

View File

@ -331,6 +331,11 @@ QVariant Thing::stateValue(const StateTypeId &stateTypeId) const
return QVariant(); 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. */ /*! 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) void Thing::setStateValue(const StateTypeId &stateTypeId, const QVariant &value)
{ {

View File

@ -133,6 +133,7 @@ public:
void setStates(const States &states); void setStates(const States &states);
Q_INVOKABLE QVariant stateValue(const StateTypeId &stateTypeId) const; 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 void setStateValue(const StateTypeId &stateTypeId, const QVariant &value);
Q_INVOKABLE State state(const StateTypeId &stateTypeId) const; Q_INVOKABLE State state(const StateTypeId &stateTypeId) const;

View File

@ -160,7 +160,7 @@ void ThingClass::setStateTypes(const StateTypes &stateTypes)
} }
/*! Returns true if this DeviceClass has a \l{StateType} with the given \a stateTypeId. */ /*! 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) { foreach (const StateType &stateType, m_stateTypes) {
if (stateType.id() == stateTypeId) { if (stateType.id() == stateTypeId) {
@ -170,6 +170,16 @@ bool ThingClass::hasStateType(const StateTypeId &stateTypeId)
return false; 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 /*! Returns the eventTypes of this DeviceClass. \{Device}{Devices} created
from this \l{DeviceClass} must have their events matching to this template. */ from this \l{DeviceClass} must have their events matching to this template. */
EventTypes ThingClass::eventTypes() const 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. */ /*! 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) { foreach (const EventType &eventType, m_eventTypes) {
if (eventType.id() == eventTypeId) { if (eventType.id() == eventTypeId) {
@ -195,6 +205,16 @@ bool ThingClass::hasEventType(const EventTypeId &eventTypeId)
return false; 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 /*! Returns the actionTypes of this DeviceClass. \{Device}{Devices} created
from this \l{DeviceClass} must have their actions matching to this template. */ from this \l{DeviceClass} must have their actions matching to this template. */
ActionTypes ThingClass::actionTypes() const 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. */ /*! 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) { foreach (const ActionType &actionType, m_actionTypes) {
if (actionType.id() == actionTypeId) { if (actionType.id() == actionTypeId) {
@ -220,6 +240,16 @@ bool ThingClass::hasActionType(const ActionTypeId &actionTypeId)
return false; 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 /*! Returns the browserItemActionTypes of this DeviceClass. \{Device}{Devices} created
from this \l{DeviceClass} may set those actions to their browser items. */ from this \l{DeviceClass} may set those actions to their browser items. */
ActionTypes ThingClass::browserItemActionTypes() const ActionTypes ThingClass::browserItemActionTypes() const

View File

@ -98,15 +98,18 @@ public:
StateTypes stateTypes() const; StateTypes stateTypes() const;
StateType getStateType(const StateTypeId &stateTypeId); StateType getStateType(const StateTypeId &stateTypeId);
void setStateTypes(const StateTypes &stateTypes); 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; EventTypes eventTypes() const;
void setEventTypes(const EventTypes &eventTypes); 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; ActionTypes actionTypes() const;
void setActionTypes(const ActionTypes &actionTypes); 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; bool browsable() const;
void setBrowsable(bool browsable); void setBrowsable(bool browsable);