public async void GetWeatherDataForLocationAsync_ShouldBuildCorrectWeatherApiUrl()
        {
            // Arrange
            var          xml         = "<?xml version='1.0' encoding='UTF-8'?><current></current>";
            var          actualUrl   = string.Empty;
            const string expectedUrl = "http://api.openweathermap.org/data/2.5/weather?mode=xml&units=metric&type=accurate&lon=19.89&lat=49.29&APPID=64b8f091998633d5aa257ee3141b0b4c";
            var          webClient   = Substitute.For <IWebClient>();

            webClient.GetStringAsync(Arg.Any <string>())
            .Returns(_ =>
            {
                actualUrl = _.Arg <string>();
                var tcs   = new TaskCompletionSource <string>();
                tcs.SetResult(xml);
                return(tcs.Task);
            });
            var appSettingsProvider = Substitute.For <IAppSettingsProvider>();

            appSettingsProvider.GetAppSettingsAsync().
            Returns(_ =>
            {
                var tcs = new TaskCompletionSource <AppSettings>();
                tcs.SetResult(new AppSettings {
                    WeatherApiUri = "http://api.openweathermap.org/data/2.5/weather", WeatherApiKey = "64b8f091998633d5aa257ee3141b0b4c"
                });
                return(tcs.Task);
            });

            // Act
            var sit         = new WeatherDataService(webClient, appSettingsProvider);
            var weatherData = await sit.GetWeatherDataForLocationAsync(new LocationSnapshot { Longitude = 19.89, Latitude = 49.29 });

            // Assert
            Assert.Equal(expectedUrl, actualUrl);
        }
Beispiel #2
0
 static Program()
 {
     BaseUrl = "http://*:60000";
     ApplicationDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
     WeatherService       = new WeatherDataService();
     timer = new Timer(OnTimerTick, null, 0, 10000);
 }
Beispiel #3
0
        public async Task <ActionResult> SyncWeather()
        {
            var service = new WeatherDataService();
            var url     = ConfigurationManager.AppSettings["api:url"];
            var apiKey  = ConfigurationManager.AppSettings["api:key"];
            var model   = new WeatherSyncModel();

            try
            {
                var result = await service.ImportWeatherData(url, apiKey);

                if (result)
                {
                    model.Success = true;
                    model.Message = "Sync done";
                }
                else
                {
                    model.Success = false;
                    model.Message = "API returned false";
                }
            }
            catch (Exception ex)
            {
                model.Success = false;
                model.Message = ex.Message;
            }

            return(View(model));
        }
Beispiel #4
0
        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            SearchButton.IsEnabled = false;
            Task <WeatherData> task;

            try
            {
                task = WeatherDataService.GetWeatherData(InputTextBox.Text);
                task.Wait();
            }
            catch (AggregateException ex) when(ex.InnerException is TimeoutException)
            {
                MessageBox.Show("Timeout. Svar ikke opnået på 5 sekunder");
                return;
            }
            catch (AggregateException ex) when(ex.InnerException is HttpListenerException)
            {
                MessageBox.Show($"Kunne ikke finde byen {InputTextBox.Text}");
                return;
            }
            finally
            {
                SearchButton.IsEnabled = true;
            }

            WeatherData data = task.Result;

            TemperatureTextBlock.Text = $"{data.Temperature} °C";
            WeatherImage.Source       = new BitmapImage(new Uri(data.Icon));
        }
Beispiel #5
0
        public ActionResult GetWeatherReport(WeatherDataViewModel weatherDataViewModel)
        {
            WeatherDataService weatherDataService = new WeatherDataService(new ExceptionService());
            var weatherDataResult = weatherDataService.GetWeatherDataFromApi(weatherDataViewModel);

            return(View("Index", weatherDataResult));
        }
        public void GetCityTest()
        {
            var service = new WeatherDataService();
            var city    = service.GetCity(1);

            Assert.IsNotNull(city);
        }
        public async void GetCitiesTest()
        {
            var service = new WeatherDataService();
            var list    = await service.GetCities();

            Assert.AreEqual(list.Count(), 51);
        }
