示例#1
0
        public async Task <bool> InitializeSdk(string hueDefault)
        {
            Write.WriteConsole(ConsoleTypes.Hue, @"Attempting to load HUE SDK..");

            try
            {
                IBridgeLocator locator = new HttpBridgeLocator();
                var            devices = new Dictionary <string, LocatedBridge>();

                var bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

                foreach (var bridge in bridgeIPs)
                {
                    Write.WriteConsole(ConsoleTypes.Hue,
                                       "Found HUE Bridge (" + bridge.BridgeId + ") at " + bridge.IpAddress);
                    devices.Add(bridge.BridgeId, bridge);
                }

                var selectdevice = "";

                if (devices.Count > 0)
                {
                    if (!string.IsNullOrWhiteSpace(hueDefault))
                    {
                        if (devices.ContainsKey(hueDefault))
                        {
                            selectdevice = devices[hueDefault].BridgeId;
                            Write.WriteConsole(ConsoleTypes.Hue,
                                               "Connected to preferred HUE Bridge (" + devices[hueDefault].BridgeId + ") at " +
                                               devices[hueDefault].IpAddress);
                        }
                        else
                        {
                            selectdevice = devices.FirstOrDefault().Key;
                            Write.WriteConsole(ConsoleTypes.Hue, @"Unable to find your preferred HUE Bridge.");
                            Write.WriteConsole(ConsoleTypes.Hue,
                                               "Connected to HUE Bridge (" + devices.FirstOrDefault().Value.BridgeId + ") at " +
                                               devices.FirstOrDefault().Value.IpAddress);
                        }
                    }
                    else
                    {
                        selectdevice = devices.FirstOrDefault().Key;
                        Write.WriteConsole(ConsoleTypes.Hue,
                                           "Connected to HUE Bridge (" + devices.FirstOrDefault().Value.BridgeId + ") at " +
                                           devices.FirstOrDefault().Value.IpAddress);
                    }
                }
                else
                {
                    Write.WriteConsole(ConsoleTypes.Hue, @"Unable to find any HUE Bridges.");
                    return(false);
                }

                if (!string.IsNullOrWhiteSpace(selectdevice))
                {
                    _client = new LocalHueClient(devices[selectdevice].IpAddress);
                    var appKey = await _client.RegisterAsync("Chromatics", "Chromatics_Bridge");

                    _client.Initialize("Chromatics");

                    //Get lights
                    var lights = await _client.GetLightsAsync();

                    foreach (var light in lights)
                    {
                        var defaultmode = BulbModeTypes.Standby;

                        if (!_HueModeMemory.ContainsKey(light.UniqueId))
                        {
                            //Save to devices.chromatics
                            _HueModeMemory.Add(light.UniqueId, defaultmode);
                            _HueStateMemory.Add(light.UniqueId, 1);

                            Write.SaveDevices();
                        }
                        else
                        {
                            //Load from devices.chromatics
                            defaultmode = _HueModeMemory[light.UniqueId];
                        }

                        _HueDevices.Add(light.UniqueId, light);
                        _HueBulbsDat.Add(light, defaultmode);
                        _HueBulbsRestore.Add(light, light.State);

                        _hueBulbs++;

                        Write.WriteConsole(ConsoleTypes.Hue, @"HUE Bulb Found: " + light.Name + " (" + light.Id + ").");

                        Write.ResetDeviceDataGrid();
                    }

                    return(true);
                }
                Write.WriteConsole(ConsoleTypes.Hue, @"HUE SDK Failed to Load. Error: Bridge Scan Error");
                return(false);
            }
            catch (Exception ex)
            {
                Write.WriteConsole(ConsoleTypes.Hue, @"HUE SDK Failed to Load. Error: " + ex.Message);
                return(false);
            }
        }