public async Task <ForecastResponseDaily> GetForecastsDaily(int locationId = 0)
        {
            string url = "http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/";

            if (locationId > 0)
            {
                url += locationId.ToString() + "?res=daily" + "&key=" + _apiKey;
            }
            else
            {
                url += "all?res=daily" + "&key=" + _apiKey;
            }

            var request        = (HttpWebRequest)WebRequest.Create(url);
            var response       = (HttpWebResponse)request.GetResponse();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            ForecastResponseDaily forecastResponse = JsonConvert.DeserializeObject <ForecastResponseDaily>(responseString);

            return(forecastResponse);
        }
Exemplo n.º 2
0
        public async Task <ForecastResponseDaily> GetForecastsDaily(int locationId = 0)
        {
            string url = "http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/";

            if (locationId > 0)
            {
                url += locationId.ToString() + "?res=daily" + "&key=" + _apiKey;
            }
            else
            {
                url += "all?res=daily" + "&key=" + _apiKey;
            }

            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(new Uri(url));

                response.EnsureSuccessStatusCode();
                ForecastResponseDaily forecastResponse = JsonConvert.DeserializeObject <ForecastResponseDaily>(await response.Content.ReadAsStringAsync());

                return(forecastResponse);
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            // Create client
            MetOfficeDataPointClient client = new MetOfficeDataPointClient("<API_KEY>");

            // Get all sites
            SiteListResponse siteListResponse = client.GetAllSites().Result;

            // Get available forcasts
            AvailableTimeStampsResponse availableTimeStampsResponse = client.GetAvailableTimestamps().Result;

            // Get all 3 hourly forecasts
            ForecastResponse3Hourly forecastResponse3Hourly = client.GetForecasts3Hourly().Result;

            // Get daily forecasts for site 14
            ForecastResponseDaily forecastResponseDaily = client.GetForecastsDaily(14).Result;

            // Get historical observations
            ForecastResponse3Hourly historicalResponse = client.GetHistoricalObservations().Result;

            // Get closest site
            GeoCoordinate coordinate = new GeoCoordinate(51.508363, -0.163006);
            Location      location   = client.GetClosestSite(coordinate).Result;
        }
Exemplo n.º 4
0
        /// <summary>
        /// The main function that actually gets the weather JSON from DataPoint, and passes it to the parser.
        /// </summary>
        /// <param name="choice">
        /// Identifies whether the user wants a daily forecast or a 3-hourly forecast
        /// </param>
        private void GetWeather(int choice)
        {
            //Run Dictionary setup function
            GetDict();
            //Make a new DataPoint Client, with the API key in the Settings file.
            MetOfficeDataPointClient client = new MetOfficeDataPointClient(Settings1.Default.mo_api_key);

            //Defines floats for try statement
            #region floats
            float  lat;
            float  lon;
            string postcode;
            #endregion
            //Attempt to get longitude and latitude values, along with the postcode. If successful, display in box.
            #region LongLatPost
            try
            {
                lat      = LongLat().lat;
                lon      = LongLat().lon;
                postcode = LongLat().postcode;
            }
            catch (Exception) //This still runs, even after a weberror in the GET function, so catch it here aswell and exit.
            {
                outputBox.Text = "Malformed or Incorrect Postcode!";
                return;                                               //Should exit this function and return to ready state
            }
            outputBox.Text = "Postcode: " + postcode;                 //Shows Postcode in text box
            outputBox.AppendText("\r\nLatitude: " + lat.ToString());  //Shows Latitude in text box
            outputBox.AppendText("\r\nLongitude: " + lon.ToString()); //Shows Longitude in text box
            #endregion
            //Get nearest weather site from coordinates from previous step.
            #region Find closest site
            GeoCoordinate coords   = new GeoCoordinate(lat, lon);
            Location      location = null;
            try
            {
                //Find closest weather site to postcode
                var locationone = client.GetClosestSite(coords);
                location = locationone.Result;
            }
            catch (AggregateException)
            {
                outputBox.Text = "Your API Key is incorrect! Please change it, or reset it to default in the Settings dialog.";
                return;
            }



            #endregion
            //Obtain Data from the Met Office, and pass to the appropriate parser.
            #region Data Aquisition, and Passing to Parser
            //Defines forecastResponse variable as dynamic, so it can be used regardless of which type the user requests
            dynamic forecastResponse = null;
            //Is choice Three Hourly?
            if (choice.Equals(1))
            {
                try
                {
                    var weatheresponse = client.GetForecasts3Hourly(null, location.ID);
                    forecastResponse = new ForecastResponse3Hourly();
                    forecastResponse = weatheresponse.Result;
                    HourlyParse(forecastResponse); //Run parsing for 3-hourly data
                }
                catch (WebException)               //Catch incorrect API Key
                {
                    outputBox.Text = "Your API Key is incorrect! Please change it, or reset it to default in the Settings dialog.";
                }
            }
            //Is choice Daily?
            else if (choice.Equals(2))
            {
                try
                {
                    var weatheresponse = client.GetForecastsDaily(location.ID);
                    forecastResponse = new ForecastResponseDaily();
                    forecastResponse = weatheresponse.Result;
                    DailyParse(forecastResponse); //Run parsing for Daily data
                }
                catch (WebException)              //Catch incorrect API Key
                {
                    outputBox.Text = "Your API Key is incorrect! Please change it, or reset it to default in the Settings dialog.";
                }
            }
            #endregion
        }