public static async Task <List <DailyForecast> > FiveDays(string cityKey) { FiveDayForecast fiveDayForecasts = new FiveDayForecast(); List <DailyForecast> dailyForecasts; string url = Base_url + string.Format(FiveDayForecast_url, cityKey, API); //http://dataservice.accuweather.com/forecasts/v1/daily/5day/298198?apikey=T6ouI10df3xiOMyjgoAq7GXf3yBsQRbh&metric=true //http://dataservice.accuweather.com/forecasts/v1/daily/5day/332283?apikey=T6ouI10df3xiOMyjgoAq7GXf3yBsQRbh&metric=true" using (HttpClient httpClient = new HttpClient()) { var response = await httpClient.GetAsync(url); if (response.IsSuccessStatusCode) { var Json = await response.Content.ReadAsStringAsync(); fiveDayForecasts = JsonConvert.DeserializeObject <FiveDayForecast>(Json); /*fiveDayForecasts = JsonConvert.DeserializeObject<List<FiveDayForecast>>(Json);*/ } } dailyForecasts = (List <DailyForecast>)fiveDayForecasts.DailyForecasts; dailyForecasts.Remove(dailyForecasts.First()); dailyForecasts.Remove(dailyForecasts.Last()); return(dailyForecasts); }
// SVAKA FUNKCIJA KOJA POZIVA API MORA BITI ASYNC private async void refreshGui(Object source = null, ElapsedEventArgs e = null) { await this.Dispatcher.Invoke(async() => { try { product = await GetFiveDayAsync(apiFiveDayUrl + cityToRefresh + apiAppId); this.Day1.Content = DateTime.Parse(product.list[0].dt_txt).Day + "/" + DateTime.Parse(product.list[0].dt_txt).Month + "/" + DateTime.Parse(product.list[0].dt_txt).Year; this.Day2.Content = DateTime.Parse(product.list[8].dt_txt).Day + "/" + DateTime.Parse(product.list[8].dt_txt).Month + "/" + DateTime.Parse(product.list[8].dt_txt).Year; this.Day3.Content = DateTime.Parse(product.list[16].dt_txt).Day + "/" + DateTime.Parse(product.list[16].dt_txt).Month + "/" + DateTime.Parse(product.list[16].dt_txt).Year; this.Day4.Content = DateTime.Parse(product.list[24].dt_txt).Day + "/" + DateTime.Parse(product.list[24].dt_txt).Month + "/" + DateTime.Parse(product.list[24].dt_txt).Year; this.Day5.Content = DateTime.Parse(product.list[32].dt_txt).Day + "/" + DateTime.Parse(product.list[31].dt_txt).Month + "/" + DateTime.Parse(product.list[31].dt_txt).Year; Overview.Content = product.list[dayComparison].weather[0].description; double min = 900000, max = 0; for (int i = 0; i < 7; i++) { if (min > product.list[dayComparison + i].main.temp_min) { min = product.list[dayComparison + i].main.temp_min; } if (max < product.list[dayComparison + i].main.temp_max) { max = product.list[dayComparison + i].main.temp_max; } } Temperatura.Content = Math.Round(min - 272.15) + "°C" + "/" + Math.Round(max - 272.15) + "°C"; CurTemp.Content = Math.Round(product.list[dayComparison].main.temp - 272.15) + "°C"; Humidity.Content = "Humidity: " + product.list[dayComparison].main.humidity + "%"; Wind.Content = "Wind: " + product.list[dayComparison].wind.speed + "km/s"; City.Content = cityToRefresh; this.Draw(image, product.list[dayComparison].weather[0].main, true); } catch (Exception ex) { if (ex is NullReferenceException) { MessageBox.Show("Cannot find information for city. Please check your spelling or try another, larger city.", "Unknown city", MessageBoxButton.OK, MessageBoxImage.Exclamation); } else { MessageBox.Show("Cannot contact server. Please check your internet connection and try again later.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } cityToRefresh = ""; if (guiRefreshTimer != null) { guiRefreshTimer.Stop(); guiRefreshTimer.Dispose(); } } }); }
static async Task <FiveDayForecast> GetFiveDayAsync(string path) { FiveDayForecast product = null; HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { // OTKOMENTARISATI KADA SE NAPRAVI KLASA CurrentForecast //product = await response.Content.ReadAsAsync<FiveDayForecast>(); string yeet = await response.Content.ReadAsStringAsync(); product = JsonConvert.DeserializeObject <FiveDayForecast>(yeet); } return(product); }
public FiveDayForecastDTO(FiveDayForecast weather) { if (weather != null) { this.City = weather.Location.City; this.WeatherInfos = new List <ThreeHourForecastDTO>(); if (weather.ThreeHourForecasts != null) { foreach (var time in weather.ThreeHourForecasts) { this.WeatherInfos.Add(new ThreeHourForecastDTO(time)); } } } }
private async void LocationMethod(string ip) { IP ip2 = await GetCurrentLocation(ip + apiIP); GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default); watcher.Start(); GeoCoordinate cord = watcher.Position.Location; if (!watcher.Position.Location.IsUnknown) { double lat = cord.Latitude; double lon = cord.Longitude; } product = await GetFiveDayAsync(apiFiveDayUrlIP + "lat=" + ip2.latitude + "&lon=" + ip2.longitude + apiAppId); searchBox.Text = product.city.name + "," + product.city.country; this.cityToRefresh = searchBox.Text; setupTimer(); }
public FiveDayForecast GetForecast(string parkCode) { FiveDayForecast forecast = new FiveDayForecast(); parkCode = parkCode.ToUpper(); const string getWeatherQuery = @"select * from weather where parkCode = @parkCode"; using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(getWeatherQuery, conn); cmd.Parameters.AddWithValue("@parkCode", parkCode); var reader = cmd.ExecuteReader(); while (reader.Read()) { forecast.WeatherDays.Add(AssignWeatherDay(reader)); forecast.ParkCode = reader["parkCode"].ToString(); } return(forecast); } }
public MainWindow() { InitializeComponent(); this.count = new DateTime(); this.product = new FiveDayForecast(); this.cityToRefresh = ""; this.dayComparison = 0; this.favWin = new FavouriteWindow(this); CitiesHistory = new List <City>(10); this.history_window = new HistoryWindow(this); this.s = new ComplexWindow(); Closing += this.OnWindowClosing; string path = Directory.GetParent(Directory.GetParent(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)).FullName).FullName; this.Background = new ImageBrush(new BitmapImage(new Uri(path + @"\images\oblacno.png"))); button.Background = new ImageBrush(new BitmapImage(new Uri(path + @"\images\search.png"))); button1.Background = new ImageBrush(new BitmapImage(new Uri(path + @"\images\history.png"))); #region CityList AllCityList = new ObservableCollection <string>(); AllCityList.Add("London,UK"); AllCityList.Add("London,US"); AllCityList.Add("Paris,FR"); AllCityList.Add("Bangkok,TH"); AllCityList.Add("Singapore,SG"); AllCityList.Add("New York,US"); AllCityList.Add("Kuala Lumpur,MY"); AllCityList.Add("Hong Kong,HK"); AllCityList.Add("Dubai,AE"); AllCityList.Add("Istanbul,TR"); AllCityList.Add("Rome,IT"); AllCityList.Add("Shangai,CN"); AllCityList.Add("Los Angeles,CL"); AllCityList.Add("Las Vegas,US"); AllCityList.Add("Miami,US"); AllCityList.Add("Toronto,CA"); AllCityList.Add("Barcelona,ES"); AllCityList.Add("Dublin,IE"); AllCityList.Add("Amsterdam,NL"); AllCityList.Add("Moscow,RU"); AllCityList.Add("Cairo,EG"); AllCityList.Add("Prague,CZ"); AllCityList.Add("Belgrade,RS"); AllCityList.Add("Novi Sad,RS"); AllCityList.Add("Tokyo,JP"); this.DataContext = this; #endregion LoadFavourites(); client2.BaseAddress = new Uri("http://api.ipstack.com/"); client2.DefaultRequestHeaders.Accept.Clear(); client2.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); string ip = GetLocalIPAddress(); Console.WriteLine(ip + " OVO JE IP ADRESA"); string ip2 = "188.2.100.218"; LocationMethod(ip2); client.BaseAddress = new Uri("http://api.openweathermap.org/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); }
public async Task <FiveDayForecast> GetFiveDayForecast(int CityID, bool UseMetric) { FiveDayForecast myFiveDayForcast = new FiveDayForecast(); myFiveDayForcast.FiveDayForcast = new List <WeatherForecast>(); Uri myURI; if (UseMetric) { myURI = new Uri(baseURL + "forecast/daily?id=" + CityID + "&cnt=6" + "&units=metric" + "&appid=" + OW_AppID); } else { myURI = new Uri(baseURL + "forecast/daily?id=" + CityID + "&cnt=6" + "&units=imperial" + "&appid=" + OW_AppID); } HttpClient myClient = new HttpClient(); HttpResponseMessage httpResponse = new HttpResponseMessage(); try { httpResponse = await myClient.GetAsync(myURI); if (httpResponse.IsSuccessStatusCode) { var rawMultiDayForecast = JsonConvert.DeserializeObject <MultiDayForecast>(httpResponse.Content.ToString()); myFiveDayForcast.City = App.CurrentAppData.City; bool isFirst = true; foreach (Forecast tmpForecast in rawMultiDayForecast.list) { if (!isFirst) { WeatherForecast addWF = new WeatherForecast(); addWF.ForcastDate = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(tmpForecast.dt), TimeZoneInfo.Utc).DateTime; addWF.CurrentTemp = tmpForecast.temp.day; addWF.DayCondition = tmpForecast.weather.FirstOrDefault().id; addWF.DayHighTemp = tmpForecast.temp.max; addWF.DayLowTemp = tmpForecast.temp.min; addWF.Humidity = tmpForecast.humidity; addWF.Pressure = tmpForecast.pressure; addWF.WindSpeed = tmpForecast.speed; myFiveDayForcast.FiveDayForcast.Add(addWF); } else { isFirst = false; } } } } catch (Exception ex) { myClient.Dispose(); throw new Exception("ERROR! Cannot retrive Weather Data.", ex); } return(myFiveDayForcast); }