コード例 #1
0
        private async Task <NestApiResponse> GetAllData()
        {
            var client = new RestClient("https://developer-api.nest.com");

            client.FollowRedirects = false;

            var request = new RestRequest(Method.GET);

            request.AddHeader("Postman-Token", "390fb86d-70f2-43bf-ac00-6882ad92e9d2");
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Authorization", "Bearer c.CxjcIdeXNmvhHFM1aGKHB0k07bPQMCrAQMdGoDAouDqnRzCR6cF9V6OOZ38s4h8kUYnEnC6wYnQbdJEScy15PhQAQUnVjZJrfNJu1RXfJ6SwbgRyS6Kv9lMuwZ9azCBhG72XiDCmDBRTtP0K");
            request.AddHeader("Content-Type", "application/json");
            IRestResponse response = await client.ExecuteTaskAsync(request);

            if (response.StatusCode == HttpStatusCode.RedirectKeepVerb)
            {
                var locationHeader = response.Headers.ToList().Where(h => h.Name == "Location").First();
                client.BaseUrl = new Uri(locationHeader.Value.ToString());
                response       = await client.ExecuteTaskAsync(request);
            }

            NestApiResponse apiResponse = JsonConvert.DeserializeObject <NestApiResponse>(response.Content);

            // Console.WriteLine(JsonConvert.SerializeObject(apiResponse, Formatting.Indented));

            return(apiResponse);
        }
コード例 #2
0
        public async Task <List <NestThermostatMeasurementEvent> > Measure()
        {
            NestApiResponse response = await this.GetAllData();

            List <NestThermostatMeasurementEvent> events = new List <NestThermostatMeasurementEvent>();

            foreach (var thermostat in response.devices.thermostats.Values)
            {
                NestThermostatMeasurementEvent e = new NestThermostatMeasurementEvent
                {
                    MeasurementTime     = DateTime.UtcNow,
                    ThermostatId        = thermostat.device_id,
                    Name                = thermostat.name,
                    WhereId             = thermostat.where_id,
                    WhereName           = thermostat.where_name,
                    LastContactTime     = thermostat.last_connection,
                    Humidity            = thermostat.humidity,
                    TemperatureScale    = thermostat.temperature_scale,
                    TargetTemperatureF  = thermostat.target_temperature_f,
                    AmbientTemperatureF = thermostat.ambient_temperature_f,
                    HvacMode            = thermostat.hvac_mode,
                    HvacState           = thermostat.hvac_state,
                };

                events.Add(e);
            }

            return(events);
        }