Пример #1
0
        private void DurationCarousel_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FanDuration target = DurationCarousel.SelectedItem as FanDuration;

            if (null == target)
            {
                return;
            }

            Models.Thermostat tm = DeviceCarousel.SelectedItem as Models.Thermostat;
            if (null == tm)
            {
                return;
            }

            tm.RollDuration = target;

            for (int i = 0; i < ViewModel.DurationList.Count; ++i)
            {
                ViewModel.DurationList[i].Brush     = (target == ViewModel.DurationList[i]) ? new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xcb, 0x00)) : new SolidColorBrush(Color.FromArgb(0xff, 0x7f, 0x7f, 0x7f));
                ViewModel.DurationList[i].BallBrush = (target == ViewModel.DurationList[i]) ? new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xcb, 0x00)) : new SolidColorBrush(Colors.Transparent);

                ViewModel.DurationList[i].PrevBallBrush = (target == ViewModel.DurationList[i] && 0 < i && 1 < ViewModel.DurationList.Count) ?
                                                          new SolidColorBrush(Color.FromArgb(0xff, 0x7f, 0x7f, 0x7f)) : new SolidColorBrush(Colors.Transparent);

                ViewModel.DurationList[i].NextBallBrush = (target == ViewModel.DurationList[i] && (i + 1) < ViewModel.DurationList.Count) ?
                                                          new SolidColorBrush(Color.FromArgb(0xff, 0x7f, 0x7f, 0x7f)) : new SolidColorBrush(Colors.Transparent);
            }
        }
        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);
            }
        }