Esempio n. 1
0
        public void TestCityNotFoundException()
        {
            CurrentWeatherService currentWeatherService = new CurrentWeatherService();
            string location = "ojfwbwovbwqobi";
            CurrentWeatherModel currentWeatherModel = new CurrentWeatherModel();

            Assert.Throws <ArgumentException>(() => (currentWeatherService.GetCurrentWeather(location)));
        }
Esempio n. 2
0
        public CurrentWeatherModel GetCurrentWeatherModel()
        {
            var cWeather = this.weather.Details.OrderBy(o => o.TimeStamp).First();

            var cWeatherModel = CurrentWeatherModel.FromObject(cWeather);

            return(cWeatherModel);
        }
Esempio n. 3
0
        public void GetCurrentWeatherTest()
        {
            CurrentWeatherService currentWeatherService = new CurrentWeatherService();
            string location = "Stuttgart";
            CurrentWeatherModel currentWeatherModel = new CurrentWeatherModel();

            currentWeatherModel = currentWeatherService.GetCurrentWeather(location);
            Assert.NotNull(currentWeatherModel);
        }
        public void WeatherResponseConverter_CorrectInputParameter()
        {
            //Arrange
            var logger = new Mock <ILogger <WeatherResponseConverter> >();
            var weatherForecastProviderSettingsAccessor = new Mock <IOptions <WeatherForecastProviderSettings> >();
            WeatherForecastProviderSettings settings    = new WeatherForecastProviderSettings
            {
                IconStorageAddress = "http://openweathermap.org/img/wn/[email protected]",
                UnitsFormat        = UnitsFormats.Metric
            };

            weatherForecastProviderSettingsAccessor.SetupGet(m => m.Value).Returns(settings);

            WeatherResponseConverter converter = new WeatherResponseConverter(logger.Object, weatherForecastProviderSettingsAccessor.Object);
            OpenWeather openWeather            = new OpenWeather
            {
                Weather = new List <WeatherDetails> {
                    new WeatherDetails {
                        Description = "rain", Icon = "10n"
                    }
                },
                Wind = new Wind {
                    Speed = 1.66999f
                },
                Main = new Main {
                    Humidity = 1, Pressure = 2, Temp = 3.3f
                },
                Timezone = 3600,      //utc+1
                Dt       = 1571708528 //utc Monday, 22 October 2019, 1:42:08
            };

            CurrentWeatherModel expectedModel = new CurrentWeatherModel
            {
                Date = "02:42 22 October",
                WeatherConditions = new WeatherConditionsModel
                {
                    Humidity           = 1,
                    IconUrl            = "http://openweathermap.org/img/wn/[email protected]",
                    Pressure           = 2,
                    Temperature        = 3,
                    TemperatureFormat  = "C",
                    WeatherDescription = "rain",
                    WindSpeed          = 1.7d
                }
            };

            //Act
            var resultModel = converter.Convert(openWeather);

            //Assert
            Assert.AreEqual(expectedModel, resultModel);
        }
        public void WeatherResponseConverter_InvalidSettings_WithoutIconStorageAddress()
        {
            //Arrange
            var logger = new Mock <ILogger <WeatherResponseConverter> >();
            var weatherForecastProviderSettingsAccessor = new Mock <IOptions <WeatherForecastProviderSettings> >();
            WeatherForecastProviderSettings settings    = new WeatherForecastProviderSettings
            {
                UnitsFormat = UnitsFormats.Metric
            };

            weatherForecastProviderSettingsAccessor.SetupGet(m => m.Value).Returns(settings);

            WeatherResponseConverter converter = new WeatherResponseConverter(logger.Object, weatherForecastProviderSettingsAccessor.Object);
            OpenWeather openWeather            = new OpenWeather
            {
                Weather = new List <WeatherDetails> {
                    new WeatherDetails {
                        Description = "rain", Icon = "10n"
                    }
                },
                Wind = new Wind {
                    Speed = 1.66999f
                },
                Main = new Main {
                    Humidity = 1, Pressure = 2, Temp = 3.3f
                },
                Timezone = 3600,      //utc+1
                Dt       = 1571708528 //utc Monday, 22 October 2019, 1:42:08
            };

            CurrentWeatherModel expectedModel = new CurrentWeatherModel
            {
                Date = "02:42 22 October",
                WeatherConditions = new WeatherConditionsModel
                {
                    Humidity           = 1,
                    IconUrl            = "http://openweathermap.org/img/wn/[email protected]",
                    Pressure           = 2,
                    Temperature        = 3,
                    TemperatureFormat  = "C",
                    WeatherDescription = "rain",
                    WindSpeed          = 1.7d
                }
            };

            //Act
            //Assert
            Assert.Throws <Exception>(() => converter.Convert(openWeather), "Something went wrond during convertion from OpenWeather to CurrentWeatherModel.");
        }
        private void setLabelValues(CurrentWeatherModel model)
        {
            string countryCity      = model.CityName + ", " + GetCountryFromCode.getCountry(model.System.CountryCode);
            string lastUpdatedLabel = this.Resources.GetString(Resource.String.lastupdated_string);

            dateLabel.Text     = DateUtils.unixTimestampToDate(model.Timestamp);
            mainTempLabel.Text = ConvertTemperatures.AppendDegreeCharacter(model.TempPressure.Temperature);
            maxTempLabel.Text  = maxLabel + " " + ConvertTemperatures.AppendDegreeCharacter(model.TempPressure.TempMax);
            minTempLabel.Text  = minLabel + " " + ConvertTemperatures.AppendDegreeCharacter(model.TempPressure.TempMin);
            areaLabel.Text     = countryCity;
            string updated = DateUtils.Get24HourTimeFromTimeStamp(model.Timestamp);

            lastUpdated.Text = lastUpdatedLabel + " " + updated;
            weatherIcon.SetImageResource(manageWeatherIcon(model.Weather));
        }
