private async void stopFan_Checked(object sender, RoutedEventArgs e) { Models.Thermostat target = DeviceCarousel.SelectedItem as Models.Thermostat; if (null == target) { return; } target.FanStop = stopFan.IsChecked.Value; if (target.fan_timer_active) { ThermostatAPI api = Global.Instance.ThermostatAPI; Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; string token = localSettings.Values["NestAccessToken"] as string; Task <string> t = Task.Run(async() => await api.SetFanOff(target.Reference)); t.Wait(); if (t.Result.Contains("error")) { var dialog = new MessageDialog(api.ErrorMessage); await dialog.ShowAsync(); if (api.AuthorizationError) { ViewModel.ToSettingButtonClickedCommand.Execute(null); } } else { target.fan_timer_active = false; } } }
private void AuthProcess_ContentLoading(WebView sender, WebViewContentLoadingEventArgs args) { if (null != args.Uri && args.Uri.AbsolutePath.Contains("/login/oauth2/pincode/")) { ViewModel.SettingView = false; ViewModel.AuthProcessView = false; ThermostatAPI api = Global.Instance.ThermostatAPI; string json = args.Uri.AbsolutePath.Replace("/login/oauth2/pincode/", ""); Task <bool> t = Task.Run(async() => await api.GetAccessToken(json)); t.Wait(); if (t.Result) { //Task m = Task.Run(async () => await SetTokenToMainApp(api.AuthToken)); //m.Wait(); var message = new ValueSet(); message.Add("Type", "COMMON"); message.Add("Command", "SETNESTTOKEN"); message.Add("NESTTOKEN", api.AuthToken); //AppServiceResponse response = await ConnectionService._connection.connection.SendMessageAsync(message); IOTOI.Common.CommonService.GetReturnData(message); ViewModel.InitDevice(); } else { ViewModel.Setting(); } } }
/// <summary> /// 생성자 /// </summary> private Global() { ThermostatAPI = new ThermostatAPI(); AuthorizationEndpoint = "https://api.home.nest.com/oauth2/access_token"; //AppParams = "fan_timer_active 30"; }
async void CheckTemperatureCallback(object state) { // do some work not connected with UI DateTime Current = DateTime.Now; ThermostatDevice target = null; double target_temperature = -1; string temperature_scale = ""; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // do some work on UI here; if (0 < ViewModel.DeviceList.Count) { for (int i = 0; i < ViewModel.DeviceList.Count; ++i) { if (ViewModel.DeviceList[i].ChangeTemperature) { if (2000 <= (Current - ViewModel.DeviceList[i].LatestdChangeTime).TotalMilliseconds) { ViewModel.DeviceList[i].ChangeTemperature = false; target = ViewModel.DeviceList[i].Reference; target_temperature = ViewModel.DeviceList[i].target_temperature; temperature_scale = ViewModel.DeviceList[i].temperature_scale; break; } } } } }); if (null == target) { return; } ThermostatAPI api = Global.Instance.ThermostatAPI; Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; string token = localSettings.Values["NestAccessToken"] as string; string t = api.SetTemperature(target, target_temperature, temperature_scale).Result; //Task<string> t = Task.Run(async () => await api.SetTemperature(ViewModel.DeviceList[target].Reference, ViewModel.DeviceList[target].target_temperature, ViewModel.DeviceList[target].temperature_scale)); //t.Wait(); if (t.Contains("error")) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { var dialog = new MessageDialog(t); dialog.ShowAsync(); if (api.AuthorizationError) { ViewModel.ToSettingButtonClickedCommand.Execute(null); } }); } }
public bool InitDevice() { SettingView = false; AuthProcessView = false; ThermostatAPI api = Global.Instance.ThermostatAPI; Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; string token = localSettings.Values["NestAccessToken"] as string; if (string.IsNullOrEmpty(token)) { Setting(); return(false); } else { api.ApplyAccessToken(token); Task <bool> t = Task.Run(async() => await api.GetDevices()); t.Wait(); if (t.Result) { int count = DeviceList.Count; if (0 == count) { foreach (ThermostatDevice d in api.ThermostatDevices) { FanDuration nfd = DurationList.Where(L => L.Duration == d.fan_timer_duration.ToString()).SingleOrDefault(); Models.Thermostat nts = new Models.Thermostat(d); nts.Duration = nfd; nts.RollDuration = nfd; nts.Tick = 1; //("C" == nts.temperature_scale) ? 1 : 2; nts.FanRun = nts.fan_timer_active == true; nts.FanStop = nts.fan_timer_active == false; if (d == api.ThermostatDevices.First()) { nts.FanBrush = new SolidColorBrush(Colors.White); nts.FanBallBrush = new SolidColorBrush(Colors.White); nts.Duration.Brush = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xcb, 0x00)); nts.Duration.BallBrush = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xcb, 0x00)); } DeviceList.Add(nts); } } else { if (api.ThermostatDevices.Count < count) { DeviceList.Clear(); } foreach (ThermostatDevice d in api.ThermostatDevices) { FanDuration nfd = DurationList.Where(L => L.Duration == d.fan_timer_duration.ToString()).SingleOrDefault(); Models.Thermostat tm = DeviceList.Where(T => T.device_id == d.device_id).SingleOrDefault(); if (null == tm || string.IsNullOrEmpty(tm.device_id)) { Models.Thermostat nts = new Models.Thermostat(d); nts.Duration = nfd; nts.RollDuration = nfd; nts.Tick = 1; //("C" == nts.temperature_scale) ? 1 : 2; nts.FanRun = nts.fan_timer_active == true; nts.FanStop = nts.fan_timer_active == false; DeviceList.Add(nts); continue; } if (null != tm || false == string.IsNullOrEmpty(tm.device_id)) { tm.Update(d, nfd); } } } if (firstInit) { firstInit = false; OnTemperatureSetting = true; } return(true); } else { Setting(); } return(false); } }