Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline event handling. #5089

Merged
merged 15 commits into from
Aug 1, 2021
52 changes: 49 additions & 3 deletions database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ void DeRestPluginPrivate::checkDbUserVersion()
updated = upgradeDbToUserVersion7();
}
else if (userVersion == 7)
{
updated = upgradeDbToUserVersion8();
}
else if (userVersion == 8)
{
// latest version
}
Expand Down Expand Up @@ -494,6 +498,36 @@ bool DeRestPluginPrivate::upgradeDbToUserVersion7()
return setDbUserVersion(7);
}

/*! Upgrades database to user_version 8. */
bool DeRestPluginPrivate::upgradeDbToUserVersion8()
{
DBG_Printf(DBG_INFO, "DB upgrade to user_version 8\n");

const char *sql[] = {
"ALTER TABLE sensors add column lastseen TEXT",
"ALTER TABLE sensors add column lastannounced TEXT",
nullptr
};

for (int i = 0; sql[i] != nullptr; i++)
{
char *errmsg = nullptr;
int rc = sqlite3_exec(db, sql[i], nullptr, nullptr, &errmsg);

if (rc != SQLITE_OK)
{
if (errmsg)
{
DBG_Printf(DBG_ERROR_L2, "SQL exec failed: %s, error: %s (%d), line: %d\n", sql[i], errmsg, rc, __LINE__);
sqlite3_free(errmsg);
}
return false;
}
}

return setDbUserVersion(8);
}

#if DECONZ_LIB_VERSION >= 0x010E00
/*! Stores a source route.
Any existing source route with the same uuid will be replaced automatically.
Expand Down Expand Up @@ -3059,6 +3093,14 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
sensor.setDeletedState(Sensor::StateNormal);
}
}
else if (strcmp(colname[i], "lastseen") == 0)
{
sensor.setLastSeen(val);
}
else if (strcmp(colname[i], "lastannounced") == 0)
{
sensor.setLastAnnounced(val);
}
}
}

Expand All @@ -3079,6 +3121,8 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c

if (isClip)
{
sensor.removeItem(RAttrLastAnnounced);
sensor.removeItem(RAttrLastSeen);
ok = true;
}
// convert from old format 0x0011223344556677 to 00:11:22:33:44:55:66:77-AB where AB is the endpoint
Expand Down Expand Up @@ -3374,7 +3418,7 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
else if (sensor.type().endsWith(QLatin1String("DoorLock")))
{
clusterId = clusterId ? clusterId : DOOR_LOCK_CLUSTER_ID;

sensor.addItem(DataTypeString, RStateLockState);
sensor.addItem(DataTypeBool, RConfigLock);
}
Expand Down Expand Up @@ -5355,7 +5399,7 @@ void DeRestPluginPrivate::saveDb()
QString fingerPrintJSON = i->fingerPrint().toString();
const QString deletedState = "normal";

QString sql = QString(QLatin1String("REPLACE INTO sensors (sid, name, type, modelid, manufacturername, uniqueid, swversion, state, config, fingerprint, deletedState, mode) VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10', '%11', '%12')"))
QString sql = QString(QLatin1String("REPLACE INTO sensors (sid, name, type, modelid, manufacturername, uniqueid, swversion, state, config, fingerprint, deletedState, mode, lastseen, lastannounced) VALUES ('%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10', '%11', '%12', '%13', '%14')"))
.arg(i->id())
.arg(i->name())
.arg(i->type())
Expand All @@ -5367,7 +5411,9 @@ void DeRestPluginPrivate::saveDb()
.arg(configJSON)
.arg(fingerPrintJSON)
.arg(deletedState)
.arg(QString::number(i->mode()));
.arg(QString::number(i->mode()))
.arg(i->lastSeen())
.arg(i->lastAnnounced());

DBG_Printf(DBG_INFO_L2, "DB sql exec %s\n", qPrintable(sql));
errmsg = NULL;
Expand Down
1 change: 1 addition & 0 deletions de_web_plugin_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,7 @@ public Q_SLOTS:
bool upgradeDbToUserVersion2();
bool upgradeDbToUserVersion6();
bool upgradeDbToUserVersion7();
bool upgradeDbToUserVersion8();
void refreshDeviceDb(const deCONZ::Address &addr);
void pushZdpDescriptorDb(quint64 extAddress, quint8 endpoint, quint16 type, const QByteArray &data);
void pushZclValueDb(quint64 extAddress, quint8 endpoint, quint16 clusterId, quint16 attributeId, qint64 data);
Expand Down
9 changes: 9 additions & 0 deletions group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
*/

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

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

