Пример #1
0
        public WeatherForecast GetForecast()
        {
            var             client  = new OpenWeatherAPI.OpenWeatherAPI(APIKey);
            var             results = client.Query(City);
            WeatherForecast weather = new WeatherForecast();

            weather.Temperature = results.Main.Temperature.CelsiusCurrent.ToString();
            weather.WindSpeed   = results.Wind.SpeedFeetPerSecond.ToString();
            weather.Pressure    = results.Main.Pressure.ToString();
            weather.City        = this.City;
            weather.DTime       = DateTime.Now;
            return(weather);
        }
        private void btnFindLocation_Click(object sender, EventArgs e)
        {
            int errorNum = 0;

            //parentForm.internetAvailable = MainFunctions.CheckForInternetConnection();
            parentForm.checkInternetConnection(false);

            if (parentForm.internetAvailable == false)
            {
                parentForm.showNoInternetMessage();
                return;
            }

            // Alternative Weather calls HERE:  https://www.obioberoi.com/2018/07/14/how-to-pull-weather-info-into-a-console-app/
            var client = new OpenWeatherAPI.OpenWeatherAPI(Constants.OpenWeatherAPIKey);

            Query results;// = null;

            try
            {
                results = client.Query(txtSearch.Text);
            }
            catch (ArithmeticException)
            {
                results = null;
            }
            catch (System.Net.WebException ex)
            {
                results = null;
                if (ex.Message.Contains("(429)"))
                {
                    errorNum = 429;
                }
            }

            // -34.206841, 138.599503);
            //var results = client.Query("Adelaide");

            if (results == null)
            {
                //btnSetLocation.Enabled = false;
                lblCityFound.Text = "City Not Found. "; //string.Empty;
                foundCityLat      = 0;
                foundCityLong     = 0;
                foundCityName     = string.Empty;

                if (errorNum == 429)
                {
                    MessageBox.Show("There was a problem getting the weather. \nThere have been too many calls to the Weather service. \n\nPlease try again later, or manually enter the GPS coordinates");
                }
                else
                {
                    MessageBox.Show("There was a problem getting the weather. \nThe internet connection may be unavailable \nOr the city name is not known.");
                }
                return;
            }

            if (results.ValidRequest == false)
            {
                //btnSetLocation.Enabled = false;
                lblCityFound.Text = "City Not Found. ";
                foundCityLat      = 0;
                foundCityLong     = 0;
                foundCityName     = string.Empty;
                MessageBox.Show("There was a problem getting the weather. \nThe internet connection may be unavailable \nOr the city name is not known.");
                return;
            }

            // if we get to here then its a valid result
            btnSetLocation.Enabled = true;
            foundCityLat           = results.Coord.Latitude;
            foundCityLong          = results.Coord.Longitude;
            foundCityName          = results.Name;
            txtSearch.Text         = results.Name;
            lblCityFound.Text      = "Found: " + results.Name;

            btnSetLocation.BackColor = Color.PeachPuff;

            FrmMain.showWeatherReport(results, true);

            //String output = string.Empty;

            //if (results.Name != string.Empty)
            //{
            //    output += "City: " + results.Name + "\n";
            //}
            //output += "--------------------\n";
            //output += "Temp (CelsiusCurrent): " + results.Main.Temperature.CelsiusCurrent + "°C\n";
            ////output += "Temp (CelsiusMinimum): " + results.Main.Temperature.CelsiusMinimum + "\n";
            ////output += "Temp (CelsiusMaximum): " + results.Main.Temperature.CelsiusMaximum + "\n";
            //output += "Humidity: " + results.Main.Humdity + "\n";
            //output += "Pressure: " + results.Main.Pressure + "\n";
            //output += "Clouds: " + results.Clouds.All.ToString(CultureInfo.InvariantCulture) + "\n";
            //if (results.Rain != null) output += "Rain: " + results.Rain.H3 + "\n"; else output += "Rain: 0" + "\n";
            //if (results.Snow != null) output += "Snow: " + results.Snow.H3 + "\n"; else output += "Snow: 0" + "\n";
            //output += "Sunrise: " + results.Sys.Sunrise + "\n";
            //output += "Sunset: " + results.Sys.Sunset + "\n";
            //output += "Visibility: " + results.Visibility + "\n";
            //output += "Wind (Degree): " + results.Wind.Degree + "°\n";
            //output += "Wind (Direction): " + results.Wind.Direction + "\n";
            //output += "Wind (Gust): " + results.Wind.Gust + "\n";
            //output += "Speed (MetersPerSecond): " + results.Wind.SpeedMetersPerSecond
            //    + " (" + Math.Round((results.Wind.SpeedMetersPerSecond * 60 * 60) / 1000f, 2) + " Kph)"
            //    + "\n";
            ////output += "Weathers: " + results.Weathers.Count() + "\n";

            //foreach (Weather w in results.Weathers)
            //{
            //    output += "Main: " + w.Main + "\n";
            //    output += "Description: " + w.Description + "\n";
            //    //output += "  Weathers Icon: " + w.Icon.ToString(CultureInfo.InvariantCulture) + "\n";
            //}
            //output += "Latitude: " + results.Coord.Latitude.ToString(CultureInfo.InvariantCulture) + "\n";
            //output += "Longitude: " + results.Coord.Longitude.ToString(CultureInfo.InvariantCulture) + "\n";

            //output += "\n\n";
            //output += "INSTRUCTIONS: If this is the correct city, press the Set button. \nOr, search for another city.";

            //MessageBox.Show(output);
        }
Пример #3
0
        private void btnFindLocation_Click(object sender, EventArgs e)
        {
            parentForm.checkInternetConnection(false);

            if (parentForm.internetAvailable == false)
            {
                parentForm.showNoInternetMessage();
                return;
            }

            // Alternative Weather calls HERE:  https://www.obioberoi.com/2018/07/14/how-to-pull-weather-info-into-a-console-app/
            var client = new OpenWeatherAPI.OpenWeatherAPI(Constants.OpenWeatherAPIKey); //YOUR-API-KEY

            Query results;                                                               // = null;

            try
            {
                results = client.Query(txtSearch.Text);
            }
            catch (ArithmeticException)
            {
                results = null;
            }

            // -34.206841, 138.599503);
            //var results = client.Query("Adelaide");

            if (results == null)
            {
                btnSetLocation.Enabled = false;
                lblCityFound.Text      = "City Not Found. ";
                foundCityLat           = 0;
                foundCityLong          = 0;
                foundCityName          = string.Empty;
                MessageBox.Show("There was a problem getting the weather. \nThe internet connection may be unavailable \nOr the city name is not known.");
                return;
            }

            if (results.ValidRequest == false)
            {
                btnSetLocation.Enabled = false;
                lblCityFound.Text      = "City Not Found. ";
                foundCityLat           = 0;
                foundCityLong          = 0;
                foundCityName          = string.Empty;
                MessageBox.Show("There was a problem getting the weather. \nThe internet connection may be unavailable \nOr the city name is not known.");
                return;
            }

            // if we get to here then its a valid result
            btnSetLocation.Enabled = true;
            foundCityLat           = results.Coord.Latitude;
            foundCityLong          = results.Coord.Longitude;
            foundCityName          = results.Name;
            txtSearch.Text         = results.Name;
            lblCityFound.Text      = "Found: " + results.Name;

            btnSetLocation.BackColor = Color.PeachPuff;

            FrmMain.showWeatherReport(results, true);
        }