Thing: Add state name based set method for possible values

This commit is contained in:
Simon Stürz 2026-02-10 09:46:27 +01:00
parent abd8dd2d97
commit b58c5646df
4 changed files with 26 additions and 17 deletions

View File

@ -3,7 +3,7 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH * Copyright (C) 2024 - 2026, chargebyte austria GmbH
* *
* This file is part of nymea. * This file is part of nymea.
* *
@ -25,10 +25,11 @@
#ifndef BROWSERESULT_H #ifndef BROWSERESULT_H
#define BROWSERESULT_H #define BROWSERESULT_H
#include <QObject>
#include <QLocale> #include <QLocale>
#include <QObject>
#include "thing.h" #include "thing.h"
#include "types/browseritem.h"
class ThingManager; class ThingManager;
@ -38,7 +39,7 @@ class BrowseResult : public QObject
public: public:
explicit BrowseResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); explicit BrowseResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0);
Thing* thing() const; Thing *thing() const;
QString itemId() const; QString itemId() const;
QLocale locale() const; QLocale locale() const;

View File

@ -3,7 +3,7 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH * Copyright (C) 2024 - 2026, chargebyte austria GmbH
* *
* This file is part of nymea. * This file is part of nymea.
* *
@ -25,10 +25,11 @@
#ifndef BROWSERITEMRESULT_H #ifndef BROWSERITEMRESULT_H
#define BROWSERITEMRESULT_H #define BROWSERITEMRESULT_H
#include <QObject>
#include <QLocale> #include <QLocale>
#include <QObject>
#include "thing.h" #include "thing.h"
#include "types/browseritem.h"
class ThingManager; class ThingManager;
@ -39,7 +40,7 @@ class BrowserItemResult : public QObject
public: public:
explicit BrowserItemResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0); explicit BrowserItemResult(Thing *thing, ThingManager *thingManager, const QString &itemId, const QLocale &locale, QObject *parent, quint32 timeout = 0);
Thing* thing() const; Thing *thing() const;
QString itemId() const; QString itemId() const;
QLocale locale() const; QLocale locale() const;

View File

