/// <summary> /// Sets the weather actual forecast. /// </summary> /// <returns> /// <c>true</c>, if weather actual forecast was set, <c>false</c> otherwise. /// </returns> /// <param name='obCurrentConditions'> /// Ob current conditions. /// </param> public bool SetWeatherActualForecast(XElement obCurrentConditions, XNode actualValue) { DateTime today = DateTime.Now; bool result = true; try { //get sunset and sunrise XNode timeInfoNode = obCurrentConditions.Nodes().Where(desc => desc.ToString().Contains("sunrise") && desc.ToString().Contains("sunset")).FirstOrDefault(); this.Day = new DateTime(today.Year, today.Month, today.Day); this.DayOfWeekDescription = this.Day.DayOfWeek.ToString() + ","; this.DayDescription = this.Day.ToString("MMMM", System.Globalization.CultureInfo.InvariantCulture) + " " + this.Day.Day; this.SunriseDescription = ((XElement)timeInfoNode).Attribute("sunrise").Value.ToString(); this.SunsetDescription = ((XElement)timeInfoNode).Attribute("sunset").Value.ToString(); //decode sunrise and sunset this.Sunrise = WeatherControl.GetDay(today, this.SunriseDescription); this.Sunset = WeatherControl.GetDay(today, this.SunsetDescription); //get the rest of the data from the next node this.CurrentTemperature = Convert.ToInt32(((XElement)actualValue).Attribute("temp").Value.ToString()); this.WeatherDescription = ((XElement)actualValue).Attribute("text").Value.ToString(); this.WeatherDescriptionCode = ((XElement)actualValue).Attribute("code").Value.ToString(); this.WeatherCode = (WeatherCode)Enum.Parse(typeof(WeatherCode), this.WeatherDescriptionCode); } catch (Exception ex) { //log.Error("Error in SetWeatherActualForecast. Reason: " +ex.Message); result = false; } return(result); }
/// <summary> /// Sets the weather next days forecast. /// </summary> /// <returns> /// <c>true</c>, if weather next forecast was set, <c>false</c> otherwise. /// </returns> /// <param name='actualValuesAndForecast'> /// Actual values and forecast. /// </param> public bool SetWeatherNextForecast(XElement actualValuesAndForecast) { DateTime today = DateTime.Now; DayOfWeek dayName = today.DayOfWeek; bool isFirstCorrected = false; bool result = true; try { //now next days forecast List <XNode> nodeForecastAux = actualValuesAndForecast.Nodes().Where(desc => desc.ToString().Contains("forecast") && desc.ToString().Contains("day")).ToList(); List <XNode> nodeForecast = new List <XNode> (); XNode actualDayForecastInfo = null; for (int i = 0; i < nodeForecastAux.Count; i++) { if (isFirstCorrected) { nodeForecast.Add(nodeForecastAux [i]); } else { string codeDay = ((XElement)nodeForecastAux [i]).Attribute("day").Value.ToString().ToUpper(); DayOfWeek nodeDayName = WeatherControl.GetDayOfWeek(codeDay.ToUpper()); //check if the first day is today (it should be) if (nodeDayName == dayName) { isFirstCorrected = true; actualDayForecastInfo = nodeForecastAux[i]; } } } if (actualDayForecastInfo != null) { this.ActualForecast = new WeatherForecast(actualDayForecastInfo); } else { this.ActualForecast = new WeatherForecast(); } //we made the next days predictions if (nodeForecast.Count > 0) { this.NextForecasts.Clear(); foreach (XNode prediction in nodeForecast) { this.NextForecasts.Add(new WeatherForecast(prediction)); } } else //error, clear result { this.NextForecasts.Clear(); result = false; } } catch (Exception ex) { //log.Error("Error in SetWeatherNextForecast. Reason: " +ex.Message); result = false; } return(result); }