private static List<StationAndAvailability> LoadStationsFromSI(List<AzureService.StationInfo> s, string cityName) { var result = s.Where(si => !string.IsNullOrEmpty(si.id)).Select(si => { var sa = new StationAndAvailability() { Station = si.GetStation(), Availability = si.GetAvailability() }; if (string.IsNullOrEmpty(sa.Station.City)) sa.Station.City = cityName; return sa; }).ToList(); return result; }
public override IObservable<StationAndAvailability> GetAvailability2(StationLocation station, bool forceUpdate = false) { if (!forceUpdate) { var availability = GetAvailabilityFromCache(station); if (availability.Availability != null) return Observable.Return<StationAndAvailability>(availability); } return DownloadUrl.GetAsync<AzureService.StationInfo>(GetStationDetailsUri(station.City, station.Number.ToString()), AzureServiceCredentials.AuthHeaders) .Retry(1) .ObserveOn(ThreadPoolScheduler.Instance) .Select(s => { var sa = new StationAndAvailability(station, s.GetAvailability()); UpdateAvailabilityCacheItem(sa); return sa; }); }
public override IObservable<StationAndAvailability> GetAvailability2(StationLocation station, bool forceUpdate = false) { if (!forceUpdate) { var availability = GetAvailabilityFromCache(station); if (availability.Availability != null) return Observable.Return<StationAndAvailability>(availability); } switch (station.City) { case "antwerpen": var urlData = string.Format("idStation={0}&s_id_idioma=en", station.Number); return DownloadUrl.PostAsync(StationInfoUrl(station.City), urlData, station) .Retry(1) .ObserveOn(ThreadPoolScheduler.Instance) .Select(s => { var availability = LoadAvailabilityFromHTML(s.Object); availability.Open = station.Open; availability.Connected = true; var sa = new StationAndAvailability(station, availability); UpdateAvailabilityCacheItem(sa); return sa; }); case "stockholm": return DownloadUrl.GetAsync(string.Format(StationInfoUrl(station.City), station.Number)) .Retry(1) .ObserveOn(ThreadPoolScheduler.Instance) .Select(s => { var availability = LoadAvailabilityFromHTML2(s); availability.Open = station.Open; availability.Connected = true; var sa = new StationAndAvailability(station, availability); UpdateAvailabilityCacheItem(sa); return sa; }); default: return base.GetAvailability2(station, forceUpdate); } }
protected void UpdateAvailabilityCacheItem(StationAndAvailability item) { CachedAvailability availability = null; lock (AvailabilityCache) { if (AvailabilityCache.TryGetValue(item.Station.City + item.Station.Number.ToString(), out availability)) availability.Update(item.Availability); else { availability = new CachedAvailability() { Availability = item.Availability, LastUpdate = DateTime.Now }; AvailabilityCache[item.Station.City + item.Station.Number.ToString()] = availability; } } }
protected StationAndAvailability GetAvailabilityFromCache(StationLocation station) { StationAndAvailability result = new StationAndAvailability(station, null); CachedAvailability availability = null; lock (AvailabilityCache) { if (AvailabilityCache.TryGetValue(station.City + station.Number.ToString(), out availability) && !availability.IsOutdated(MaxCacheAge)) result.Availability = availability.Availability; } return result; }