Example #1
0
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label16.Text      = "Status: defining weather data for a selected city...";
            label16.ForeColor = Color.Gray;

            string city = Convert.ToString(comboBox1.SelectedItem);

            Task.Run(() =>
            {
                OpenWeather.OpenWeather openWeather = weather.GetOpenWeatherByCity(city);

                if (openWeather != null)
                {
                    coordinate = new GeoCoordinate(openWeather.Coord.Lat, openWeather.Coord.Lon);

                    openWeatherOneAPI = weather.GetWeather(coordinate);

                    if (InvokeRequired)
                    {
                        Invoke(new Action(() => SetParameters()));
                    }
                    else
                    {
                        SetParameters();
                    }
                }
                else
                {
                    label16.Text      = "Status: city geodata not defined.";
                    label16.ForeColor = Color.Red;
                }
            });
        }
Example #2
0
        public OpenWeatherOneCallAPI.OpenWeatherOneCallAPI GetWeather(GeoCoordinate coordinate)
        {
            string appid = "e67f327620e3cc056d936f8d1fbf20fd";

            string url;

            if (coordinate != null)
            {
                url = $"https://api.openweathermap.org/data/2.5/onecall?units=metric&lat={coordinate.Latitude}&lon={coordinate.Longitude}&appid={appid}";
            }
            else
            {
                throw new Exception();
            }

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
                {
                    string line;
                    if ((line = streamReader.ReadToEnd()) != null)
                    {
                        OpenWeatherOneCallAPI.OpenWeatherOneCallAPI weather =
                            JsonConvert.DeserializeObject <OpenWeatherOneCallAPI.OpenWeatherOneCallAPI>(line);
                        return(weather);
                    }
                }
            }
            catch
            {
                MessageBox.Show("Failed to connect to the service. Check your internet connection. You may need a VPN.");
            }

            return(null);
        }
Example #3
0
        private void GeoDetect()
        {
            if (coordinate == null)
            {
                label16.Text      = "Status: device geodata definition...";
                label16.ForeColor = Color.Gray;
            }

            int seconds = 10;
            int step    = progressBar1.Maximum / seconds;

            for (int i = 0; i < seconds; i++)
            {
                if (geoDetected == false)
                {
                    if (watcher.Status == GeoPositionStatus.Ready && !watcher.Position.Location.IsUnknown)
                    {
                        coordinate        = watcher.Position.Location;
                        openWeatherOneAPI = weather.GetWeather(coordinate);

                        if (InvokeRequired)
                        {
                            Invoke(new Action(() => SetParameters()));
                        }
                        else
                        {
                            SetParameters();
                        }

                        geoDetected = true;
                        break;
                    }
                }

                if (InvokeRequired)
                {
                    Invoke(new Action(() => ProgressBar_Change(i * step)));
                }
                else
                {
                    ProgressBar_Change(i * step);
                }

                Thread.Sleep(1000);
            }

            if (geoDetected == false)
            {
                MessageBox.Show("Failed to determine your device’s location.\n" +
                                "Please select a city yourself.");

                if (label16.InvokeRequired)
                {
                    label16.Invoke(new Action(() =>
                    {
                        label16.Text      = "Status: device geodata not defined.";
                        label16.ForeColor = Color.Red;
                    }));
                }
                else
                {
                    label16.Text      = "Status: device geodata not defined.";
                    label16.ForeColor = Color.Red;
                }
            }

            if (comboBox1.InvokeRequired)
            {
                comboBox1.Invoke(new Action(() => comboBox1.Enabled = true));
            }
            else
            {
                comboBox1.Enabled = true;
            }

            if (progressBar1.InvokeRequired)
            {
                progressBar1.Invoke(new Action(() => progressBar1.Value = 0));
            }
            else
            {
                progressBar1.Value = 0;
            }
        }