Example #1
0
        public GroupService(Config config, LightService lightService)
        {
            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (lightService is null)
            {
                throw new ArgumentNullException(nameof(lightService));
            }

            this.lightService = lightService;
            this.nameToGroup  = new Dictionary <string, Group>();
            this.groupValues  = new Dictionary <Group, int>();
            this.groupToLamps = new Dictionary <Group, IEnumerable <string> >();

            foreach (var group in config.Groups)
            {
                nameToGroup[group.Name] = group;
                groupValues[group]      = group.Percentage;
                foreach (var lamp in group.Lamps)
                {
                    if (lightService.IsNameExisting(lamp))
                    {
                        lightService.UpdateFactor(lamp, group.Percentage / 100d, 1);
                    }
                }
            }

            foreach (var group in config.Groups)
            {
                groupToLamps[group] = GetLampsInGroupHelper(group.Name, new List <string>()).Distinct();
            }
        }
Example #2
0
        private IEnumerable <(string name, IPresetAware oldValue, Lamp newValue)> GetPresetValuesForLamps(Preset preset)
        {
            Lamp defaultLamp = new Lamp()
            {
                Strenght = 0
            };
            var lamps = preset.LampValues.Keys
                        .Where(i => groupService.IsNameExisting(i))
                        .SelectMany(i => groupService.GetLampsInGroup(i).Select(j => (name: j, value: preset.LampValues[i])))
                        .Union(preset.LampValues.Keys.Where(i => lightService.IsNameExisting(i)).Select(j => (name: j, value: preset.LampValues[j])))
                        .Select(i => (name: i.name, oldValue: lightService.GetTransitionable(i.name), newValue: i.value));

            lamps = lamps
                    .Union(lightService.GetLampsNotNamed(lamps.Select(i => i.name))
                           .Select(i => (i, lightService.GetTransitionable(i), defaultLamp))
                           ).ToList();

            lamps = lamps
                    .Select(i => i.name)
                    .Distinct()
                    .Select(i => lamps.FirstOrDefault(j => j.name == i))
                    .ToList();
            return(lamps);
        }