Пример #1
0
        protected void UpdateGroups(PhilipsHueBridge bridge, List <Group> groups)
        {
            foreach (Group group in groups)
            {
                string groupKey = $"{bridge.BridgeId}-{group.Id}";

                GroupDataModel groupDataModel = null;
                if (TryGetDynamicChild(groupKey, out DynamicChild <GroupDataModel> dynamicChild))
                {
                    groupDataModel = dynamicChild.Value;
                }

                if (groupDataModel != null)
                {
                    groupDataModel.HueGroup = group;
                }
                else
                {
                    groupDataModel = AddDynamicChild(groupKey, new GroupDataModel(group, bridge.BridgeInfo)).Value;
                    Groups.Add(groupDataModel);
                }

                groupDataModel.DataModelDescription.Name = groupDataModel.Name;
            }

            // Remove groups that no longer exist
            List <GroupDataModel> groupsToRemove = Groups
                                                   .Where(dmg => dmg.HueBridge.Config?.BridgeId == bridge.BridgeId && groups.All(g => g.Id == dmg.HueGroup.Id))
                                                   .ToList();

            foreach (GroupDataModel groupDataModel in groupsToRemove)
            {
                RemoveDynamicChildByKey($"{bridge.BridgeId}-{groupDataModel.HueGroup.Id}");
                Groups.Remove(groupDataModel);
            }
        }