public static async Task <XmlDocument> LoadWeather(string City, LangChooser lang, UnitChooser unit) { string apiKey = "1e98a6276b52c465746e1853aef68c90"; // my personal apiKey free and has a limit per day string langMode = "en"; string units = "standard"; XmlDocument myRestXML = new XmlDocument(); //unit choice if (unit == UnitChooser.Imperial) { units = "imperial"; } else if (unit == UnitChooser.Metric) { units = "metric"; } else if (unit == UnitChooser.Standard) { units = "standard"; } //language choice if (lang == LangChooser.Hungarian) { langMode = "hu"; } else if (lang == LangChooser.German) { langMode = "de"; } else if (lang == LangChooser.English) { langMode = "en"; } string url = $"http://api.openweathermap.org/data/2.5/weather?q={ City }&units={ units }&lang={ langMode }&mode=xml&appid={ apiKey }"; using (HttpResponseMessage response = await Api_Helper.ApiClient.GetAsync(url)) { string streamString; if (response.IsSuccessStatusCode) { streamString = await response.Content.ReadAsStringAsync(); myRestXML.LoadXml(streamString); return(myRestXML); } else { //MessageBox.Show(response.ReasonPhrase, "Hiba", MessageBoxButtons.OK); // throw new Exception(response.ReasonPhrase); return(new XmlDocument()); } } }
private void engFlag_CBox_CheckedChanged(object sender, EventArgs e) { if (hunFlag_CBox.Checked) { hunFlag_CBox.Checked = false; } if (gerFlag_CBox.Checked) { gerFlag_CBox.Checked = false; } languageChoice = LangChooser.English; }
private void gerFlag_CBox_CheckedChanged(object sender, EventArgs e) { if (engFlag_CBox.Checked) { engFlag_CBox.Checked = false; } if (hunFlag_CBox.Checked) { hunFlag_CBox.Checked = false; } languageChoice = LangChooser.German; }
private void btn_GO_Click(object sender, EventArgs e) { ValidateChildren(ValidationConstraints.Enabled); decimal lon = 0; decimal lat = 0; string id = ""; string name = ""; int searchType = 0; LangChooser lc = LangChooser.Hungarian; UnitChooser uc = UnitChooser.Standard; if (textBox_ID.Visible == true) { id = textBox_ID.Text; searchType = 1; } if (textBox_Coord_Lat.Visible == true) { lon = decimal.Parse(textBox_Coord_Long.Text); lat = decimal.Parse(textBox_Coord_Lat.Text); searchType = 2; } if (textBox_Name.Visible == true) { name = textBox_Name.Text; searchType = 3; } lc = languageChoice; uc = unitChoice; Result_Form f1 = new Result_Form(id, lon, lat, name, searchType, lc, uc); f1.Show(); }
public async void LoadWeatherData(string id, decimal lon, decimal lat, string city, int searchType, LangChooser lc, UnitChooser uc) { XmlDocument GotResponse = new XmlDocument(); if (searchType == 1) { GotResponse = await WeatherProcessor.LoadWeather(id, lc, uc); } if (searchType == 2) { GotResponse = await WeatherProcessor.LoadWeather(lat, lon, lc, uc); } if (searchType == 3) { GotResponse = await WeatherProcessor.LoadWeather(city, lc, uc); } if (GotResponse.LastChild != null) { foreach (XmlElement element in GotResponse.LastChild) { //LastChild is current the other oe is the format header //on this level we have 11 nodes if (element.Name == "city") { resultWeater.ID = int.Parse(element.GetAttribute("id")); resultWeater.Name = element.GetAttribute("name"); foreach (XmlElement item in element) // city has 4 childNodes - iterete through { if (item.Name == "coord") { resultWeater.Longitude = decimal.Parse(item.GetAttribute("lon")); resultWeater.Latitude = decimal.Parse(item.GetAttribute("lat")); } if (item.Name == "country") { resultWeater.CountrySign = item.InnerText; } if (item.Name == "sun") { resultWeater.SunRise = DateTime.Parse(item.GetAttribute("rise")); resultWeater.SunSet = DateTime.Parse(item.GetAttribute("set")); } } } if (element.Name == "temperature") { resultWeater.Temperature = decimal.Parse(element.GetAttribute("value")); resultWeater.TemperatureUnit = element.GetAttribute("unit"); } if (element.Name == "humidity") { resultWeater.Humidity = int.Parse(element.GetAttribute("value")); resultWeater.HumidityUnit = element.GetAttribute("unit"); } if (element.Name == "pressure") { resultWeater.Pressure = int.Parse(element.GetAttribute("value")); resultWeater.PressureUnit = element.GetAttribute("unit"); } if (element.Name == "wind") { foreach (XmlElement item in element) { if (item.Name == "speed") { resultWeater.WindSpeed = decimal.Parse(item.GetAttribute("value")); resultWeater.WindSpeedUnit = item.GetAttribute("unit"); } if (item.Name == "direction") { resultWeater.WindDirection = item.GetAttribute("name"); } } } if (element.Name == "clouds") { resultWeater.CloudName = element.GetAttribute("name"); } if (element.Name == "visibility") { resultWeater.Visibility = int.Parse(element.GetAttribute("value")); } if (element.Name == "lastupdate") { resultWeater.LastUpdate = DateTime.Parse(element.GetAttribute("value")); } } } else { MessageBox.Show("Nem található ilyen adat, a bezárást követően próblája újra", "Hiba", MessageBoxButtons.OK); this.Close(); } DisplayWeather(); }
public Result_Form(string id, decimal lon, decimal lat, string city, int searchType, LangChooser lc, UnitChooser uc) { InitializeComponent(); this.Paint += Result_Form_Paint; FillNameDayList(); GetCurrentExchangeRates(); DisplayNameDay(); LoadWeatherData(id, lon, lat, city, searchType, lc, uc); DisplayWeather(); mnbRateDataGridView.DataSource = Rates; dgwSaveExchange.DataSource = _controller.ExchangeManager.Exchanges; }