/// <summary>
        /// Helper method that prepares FCC data
        /// </summary>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <param name="county"></param>
        /// <param name="domain"></param>
        /// <returns></returns>
        private List <BroadbandSpeedGraph> PrepareFCCData(string city, string state, string domain, County county)
        {
            //Get the census code
            string newCity = char.ToUpper(city[0]) + city.Substring(1);
            int    code    = county.GetCensusCode(city, state, HomeController._database);

            //Get list of broadbands in that census code
            BroadbandAPI     ba         = new BroadbandAPI();
            List <Broadband> broadbands = (ba.GetBroadbandSpeed(code, city, state)).OrderByDescending(o => o.provider).ToList();

            //Prepare graph data
            List <BroadbandSpeedGraph> fccData = new List <BroadbandSpeedGraph>();

            foreach (Broadband br in broadbands)
            {
                if (br.speed != 0)
                {
                    fccData.Add(new BroadbandSpeedGraph(br.provider, br.speed));
                }
            }

            //Add user broadband
            Broadband        b             = new Broadband();
            List <Broadband> userBroadband = b.GetUserProviderData(city, state, domain, HomeController._database);

            foreach (Broadband br in userBroadband)
            {
                fccData.Add(new BroadbandSpeedGraph(br.provider, br.speed));
            }

            return(fccData);
        }
Exemplo n.º 2
0
        public Broadband AddBroadband(Guid id)
        {
            var broadband = new Broadband()
            {
                Id    = Guid.NewGuid(),
                Speed = random.Next(1, 20)
            };

            this.Get(id).Broadband = broadband;

            return(broadband);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calls broadband api to get speed information
        /// </summary>
        /// <param name="code"></param>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public List <Broadband> GetBroadbandSpeed(int code, string city, string state)
        {
            HttpClient       httpClient    = new HttpClient();
            List <Broadband> broadbandList = new List <Broadband>();

            string url = String.Format(_url, code);
            HttpResponseMessage msg = null;

            try
            {
                msg = httpClient.GetAsync(url).Result;
            }
            catch (AggregateException)
            {
                Debug.WriteLine("API (Broadband): Cannot get speed results.");
            }


            if (msg == null)
            {
                return(null);
            }

            using (msg)
            {
                if (msg.IsSuccessStatusCode)
                {
                    var data = msg.Content.ReadAsStringAsync().Result;

                    //Deserialize in this format because there are many tuples of broadband
                    var json = JsonConvert.DeserializeObject <List <JsonModels.Broadband> >(data);

                    //Create a dictionary with the following: key (provider) and value (dictionary)
                    //Dictionary value with the following: key (speed) and value (total values) [There is only 1 item in this dict]
                    Dictionary <String, Dictionary <double, int> > providers = new Dictionary <String, Dictionary <double, int> >();

                    foreach (var j in json)
                    {
                        //If there is no repeats, htne only allow 1 provider in the list
                        if (!providers.ContainsKey(j.provider))
                        {
                            //SoQL query sometimes gets same city name in different states, so filter
                            if (j.state.Equals(state))
                            {
                                //Create a dictionary of speed so you can later get the avg
                                Dictionary <double, int> speedDict = new Dictionary <double, int>();
                                speedDict.Add(j.speed, 1);

                                Broadband bb = new Broadband
                                {
                                    blockcode = j.blockcode,
                                    provider  = j.provider,
                                    state     = j.state,
                                    city      = city,
                                    speedDict = speedDict
                                };

                                providers.Add(j.provider, speedDict);
                                broadbandList.Add(bb);
                            }
                        }
                        else
                        {
                            //the dictionary is only of size 1
                            Dictionary <double, int> tempSpeed = providers[j.provider];

                            //update the total evaluated and the total speed
                            double key   = tempSpeed.Keys.First();
                            int    total = tempSpeed[key];
                            tempSpeed.Remove(key);
                            tempSpeed.Add(key + j.speed, total + 1);
                            providers[j.provider] = tempSpeed;
                        }
                    }

                    //After finishing speed map, update the speeds
                    foreach (Broadband b in broadbandList)
                    {
                        double key = b.speedDict.Keys.First();
                        b.speed = key / b.speedDict[key];
                    }
                }
            }
            return(broadbandList);
        }