//Create the gui for the user private void BuildViewByData(WeatherData data) { CityText.Content = data.City.Name + ", " + data.City.Country; SunText.Content = data.City.Sun.Rise.ToShortTimeString() + "/" + data.City.Sun.Set.ToShortTimeString(); TempText.Content = data.Temp.Value + "°C"; WindText.Content = data.Wind.Speed.Value + "m/s " + data.Wind.Speed.Name; LastTimeText.Content = data.LastUpdateTime.ToString("F"); BitmapImage logo = new BitmapImage(); logo.BeginInit(); logo.UriSource = new Uri(data.Weather.Icon); logo.EndInit(); Icon.Source = logo; IconLabel.Content = data.Weather.Value; }
async void OnGetWeatherButtonClicked(object sender, EventArgs e) { WeatherData weatherData = await _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint)); BindingContext = weatherData; }
public StatisticsDisplay(WeatherData wd) { weatherDate = wd; wd.registerObserver(this); }
/// <summary> /// Parse xml from openweathermap.org /// </summary> ForecastsList GetDataFromXml(XmlDocument doc) { doc.Save("forecast.xml"); //сохранять больше не требуется, но будем и дальше это делать для сущей красоты var result = new ForecastsList(); result.ParseUnitsFromXml(doc); var elements = doc.SelectNodes("//time"); //парсинг с помощью xpath foreach (XmlNode node in elements) { var forecast = new WeatherData(); var timeFromValue = node.SelectSingleNode("@from").Value; var timeToValue = node.SelectSingleNode("@to").Value; var timeFrom = DateTime.Parse(timeFromValue); var timeTo = DateTime.Parse(timeToValue); forecast.DateStr = timeFrom.ToShortDateString(); forecast.TimeStr = timeFrom.ToShortTimeString() + " - " + timeTo.ToShortTimeString(); forecast.Temperature = /*float.Parse(*/node.SelectSingleNode("temperature/@value").Value;//); forecast.WindDirection = node.SelectSingleNode("windDirection/@name").Value; forecast.WindSpeed = /*float.Parse(*/node.SelectSingleNode("windSpeed/@mps").Value;//); forecast.WindDescription = node.SelectSingleNode("windSpeed/@name").Value; forecast.Pressure = /*float.Parse(*/node.SelectSingleNode("pressure/@value").Value;//); var _precipitationType = node.SelectSingleNode("precipitation/@type"); forecast.PrecipitationDescription = _precipitationType == null ? "" : _precipitationType.Value; if (forecast.PrecipitationDescription == "show") { forecast.PrecipitationDescription = "snow"; } var _precValue = /*float.Parse(*/node.SelectSingleNode("precipitation/@value");//); forecast.Precipitation = _precValue == null ? "" : _precValue.Value; forecast.Humidity = /*float.Parse(*/node.SelectSingleNode("humidity/@value").Value;//); forecast.CloudsDescription = node.SelectSingleNode("clouds/@value").Value; forecast.Clouds = node.SelectSingleNode("clouds/@all").Value; result.Forecasts.Add(forecast); } return result; }
private async Task RefreshCircularGaugeAnimationAsync() { #region ANIMATED int i = 0; bool ended = false; double prevWind = weather.LstWeather[0].WindSpeed; WeatherData item = weather.LstWeather[0]; DateTime nextDay = weather.LstWeather[0].Date; DateTime temps = DateTime.Now; int minutes; try { double linearSpeed = 0; while (i < weather.LstWeather.Count - 1 && !ended) { item = weather.LstWeather[i]; if (i < weather.LstWeather.Count - 1) { nextDay = weather.LstWeather[i + 1].Date; } minutes = TimeIncrement(prevWind, item.WindSpeed); windSpeed.chartView.Chart.Header.Text = string.Format("{0:0.00} m/s - {1:0.00} º", item.WindSpeed, item.WindDegree); windSpeed.chartView.Chart.SubHeader.Text = item.Date.ToString(); //windSpeed2.chartView.Chart.Header.Text = string.Format("{0:0.00} m/s - {1:0.00} º", item.WindSpeed, item.WindDegree); if (windSpeed.wndSpeedGauge.Value < item.WindSpeed) { bool endUp = false; try { while (!endUp) { temps += TimeSpan.FromMinutes(minutes); if (linearSpeed + 0.15 < item.WindSpeed) { linearSpeed += 0.15; //windSpeed2.chartView.Chart.Axes.Bottom.SetMinMax(weather.LstWeather[0].Date, temps); //windSpeed2.wndSpeedLinear.Add(temps, linearSpeed); windSpeed.wndSpeedGauge.Value += 0.15; } else { //windSpeed2.wndSpeedLinear.Add(item.Date, item.WindSpeed); windSpeed.wndSpeedGauge.Value = item.WindSpeed; endUp = true; } //windSpeed2.chartView.Chart.Axes.Bottom.SetMinMax(weather.LstWeather[0].Date, item.Date); await Task.Delay(10); } } catch (ObjectDisposedException) { endUp = true; ended = true; } } else { bool endDown = false; try { while (!endDown) { temps += TimeSpan.FromMinutes(minutes); if (linearSpeed - 0.15 > item.WindSpeed) { linearSpeed -= 0.15; //windSpeed2.chartView.Chart.Axes.Bottom.SetMinMax(weather.LstWeather[0].Date, temps); //windSpeed2.wndSpeedLinear.Add(temps, linearSpeed); windSpeed.wndSpeedGauge.Value -= 0.15; } else { //windSpeed2.wndSpeedLinear.Add(item.Date, item.WindSpeed); windSpeed.wndSpeedGauge.Value = item.WindSpeed; endDown = true; } //windSpeed2.chartView.Chart.Axes.Bottom.SetMinMax(weather.LstWeather[0].Date, item.Date); await Task.Delay(10); } } catch (ObjectDisposedException) { endDown = true; ended = true; } } i++; temps = item.Date; prevWind = item.WindSpeed; } //ShowChart(windSpeed.chartView.Chart); //ShowChart(windSpeed2.chartView.Chart); } catch (ObjectDisposedException) { ended = true; } //ShowChart(windSpeed.chartView.Chart); //ShowChart(windSpeed2.chartView.Chart); #endregion }
/// <summary> /// This method is responsible to send http request to the provided URL and get the reponse to XML, /// The XML will provide all the detalis we need to build Weather Data instance and return it. /// </summary> public WeatherData GetWeatherData(Location location) { //wData is the instance that holds the weather info. var wData = new WeatherData(); //xdoc is var that holds the xml elements and parse them XDocument xdoc; //api var holds the http response. var api = string.Format("http://api.worldweatheronline.com/free/v2/weather.ashx?key=dbc688a2aa9e77e11005ceb43ab37&q={0}&format=xml", location.Name); //Parse the XML try { xdoc = XDocument.Load(api); var list = from item in xdoc.Descendants("data") select new { Name = item.Element("request").Element("query").Value, LastTime = item.Element("current_condition").Element("observation_time").Value, TempValue = item.Element("current_condition").Element("temp_C").Value, LastDate = item.Element("weather").Element("date").Value, SunRise = item.Element("weather").Element("astronomy").Element("sunrise").Value, SunSet = item.Element("weather").Element("astronomy").Element("sunset").Value, WindSpeedValue = item.Element("weather").Element("hourly").Element("windspeedMiles").Value, IconName = item.Element("weather").Element("hourly").Element("weatherIconUrl").Value, IconText = item.Element("weather").Element("hourly").Element("weatherDesc").Value }; //Insert the relevant parameters to wData instance. foreach (var data in list) { wData.City = new City(); var tokens = data.Name.Split(','); wData.City.Name = tokens[0]; wData.City.Country = tokens[1]; wData.City.Sun.Rise = DateTime.Parse(data.SunRise); wData.City.Sun.Set = DateTime.Parse(data.SunSet); wData.Temp = new Temperature(); wData.Temp.Value = double.Parse(data.TempValue); wData.Wind = new Wind(); wData.Wind.Speed = new Speed(); wData.Wind.Speed.Value = double.Parse(data.WindSpeedValue); wData.Weather = new Weather(); wData.Weather.Icon = data.IconName; wData.Weather.Value = data.IconText; wData.LastUpdateTime = DateTime.Parse(data.LastDate + " " + data.LastTime); } } //Exceptions catch (WebException) { throw new WeatherDataServiceException("Web connectivity Exception"); } catch (Exception) { throw new WeatherDataServiceException("Parsing Exception"); } //return the wData instance which now is updated . return wData; }
public void ChangeCenter(WeatherData data) { Map.Center = data.Coords; Pin.Location = data.Coords; AddTileLayers(); }
/// <summary> /// This method is responsible to send http request to the web service and get the reponse to XML, /// The XML will provide all the info we need to build wData instance and return it. /// </summary> public WeatherData GetWeatherData(Location location) { //xdoc is var that holds the xml elements and parse them XDocument xdoc; //wData is the instance that holds the weather info. var wData = new WeatherData(); //api var holds the http response. var api = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&mode=xml", location.Name); try { //Parse the xdoc and get the relevant info. xdoc = XDocument.Load(api); var list = from item in xdoc.Descendants("current") select new { Name = item.Element("city").Attribute("name").Value, CoordLat = item.Element("city").Element("coord").Attribute("lat").Value, CoordLon = item.Element("city").Element("coord").Attribute("lon").Value, Country = item.Element("city").Element("country").Value, SunRise = item.Element("city").Element("sun").Attribute("rise").Value, SunSet = item.Element("city").Element("sun").Attribute("set").Value, TempValue = item.Element("temperature").Attribute("value").Value, WindSpeedValue = item.Element("wind").Element("speed").Attribute("value").Value, WindSpeedText = item.Element("wind").Element("speed").Attribute("name").Value, IconName = item.Element("weather").Attribute("icon").Value, IconText = item.Element("weather").Attribute("value").Value, LastUpdate = item.Element("lastupdate").Attribute("value").Value }; //Insert the relevant parameters to wData instance. foreach (var data in list) { //Insert the city parameters wData.City = new City(); wData.City.Name = data.Name; wData.City.Country = data.Country; wData.City.Coords.Lat = double.Parse(data.CoordLat); wData.City.Coords.Lon = double.Parse(data.CoordLon); wData.City.Sun.Rise = DateTime.Parse(data.SunRise); wData.City.Sun.Set = DateTime.Parse(data.SunSet); //Insert the temperature parameters wData.Temp = new Temperature(); wData.Temp.Value = double.Parse(data.TempValue); //Kelvin unit becomes C wData.Temp.Value -= 272.15; //Insert the wind parameters wData.Wind = new Wind(); wData.Wind.Speed = new Speed(); wData.Wind.Speed.Value = double.Parse(data.WindSpeedValue); wData.Wind.Speed.Name = data.WindSpeedText; //Insert the weather conclusion with the suitable icon. wData.Weather = new Weather(); wData.Weather.Icon = "http://openweathermap.org/img/w/" + data.IconName + ".png"; wData.Weather.Value = data.IconText; //Insert the last update time wData.LastUpdateTime = DateTime.Parse(data.LastUpdate); } } //Exceptions catch (WebException) { throw new WeatherDataServiceException("Web connectivity Exception"); } catch (Exception) { throw new WeatherDataServiceException("Parsing Exception"); } //return the wData instance. return wData; }
protected override async void OnAppearing() { string[,] alldate = new string[32, 4] { { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, { "", "", "", "" }, }; //6日のデータ格納する型(0行目は並び替え用) var result = await VEvent.GetStringAsync(uri); int x = 1; //ループカウント foreach (VEvent evset in result) { //6日のデータを格納 WeatherData weatherData = await _restService.GetWeatherData(GenerateRequestUri(Constants.OpenWeatherMapEndpoint, evset.CODE)); BindingContext = weatherData; alldate[x, 0] = evset.DTSTART; alldate[x, 1] = evset.SUMMARY; alldate[x, 2] = evset.ADDRESS; alldate[x, 3] = weatherData.Weather[0].Description; x++; } for (int i = 1; i <= 31; i++) //日付の昇順に並び替え { if (alldate[i, 0] == "") { break; } for (int j = i + 1; j <= 31; j++) { if (alldate[j, 0] == "") { break; } if (int.Parse(alldate[i, 0]) > int.Parse(alldate[j, 0])) { alldate[0, 0] = alldate[i, 0]; alldate[0, 1] = alldate[i, 1]; alldate[0, 2] = alldate[i, 2]; alldate[0, 3] = alldate[i, 3]; alldate[i, 0] = alldate[j, 0]; alldate[i, 1] = alldate[j, 1]; alldate[i, 2] = alldate[j, 2]; alldate[i, 3] = alldate[j, 3]; alldate[j, 0] = alldate[0, 0]; alldate[j, 1] = alldate[0, 1]; alldate[j, 2] = alldate[0, 2]; alldate[j, 3] = alldate[0, 3]; } } } int y = 1; //年月日を日付に変更 string month; string day; while (alldate[y, 0] != "") { if (alldate[y, 0].Substring(4, 1) == "0") { month = alldate[y, 0].Substring(5, 1) + "月"; } else { month = alldate[y, 0].Substring(4, 2) + "月"; } day = alldate[y, 0].Substring(6, 2) + "日"; alldate[y, 0] = month + day; y++; } int cnt = 1; //ループカウント foreach (VEvent ev in result) //ラベルに表示 { if (cnt == 1) { day1.Text = alldate[cnt, 0]; today.Text = alldate[cnt, 0]; summary1.Text = alldate[cnt, 1]; summarytoday.Text = alldate[cnt, 1]; venue1.Text = alldate[cnt, 2]; venuetoday.Text = alldate[cnt, 2]; weather1.Text = alldate[cnt, 3]; weathertoday.Text = alldate[cnt, 3]; } if (cnt == 2) { day2.Text = alldate[cnt, 0]; summary2.Text = alldate[cnt, 1]; venue2.Text = alldate[cnt, 2]; weather2.Text = alldate[cnt, 3]; } if (cnt == 3) { day3.Text = alldate[cnt, 0]; summary3.Text = alldate[cnt, 1]; venue3.Text = alldate[cnt, 2]; weather3.Text = alldate[cnt, 3]; } if (cnt == 4) { day4.Text = alldate[cnt, 0]; summary4.Text = alldate[cnt, 1]; venue4.Text = alldate[cnt, 2]; weather4.Text = alldate[cnt, 3]; } if (cnt == 5) { day5.Text = alldate[cnt, 0]; summary5.Text = alldate[cnt, 1]; venue5.Text = alldate[cnt, 2]; weather5.Text = alldate[cnt, 3]; } if (cnt == 6) { day6.Text = alldate[cnt, 0]; summary6.Text = alldate[cnt, 1]; venue6.Text = alldate[cnt, 2]; weather6.Text = alldate[cnt, 3]; } cnt++; } }
public WeatherForecast(WeatherData wd) { weatherData = wd; weatherData.registerObserver(this); }