/// <summary> /// This event handler method calls /// the ProcessLocalWeather method of the ProcessorClass class /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void DisplayWeatherForecastButton_Click(object sender, EventArgs e) { try { /* Set input parameters for the API */ LocalWeatherInput input = new LocalWeatherInput(); input.query = SearchTextBox.Text; input.num_of_days = NoOfDaysDropDownList.SelectedValue.ToString(); input.format = "JSON"; /*Call the LocalWeather method and pass in the input parameters*/ API_Implementation api = new API_Implementation(); LocalWeather localWeather = api.GetLocalWeather(input); /* Display Output */ DisplayResultsTextBox.Text = "\r\n Cloud Cover: " + localWeather.data.current_Condition[0].cloudcover; DisplayResultsTextBox.Text += "\r\n Humidity: " + localWeather.data.current_Condition[0].humidity; DisplayResultsTextBox.Text += "\r\n Temp C: " + localWeather.data.current_Condition[0].temp_C; DisplayResultsTextBox.Text += "\r\n Visibility: " + localWeather.data.current_Condition[0].weatherDesc[0].value; DisplayResultsTextBox.Text += "\r\n Observation Time: " + localWeather.data.current_Condition[0].observation_time; DisplayResultsTextBox.Text += "\r\n Pressure: " + localWeather.data.current_Condition[0].pressure; DisplayResultsTextBox.Text += "\r\n Wind Speed: " + localWeather.data.current_Condition[0].windspeedKmph; DisplayResultsTextBox.Text += "\r\n Wind Direction: " + localWeather.data.current_Condition[0].winddirDegree; DisplayResultsTextBox.Text += "\r\n Precipitation: " + localWeather.data.current_Condition[0].precipMM; DisplayResultsTextBox.Text += "\r\n Chance of Overcast: " + localWeather.data.current_Condition[0].chanceofovercast; DisplayResultsTextBox.Text += "\r\n Chance of Rain: " + localWeather.data.current_Condition[0].chanceofrain; DisplayResultsTextBox.Text += "\r\n Chance of Snow: " + localWeather.data.current_Condition[0].chanceofsnow; DisplayResultsTextBox.Text += "\r\n Chance of Sunny: " + localWeather.data.current_Condition[0].chanceofsunny; DisplayResultsTextBox.Text += "\r\n Chance of Thunder: " + localWeather.data.current_Condition[0].chanceofthunder; } catch (Exception ex) { ex.GetBaseException(); } }
/// <summary> /// Adds information to the right of Linked list. ie if we want to fetch data far into the future /// </summary> /// <param name="future"></param> public void AddRight(LocalWeather future) { lock (accesssynch) { if (future.data.weather == null) { HandleError(); return; } if (m_end == null) { SetWeather(future, null); return; } List <premium.localweather.Weather> localweathers = future.data.weather; foreach (premium.localweather.Weather weather in localweathers) { WeatherData data = fillDataFuture(weather); data.m_location = future.data.request[0].query; Node node = new Node(); node.data = data; node.datetime = data.m_date; m_end.next = node; node.prev = m_end; m_end = node; } moveright(); } }
private WeatherData GetInfo() { PastWeather pastWeather = null; WeatherData data = new WeatherData(); // set input parameters for the API LocalWeatherInput input = new LocalWeatherInput(); input.query = m_info_future.query; input.num_of_days = m_info_future.num_of_days; input.date = m_info_future.date; input.format = "JSON"; // call the local weather method with input parameters OpenWeatherAPI api = new OpenWeatherAPI(); LocalWeather localWeather = api.GetLocalWeather(input); //set data in datastore based on which kind of datastore refresh was requested if (m_reftype == RefreshType.AddLeft) { DataStore.Instance().AddLeft(pastWeather); } else if (m_reftype == RefreshType.AddLeft) { DataStore.Instance().AddRight(localWeather); } else { DataStore.Instance().SetWeather(localWeather, pastWeather); } return(data); }
/// <summary> /// This method consumes the API to display local weather /// </summary> public void Get_LocalWeather() { try { /* Set input parameters for the API */ LocalWeatherInput input = new LocalWeatherInput(); input.query = Constants.query; input.num_of_days = Constants.Days; input.format = Constants.Format; /*Call the LocalWeather method and pass in the input parameters*/ API_Implementation api = new API_Implementation(); LocalWeather localWeather = api.GetLocalWeather(input); /* Display Output */ DisplayResultsTextBox.Text = "\r\n Cloud Cover: " + localWeather.data.current_Condition[0].cloudcover; DisplayResultsTextBox.Text += "\r\n Humidity: " + localWeather.data.current_Condition[0].humidity; DisplayResultsTextBox.Text += "\r\n Temp C: " + localWeather.data.current_Condition[0].temp_C; DisplayResultsTextBox.Text += "\r\n Visibility: " + localWeather.data.current_Condition[0].weatherDesc[0].value; DisplayResultsTextBox.Text += "\r\n Observation Time: " + localWeather.data.current_Condition[0].observation_time; DisplayResultsTextBox.Text += "\r\n Pressue: " + localWeather.data.current_Condition[0].pressure; } catch (Exception ex) { ex.GetBaseException(); } }
/// <summary> /// This Constructor initializes the global variables /// </summary> public Constants() { query = SearchTextBox.Text.ToString(); Format = "JSON"; Days = NoOfDaysDropDownList.SelectedValue.ToString(); Results = NoOfDaysDropDownList.SelectedValue.ToString(); input = new LocalWeatherInput(); api = new API_Implementation(); localWeather = api.GetLocalWeather(input); }
/// <summary> /// This method gets local weather condition /// </summary> /// <param name="input"></param> /// <returns>local weather</returns> public LocalWeather GetLocalWeather(LocalWeatherInput input) { /*Create URL based on input paramters*/ string apiURL = ApiBaseURL + "weather.ashx?q=" + input.query + "&format=" + input.format + "&extra=" + input.extra + "&num_of_days=" + input.num_of_days + "&date=" + input.date + "&fx=" + input.fx + "&cc=" + input.cc + "&includelocation=" + input.includelocation + "&show_comments=" + input.show_comments + "&callback=" + input.callback + "&key=" + APIKey; /*Get the web response*/ string result = RequestHandler.Process(apiURL); /*Serialize the json output and parse in the helper class*/ LocalWeather lWeather = (LocalWeather) new JavaScriptSerializer().Deserialize(result, typeof(LocalWeather)); return(lWeather); }
public LocalWeather Adapt(RootObject obj) { LocalWeather lobj = null; if (obj == null) { return(null); } try { lobj = new LocalWeather(); lobj.data = new Data(); lobj.data.current_Condition = new List <Current_Condition>(); lobj.data.current_Condition.Add(new Current_Condition()); lobj.data.weather = new List <premium.localweather.Weather>(); lobj.data.weather.Add(new premium.localweather.Weather()); lobj.data.weather[0].hourly = new List <Hourly>(); lobj.data.weather[0].hourly.Add(new premium.localweather.Hourly()); lobj.data.weather[0].hourly[0].weatherDesc = new List <WeatherDesc>(); lobj.data.weather[0].hourly[0].weatherDesc.Add(new premium.localweather.WeatherDesc()); lobj.data.request = new List <Request>(); lobj.data.request.Add(new Request()); lobj.data.request[0].query = obj.name; lobj.data.weather[0].hourly[0].tempC = (int)obj.main.temp; lobj.data.weather[0].maxtempC = (int)obj.main.temp_max; lobj.data.weather[0].mintempC = (int)obj.main.temp_min; lobj.data.weather[0].hourly[0].weatherDesc[0].value = obj.weather[0].main; } catch (NullReferenceException e) { lobj = null; } catch (Exception e) { lobj = null; } return(lobj); }
public LocalWeather GetLocalWeather(LocalWeatherInput input) { // create URL based on input paramters string apiURL = ApiBaseURL + "weather.ashx?q=" + input.query + "&format=" + input.format + "&extra=" + input.extra + "&num_of_days=" + input.num_of_days + "&date=" + input.date + "&fx=" + input.fx + "&cc=" + input.cc + "&includelocation=" + input.includelocation + "&show_comments=" + input.show_comments + "&callback=" + input.callback + "&key=" + PremiumAPIKey; // get the web response string result = new RequestHandler(apiURL).Process(); // deserialize the json output into the c#classes created earlier LocalWeather lWeather = null; if (result != null) { result = result.Replace("No moonset", "11:47 AM"); } try { lWeather = (LocalWeather) new JavaScriptSerializer().Deserialize(result, typeof(LocalWeather)); } catch (FormatException ex) { } //LocalWeather lWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<LocalWeather>(result); return(lWeather); }
public static void BeginFetchLocalWeathers(List<Place> gradovi) { if (places == null) places = new List<Place>(); if (localWeathers == null) localWeathers = new List<LocalWeather>(); if (gradovi == null ? false : gradovi.Any()) { var mjesta = gradovi.Distinct().ToList(); var parameters = (from p in mjesta select p.Parameters).Distinct().ToList(); //Napravimo po jedan LocalWeather objekt za svaki distinct parametar parameters.ForEach(param => { if (!localWeathers.Where(i => i.Parameters == param).Any()) { //Ovaj try-catch je dodan za svaki slučaj da se ne skrši pozivanje konstruktora (ako su parametri takvi da skrše jer se ne mogu splitati) try { LocalWeather lw = new LocalWeather(param); localWeathers.Add(lw); } catch (Exception) { //throw new ArgumentException(String.Format("Pozvan konstruktor sa neispravnim parametrima: ", param)); //neznam kaj se radi s exceptionima, neki log ili mail di se to može slati? throw; } } }); //Napravimo po jedan Place objekt za svaki grad/mjesto i pridružimo mu njegovu prognozu mjesta.ForEach(mjesto => { if (!places.Where(i => i.Id == mjesto.Id).Any()) { mjesto.LocalWeather = localWeathers.SingleOrDefault(i => i.Parameters == mjesto.Parameters); if (mjesto.LocalWeather != null) { places.Add(mjesto); } } else { if (mjesto.LocalWeather == null) { mjesto.LocalWeather = localWeathers.SingleOrDefault(i => i.Parameters == mjesto.Parameters); } else if (mjesto.LocalWeather.Parameters != mjesto.Parameters) { mjesto.LocalWeather = localWeathers.SingleOrDefault(i => i.Parameters == mjesto.Parameters); } //Ako je i dalje null na neku foru onda maknem to mjesto iz popisa mjesta koja imaju prognoze if (mjesto.LocalWeather == null) { places.RemoveAll(i => i.Id == mjesto.Id); } } }); } BeginRefreshLocalWeathers(localWeathers); }
/// <summary> /// Main interface to set weather information to be used later. /// If input is valid, we clear all current data and set new data /// </summary> /// <param name="future"></param> /// <param name="past"></param> public void SetWeather(LocalWeather future, PastWeather past = null) { lock (accesssynch) { if (future == null || future.data == null || future.data.weather == null) { HandleError(); return; } Clear(); if (past != null)//for historical data { List <premium.pastweather.Weather> pastweathers = past.data.weather; foreach (premium.pastweather.Weather weather in pastweathers) { //create input to usable and storable weatherdata object WeatherData data = fillDataPast(weather); data.m_location = past.data.request[0].query; data.m_rainchance = 0; //Create a doubly Linked list chain to store weather of each date Node node = new Node(); node.data = data; node.datetime = data.m_date; if (m_start == null) { m_start = node; m_left = node; } else { m_end.next = node; node.prev = m_end; } m_right = node; m_end = node; } } if (future.data.weather != null)//for future data { List <premium.localweather.Weather> localweathers = future.data.weather; foreach (premium.localweather.Weather weather in localweathers) { WeatherData data = fillDataFuture(weather); data.m_location = future.data.request[0].query; //Create a doubly Linked list chain to store weather of each date Node node = new Node(); node.data = data; node.datetime = data.m_date; if (m_today == null) { m_today = node; } if (m_start == null) { m_start = node; m_left = node; } else { m_end.next = node; node.prev = m_end; } m_right = node; m_end = node; } } } }