public ForecastWeatherData(ForecastIOResponse response)
 {
     Temperature = response.currently.temperature;
     WindDirection = (int)response.currently.windBearing;
     WindSpeed = (int)response.currently.windSpeed;
     Condition = response.currently.summary;
     Humidity = (int)(response.currently.humidity*100);
     Pressure = response.currently.pressure/ 1.3332239;
 }
Пример #2
0
        public ForecastIOResponse Get()
        {
            try
            {
                var uri = (_time == null)
                            ? string.Format(CurrentForecastUrl, _apiKey, _latitude, _longitude, _unit, _lang, _extend, _exclude)
                            : string.Format(PeriodForecastUrl, _apiKey, _latitude, _longitude, _time, _unit, _lang, _extend, _exclude);


                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "GET";
                request.Headers.Add("Content-Encoding", "gzip");
                request.AutomaticDecompression = DecompressionMethods.GZip;
                request.ContentType            = "application/json";

                var response = (HttpWebResponse)request.GetResponse();
                ForecastIOResponse result = null;

                using (Stream responseStream = response.GetResponseStream())
                {
                    var reader  = new StreamReader(responseStream);
                    var jsonOut = reader.ReadToEnd();
                    reader.Close();
                    result = JsonConvert.DeserializeObject <ForecastIOResponse>(jsonOut);
                }

                // Set response values.
                _apiResponseTime = response.Headers["X-Response-Time"];
                _apiCallsMade    = response.Headers["X-Forecast-API-Calls"];

                return(result);
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder(500);
                sb.AppendLine("Error while requesting weather from source!");

                WebException wex = ex as WebException;
                if (wex != null)
                {
                    HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
                    if (httpResponse != null)
                    {
                        int    statusCode = (int)httpResponse.StatusCode;
                        string statusDesc = httpResponse.StatusDescription;

                        sb.AppendLine(string.Format("Http Status Code: {0}", statusCode));
                        sb.AppendLine(string.Format("Http Status Desc: {0}", statusDesc));

                        if (httpResponse.Headers != null)
                        {
                            sb.AppendLine("All response header values:");
                            foreach (var key in httpResponse.Headers.AllKeys)
                            {
                                string value = httpResponse.Headers[key];
                                sb.AppendLine(string.Format("{0}: {1}", key, value));
                            }
                        }
                        else
                        {
                            sb.AppendLine("Unable to get response headers!");
                        }
                    }
                }
                throw new ForecastIOException(sb.ToString(), ex);
            }
        }
Пример #3
0
 public SmartViewModel(ForecastIOResponse forecast)
 {
     this.Forecast = forecast;
 }
Пример #4
0
 /// <summary>
 /// Gets the weather from the ForecastIO Api
 /// </summary>
 /// <returns>api object: ForecastIOResponse</returns>
 public ForecastIOResponse GetWeather()
 {
     try
     {
         //My API Key    5e0d94d071dee1b6acb0ad259cc54b1b
         // 55.395833f, 10.388611f these are the coordinates
         // for Odense
         var req = new ForecastIORequest("5e0d94d071dee1b6acb0ad259cc54b1b", 55.395833f, 10.388611f, DateTime.Now, Unit.si);
         var response = req.Get();
         var currentTemp = response.currently.apparentTemperature;
         // retrieve houes between 0 - 23
         return response;
     }
     catch (Exception)
     {
         ForecastIOResponse io = new ForecastIOResponse();
         return io;
     }
 }