Esempio n. 7
0
        public WeatherModel(CurrentWeatherModel model)
        {
            var weather = model.Weather?.FirstOrDefault();

            this.AnnouncedAt = DateTimeOffset.FromUnixTimeSeconds(model.Dt).ToLocalTime();
            this.CurrentTemperatureKelvin = model.Main?.Temp ?? -99;
            this.CurrentTemperature       = this.CurrentTemperatureKelvin.ConvertToCelsius();
            this.CurrentWeatherText       = weather?.Main ?? string.Empty;
            this.CityName         = model.Name ?? string.Empty;
            this.CurrentWeatherId = weather?.Id;

            if (_iconConvertData == null)
            {
                return;
            }

            this.CurrentWeatherIcon = _iconConvertData[$"wi_owm_day_{this.CurrentWeatherId}"];
        }
Esempio n. 8
0
        public async Task <CurrentWeatherModel> GetCurrentWeatherAsync(string location)
        {
            var query = $"weather?q={location}&mode=xml&units=metric&appid={APP_ID}";
            //var response = await client.GetAsync(query);

            //var s = await response.Content.ReadAsStringAsync();
            //var x = XElement.Load(new StringReader(s));
            //var model = new CurrentWeatherModel();
            //var data = x.Element.(w => new CurrentWeatherModel
            //{
            //    Description = w.Element("clouds").Attribute("name").Value,
            //    ID = int.Parse(w.Element("city").Attribute("id").Value),
            //    IconID = w.Element("weather").Attribute("icon").Value,
            //    WindSpeed = double.Parse(w.Element("speed").Attribute("value").Value),
            //    Temperature = double.Parse(w.Element("temperature").Attribute("value").Value),

            //});

            //return data;
            CurrentWeatherModel model = new CurrentWeatherModel();

            return(model);
        }