@ -3,7 +3,7 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH * Copyright (C) 2024 - 2026, chargebyte austria GmbH
* *
* This file is part of nymea. * This file is part of nymea.
* *
@ -313,7 +313,6 @@ States Thing::states() const
return m_states; return m_states;
} }
/*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */ /*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */
bool Thing::hasParam(const QString &paramName) const bool Thing::hasParam(const QString &paramName) const
{ {
@ -321,7 +320,6 @@ bool Thing::hasParam(const QString &paramName) const
return m_params.hasParam(paramTypeId); return m_params.hasParam(paramTypeId);
} }
/*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */ /*! Returns true, a \l{Param} with the given \a paramTypeId exists for this thing. */
bool Thing::hasParam(const ParamTypeId &paramTypeId) const bool Thing::hasParam(const ParamTypeId &paramTypeId) const
{ {
@ -516,7 +514,7 @@ void Thing::setStateValue(const QString &stateName, const QVariant &value)
setStateValue(stateTypeId, value); setStateValue(stateTypeId, value);
} }
/*! Sets the minimum value for the \l{State} matching the given \a stateTypeId in this thing to value. */ /*! Sets the minimum value for the \l{State} matching the given \a stateTypeId in this thing to \a minValue. */
void Thing::setStateMinValue(const StateTypeId &stateTypeId, const QVariant &minValue) void Thing::setStateMinValue(const StateTypeId &stateTypeId, const QVariant &minValue)
{ {
StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); StateType stateType = m_thingClass.stateTypes().findById(stateTypeId);
@ -559,14 +557,14 @@ void Thing::setStateMinValue(const StateTypeId &stateTypeId, const QVariant &min
qCWarning(dcThing()).nospace() << this << ": Failed setting minimum state value " << stateType.name() << " to " << minValue; qCWarning(dcThing()).nospace() << this << ": Failed setting minimum state value " << stateType.name() << " to " << minValue;
} }
/*! Sets the minimum value for the \l{State} matching the given \a stateName in this thing to value. */ /*! Sets the minimum value for the \l{State} matching the given \a stateName in this thing to \a minValue. */
void Thing::setStateMinValue(const QString &stateName, const QVariant &minValue) void Thing::setStateMinValue(const QString &stateName, const QVariant &minValue)
{ {
StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id();
setStateMinValue(stateTypeId, minValue); setStateMinValue(stateTypeId, minValue);
} }
/*! Sets the maximum value for the \l{State} matching the given \a stateTypeId in this thing to value. */ /*! Sets the maximum value for the \l{State} matching the given \a stateTypeId in this thing to \a maxValue. */
void Thing::setStateMaxValue(const StateTypeId &stateTypeId, const QVariant &maxValue) void Thing::setStateMaxValue(const StateTypeId &stateTypeId, const QVariant &maxValue)
{ {
StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); StateType stateType = m_thingClass.stateTypes().findById(stateTypeId);
@ -619,7 +617,7 @@ void Thing::setStateMaxValue(const StateTypeId &stateTypeId, const QVariant &max
qCWarning(dcThing()).nospace() << this << ": Failed setting maximum state value " << stateType.name() << " to " << maxValue; qCWarning(dcThing()).nospace() << this << ": Failed setting maximum state value " << stateType.name() << " to " << maxValue;
} }
/*! Sets the maximum value for the \l{State} matching the given \a stateName in this thing to value. */ /*! Sets the maximum value for the \l{State} matching the given \a stateName in this thing to \a maxValue. */
void Thing::setStateMaxValue(const QString &stateName, const QVariant &maxValue) void Thing::setStateMaxValue(const QString &stateName, const QVariant &maxValue)
{ {
StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id(); StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id();
@ -697,6 +695,14 @@ void Thing::setStateMinMaxValues(const QString &stateName, const QVariant &minVa
setStateMinMaxValues(stateTypeId, minValue, maxValue); setStateMinMaxValues(stateTypeId, minValue, maxValue);
} }
/*! Sets the possible values for the \l{State} matching the given \a stateName in this thing to \a values. */
void Thing::setStatePossibleValues(const QString &stateName, const QVariantList &values)
{
const StateTypeId stateTypeId = m_thingClass.stateTypes().findByName(stateName).id();
setStatePossibleValues(stateTypeId, values);
}
/*! Sets the possible values for the \l{State} matching the given \a stateTypeId in this thing to \a values. */
void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values) void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values)
{ {
StateType stateType = m_thingClass.stateTypes().findById(stateTypeId); StateType stateType = m_thingClass.stateTypes().findById(stateTypeId);
@ -704,6 +710,7 @@ void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVarian
qCWarning(dcThing()) << "No such state type" << stateTypeId.toString() << "in" << m_name << "(" + thingClass().name() + ")"; qCWarning(dcThing()) << "No such state type" << stateTypeId.toString() << "in" << m_name << "(" + thingClass().name() + ")";
return; return;
} }
for (int i = 0; i < m_states.count(); ++i) { for (int i = 0; i < m_states.count(); ++i) {
if (m_states.at(i).stateTypeId() == stateTypeId) { if (m_states.at(i).stateTypeId() == stateTypeId) {
if (values == m_states.at(i).possibleValues()) { if (values == m_states.at(i).possibleValues()) {
@ -739,10 +746,10 @@ void Thing::setStatePossibleValues(const StateTypeId &stateTypeId, const QVarian
return; return;
} }
} }
qCWarning(dcThing()).nospace() << this << ": Failed setting maximum state value " << stateType.name() << " to " << values; qCWarning(dcThing()).nospace() << this << ": Failed setting possible state values " << stateType.name() << " to " << values;
Q_ASSERT_X(false, Q_ASSERT_X(false,
m_name.toUtf8(), m_name.toUtf8(),
QString("Failed setting possible state values for %1 to %2").arg(stateType.name()).arg(QString(QJsonDocument::fromVariant(values).toJson())).toUtf8()); QString("Failed setting possible state values for %1 to %2").arg(stateType.name(), QString(QJsonDocument::fromVariant(values).toJson())).toUtf8());
} }
/*! Returns the \l{State} with the given \a stateTypeId of this thing. */ /*! Returns the \l{State} with the given \a stateTypeId of this thing. */

View File

@ -3,7 +3,7 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2013 - 2024, nymea GmbH * Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH * Copyright (C) 2024 - 2026, chargebyte austria GmbH
* *
* This file is part of nymea. * This file is part of nymea.
* *
@ -32,7 +32,6 @@
#include "types/state.h" #include "types/state.h"
#include "types/param.h" #include "types/param.h"
#include "types/event.h" #include "types/event.h"
#include "types/browseritem.h"
#include <QObject> #include <QObject>
#include <QUuid> #include <QUuid>
@ -145,6 +144,7 @@ public:
Q_INVOKABLE void setStateMinMaxValues(const StateTypeId &stateTypeId, const QVariant &minValue, const QVariant &maxValue); Q_INVOKABLE void setStateMinMaxValues(const StateTypeId &stateTypeId, const QVariant &minValue, const QVariant &maxValue);
Q_INVOKABLE void setStateMinMaxValues(const QString &stateName, const QVariant &minValue, const QVariant &maxValue); Q_INVOKABLE void setStateMinMaxValues(const QString &stateName, const QVariant &minValue, const QVariant &maxValue);
Q_INVOKABLE void setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values); Q_INVOKABLE void setStatePossibleValues(const StateTypeId &stateTypeId, const QVariantList &values);
Q_INVOKABLE void setStatePossibleValues(const QString &stateName, const QVariantList &values);
Q_INVOKABLE State state(const StateTypeId &stateTypeId) const; Q_INVOKABLE State state(const StateTypeId &stateTypeId) const;
Q_INVOKABLE State state(const QString &stateName) const; Q_INVOKABLE State state(const QString &stateName) const;