static void Main(string[] args)
        {
            var client = new OpenWeatherAPI.API(System.Configuration.ConfigurationManager.AppSettings["APIKey"]);

            Console.WriteLine("OpenWeatherAPI Example Application");
            Console.WriteLine();

            Console.WriteLine("Enter city to get weather data for:");
            var city = Console.ReadLine();

            Console.WriteLine();

            Console.WriteLine($"Fetching weather data for '{city}'");
            var currentWeatherResults = client.CurrentWeather(city, OpenWeatherAPI.UnitsEnum.Imperial);

            Console.WriteLine($"The temperature in {city} is {currentWeatherResults.Main.Temperature.Current}F. There is {currentWeatherResults.Wind.Speed} mph wind in the {currentWeatherResults.Wind.Direction} direction.");

            var oneCallResults = client.OneCall(currentWeatherResults.Coord.Latitude, currentWeatherResults.Coord.Longitude, UnitsEnum.Imperial);

            foreach (Day day in oneCallResults.Daily)
            {
                Console.WriteLine($"{day.DateTime} : {day.Temp.Maximum}/{day.Temp.Minimum} ({day.Weathers[0].Description})");
            }

            Console.ReadLine();
        }
Пример #2
0
        public override void OnStart()
        {
            this.configuration = PackageHost.GetSettingAsConfigurationSection <OpenWeatherSection>("openWeatherSection", true);
            PackageHost.WriteInfo("Package starting - IsRunning: {0} - IsConnected: {1}", PackageHost.IsRunning, PackageHost.IsConnected);

            int nbSeconde = 0;

            Task.Factory.StartNew(() =>
            {
                while (PackageHost.IsRunning)
                {
                    if (nbSeconde == 0)
                    {
                        foreach (StationElement station in this.configuration.Stations)
                        {
                            PackageHost.WriteInfo("Getting forecast for {0} ...", station.Name);
                            try
                            {
                                var api    = new OpenWeatherAPI.API(this.configuration.ApiKey, this.configuration.Language);
                                var result = api.QueryWeather((float)station.Longitude, (float)station.Latitude);
                                PackageHost.PushStateObject <WeatherInfo>(station.Name, result, lifetime: (int)this.configuration.RefreshInterval.TotalSeconds * 2);
                                PackageHost.WriteInfo("Weather for {0} updated.", station.Name);

                                if (result != null && result.LastEx != null)
                                {
                                    throw result.LastEx;
                                }
                            }
                            catch (Exception ex)
                            {
                                PackageHost.WriteError("Unable to get the weather for {0} : {1}", station.Name, ex.ToString());
                            }
                        }
                    }
                    Thread.Sleep(1000);
                    nbSeconde++;

                    if (nbSeconde == (int)this.configuration.RefreshInterval.TotalSeconds)
                    {
                        nbSeconde = 0;
                    }
                }
            });
        }
Пример #3
0
 public WeatherService()
 {
     client = new OpenWeatherAPI.API(API_KEY);
 }