Пример #1
0
        static void Main(string[] args)
        {
            JObject localStoreSettings;
            var     bridgeIp       = string.Empty;
            var     bridgeUsername = string.Empty;

            // This is a suggested example of how to implement the HueLib Library.
            // Suggestions are welcome!
            try
            {
                using (var localStoreFile = new FileStream($"{Directory.GetCurrentDirectory()}/localSettings.json",
                                                           FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    using (var sr = new StreamReader(localStoreFile))
                    {
                        localStoreSettings = JObject.Parse(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception e)
            {
                localStoreSettings = new JObject();
            }

            // Search for all Bridge IP addresses on the local network.
            // This value should be stored for later usage in some way.
            bridgeIp = GetOrCreateLocalSetting(localStoreSettings, "HueBridgeIp",
                                               async() => await GetFirstValidBridgeIp()).Result;

            // Prompt the user to tap the button on top of the hue bridge.
            // Once tapped this will return a username string.
            // This value should be stored to stop extraneous usernames being created.
            bridgeUsername = GetOrCreateLocalSetting(localStoreSettings, "HueBridgeUsername",
                                                     async() => await GetHueBridgeUsername(bridgeIp)).Result;

            while (string.IsNullOrWhiteSpace(bridgeUsername))
            {
                Console.WriteLine("Please tap the button on top of the Hue bridge to authenticate this application");

                Task.Delay(TimeSpan.FromSeconds(10)).Wait();

                bridgeUsername = GetOrCreateLocalSetting(localStoreSettings, "HueBridgeUsername",
                                                         async() => await GetHueBridgeUsername(bridgeIp)).Result;
            }

            var client = new HueClient(bridgeIp, bridgeUsername);

            switch (Console.ReadLine())
            {
            case "1":
                var turnOffResult = client.TurnOffAllLights();
                turnOffResult.Wait();
                break;

            case "2":
                var turnOnResult = client.TurnOnAllLights();
                turnOnResult.Wait();
                break;
            }

            Console.ReadLine();
        }