Skip to content

Commit

Permalink
Move setValue() methods to Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ebaauw committed Jul 16, 2021
1 parent 72ee23a commit c1e06f4
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 144 deletions.
1 change: 1 addition & 0 deletions de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ class ApiConfig : public Resource
{
public:
ApiConfig();
void didSetValue(ResourceItem *i);
};

class TcpClient
Expand Down
12 changes: 12 additions & 0 deletions group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
*/

#include "de_web_plugin_private.h"
#include "group.h"
#include <QStringList>

Expand Down Expand Up @@ -125,6 +126,17 @@ bool Group::isColorLoopActive() const
return m_colorLoopActive;
}

/*! Handles admin when ResourceItem value has been set.
* \param i ResourceItem
*/
void Group::didSetValue(ResourceItem *i)
{
plugin->enqueueEvent(Event(RGroups, i->descriptor().suffix, id(), i));
plugin->updateGroupEtag(this);
plugin->saveDatabaseItems |= DB_GROUPS;
plugin->queSaveDb(DB_GROUPS, DB_SHORT_SAVE_DELAY);
}

/*! multiDeviceIds to string. */
const QString Group::midsToString() const
{
Expand Down
1 change: 1 addition & 0 deletions group.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Group : public Resource
void setIsOn(bool on);
void setColorLoopActive(bool colorLoopActive);
bool isColorLoopActive() const;
void didSetValue(ResourceItem *i);
const QString midsToString() const;
void setMidsFromString(const QString &mids);
const QString dmToString() const;
Expand Down
69 changes: 0 additions & 69 deletions light_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,75 +274,6 @@ void LightNode::didSetValue(ResourceItem *i)
plugin->queSaveDb(DB_LIGHTS, DB_SHORT_SAVE_DELAY);
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool LightNode::setValue(const char *suffix, qint64 val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toNumber() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool LightNode::setValue(const char *suffix, const QString &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toString() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool LightNode::setValue(const char *suffix, const QVariant &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toVariant() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Mark received command and update lastseen. */
void LightNode::rx()
{
Expand Down
3 changes: 0 additions & 3 deletions light_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ class LightNode : public Resource,
void setColorLoopSpeed(uint8_t speed);
uint8_t colorLoopSpeed() const;
void didSetValue(ResourceItem *i);
bool setValue(const char *suffix, qint64 val, bool forceUpdate = false);
bool setValue(const char *suffix, const QString &val, bool forceUpdate = false);
bool setValue(const char *suffix, const QVariant &val, bool forceUpdate = false);
void rx();
const deCONZ::SimpleDescriptor &haEndpoint() const;
void setHaEndpoint(const deCONZ::SimpleDescriptor &endpoint);
Expand Down
69 changes: 69 additions & 0 deletions resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,75 @@ QVariant Resource::toVariant(const char *suffix) const
return QVariant();
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Resource::setValue(const char *suffix, qint64 val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toNumber() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Resource::setValue(const char *suffix, const QString &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toString() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Resource::setValue(const char *suffix, const QVariant &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toVariant() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

int Resource::itemCount() const
{
return m_rItems.size();
Expand Down
4 changes: 4 additions & 0 deletions resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ class Resource
qint64 toNumber(const char *suffix) const;
const QString &toString(const char *suffix) const;
QVariant toVariant(const char *suffix) const;
virtual void didSetValue(ResourceItem *i) = 0;
bool setValue(const char *suffix, qint64 val, bool forceUpdate = false);
bool setValue(const char *suffix, const QString &val, bool forceUpdate = false);
bool setValue(const char *suffix, const QVariant &val, bool forceUpdate = false);
int itemCount() const;
ResourceItem *itemForIndex(size_t idx);
const ResourceItem *itemForIndex(size_t idx) const;
Expand Down
11 changes: 11 additions & 0 deletions rest_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ ApiConfig::ApiConfig() :
{
}

/*! Handles admin when ResourceItem value has been set.
* \param i ResourceItem
*/
void ApiConfig::didSetValue(ResourceItem *i)
{
plugin->enqueueEvent(Event(RConfig, i->descriptor().suffix, 0)); // FIXME
plugin->updateEtag(plugin->gwConfigEtag);
plugin->saveDatabaseItems |= DB_CONFIG;
plugin->queSaveDb(DB_CONFIG, DB_SHORT_SAVE_DELAY);
}

/*! Init the configuration. */
void DeRestPluginPrivate::initConfig()
{
Expand Down
69 changes: 0 additions & 69 deletions sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,75 +278,6 @@ void Sensor::didSetValue(ResourceItem *i)
plugin->queSaveDb(DB_SENSORS, DB_SHORT_SAVE_DELAY);
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Sensor::setValue(const char *suffix, qint64 val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toNumber() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Sensor::setValue(const char *suffix, const QString &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toString() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Set ResourceItem value.
* \param suffix ResourceItem suffix
* \param val ResourceIetm value
*/
bool Sensor::setValue(const char *suffix, const QVariant &val, bool forceUpdate)
{
ResourceItem *i = item(suffix);
if (!i)
{
return false;
}
if (forceUpdate || i->toVariant() != val)
{
if (!(i->setValue(val)))
{
return false;
}
didSetValue(i);
return true;
}
return false;
}

/*! Mark received command and update lastseen. */
void Sensor::rx()
{
Expand Down
3 changes: 0 additions & 3 deletions sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,6 @@ class Sensor : public Resource,
SensorMode mode() const;
void setMode(SensorMode mode);
void didSetValue(ResourceItem *i);
bool setValue(const char *suffix, qint64 val, bool forceUpdate = false);
bool setValue(const char *suffix, const QString &val, bool forceUpdate = false);
bool setValue(const char *suffix, const QVariant &val, bool forceUpdate = false);
void rx();
uint8_t resetRetryCount() const;
void setResetRetryCount(uint8_t resetRetryCount);
Expand Down

0 comments on commit c1e06f4

Please sign in to comment.