Skip to content

Commit

Permalink
Improve maintenance of device membership groups (1)
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Nov 21, 2018
1 parent 2dd57b0 commit 1345700
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 9 deletions.
40 changes: 32 additions & 8 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ void DeRestPluginPrivate::checkSensorGroup(Sensor *sensor)
return;
}

Group *group = 0;
Group *group = nullptr;

{
std::vector<Group>::iterator i = groups.begin();
Expand All @@ -1894,7 +1894,7 @@ void DeRestPluginPrivate::checkSensorGroup(Sensor *sensor)
for (; i != end; ++i)
{
if (i->state() == Group::StateNormal &&
i->deviceIsMember(sensor->id()))
(i->deviceIsMember(sensor->uniqueId()) || i->deviceIsMember(sensor->id())))
{
group = &*i;
break;
Expand Down Expand Up @@ -1929,7 +1929,7 @@ void DeRestPluginPrivate::checkSensorGroup(Sensor *sensor)

for (; i != end; ++i)
{
if (i->state() == Group::StateNormal && i->id() == gid)
if (!gid.isEmpty() && i->state() == Group::StateNormal && i->id() == gid)
{
group = &*i;
break;
Expand Down Expand Up @@ -1963,7 +1963,7 @@ void DeRestPluginPrivate::checkSensorGroup(Sensor *sensor)

for (; i != end; ++i)
{
if (i->state() == Group::StateNormal && i->id() == gid)
if (!gid.isEmpty() && i->state() == Group::StateNormal && i->id() == gid)
{
group = &*i;
break;
Expand All @@ -1975,6 +1975,19 @@ void DeRestPluginPrivate::checkSensorGroup(Sensor *sensor)
{
group = addGroup();
group->setName(sensor->name());
ResourceItem *item2 = group->addItem(DataTypeString, RAttrUniqueId);
DBG_Assert(item2);
if (item2)
{
const QString uid = generateUniqueId(sensor->address().ext(), 0, 0);
item2->setValue(uid);
}
}

DBG_Assert(group);
if (!group)
{
return;
}

if (group->addDeviceMembership(sensor->id()))
Expand Down Expand Up @@ -2002,7 +2015,7 @@ void DeRestPluginPrivate::checkOldSensorGroups(Sensor *sensor)

ResourceItem *item = sensor->item(RConfigGroup);

if (!item || !item->lastSet().isValid())
if (!item || !item->lastSet().isValid() || item->toString().isEmpty())
{
return;
}
Expand All @@ -2025,9 +2038,14 @@ void DeRestPluginPrivate::checkOldSensorGroups(Sensor *sensor)
queSaveDb(DB_GROUPS, DB_SHORT_SAVE_DELAY);
}
}
else if (i->deviceIsMember(sensor->id()))
else if (i->deviceIsMember(sensor->uniqueId()) || i->deviceIsMember(sensor->id()))
{
if (i->state() == Group::StateNormal)
if (!i->removeDeviceMembership(sensor->uniqueId()))
{
i->removeDeviceMembership(sensor->id());
}

if (i->state() == Group::StateNormal && !i->hasDeviceMembers())
{
DBG_Printf(DBG_INFO, "delete old group %u of sensor %s\n", i->address(), qPrintable(sensor->name()));
i->setState(Group::StateDeleted);
Expand Down Expand Up @@ -2066,12 +2084,18 @@ void DeRestPluginPrivate::deleteGroupsWithDeviceMembership(const QString &id)
{
if (i->deviceIsMember(id) && i->state() == Group::StateNormal)
{
i->setState(Group::StateDeleted);
i->removeDeviceMembership(id);

updateGroupEtag(&*i);
queSaveDb(DB_GROUPS | DB_LIGHTS, DB_SHORT_SAVE_DELAY);

if (i->hasDeviceMembers())
{
continue;
}

i->setState(Group::StateDeleted);

// for each node which is part of this group send a remove group request (will be unicast)
// note: nodes which are curently switched off will not be removed!
std::vector<LightNode>::iterator j = nodes.begin();
Expand Down
11 changes: 11 additions & 0 deletions database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3056,6 +3056,17 @@ static int sqliteLoadAllSensorsCallback(void *user, int ncols, char **colval , c
item->setValue(false);
}
}

// when reachable and assigned to a group, force check of group membership
if (item->toBool())
{
item = sensor.item(RConfigGroup);
if (item && !item->toString().isEmpty())
{
Event e(RSensors, REventValidGroup, sensor.id());
d->enqueueEvent(e);
}
}
}

sensor.address().setExt(extAddr);
Expand Down
6 changes: 6 additions & 0 deletions group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,9 @@ bool Group::deviceIsMember(const QString &id) const
}
return true;
}

/*! Returns true if group is controlled by devices. */
bool Group::hasDeviceMembers() const
{
return !m_deviceMemberships.empty();
}
1 change: 1 addition & 0 deletions group.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Group : public Resource
bool addDeviceMembership(const QString &id);
bool removeDeviceMembership(const QString &id);
bool deviceIsMember(const QString &id) const;
bool hasDeviceMembers() const;

uint16_t colorX;
uint16_t colorY;
Expand Down
2 changes: 1 addition & 1 deletion rest_groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ Group *DeRestPluginPrivate::addGroup()
}
}

return 0;
return nullptr;
}

/*! Put all parameters in a map for later json serialization.
Expand Down

0 comments on commit 1345700

Please sign in to comment.