Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.8 kB
2
Indexable
Never
 static async Task CheckWeatherValue(DateTime date, string location, List<string> conditions, SpeechSynthesizer synthesizer)
        {
            //settings
            string locationEncoded = HttpUtility.UrlEncode(location);
            string countryCode = "it";
            string units = "metric";
            string lang = "it";
            int limit = 1;

            //get location data

            string geoLocationUrl = $"https://api.openweathermap.org/geo/1.0/direct?q={locationEncoded},{countryCode}&limit={limit}&appid={openWeatherMapKey}";
            //ex https://api.openweathermap.org/geo/1.0/direct?q=Monticello%20Brianza,it&limit=1&appid=a0cf88c9f460bde3f0aa2e375fe3b0fa

            try
            {
                HttpResponseMessage geoLocationResponse = await client.GetAsync(geoLocationUrl);

                if (geoLocationResponse.IsSuccessStatusCode)
                {
                    Console.WriteLine("coordinates obtained");
                    List<Location> coordinates = await geoLocationResponse.Content.ReadFromJsonAsync<List<Location>>();
                    float lat = coordinates[0].Lat;
                    float lon = coordinates[0].Lon;

                    //get weather predictions
                    string weatherPredictionsUrl = $"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=current,minutely,hourly,alerts&units={units}&lang={lang}&appid={openWeatherMapKey}";
                    //ex https://api.openweathermap.org/data/2.5/onecall?lat=45.7087797&lon=9.3141406&exclude=current,minutely,hourly,alerts&appid=a0cf88c9f460bde3f0aa2e375fe3b0fa

                    var weatherPredictionsResponse = await client.GetAsync(weatherPredictionsUrl);
                    if (weatherPredictionsResponse.IsSuccessStatusCode)
                    {
                        Console.WriteLine("weather data obtained");

                        CurrentWeatherForecasts currentWeatherForecasts = await weatherPredictionsResponse.Content.ReadFromJsonAsync<CurrentWeatherForecasts>();
                        StringBuilder outputString = new StringBuilder($"il {date.ToString("dd/MM/yyyy")} a {location}");

                        foreach (var day in currentWeatherForecasts.daily)
                        {
                            if (UnixTimeStampToDateTime(day.dt).Date.Equals(date))
                            {
                                if (conditions.Contains("meteo") || conditions.Contains("tempo") || conditions.Contains("previsione") || conditions.Contains("previsioni"))
                                    outputString.Append($" , le previsioni meteo danno {day.weather[0].description}");
                                if (conditions.Contains("temperatura"))
                                    outputString.Append($" , temperatura media di {day.feels_like.day} gradi celsius");
                                if (conditions.Contains("umidità"))
                                    outputString.Append($" , livello di umidità del {day.humidity} %");
                                if (conditions.Contains("pressione"))
                                    outputString.Append($" , pressione a {day.pressure} millibar");
                            }
                        }

                        Console.WriteLine(outputString.ToString());
                        await synthesizer.SpeakTextAsync(outputString.ToString());

                    }

                }
            }
            catch (Exception)
            {
                Console.WriteLine("purtroppo non sono riuscito a recuperare i dati meteo");
                await synthesizer.SpeakTextAsync("purtroppo non sono riuscito a recuperare i dati meteo");
            }
        }