예제 #1
0
        private static async Task Connect(bool demoMode, bool useSimulator, ConnectionConfiguration bridgeConfig)
        {
            var hub = (IHubContext <StatusHub>?)Startup.ServiceProvider.GetService(typeof(IHubContext <StatusHub>));

            if (hub == null)
            {
                throw new Exception("Unable to get PreviewHub from ServiceProvider");
            }

            await hub.Clients.All.SendAsync("StatusMsg", $"Connecting to bridge {bridgeConfig.Ip}");

            try
            {
                //Initialize streaming client
                var client = new LightDJStreamingHueClient(bridgeConfig.Ip, bridgeConfig.Key, bridgeConfig.EntertainmentKey, demoMode);

                //Get the entertainment group
                Dictionary <string, LightLocation> locations = new Dictionary <string, LightLocation>();
                if (demoMode)
                {
                    string demoJson = await File.ReadAllTextAsync($"{bridgeConfig.Ip}_{bridgeConfig.GroupId}.json");

                    locations = JsonConvert.DeserializeObject <Dictionary <string, LightLocation> >(demoJson);
                    _groupId  = bridgeConfig.GroupId;
                }
                else
                {
                    var all = await client.LocalHueClient.GetEntertainmentGroups();

                    var group = all.Where(x => x.Id == bridgeConfig.GroupId).FirstOrDefault();

                    if (group == null)
                    {
                        throw new Exception($"No Entertainment Group found with id {bridgeConfig.GroupId}. Create one using the Philips Hue App or the Q42.HueApi.UniversalWindows.Sample");
                    }
                    else
                    {
                        await hub.Clients.All.SendAsync("StatusMsg", $"Using Entertainment Group {group.Id} for bridge {bridgeConfig.Ip}");

                        Console.WriteLine($"Using Entertainment Group {group.Id}");
                        _groupId = group.Id;
                    }

                    locations = group.Locations;
                }

                //Create a streaming group
                var stream = new StreamingGroup(locations);
                stream.IsForSimulator = useSimulator;


                //Connect to the streaming group
                if (!demoMode)
                {
                    await client.Connect(_groupId, simulator : useSimulator);
                }

                //Start auto updating this entertainment group
                client.AutoUpdate(stream, _cts.Token, 50, onlySendDirtyStates: false);

                StreamingHueClients.Add(client);
                StreamingGroups.Add(stream);

                await hub.Clients.All.SendAsync("StatusMsg", $"Succesfully connected to bridge {bridgeConfig.Ip}");
            }
            catch (Exception ex)
            {
                await hub.Clients.All.SendAsync("StatusMsg", $"Failed to connect to bridge {bridgeConfig.Ip}, exception: " + ex);

                throw;
            }
        }
예제 #2
0
        public static async Task <List <StreamingGroup> > SetupAndReturnGroupAsync(string groupName)
        {
            var  configSection = GetGroupConfigurations();
            var  currentGroup  = configSection.Where(x => x.Name == groupName).FirstOrDefault();
            bool demoMode      = currentGroup.Name == "DEMO";
            bool useSimulator  = demoMode ? true : currentGroup.Connections.First().UseSimulator;

            //Disconnect any current connections
            Disconnect();
            _cts = new CancellationTokenSource();

            foreach (var bridgeConfig in currentGroup.Connections)
            {
                //Initialize streaming client
                var client = new LightDJStreamingHueClient(bridgeConfig.Ip, bridgeConfig.Key, bridgeConfig.EntertainmentKey, demoMode);

                //Get the entertainment group
                Dictionary <string, LightLocation> locations = null;
                if (demoMode)
                {
                    string demoJson = await File.ReadAllTextAsync($"{bridgeConfig.Ip}_{bridgeConfig.GroupId}.json");

                    locations = JsonConvert.DeserializeObject <Dictionary <string, LightLocation> >(demoJson);
                    _groupId  = bridgeConfig.GroupId;
                }
                else
                {
                    var all = await client.LocalHueClient.GetEntertainmentGroups();

                    var group = all.Where(x => x.Id == bridgeConfig.GroupId).FirstOrDefault();

                    if (group == null)
                    {
                        throw new Exception($"No Entertainment Group found with id {bridgeConfig.GroupId}. Create one using the Philips Hue App or the Q42.HueApi.UniversalWindows.Sample");
                    }
                    else
                    {
                        Console.WriteLine($"Using Entertainment Group {group.Id}");
                        _groupId = group.Id;
                    }

                    locations = group.Locations;
                }

                //Create a streaming group
                var stream = new StreamingGroup(locations);
                stream.IsForSimulator = useSimulator;


                //Connect to the streaming group
                if (!demoMode)
                {
                    await client.Connect(_groupId, simulator : useSimulator);
                }

                //Start auto updating this entertainment group
                client.AutoUpdate(stream, _cts.Token, 50, onlySendDirtyStates: true);

                StreamingHueClients.Add(client);
                StreamingGroups.Add(stream);
            }

            var baseLayer   = GetNewLayer(isBaseLayer: true);
            var effectLayer = GetNewLayer(isBaseLayer: false);

            Layers = new List <EntertainmentLayer>()
            {
                baseLayer, effectLayer
            };
            CurrentConnection             = currentGroup;
            EffectSettings.LocationCenter = currentGroup.LocationCenter ?? new LightLocation()
            {
                0, 0, 0
            };

            //Optional: calculated effects that are placed on this layer
            baseLayer.AutoCalculateEffectUpdate(_cts.Token);
            effectLayer.AutoCalculateEffectUpdate(_cts.Token);

            return(StreamingGroups);
        }