/// <summary> /// Create a new NewWeather object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="owner">Initial value of the owner property.</param> public static NewWeather CreateNewWeather(global::System.Int32 id, global::System.Int32 owner) { NewWeather newWeather = new NewWeather(); newWeather.ID = id; newWeather.owner = owner; return(newWeather); }
public NewWeather GetWeatherinfo(string place, string region) { string requestUristring = String.Format(@"http://www.yr.no/place/Sweden/{0}/{1}/forecast.xml", region, place); var request = WebRequest.Create(requestUristring); var coordinates = GetCoordinates(place); decimal longitude = decimal.Parse(coordinates[0], System.Globalization.CultureInfo.InvariantCulture); decimal latitude = decimal.Parse(coordinates[1], System.Globalization.CultureInfo.InvariantCulture); List <double> informationdouble = new List <double>(); List <DateTime> informationdate = new List <DateTime>(); using (var response = request.GetResponse()) { using (StreamReader stream = new StreamReader(response.GetResponseStream())) { var document = XDocument.Load(stream); var list = document.Descendants("forecast") .Take(1) .ToList(); var timelist = list.Descendants("time") .ToList(); foreach (XElement element in timelist) { if (element.Attribute("period").Value == "0") { var date = element.Attribute("from").Value; DateTime myDate = DateTime.Parse(date, System.Globalization.CultureInfo.InvariantCulture); informationdate.Add(myDate); var templist = element.Descendants("temperature").ToList(); var value = templist[0] .LastAttribute .Value .ToString(); if (informationdouble.Count < 5) { informationdouble.Add(double.Parse(value, System.Globalization.CultureInfo.InvariantCulture)); } else { NewWeather newWeather = new NewWeather { //Kör en fullösning tills vidare day1day = informationdate[0], day2day = informationdate[1], day3day = informationdate[2], day4day = informationdate[3], day5day = informationdate[4], day1temp = (decimal)informationdouble[0], day2temp = (decimal)informationdouble[1], day3temp = (decimal)informationdouble[2], day4temp = (decimal)informationdouble[3], day5temp = (decimal)informationdouble[4], latitude = latitude, longitude = longitude }; return(newWeather); } } } return(null); } } }
// public List<double> GetWeatherinfo(string place) //public NewWeather GetWeatherinfo(string place) public NewWeather GetWeatherinfo(string place, string region) { //Change this one to a dynamic string // string requestUristring = String.Format(@"http://www.yr.no/place/Sweden/stockholm/norrtelje/forecast.xml"); //string requestUristring = String.Format(@"http://www.yr.no/place/Sweden/stockholm/{0}/forecast.xml", place); //right one string requestUristring = String.Format(@"http://www.yr.no/place/Sweden/{0}/{1}/forecast.xml", region, place); // string requestUristring = String.Format(@"http://api.geonames.org/search?name_startsWith={0}&country=GB&maxRows=10&username=elsteffo", place); var request = WebRequest.Create(requestUristring); var coordinates = GetCoordinates(place); decimal longitude = decimal.Parse(coordinates[0], System.Globalization.CultureInfo.InvariantCulture); decimal latitude = decimal.Parse(coordinates[1], System.Globalization.CultureInfo.InvariantCulture); //List<string> information = new List<string>(); List <double> informationdouble = new List <double>(); List <DateTime> informationdate = new List <DateTime>(); using (var response = request.GetResponse()) { using (StreamReader stream = new StreamReader(response.GetResponseStream())) { var document = XDocument.Load(stream); var list = document.Descendants("forecast") .Take(1) .ToList(); var timelist = list.Descendants("time") //.Take(5) //where last attribute(period) = 0 .ToList(); string tmpDate = "0000-00-00"; foreach (XElement element in timelist) { if (element.Attribute("period").Value == "2" || element.Attribute("period").Value == "3")//ska ersättas med 2 { if (element.Attribute("from").Value.Substring(0, 10) != tmpDate) { var date = element.Attribute("from").Value; DateTime myDate = DateTime.Parse(date, System.Globalization.CultureInfo.InvariantCulture); informationdate.Add(myDate); //använd substring för date istället för datetime. var templist = element.Descendants("temperature").ToList(); var value = templist[0] .LastAttribute .Value .ToString(); if (informationdouble.Count < 5) { informationdouble.Add(double.Parse(value, System.Globalization.CultureInfo.InvariantCulture)); } else { NewWeather newWeather = new NewWeather { // Day1day = ... day1day = informationdate[0], day2day = informationdate[1], day3day = informationdate[2], day4day = informationdate[3], day5day = informationdate[4], day1temp = (decimal)informationdouble[0], day2temp = (decimal)informationdouble[1], day3temp = (decimal)informationdouble[2], day4temp = (decimal)informationdouble[3], day5temp = (decimal)informationdouble[4], latitude = latitude, longitude = longitude //owner = get from session or something //place = place //check that it exists and is the right one first //latitude = call that method from here //longitude = call that method from here }; //var coordinates = GetCoordinates(place); //return informationdouble; return(newWeather); } } tmpDate = element.Attribute("from").Value.Substring(0, 10); } //-- //DateTime myDate = DateTime.ParseExact("2009-05-08 14:40:52,531", "yyyy-MM-dd HH:mm:ss,fff", // System.Globalization.CultureInfo.InvariantCulture) // konvertera därefter om dessa element till rätt typ //Returnera även datum från listan //returnera sedan en array av dessa element ut till action-controllern. //Create the newWeatherObject here /* Weather weather = new Weather * { * // Day1day = ... * };*/ } // } return(null); /* NewWeather newWeather2 = new NewWeather * { * }; * return newWeather2; * */ } } }
/// <summary> /// Deprecated Method for adding a new object to the NewWeathers EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToNewWeathers(NewWeather newWeather) { base.AddObject("NewWeathers", newWeather); }