Exemplo n.º 1
0
        public async Task <IReadOnlyList <PairingOption> > GetPairingOptionsAsync(CancellationToken cancellation)
        {
            var bridges = await HueBridgeDiscovery.FastDiscoveryAsync(TimeSpan.FromSeconds(1));

            return(bridges.Select(b => new PairingOption {
                DisplayName = $"{DisplayName} - {b.IpAddress}",
                Id = b.IpAddress.ToString()
                     // TODO: This should be BridgeId, but it means RestorePairAsync needs to look up the bridges by ID
                     // Could add a last-seen data to speed up lookup. Relying on IP for ID just means people are going to get unpaired
            }).ToArray());
        }
Exemplo n.º 2
0
        public async Task InitializeAsync()
        {
            List <LocatedBridge> bridges = await HueBridgeDiscovery.FastDiscoveryAsync(TimeSpan.FromSeconds(5));

            LocatedBridge locatedBridge = bridges.First();

            if (string.IsNullOrEmpty(_appKey))
            {
                throw new NotImplementedException("Registering application is not yet implemented");
            }

            LocalHueClient hueClient = new LocalHueClient(locatedBridge.IpAddress, _appKey);

            if (!await hueClient.CheckConnection())
            {
                throw new Exception("Could not establish connection to bridge");
            }

            _hueClient = hueClient;
        }
Exemplo n.º 3
0
        public static async Task <List <BridgeData> > Discover(int time = 5)
        {
            LogUtil.Write("Hue: Discovery Started.");
            var output = new List <BridgeData>();

            try {
                var discovered = await HueBridgeDiscovery.CompleteDiscoveryAsync(TimeSpan.FromSeconds(time), TimeSpan.FromSeconds(time));

                output = discovered.Select(bridge => new BridgeData(bridge)).ToList();
                LogUtil.Write($"Hue: Discovery complete, found {discovered.Count} devices.");
            } catch (TaskCanceledException e) {
                LogUtil.Write("Discovery exception, task canceled: " + e.Message, "WARN");
            } catch (OperationCanceledException e) {
                LogUtil.Write("Discovery exception, operation canceled: " + e.Message, "WARN");
            }catch (SocketException f) {
                LogUtil.Write("Socket exception, task canceled: " + f.Message, "WARN");
            } catch (HttpRequestException g) {
                LogUtil.Write("HTTP exception, task canceled: " + g.Message, "WARN");
            }

            return(output);
        }
Exemplo n.º 4
0
        public static async Task Setup(CancellationToken token)
        {
            var bridges = await HueBridgeDiscovery.FastDiscoveryWithNetworkScanFallbackAsync(TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(30));

            var bridge = bridges.FirstOrDefault();

            RegisterEntertainmentResult registeredInfos;

            // Is the Hue credentials present ?
            if (!File.Exists(credentialPath))
            {
                Console.WriteLine("No credentials found please press the bridge button");

                // Wait for the user to press the link button
                await Task.Delay(TimeSpan.FromSeconds(30));

                var client = new LocalHueClient(bridge.IpAddress);
                registeredInfos = await client.RegisterAsync("ScreenHueSync", Environment.MachineName, true);

                hueCredential = new HueCredential()
                {
                    Username = registeredInfos.Username,
                    Key      = registeredInfos.StreamingClientKey
                };
                File.WriteAllText(credentialPath, Newtonsoft.Json.JsonConvert.SerializeObject(hueCredential));
                Console.WriteLine("Registration success credentials are :");
                Console.WriteLine("Username : "******"Key : " + registeredInfos.StreamingClientKey);
            }
            else
            {
                hueCredential = Newtonsoft.Json.JsonConvert.DeserializeObject <HueCredential>(File.ReadAllText(credentialPath));
            }

            registeredInfos = new RegisterEntertainmentResult()
            {
                Username           = hueCredential.Username,
                StreamingClientKey = hueCredential.Key
            };

            Console.WriteLine("Get client");
            Client = new StreamingHueClient(bridge.IpAddress, registeredInfos.Username, registeredInfos.StreamingClientKey);

            //Get the entertainment group
            Console.WriteLine("Get entertainment group");
            var all = await Client.LocalHueClient.GetEntertainmentGroups();

            var group = all.Last();

            //Create a streaming group
            Console.WriteLine("Get streaming group");
            StreamingGroup = new StreamingGroup(group.Locations);

            //Connect to the streaming group
            Console.WriteLine("Connect to group");
            await Client.Connect(group.Id);

            Console.WriteLine("Done !");
            BaseLayer = StreamingGroup.GetNewLayer(true);
            Ready     = true;

            //Start auto updating this entertainment group
            _ = Client.AutoUpdate(StreamingGroup, token, 50);
        }