Esempio n. 9
0
        //diese Methode gibt ein CurrentWeatherModel zurück, für die gefilterte Stadt die per Parameter übergeben wird
        public CurrentWeatherModel GetCurrentWeather(string location)
        {
            //CurrentWeather Api-string
            var url     = $"weather?q={location}&units=metric&appid={APP_ID}";
            var request = WebRequest.Create(client.BaseAddress + url);

            System.IO.Stream responseStream;
            try
            {
                responseStream = request.GetResponse().GetResponseStream();
            }
            catch
            {   //Falls der Parameter eine falsche Stadt bekommt, fogt ExceptionHandling
                throw new System.ArgumentException("City not found!");
            }

            if (responseStream != null)
            {
                using (var streamReader = new StreamReader(responseStream))
                {
                    while (streamReader.Peek() > -1)
                    {
                        //Json wird deserialisiert und in die Properties des Models geschrieben
                        model = new CurrentWeatherModel();
                        model = JsonConvert.DeserializeObject <CurrentWeatherModel>(streamReader.ReadLine());
                        //return der models
                        return(model);
                    }
                }
            }
            else
            {// Exceptions, falls der API-Key ungültig wird
                throw new System.ArgumentException("Invalid API key.");
            }

            throw new System.ArgumentException("Invalid API key.");
        }
        /// <summary>
        /// Diese Funktion schreibt alle Wetterdaten in die Properties für die Oberfläche.
        /// Task läuft asynchron
        /// </summary>
        private async void  ShowWeather()
        {
            //CurrentWeather
            CurrentWeatherModel model = new CurrentWeatherModel();

            //Await und LambdaExpression triggert den Service für das jetzige Wetter
            model = await Task.Run(() => currentWeatherService.GetCurrentWeather(Location));

            MainViewModel.city_name = model.name;

            //Modelle werden ausgelesen
            Temperature   = model.main.temp + " C°";
            Description   = "Description: " + model.weather[0].description;
            TempFeelsLike = "Feels Like: " + model.main.feels_like + "C°";
            TempMin       = "Min Temperature: " + model.main.temp_min + "C°";
            TempMax       = "Max Temperature: " + model.main.temp_max + "C°";
            WindSpeed     = "Wind Speed: " + model.wind.speed + "m/s";
            City          = model.name;
            Sunrise       = "sunrise: " + calcuteConverter.UnixTimeStampConverter(model.sys.sunrise);
            Sunset        = "sunset: " + calcuteConverter.UnixTimeStampConverter(model.sys.sunset);
            WeatherIcon   = iconPick.pickIcon(model.weather[0].icon);

            //WeatherForecast
            ForecastModel modelForecast = new ForecastModel();

            //Await und LambdaExpression triggert den Service für den Forecast
            modelForecast = await Task.Run(() => forecastService.GetForecast(Location));

            //Modelle werden ausgelesen
            DateTime thisDay = DateTime.Now;
            string   date    = thisDay.GetDateTimeFormats('D')[0];

            Day1         = "Heute";
            Icon1        = iconPick.pickIcon(modelForecast.list[4].weather[0].icon);
            Description1 = modelForecast.list[4].weather[0].description;
            Degree1      = Convert.ToString(modelForecast.list[4].main.temp_min) + "C°";
            TempMax1     = Convert.ToString(modelForecast.list[4].main.temp_max) + "C°";

            string date2 = thisDay.AddDays(1).GetDateTimeFormats('D')[0];

            Day2         = date2.Split(',')[0];
            Icon2        = iconPick.pickIcon(modelForecast.list[12].weather[0].icon);
            Description2 = modelForecast.list[12].weather[0].description;
            Degree2      = Convert.ToString(modelForecast.list[12].main.temp_min) + "C°";
            TempMax2     = Convert.ToString(modelForecast.list[12].main.temp_max) + "C°";


            string date3 = thisDay.AddDays(2).GetDateTimeFormats('D')[0];

            Day3         = date3.Split(',')[0];
            Icon3        = iconPick.pickIcon(modelForecast.list[20].weather[0].icon);
            Description3 = modelForecast.list[20].weather[0].description;
            Degree3      = Convert.ToString(modelForecast.list[20].main.temp_min) + "C°";
            TempMax3     = Convert.ToString(modelForecast.list[20].main.temp_max) + "C°";


            string date4 = thisDay.AddDays(3).GetDateTimeFormats('D')[0];

            Day4         = date4.Split(',')[0];
            Icon4        = iconPick.pickIcon(modelForecast.list[28].weather[0].icon);
            Description4 = modelForecast.list[28].weather[0].description;
            Degree4      = Convert.ToString(modelForecast.list[28].main.temp_min) + "C°";
            TempMax4     = Convert.ToString(modelForecast.list[28].main.temp_max) + "C°";

            string date5 = thisDay.AddDays(4).GetDateTimeFormats('D')[0];

            Day5         = date5.Split(',')[0];
            Icon5        = iconPick.pickIcon(modelForecast.list[36].weather[0].icon);
            Description5 = modelForecast.list[36].weather[0].description;
            Degree5      = Convert.ToString(modelForecast.list[36].main.temp_min) + "C°";
            TempMax5     = Convert.ToString(modelForecast.list[36].main.temp_max) + "C°";
        }