/*! Handles admin when ResourceItem value has been set.
* \param i ResourceItem
*/
void Group::didSetValue(ResourceItem *i)
{
enqueueEvent(Event(RGroups, i->descriptor().suffix, id(), i));
}

/*! 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) override;
const QString midsToString() const;
void setMidsFromString(const QString &mids);
const QString dmToString() const;
Expand Down
82 changes: 1 addition & 81 deletions light_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ LightNode::LightNode() :
m_sceneCapacity(16)

{
QDateTime now = QDateTime::currentDateTime();
lastStatePush = now;
lastAttrPush = now;

// add common items
addItem(DataTypeBool, RStateOn);
addItem(DataTypeString, RStateAlert);
Expand Down Expand Up @@ -272,79 +268,7 @@ uint8_t LightNode::colorLoopSpeed() const
void LightNode::didSetValue(ResourceItem *i)
{
enqueueEvent(Event(RLights, i->descriptor().suffix, id(), i));
plugin->updateLightEtag(this);
setNeedSaveDatabase(true);
plugin->saveDatabaseItems |= DB_LIGHTS;
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. */
Expand All @@ -356,10 +280,6 @@ void LightNode::rx()
{
setValue(RAttrLastSeen, lastRx().toUTC());
}
// else
// {
// item(RAttrLastSeen)->setValue(lastRx().toUTC());
// }
}

/*! Returns the lights HA endpoint descriptor.
Expand Down Expand Up @@ -646,7 +566,7 @@ void LightNode::setHaEndpoint(const deCONZ::SimpleDescriptor &endpoint)
case DEV_ID_HA_WINDOW_COVERING_DEVICE: ltype = QLatin1String("Window covering device"); break;
case DEV_ID_DOOR_LOCK: ltype = QLatin1String("Door Lock"); break;
case DEV_ID_DOOR_LOCK_UNIT: ltype = QLatin1String("Door Lock Unit"); break;

case DEV_ID_FAN: ltype = QLatin1String("Fan"); break;
case DEV_ID_CONFIGURATION_TOOL: removeItem(RStateOn);
removeItem(RStateAlert);
Expand Down
5 changes: 1 addition & 4 deletions light_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ class LightNode : public Resource,
bool supportsColorLoop() const;
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 didSetValue(ResourceItem *i) override;
void rx();
const deCONZ::SimpleDescriptor &haEndpoint() const;
void setHaEndpoint(const deCONZ::SimpleDescriptor &endpoint);
Expand Down
75 changes: 69 additions & 6 deletions resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,6 @@ Resource::Resource(const char *prefix) :

/*! Copy constructor. */
Resource::Resource(const Resource &other) :
lastStatePush(other.lastStatePush),
lastAttrPush(other.lastAttrPush),
m_prefix(other.m_prefix),
m_rItems(other.m_rItems)
{
Expand All @@ -921,8 +919,6 @@ Resource &Resource::operator=(const Resource &other)
{
if (this != &other)
{
lastStatePush = other.lastStatePush;
lastAttrPush = other.lastAttrPush;
m_prefix = other.m_prefix;
m_rItems = other.m_rItems;
}
Expand All @@ -934,8 +930,6 @@ Resource &Resource::operator=(Resource &&other) noexcept
{
if (this != &other)
{
lastStatePush = std::move(other.lastStatePush);
lastAttrPush = std::move(other.lastAttrPush);
m_prefix = other.m_prefix;
m_rItems = std::move(other.m_rItems);
}
Expand Down Expand Up @@ -1056,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
6 changes: 4 additions & 2 deletions resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ 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 *) {};
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;
QDateTime lastStatePush;
QDateTime lastAttrPush;

private:
Resource() = delete;
Expand Down
4 changes: 3 additions & 1 deletion rest_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ void DeRestPluginPrivate::initTimezone()
item->setValue(QVariant());

dl.removeItem(RConfigReachable);
dl.removeItem(RAttrLastAnnounced);
dl.removeItem(RAttrLastSeen);

dl.setModelId(QLatin1String("PHDL00"));
dl.setManufacturer(QLatin1String("Philips"));
Expand Down Expand Up @@ -1088,7 +1090,7 @@ void DeRestPluginPrivate::basicConfigToMap(const ApiRequest &req, QVariantMap &m
map["swversion"] = swversion;
map["apiversion"] = QString(GW_API_VERSION);
map["datastoreversion"] = QLatin1String("93");
}
}
else
{
map["modelid"] = QLatin1String("BSB002");
Expand Down