Skip to content

Commit

Permalink
Improve removing device membership from groups
Browse files Browse the repository at this point in the history
  • Loading branch information
manup committed Jun 20, 2017
1 parent 22734b9 commit fed6a6b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ 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);
Expand Down
16 changes: 16 additions & 0 deletions group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ Scene *Group::getScene(quint8 sceneId)
return 0;
}

/*! Returns true if device with \p id was added to the group. */
bool Group::addDeviceMembership(const QString &id)
{
if (std::find(m_deviceMemberships.begin(), m_deviceMemberships.end(), id) == m_deviceMemberships.end())
Expand All @@ -245,6 +246,21 @@ bool Group::addDeviceMembership(const QString &id)
return false;
}

/*! Returns true if device with \p id was removed from the group. */
bool Group::removeDeviceMembership(const QString &id)
{
std::vector<QString>::iterator i = std::find(m_deviceMemberships.begin(), m_deviceMemberships.end(), id);

if (i != m_deviceMemberships.end())
{
*i = m_deviceMemberships.back();
m_deviceMemberships.pop_back();
return true;
}

return false;
}

/*! Returns true if device with \p id controls the group. */
bool Group::deviceIsMember(const QString &id) const
{
Expand Down
1 change: 1 addition & 0 deletions group.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Group : public Resource
void setLightsequenceFromString(const QString &deviceIds);
Scene *getScene(quint8 sceneId);
bool addDeviceMembership(const QString &id);
bool removeDeviceMembership(const QString &id);
bool deviceIsMember(const QString &id) const;

uint16_t colorX;
Expand Down
4 changes: 3 additions & 1 deletion rest_groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,7 @@ int DeRestPluginPrivate::deleteGroup(const ApiRequest &req, ApiResponse &rsp)
}

group->setState(Group::StateDeleted);
group->m_deviceMemberships.clear();

// remove any known scene
group->scenes.clear();
Expand All @@ -1372,13 +1373,14 @@ int DeRestPluginPrivate::deleteGroup(const ApiRequest &req, ApiResponse &rsp)

if (groupInfo)
{
i->setNeedSaveDatabase(true);
groupInfo->actions &= ~GroupInfo::ActionAddToGroup; // sanity
groupInfo->actions |= GroupInfo::ActionRemoveFromGroup;
groupInfo->state = GroupInfo::StateNotInGroup;
}
}

updateEtag(gwConfigEtag);
updateGroupEtag(group);
rsp.httpStatus = HttpStatusOk;

return REQ_READY_SEND;
Expand Down

0 comments on commit fed6a6b

Please sign in to comment.