示例#1
0
        public async Task<bool> GetPollutionsForPlaceAsync(Place place)
        {
            var result = await _HttpService.GetStringAsync($"{_BaseUrl}/pollutions&zip={place.Zip}");
            if (result != null)
            {
                var update = JsonConvert.DeserializeObject<Place>(result);
                place.PollutionToday = update.PollutionToday;
                place.PollutionTomorrow = update.PollutionTomorrow;
                place.PollutionAfterTomorrow = update.PollutionAfterTomorrow;

                return true;
            }

            return false;
        }
        /// <summary>
        /// Checks if the last update for this city is older than 12 hours and updates it if needed.
        /// Provides the pollution values afterwards
        /// </summary>
        /// <param name="zip">Zip code of the city</param>
        /// <returns>List of pollution values</returns>
        public ServiceResult<Place> GetPollutionsForPlace(string zip)
        {
            // Get the latest update for this city
            var latestUpdate =
                (from p in pollutionTable
                 where p.City_Zip == zip
                 orderby p.TimeStamp descending
                 select p.TimeStamp).FirstOrDefault();

            // Check if pollution is available and up to date
            var lastAllowedDate = DateTime.Now.AddHours(-12);
            if (latestUpdate == null || latestUpdate < lastAllowedDate)
            {
                // Upadate this place
                var updateResult = updateService.GetUpdatedPollutions(zip, pollenTable.ToList());
                if (updateResult.Success)
                    // Use the DataTableHelper for Bulk Inserts, because SubmitChanges() updates each row as a single transaction
                    updateResult.Content.BulkCopyToDatabase(DataContext);
            }

            // Get latest pollution for this city
            var pollutions =
                from pollution in pollutionTable
                join pollen in pollenTable on pollution.Pollen_Id equals pollen.Id
                join city in cityTable on pollution.City_Zip equals city.Zip into joined
                where pollution.City_Zip == zip
                from c in joined.DefaultIfEmpty()
                group new
                {
                    pollution,
                    pollen,
                    city = c
                }
                by pollution.Pollen_Id into grp
                select grp.OrderByDescending(g => g.pollution.TimeStamp).First();

            var place = new Place();
            place.Zip = zip;

            foreach (var p in pollutions)
            {
                place.PollutionToday.Add(new Pollution { Pollen = p.pollen.ToPollen(), Intensity = p.pollution.ValueToday, Date = p.pollution.TimeStamp });
                place.PollutionTomorrow.Add(new Pollution { Pollen = p.pollen.ToPollen(), Intensity = p.pollution.ValueTomorrow, Date = p.pollution.TimeStamp });
                place.PollutionAfterTomorrow.Add(new Pollution { Pollen = p.pollen.ToPollen(), Intensity = p.pollution.ValueAfterTomorrow, Date = p.pollution.TimeStamp });
            }

            return new ServiceResult<Place>(place);
        }
示例#3
0
        public async Task RefreshAsync()
        {
            IsLoading = true;

            Places.Clear();

            // Check settings
            await _SettingsService.LoadSettingsAsync();
            if (_SettingsService.CurrentSettings.UseCurrentLocation)
            {
                // Add current location
                var geolocation = await _PlaceService.GetCurrentGeoLocationAsync();
                if (geolocation != null)
                {
                    var currentPlace = new Place();
                    currentPlace.Name = "Current position";
                    currentPlace.Zip = geolocation.Zip;
                    currentPlace.IsCurrentPosition = true;
                    Places.Add(currentPlace);
                }
            }

            // Load locally saved places
            var savedPlaces = await _FileSystemService.ReadObjectFromFileAsync<List<Place>>("places.json");
            if (savedPlaces != null)
            {
                foreach(var place in savedPlaces)
                {
                    Places.Add(place);
                }
            }

            // Update
            foreach (var place in Places)
            {
                var success = await _PollenService.GetPollutionsForPlaceAsync(place);
                place.RecalculateMaxPollution();
            }

            IsLoading = false;
            IsLoaded = true;
        }