private async Task<IList<DayWeather>> GetCityWeatherData(Uri serviceUrl)
        {
            Debug.WriteLine($"Start download weather from: {serviceUrl.AbsoluteUri}");
            string jsonStr = await ServiceManager.DownloadService.Load(serviceUrl);

            IList<DayWeather> weather = new List<DayWeather>();
            JObject json = JObject.Parse(jsonStr);
            Location location = new Location()
            {
                CityID = json["city"]["id"].Value<int?>(),
                Name = json["city"]["name"].Value<string>(),
                Position = new Geoposition()
                {
                    lat = json["city"]["coord"]["lat"].Value<double>(),
                    lon = json["city"]["coord"]["lon"].Value<double>()
                }
            };
            foreach (var day in json["list"])
            {
                weather.Add(new DayWeather()
                {
                    Location = location,
                    Day = FromIntervalSince1970(day["dt"].Value<double>()),
                    Code = day["weather"][0]["id"].Value<int>().ToString(),
                    Name = day["weather"][0]["main"].Value<string>(),
                    Description = day["weather"][0]["description"].Value<string>(),
                    Temp = day["temp"]["day"].Value<double>()
                });
            }

            return weather;
        }
Esempio n. 2
0
 static DBLocation FromLocation(Location location)
 {
     return new DBLocation()
     {
         CityID = location.CityID.Value,
         Name = location.Name,
         Lat = location.Position.lat,
         Lon = location.Position.lon,
         Country = location.Country
     };
 }
 public Task<IList<DayWeather>> GetUpcomingWeatherForLocation(Location location)
 {
     if (location.CityID.HasValue)
     {
         return GetUpcomingWeatherForCityId(location.CityID.Value);
     }
     else
     {
         return GetUpcomingWeatherForPosition(location.Position);
     }
 }
        public async Task TestGetUpcomingWeatherForPosition()
        {
            var location = new Location()
            {
                //lat 47.453991 - long 15.27019: Kapfenberg
                Position = new Geoposition()
                {
                    lat = 47.453991,
                    lon = 15.27019
                }
            };
            var weather = await s.GetUpcomingWeatherForLocation(location);
            Assert.IsTrue(weather.Count > 0);

            DateTime iter = DateTime.Today;
            foreach (var w in weather)
            {
                Assert.AreEqual("Kapfenberg", w.Location.Name);
                Assert.AreEqual(iter, w.Day);
                iter = iter.AddDays(1);
            }
        }
Esempio n. 5
0
        private async void StartLoadWeather()
        {

            Location loc = ServiceManager.SettingsService.GetCurrentCityLocation();
            if(loc == null)
            {
                Geoposition pos = Geoposition.EMPTY;
                try
                {
                    pos = await ServiceManager.GeoService.GetCurrentPosition();
                    loc = new Location()
                    {
                        Position = pos
                    };
                }
                catch (GeoServiceException ex)
                {
                    await (new MessageDialog(ex.Message)).ShowAsync();

                    //Test Position - Kapfenberg
                    loc = new Location()
                    {
                        Position = new Geoposition()
                        {
                            lat = 47.453991,
                            lon = 15.27019
                        }
                    };
                }
            }
            if (loc != null)
            {
                DaysWeather = new ObservableCollection<DayWeather>(
                    await ServiceManager.WeahterService.GetUpcomingWeatherForLocation(loc)
                );
                CurrentDayWeather = DaysWeather.FirstOrDefault();
            }
        }
        public async Task TestGetUpcomingWeatherForCity()
        {
            var location = new Location()
            {
                CityID = 2774773, //2774773: Kapfenberg
                Position = new Geoposition()
                {
                    lat = 11,
                    lon = 11
                }
            };
            var weather = await s.GetUpcomingWeatherForLocation(location);
            Assert.IsTrue(weather.Count > 0);

            DateTime iter = DateTime.Today;
            foreach (var w in weather)
            {
                Assert.AreEqual("Kapfenberg", w.Location.Name);
                Assert.AreEqual(iter, w.Day);

                iter = iter.AddDays(1);
            }
        }
        public void SetCurrentCityLocation(Location location)
        {
            var localSettings = ApplicationData.Current.LocalSettings;
            if (location != null) {

                if (!location.CityID.HasValue || location.Name == null)
                {
                    Debug.WriteLine("CurrentCityLocation not saved: Name or CityId not defined");
                    //Zum Speichern des Ortes muss dieser immer vollständig sein
                    //-> Ansonsten wird er das nächste mal einfach per GEO-Position ermittelt
                    return;
                }
                localSettings.Values["currentCityId"] = location.CityID.Value;
                localSettings.Values["currentCityName"] = location.Name;
                localSettings.Values["currentCityLat"] = location.Position.lat;
                localSettings.Values["currentCityLon"] = location.Position.lon;
            }
            else
            {
                localSettings.Values.Remove("currentCityId");
                localSettings.Values.Remove("currentCityName");
                localSettings.Values.Remove("currentCityLat");
                localSettings.Values.Remove("currentCityLon");
            }
        }
        public async Task<IList<DayWeather>> GetWeatherAroundPosition(Geoposition position, int maxCnt)
        {
            Uri serviceUrl = new Uri(BASE_URL, $"find?lat={position.lat}&lon={position.lon}&cnt={maxCnt}&units=metric&lang={GetLangForApi()}&appid={API_KEY}");
            Debug.WriteLine($"Start download weather from: {serviceUrl.AbsoluteUri}");
            string jsonStr = await ServiceManager.DownloadService.Load(serviceUrl);

            JObject json = JObject.Parse(jsonStr);
            HashSet<string> incluedCityNames = new HashSet<string>();
            IList<DayWeather> weather = new List<DayWeather>();

            foreach (var city in json["list"])
            {
                Location location = new Location()
                {
                    CityID = city["id"].Value<int?>(),
                    Name = city["name"].Value<string>(),
                    Position = new Geoposition()
                    {
                        lat = city["coord"]["lat"].Value<double>(),
                        lon = city["coord"]["lon"].Value<double>()
                    }
                };
                if (!incluedCityNames.Contains(location.Name))
                {
                    weather.Add(new DayWeather()
                    {
                        Location = location,
                        Day = FromIntervalSince1970(city["dt"].Value<double>()),
                        Code = city["weather"][0]["id"].Value<int>().ToString(),
                        Name = city["weather"][0]["main"].Value<string>(),
                        Description = city["weather"][0]["description"].Value<string>(),
                        Temp = city["main"]["temp"].Value<double>()
                    });
                    incluedCityNames.Add(location.Name);
                }
            }

            return weather;
        }
 public void TestSetCurrentCityVal()
 {
     var location = new Location() {
         Name = "myCity",
         CityID = 1099,
         Position = new Geoposition() {
             lat = 10.11,
             lon = 11.12
         }
     };
     s.SetCurrentCityLocation(location);
     var saved = s.GetCurrentCityLocation();
     Assert.AreEqual("myCity", saved.Name);
     Assert.AreEqual(1099, saved.CityID);
     Assert.AreEqual(10.11, saved.Position.lat, 0.0001);
     Assert.AreEqual(11.12, saved.Position.lon, 0.0001);
 }
 public void TestSetCurrentCityValEmpty()
 {
     var location = new Location()
     {
         Name = "myCity",
         CityID = 1009,
         Position = new Geoposition()
         {
             lat = 10.11,
             lon = 11.12
         }
     };
     s.SetCurrentCityLocation(location);
     s.SetCurrentCityLocation(null);
     Assert.IsNull(s.GetCurrentCityLocation());
 }