Beispiel #8
0
        static async Task GetWeather()
        {
            bool exitApp = false;
            IWeatherDataService weatherDataService = new WeatherDataService();

            while (!exitApp)
            {
                Console.WriteLine("What city would you like to get the weather for?");
                string location = Console.ReadLine();

                var response = await weatherDataService.GetTodaysWeatherDataByCity(location);

                var weather = response
                              .ConsolidatedWeather
                              .OrderByDescending(x => x.Predictability)
                              .First();

                Console.WriteLine($"The minimum temperature for {response.Title} is {Math.Round(weather.MinTemp)}c");
                Console.WriteLine($"The maxiumum temperature for {response.Title} is {Math.Round(weather.MaxTemp)}c");
                Console.WriteLine($"The most likely temperature for {response.Title} is {Math.Round(weather.TheTemp)}c");

                Console.WriteLine("Would you like to get the weather for another city? Y/N");
                string anotherCity = Console.ReadLine().ToLower();
                while (!(anotherCity == "n" || anotherCity == "y"))
                {
                    Console.WriteLine("Please enter y or n again!");
                    anotherCity = Console.ReadLine().ToLower();
                }

                exitApp = anotherCity.Equals("n");
            }
        }
        public async void ImportWeatherDataForOneCityTest()
        {
            const int cityId  = 18;
            var       service = new WeatherDataService();
            var       result  = await service.ImportWeatherData(Url, ApiKey, cityId);

            Assert.IsTrue(result);
        }
        public void WeatherDataApiUnitTest()
        {
            WeatherDataViewModel weatherViewModel = new WeatherDataViewModel();

            weatherViewModel.Location = "Cambridge";

            WeatherDataService weatherDataService = new WeatherDataService(new ExceptionService());
            var weatherDataResult = weatherDataService.GetWeatherDataFromApi(weatherViewModel);

            Assert.IsNotNull(weatherDataResult);
        }
 public ListViewHeaderViewModel()
 {
     Task.Run(async() =>
     {
         IsBusy          = true;
         var weatherData = new WeatherDataService();
         Weathers        = (await weatherData.GetItemsAsync(1, 50)).ToList();
         SelectedWeather = Weathers.FirstOrDefault();
         IsBusy          = false;
     });
 }
Beispiel #12
0
 private void UpdateData(string city)
 {
     WeatherDataService.UpdateData();
     if (Source.Count == 0)
     {
         textBlock.Text = $"找不到 \"{city}\" 的信息";
     }
     else
     {
         textBlock.Text = city;
     }
 }
        public async void GetWeatherDataForLocationAsync_ShouldGetCompleteWeatherData()
        {
            // Arrange
            var xml       = "<?xml version='1.0' encoding='UTF-8'?><current><city id='3095140' name='Koscielisko'><coord lon='19.89' lat='49.29'></coord><country>PL</country><sun rise='2018-01-01T06:35:44' set='2018-01-01T14:53:07'></sun></city><temperature value='3' min='3' max='3' unit='metric'></temperature><humidity value='86' unit='%'></humidity><pressure value='1007' unit='hPa'></pressure><wind><speed value='4.1' name='Gentle Breeze'></speed><gusts></gusts><direction value='250' code='WSW' name='West-southwest'></direction></wind><clouds value='90' name='overcast clouds'></clouds><visibility value='10000'></visibility><precipitation mode='rain' value='10'></precipitation><weather number='804' value='overcast clouds' icon='04n'></weather><lastupdate value='2018-01-01T18:00:00'></lastupdate></current>";
            var webClient = Substitute.For <IWebClient>();

            webClient.GetStringAsync(Arg.Any <string>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <string>();
                tcs.SetResult(xml);
                return(tcs.Task);
            });
            var appSettingsProvider = Substitute.For <IAppSettingsProvider>();

            appSettingsProvider.GetAppSettingsAsync().
            Returns(_ =>
            {
                var tcs = new TaskCompletionSource <AppSettings>();
                tcs.SetResult(new AppSettings());
                return(tcs.Task);
            });

            // Act
            var sit         = new WeatherDataService(webClient, appSettingsProvider);
            var weatherData = await sit.GetWeatherDataForLocationAsync(new LocationSnapshot());

            // Assert
            Assert.Equal("Koscielisko", weatherData.CityName);
            Assert.Equal("Poland", weatherData.CountryName);
            Assert.Equal(3d, weatherData.Temperature);
            Assert.Equal(3d, weatherData.TemperatureMin);
            Assert.Equal(3d, weatherData.TemperatureMax);
            Assert.Equal(DateTime.Parse("2018-01-01T06:35:44").ToLocalTime(), weatherData.SunRise);
            Assert.Equal(DateTime.Parse("2018-01-01T14:53:07").ToLocalTime(), weatherData.SunSet);
            Assert.Equal(86d, weatherData.Humidity);
            Assert.Equal(1007d, weatherData.Pressure);
            Assert.Equal(4.1 * 0.001 * 3600, weatherData.WindSpeed);
            Assert.Equal("Gentle Breeze", weatherData.WindSpeedName);
            Assert.Equal(250d, weatherData.WindDirection);
            Assert.Equal("West-southwest", weatherData.WindDirectionName);
            Assert.Equal(90d, weatherData.Cloudiness);
            Assert.Equal("overcast clouds", weatherData.CloudinessName);
            Assert.Equal(10d, weatherData.Precipitation);
            Assert.Equal("rain", weatherData.PrecipitationName);
            Assert.Equal(DateTime.Parse("2018-01-01T18:00:00").ToLocalTime(), weatherData.LastUpdate);
        }
Beispiel #14
0
 public WeatherBl()
 {
     _weatherDataService = new WeatherDataService();
 }
 public WeatherController(WeatherDataService weatherDataService)
 {
     _weatherDataService = weatherDataService;
 }
 public WeatherDataServiceTests()
 {
     _mockMessageHandler = new Mock <HttpMessageHandler>();
     _service            = new WeatherDataService(new TestClientAccessor(_mockMessageHandler.Object));
 }