private async void OptDiscoverBind_Checked(object sender, RoutedEventArgs e)
        {
            List <HueIpAddress> r = await HueDiscoveryService.discover();

            lstHueIps.Items.Clear();
            foreach (HueIpAddress hue in r)
            {
                lstHueIps.Items.Add(hue);
            }
        }
Esempio n. 2
0
        private async void Application_Startup(object sender, StartupEventArgs e)
        {
            MainWindow               = new MainWindow();
            MainWindow.Closing      += MainWindow_Closing;
            _notifyIcon              = new System.Windows.Forms.NotifyIcon();
            _notifyIcon.DoubleClick += (s, args) => ShowMainWindow();
            _notifyIcon.Icon         = DeskHue.Properties.Resources.LightBulb;
            _notifyIcon.Visible      = true;

            CreateContextMenu();
            MainWindow.WindowState = WindowState.Minimized;
            MainWindow.Show();

            HueConfig config = ConfigService.loadConfig();

            if (!config.bound)
            {
                settingsParingWindow = new SettingsParingWindow(ShowMainWindow);
                settingsParingWindow.Show();
            }
            else if (config.autobound)
            {
                var ips = await HueDiscoveryService.discover();

                HueIpAddress matchedIp = null;
                try
                {
                    matchedIp = ips.First(i => i.id == config.deviceId);
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.Show("Paired bridge not found. Re-pairing");
                    ConfigService.saveConfig(new HueConfig());
                    settingsParingWindow = new SettingsParingWindow(ShowMainWindow);
                    settingsParingWindow.Show();
                }

                if (matchedIp != null)
                {
                    config.ip = matchedIp.internalipaddress;
                    ConfigService.saveConfig(config);
                }
            }
        }
        private async void BtnPair_Click(object sender, RoutedEventArgs e)
        {
            HueIpAddress ip = null;

            bool autobound = false;

            if (optDiscoverAuto.IsChecked.HasValue && optDiscoverAuto.IsChecked.Value)
            {
                var ips = await HueDiscoveryService.discover();

                if (ips.Count == 1)
                {
                    ip        = ips[0];
                    autobound = true;
                }
                else
                {
                    MessageBox.Show("More than one hue found. Manual discovery required.");
                    return;
                }
            }
            else if (optDiscoverBind.IsChecked.HasValue && optDiscoverBind.IsChecked.Value)
            {
                if (lstHueIps.SelectedItems.Count == 1)
                {
                    ip        = (HueIpAddress)lstHueIps.SelectedItems[0];
                    autobound = false;
                }
                else
                {
                    MessageBox.Show("You must select an IP or discover automatically");
                    return;
                }
            }

            var client = new RestClient("http://" + ip);

            var request        = new RestRequest("/api", Method.POST);
            var newUserRequest = new NewUserRequest();

            newUserRequest.devicetype = "DeskHue - Windows";
            request.AddJsonBody(newUserRequest);
            var response = await client.ExecuteAsync <List <NewUserResponse> >(request);

            List <NewUserResponse> items = response.Data;

            if (items.Count != 1)
            {
                MessageBox.Show("Incorrect or malformed response from hue bridge: " + response.Content);
                return;
            }

            NewUserResponse newUserResponse = items[0];

            if (newUserResponse.error != null)
            {
                MessageBox.Show(newUserResponse.error.description);
                return;
            }
            else if (newUserResponse.success != null)
            {
                HueConfig hueConfig = new HueConfig();
                hueConfig.ip        = ip.internalipaddress;
                hueConfig.deviceId  = ip.id;
                hueConfig.username  = newUserResponse.success.username;
                hueConfig.bound     = true;
                hueConfig.autobound = autobound;
                ConfigService.saveConfig(hueConfig);
                this.Close();
                showMainWindow();
            }
            else
            {
                MessageBox.Show("Incorrect or malformed response from hue bridge: " + response.Content);
            }
        }