private async Task LoadSettings() { if (!(await SettingsService.IsFilePresent())) { await SettingsService.SaveSettings(_options); } Config = await SettingsService.LoadSettings(); if (string.IsNullOrEmpty(Config.RedirectUri)) { await SettingsService.DeleteSettings(); await SettingsService.SaveSettings(_options); } if (Config.UseWorkingHours) { pnlWorkingHours.Visibility = Visibility.Visible; SyncOptions(); } else { pnlWorkingHours.Visibility = Visibility.Collapsed; SyncOptions(); } if (Config.IsPhillipsEnabled) { pnlPhillips.Visibility = Visibility.Visible; SyncOptions(); } else { pnlPhillips.Visibility = Visibility.Collapsed; } if (Config.IsYeelightEnabled) { pnlYeelight.Visibility = Visibility.Visible; SyncOptions(); } else { pnlYeelight.Visibility = Visibility.Collapsed; } if (Config.IsLIFXEnabled) { getTokenLink.Visibility = Visibility.Visible; pnlLIFX.Visibility = Visibility.Visible; SyncOptions(); } else { getTokenLink.Visibility = Visibility.Collapsed; pnlLIFX.Visibility = Visibility.Collapsed; } if (Config.IsCustomApiEnabled) { pnlCustomApi.Visibility = Visibility.Visible; customApiAvailableMethod.SelectedValue = Config.CustomApiAvailableMethod; customApiBusyMethod.SelectedValue = Config.CustomApiBusyMethod; customApiBeRightBackMethod.SelectedValue = Config.CustomApiBeRightBackMethod; customApiAwayMethod.SelectedValue = Config.CustomApiAwayMethod; customApiDoNotDisturbMethod.SelectedValue = Config.CustomApiDoNotDisturbMethod; customApiOfflineMethod.SelectedValue = Config.CustomApiOfflineMethod; customApiOffMethod.SelectedValue = Config.CustomApiOffMethod; SyncOptions(); } else { pnlCustomApi.Visibility = Visibility.Collapsed; } }
private async Task InteractWithLights() { while (true) { await Task.Delay(Convert.ToInt32(Config.PollingInterval * 1000)); if (Config.SyncLights) { if (!Config.UseWorkingHours || (Config.UseWorkingHours && IsInWorkingHours(Config.WorkingHoursStartTime, Config.WorkingHoursEndTime))) { switch (lightMode) { case "Graph": try { presence = await System.Threading.Tasks.Task.Run(() => GetPresence()); //if (presence.Availability != savedAvailability) //{ await SetColor(presence.Availability); //} if (DateTime.Now.AddMinutes(-5) > settingsLastSaved) { await SettingsService.SaveSettings(Config); settingsLastSaved = DateTime.Now; } MapUI(presence, null, null); //savedAvailability = presence.Availability; } catch (Exception e) { DiagnosticsClient.TrackException(e); } break; case "Theme": try { var theme = ((SolidColorBrush)SystemParameters.WindowGlassBrush).Color; var color = $"#{theme.ToString().Substring(3)}"; lblTheme.Content = $"Theme Color is {color}"; lblTheme.Foreground = (SolidColorBrush)SystemParameters.WindowGlassBrush; lblTheme.Visibility = Visibility.Visible; if (lightMode == "Theme") { await SetColor(color); } if (DateTime.Now.Minute % 5 == 0) { await SettingsService.SaveSettings(Config); } } catch (Exception ex) { DiagnosticsClient.TrackException(ex); } break; default: break; } } } } }
private async Task CallGraph() { if (_graphServiceClient == null) { _graphServiceClient = _graphservice.GetAuthenticatedGraphClient(typeof(WPFAuthorizationProvider)); } stopThemePolling = true; stopGraphPolling = false; signInPanel.Visibility = Visibility.Collapsed; lblTheme.Visibility = Visibility.Collapsed; loadingPanel.Visibility = Visibility.Visible; var(profile, presence) = await System.Threading.Tasks.Task.Run(() => GetBatchContent()); var photo = await System.Threading.Tasks.Task.Run(() => GetPhoto()); if (photo == null) { MapUI(presence, profile, new BitmapImage(new Uri("pack://application:,,,/PresenceLight;component/images/UnknownProfile.png"))); } else { MapUI(presence, profile, LoadImage(photo)); } if (!string.IsNullOrEmpty(Config.HueApiKey) && !string.IsNullOrEmpty(Config.HueIpAddress) && !string.IsNullOrEmpty(Config.SelectedHueLightId)) { await _hueService.SetColor(presence.Availability, Config.SelectedHueLightId); } if (Config.IsLIFXEnabled && !string.IsNullOrEmpty(Config.LIFXApiKey)) { await _lifxService.SetColor(presence.Availability, (Selector)Config.SelectedLIFXItemId); } loadingPanel.Visibility = Visibility.Collapsed; this.signInPanel.Visibility = Visibility.Collapsed; hueIpAddress.IsEnabled = false; dataPanel.Visibility = Visibility.Visible; await SettingsService.SaveSettings(Config); while (true) { if (stopGraphPolling) { stopGraphPolling = false; notificationIcon.Text = PresenceConstants.Inactive; notificationIcon.Icon = new BitmapImage(new Uri(IconConstants.GetIcon(String.Empty, IconConstants.Inactive))); return; } await Task.Delay(Convert.ToInt32(Config.PollingInterval * 1000)); try { presence = await System.Threading.Tasks.Task.Run(() => GetPresence()); if (!string.IsNullOrEmpty(Config.HueApiKey) && !string.IsNullOrEmpty(Config.HueIpAddress) && !string.IsNullOrEmpty(Config.SelectedHueLightId)) { await _hueService.SetColor(presence.Availability, Config.SelectedHueLightId); } if (Config.IsLIFXEnabled && !string.IsNullOrEmpty(Config.LIFXApiKey)) { await _lifxService.SetColor(presence.Availability, (Selector)Config.SelectedLIFXItemId); } MapUI(presence, null, null); } catch { } } }