예제 #1
0
        public Task AppendToCacheAsync(Int64 geohash, BingResult bingResult)
        {
            if (_precision <= 0)
            {
                throw new ArgumentException("Precision must be greater than 0");
            }

            _cache.Add(geohash, bingResult);
            return(Task.CompletedTask);
        }
예제 #2
0
        public async Task <MessageHandlerResults> ProcessMessageAsync(Message msg, string pipelineName, int idx)
        {
            //TODO: Replace all console logging of exceptions to generic log solution

            double lat;
            double lon;

            //TODO: Catch more specific error if property doesn't exist or use another method to get the properties
            try
            {
                lat = double.Parse(msg.Properties[_latProperty]);
                lon = double.Parse(msg.Properties[_lonProperty]);
            }
            catch (Exception)
            {
                Console.WriteLine($"Unable to find required properties: '{_latProperty}' and '{_lonProperty}' on message");
                return(MessageHandlerResults.FailStopProcessing);
            }

            var geoHash = GeoHash.EncodeInt(lat, lon, _geoHashCacheProvider.Precision);

            BingResult bingParsingResult;

            //if we have the result in the cache, fetch it
            if (await _geoHashCacheProvider.ContainsGeoHashAsync(geoHash))
            {
                bingParsingResult = await _geoHashCacheProvider.GetAsync(geoHash);

                Debug.WriteLine("Found in cache");
            }
            //else, query bing directly
            else
            {
                Debug.WriteLine("Not found in cache");
                double geoHashCenterLat, geoHashCenterLon;

                //get the center of the geoHash in order to call Bing API with this {lat,lon}
                var decodedGeoHash = GeoHash.DecodeInt(geoHash, _geoHashCacheProvider.Precision);
                geoHashCenterLat = decodedGeoHash.Coordinates.Lat;
                geoHashCenterLon = decodedGeoHash.Coordinates.Lon;

                var    bingUrl = $"http://dev.virtualearth.net/REST/v1/Locations/{geoHashCenterLat},{geoHashCenterLon}?key={_bingMapsKey}";
                string bingResult;

                try
                {
                    var client = new HttpClient();
                    bingResult = await client.GetStringAsync(bingUrl);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception occurred while calling Bing to Lookup coordinates");
                    Console.WriteLine(ex);

                    return(MessageHandlerResults.FailStopProcessing);
                }

                try
                {
                    var json    = JObject.Parse(bingResult);
                    var address = json["resourceSets"][0]["resources"][0]["address"];

                    //create a bing parsing result instance and cache it
                    bingParsingResult = new BingResult()
                    {
                        City = (string)address["locality"], Country = (string)address["countryRegion"], District = (string)address["adminDistrict"]
                    };
                    await _geoHashCacheProvider.AppendToCacheAsync(geoHash, bingParsingResult);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("An exception occurred while trying to parse Location Lookup results from Bing");
                    Console.WriteLine(ex);

                    return(MessageHandlerResults.FailStopProcessing);
                }
            }

            //add values to msg
            msg.Properties.Add("country", bingParsingResult.Country);
            msg.Properties.Add("district", bingParsingResult.District);
            msg.Properties.Add("city", bingParsingResult.City);

            return(MessageHandlerResults.Success);
        }