Beispiel #1
0
        static void WeatherForecast(IOptions <SecretsModel> options)
        {
            var openWeatherMapApiKey = options.Value.OpenWeatherMapApiKey;

            if (String.IsNullOrEmpty(openWeatherMapApiKey))
            {
                Console.WriteLine("Whoops! API key is not defined.");
                return;
            }

            Console.Write("Enter your town name:");
            string town = Console.ReadLine();

            string resp = "";

            using (var wc = new WebClient())
            {
                resp = wc.DownloadString($"http://api.openweathermap.org/data/2.5/weather?q={town}&appid={openWeatherMapApiKey}");
            }
            WeatherRoot wr = JsonSerializer.Deserialize <WeatherRoot>(resp);

            Console.WriteLine();
            Console.WriteLine("Temperature: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C. Feels like: " + Weather.KtoF(wr.Main.Temp) + "°F or " + Weather.KtoC(wr.Main.Temp) + "°C");
            Console.WriteLine("Wind speed: " + wr.Wind.Speed + " m/s. Air pressure is " + wr.Main.Pressure + "mmHg or " + Math.Round(wr.Main.Pressure * 133.322, 1) + " Pascals.");
            long currentTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            bool SunWhat     = currentTime > wr.Sys.Sunrise;
            long whatNext    = SunWhat ? wr.Sys.Sunset : wr.Sys.Sunrise;
            long diff        = whatNext - currentTime;
            var  dto         = DateTimeOffset.FromUnixTimeSeconds(diff);

            if (SunWhat) //If sun should be setting...
            {
                Console.WriteLine("It's day right now. The sun will set in " + dto.ToString("HH:mm:ss"));
            }
            else
            {
                Console.WriteLine("It's night right now. The sun will rise in " + dto.ToString("HH:mm:ss"));
            }
        }