Beispiel #1
0
        public static async Task <GetResultModel <StationModel[]> > LoadStations(
            CountryModel country     = null,
            CityModel city           = null,
            PollutantModel pollutant = null
            )
        {
            string url = apiEndpoint;

            if (!(country is null))
            {
                url += $"&country={country.Code}";
            }

            if (!(city is null))
            {
                url += "&city=" + city.Name;
            }

            if (!(pollutant is null))
            {
                url += $"&parameter={pollutant.Id}";
            }

            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    GetResultModel <StationModel[]> data = await response.Content.ReadAsAsync <GetResultModel <StationModel[]> >();

                    return(data);
                }

                throw new Exception(response.ReasonPhrase);
            }
        }
Beispiel #2
0
        public static async Task <List <LatestMeasurementsModel> > LoadLatestMeasurements(
            PollutantModel pollutant,
            CountryModel country = null,
            CityModel city       = null
            )
        {
            if (pollutant is null)
            {
                throw new ArgumentNullException("Air pollutant cannot be null.");
            }

            string url = $"{apiEndpoint}?has_geo=true&parameter={pollutant.Id}";

            if (!(country is null))
            {
                url += $"&country={country.Code}";
            }

            if (!(city is null))
            {
                url += "&city=" + city.Name;
            }

            List <LatestMeasurementsModel> measurements = new List <LatestMeasurementsModel>();

            // first call to api to find out how many measurements are there
            int totalMeasurements = await FetchAndDeserializeMeasurements(measurements, url, 1);

            if (totalMeasurements > 10000)
            {
                int pages = (int)Math.Ceiling((double)totalMeasurements / 10000);
                // fetch all pages
                Task[] tasks = new Task[pages - 1];
                for (int page = 2, i = 0; page <= pages; page++, i++)
                {
                    tasks[i] = FetchAndDeserializeMeasurements(measurements, url, page);
                }

                // wait until all pages are fetched
                await Task.WhenAll(tasks);
            }
            return(measurements);
        }
        //fetch pollutant records from pvt api on the basis of lat long
        public List <PollutantModel> fetchPollutants(double latitude, double longitude)

        {
            List <PollutantModel> objPollutantList = new List <PollutantModel>();

            try
            {
                string URL1           = "https://api.waqi.info/feed/geo:" + latitude + ";" + longitude + "/";
                string urlParameters1 = "?token=9890a36bde6c6f1aa7d923ef95c8b26dca940d49";

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(URL1);


                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));



                //for (int i=1;i<25;i++)
                //{
                //    HttpResponseMessage respons = client.GetAsync(urlParameters1).Result;
                //    if (respons != null || respons.IsSuccessStatusCode)
                //    {

                //        var result = respons.Content.ReadAsStringAsync().Result;
                //    }

                // }


                HttpResponseMessage response = client.GetAsync(urlParameters1).Result;

                if (response != null || response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync().Result;



                    JObject obj = JObject.Parse(result);

                    //below is the call back in if else if pollutant data comes then else call again for data to come
                    if (!obj["data"].HasValues)
                    {
                        if (count < 5)
                        {
                            count            = count + 1;
                            objPollutantList = fetchPollutants(latitude, longitude);
                        }
                        return(objPollutantList);
                    }
                    else
                    {
                        count = 0;

                        JObject objResult = (JObject)obj["data"];
                        JObject obj1      = (JObject)obj["data"]["iaqi"];
                        JArray  obj2      = (JArray)obj["data"]["city"]["geo"];
                        JValue  obj3      = (JValue)obj["data"]["aqi"];
                        string  aqi       = (string)obj3.ToObject(typeof(string));


                        // IList<string> keys = obj1.Properties().Select(p => p.Name).ToList();

                        double[] arr = (double[])obj2.ToObject(typeof(double[]));

                        if (latitude == arr[0] || latitude == arr[1] && longitude == arr[1] || longitude == arr[0])
                        {
                            Dictionary <string, JObject> dictObj = obj1.ToObject <Dictionary <string, JObject> >();



                            //  JArray pollutantList = (JArray)obj["data"];
                            // var xmlfile = dictObj["co"];
                            //objStationList = (List<StationModel>)pollutantList.ToObject(typeof(List<StationModel>));


                            foreach (var value in dictObj)
                            {
                                PollutantModel pm             = new PollutantModel();
                                string         pollutant      = value.Key;
                                JObject        pollutantValue = (JObject)value.Value;
                                JValue         val            = (JValue)pollutantValue["v"];
                                int            pvalue         = (int)val.ToObject(typeof(int));
                                pm.PollutantName  = pollutant;
                                pm.PollutantValue = pvalue.ToString();
                                objPollutantList.Add(pm);
                            }
                            PollutantModel pm1 = new PollutantModel();
                            pm1.PollutantName = "Aqi";

                            if (aqi == "" || aqi == "-")
                            {
                                pm1.PollutantValue = "-1";
                            }
                            else
                            {
                                pm1.PollutantValue = aqi;
                            }

                            objPollutantList.Add(pm1);
                        }
                    }
                }
            }

            catch (Exception ex)
            {
                string message = ex.ToString();
            }

            return(objPollutantList);
        }
Beispiel #4
0
        public static async Task <List <MeasurementsModel> > LoadMeasurements(
            PollutantModel pollutant,
            CountryModel country,
            CityModel city       = null,
            StationModel station = null,
            DateTime?dateFrom    = null,
            DateTime?dateTo      = null
            )
        {
            string url = $"{apiEndpoint}?has_geo=true";

            if (!(country is null))
            {
                url += $"&country={country.Code}";
            }

            if (!(city is null))
            {
                url += "&city=" + city.Name;
            }

            if (!(pollutant is null))
            {
                url += $"&parameter={pollutant.Id}";
            }

            if (!(station is null))
            {
                url += $"&location={station.Location}";
            }

            if (!(dateFrom is null))
            {
                url += $"&date_from={dateFrom.Value.ToString("s", CultureInfo.InvariantCulture)}";
            }

            if (!(dateTo is null))
            {
                url += $"&date_to={dateTo.Value.ToString("s", CultureInfo.InvariantCulture)}";
            }

            List <MeasurementsModel> measurements = new List <MeasurementsModel>();

            // first call to api to find out how many measurements are there

            int totalMeasurements = await FetchAndDeserializeMeasurements(measurements, url, 1);

            if (totalMeasurements > 10000)
            {
                int pages = (int)Math.Ceiling((double)totalMeasurements / 10000);
                // fetch all pages
                Task[] tasks = new Task[pages - 1];
                for (int page = 2, i = 0; page <= pages; page++, i++)
                {
                    tasks[i] = FetchAndDeserializeMeasurements(measurements, url, page);
                }

                // wait until all pages are fetched
                await Task.WhenAll(tasks);
            }
            return(measurements);
        }