diff --git a/Somfy.cpp b/Somfy.cpp index bffebd2..f1c9b6b 100644 --- a/Somfy.cpp +++ b/Somfy.cpp @@ -3751,12 +3751,18 @@ SomfyShade *SomfyShadeController::addShade(JsonObject &obj) { } SomfyShade *SomfyShadeController::addShade() { uint8_t shadeId = this->getNextShadeId(); - // So the next shade id will be the first one we run into with an id of 255 so - // if it gets deleted in the middle then it will get the first slot that is empty. - // There is no apparent way around this. In the future we might actually add an indexer + // Shades are packed into the lowest slots when they are loaded, so once one has been + // deleted a shade's slot no longer matches its id. + // In the future we might actually add an indexer // to it for sorting later. The time has come so the sort order is set below. if(shadeId == 255) return nullptr; - SomfyShade *shade = &this->shades[shadeId - 1]; + SomfyShade *shade = nullptr; + for(uint8_t i = 0; i < SOMFY_MAX_SHADES; i++) { + if(this->shades[i].getShadeId() == 255) { + shade = &this->shades[i]; + break; + } + } if(shade) { shade->setShadeId(shadeId); shade->sortOrder = this->getMaxShadeOrder() + 1; @@ -3848,9 +3854,16 @@ SomfyRoom *SomfyShadeController::addRoom(JsonObject &obj) { } SomfyRoom *SomfyShadeController::addRoom() { uint8_t roomId = this->getNextRoomId(); - // So the next room id will be the first one we run into with an id of 0 so + // Rooms are packed into the lowest slots when they are loaded, so once one has been + // deleted a room's slot no longer matches its id. if(roomId == 0) return nullptr; - SomfyRoom *room = &this->rooms[roomId - 1]; + SomfyRoom *room = nullptr; + for(uint8_t i = 0; i < SOMFY_MAX_ROOMS; i++) { + if(this->rooms[i].roomId == 0) { + room = &this->rooms[i]; + break; + } + } if(room) { room->roomId = roomId; room->sortOrder = this->getMaxRoomOrder() + 1; @@ -3870,12 +3883,18 @@ SomfyGroup *SomfyShadeController::addGroup(JsonObject &obj) { } SomfyGroup *SomfyShadeController::addGroup() { uint8_t groupId = this->getNextGroupId(); - // So the next shade id will be the first one we run into with an id of 255 so - // if it gets deleted in the middle then it will get the first slot that is empty. - // There is no apparent way around this. In the future we might actually add an indexer + // Groups are packed into the lowest slots when they are loaded, so once one has been + // deleted a group's slot no longer matches its id. + // In the future we might actually add an indexer // to it for sorting later. if(groupId == 255) return nullptr; - SomfyGroup *group = &this->groups[groupId - 1]; + SomfyGroup *group = nullptr; + for(uint8_t i = 0; i < SOMFY_MAX_GROUPS; i++) { + if(this->groups[i].getGroupId() == 255) { + group = &this->groups[i]; + break; + } + } if(group) { group->setGroupId(groupId); group->sortOrder = this->getMaxGroupOrder() + 1;