public static bool IsLocationChanged(Location loc) { bool val = false; if (HttpContext.Current.Cache["Location"] != null) { Location locContext = (Location)HttpContext.Current.Cache.Get("Location"); val = ((locContext.Latitude != loc.Latitude) || (locContext.Longitude != loc.Longitude)); } return val; }
public bool IsItGoingToRain(Location loc) { CultureInfo culture = (CultureInfo)CultureInfo.InvariantCulture.Clone(); //TODO: Fetch the weather from string weatherurl = string.Format("http://api.wunderground.com/api/886500b765f937a7/conditions/forecast/q/{0},{1}.xml", loc.Latitude.ToString("f5", culture), loc.Longitude.ToString("f5", culture)); //Feature: conditions - Returns the current temperature, weather condition, humidity, wind, //'feels like' temperature, barometric pressure, and visibility. return _getAnswerDataFromURL(weatherurl); }
protected void Page_Load(object sender, EventArgs e) { Response.ContentType = "application/json"; bool isItGoingtoRain = false; Location loc = null; RainHelper rainHelper = new RainHelper(); float lat = string.IsNullOrEmpty(Request["lat"]) ? float.NaN : float.Parse(Request["lat"], CultureInfo.InvariantCulture); float lon = string.IsNullOrEmpty(Request["lat"]) ? float.NaN : float.Parse(Request["lon"], CultureInfo.InvariantCulture); if (float.IsNaN(lat) || float.IsNaN(lon)) { // Oh well -- no location available. Let's do a geoip lookup and see if that works string ip = string.IsNullOrEmpty(Request["ip"]) ? Request.UserHostAddress : Request["ip"]; // This is because AppHarbor doesn't give me the ip address if (ip != null && ip.StartsWith("10.") && !string.IsNullOrEmpty(Request.Headers["X-Forwarded-For"])) ip = Request.Headers["X-Forwarded-For"]; // Localhost? I guess we will just use a location in copenhagen if (ip == "::1" || ip == "127.0.0.1") ip = "87.72.246.106"; IPAddress ipaddr = IPAddress.Parse(ip); //Check if Location exists in cache if (HttpContext.Current.Cache["Location"] == null) { //Get Location from url and add it into cache loc = rainHelper.GetLocationByIp(ipaddr); CacheHelp.SetLocationToContext(loc); } else loc = CacheHelp.GetLocationFromContext(); //Get Location from the cache } else { loc = new Location() { Longitude = lon, Latitude = lat, }; //if Location is changed, then we must set new values in cache if (HttpContext.Current.Cache["Location"] == null) CacheHelp.SetLocationToContext(loc); else { if (CacheHelp.IsLocationChanged(loc)) { CacheHelp.ClearCacheItems(); CacheHelp.SetLocationToContext(loc); } } } // //Check for IsItGoingToRain if (HttpContext.Current.Cache["IsItGoingToRain"] == null) { isItGoingtoRain = rainHelper.IsItGoingToRain(loc); CacheHelp.SetIsItGoingToRainToContext(isItGoingtoRain); //Set the other key in cache } else isItGoingtoRain = CacheHelp.GetIsItGoingToRainFromContext(); //Read key value from the cache Output = isItGoingtoRain ? "Yes" : "No"; }
public static void SetLocationToContext(Location loc) { //Set Location to the cache //lifetime for the lookup is 5 minutes HttpContext.Current.Cache.Insert("Location", loc, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); }