Example #1
0
 public void CacheResponse(string stop, StopInfoResponse response)
 {
     Cache[stop] = response;
 }
Example #2
0
        public StopInfoResponse GetStopData(string stop)
        {
            StopInfoResponse response = new StopInfoResponse();

            response.ResponseTime = dateTimeService.Current();

            if (dataCache.GetCachedResponse(stop) != null && dataCache.GetCachedResponse(stop).ResponseTime == response.ResponseTime)
            {
                response.StopInfos = dataCache.GetCachedResponse(stop).StopInfos;
            }
            else
            {
                var stops = GetStops().Where(x => x.Name.ToLower() == stop.ToLower());
                var buses = GetBuses();

                using (var client = new WebClient())
                {
                    foreach (var s in stops) //each stopInfo
                    {
                        var      stopInfoJson = client.DownloadString(new Uri("http://87.98.237.99:88/delays?stopId=" + s.Id));
                        StopInfo info         = dataParseService.ParseStopInfo(stopInfoJson);

                        info.Coordinates = s.Coordinates;
                        response.StopInfos.Add(info);
                    }
                }


                foreach (var s in response.StopInfos)
                {
                    foreach (var r in s.BusInfos) // assign Bus names instead of ID's
                    {
                        r.RouteID = buses.Where(x => x.Id == r.RouteID).Select(y => y.Name).FirstOrDefault();
                    }
                }

                dataCache.CacheResponse(stop, response);
            }

            foreach (var s in response.StopInfos)
            {
                if (s.BusInfos.Count > 0) //ask for a weather for active stops
                {
                    //ask Cache first
                    using (var client = new WebClient())
                    {
                        if (weatherCache.GetCachedWeather(s.Coordinates) != null &&
                            weatherCache.GetCachedWeather(s.Coordinates).Time < dateTimeService.Now.AddMinutes(15))
                        {
                            s.WeatherInfo = weatherCache.GetCachedWeather(s.Coordinates);
                        }
                        else
                        {
                            //get data and cache them
                            var         weatherInfoJson = client.DownloadString(new Uri("http://api.openweathermap.org/data/2.5/weather?lat=" + s.Coordinates.Latitude + "&lon=" + s.Coordinates.Longitude + "&appid=" + weatherApiKey));
                            WeatherInfo weatherInfo     = dataParseService.ParseWeatherInfo(weatherInfoJson);
                            s.WeatherInfo      = weatherInfo;
                            s.WeatherInfo.Time = dateTimeService.Now;
                            weatherCache.CacheWeather(s.Coordinates, weatherInfo);
                        }
                    }
                }
            }

            return(response);
        }