Example #1
0
        public void OnGet()
        {
            // this new array will hold only specimens that like water.
            List <Specimen> waterLovingSpecimens = new List <Specimen>();
            // what is the precip?
            long precip = 0;

            string plantJson = GetData("http://plantplaces.com/perl/mobile/viewplantsjsonarray.pl?WetTolerant=on");

            Plant[] allPlants       = Plant.FromJson(plantJson);
            String  key             = System.IO.File.ReadAllText("WeatherAPIKey.txt");
            string  weatherEndpoint = "https://api.weatherbit.io/v2.0/current?&city=Cincinnati&country=USA&key=" + key;
            string  strWeatherJson  = GetData(weatherEndpoint);

            QuickTypeWeather.Weather      weather     = QuickTypeWeather.Weather.FromJson(strWeatherJson);
            List <QuickTypeWeather.Datum> weatherData = weather.Data;

            foreach (QuickTypeWeather.Datum weatherDatum in weatherData)
            {
                precip = weatherDatum.Precip;
            }

            string jsonString = GetData("https://www.plantplaces.com/perl/mobile/viewspecimenlocations.pl?Lat=39.14455075&Lng=-84.5093939666667&Range=0.5&Source=location&Version=2");
            // do some validation before we consume the data.
            string  strSchema  = System.IO.File.ReadAllText("SpecimenSchema.json");
            JSchema schema     = JSchema.Parse(strSchema);
            JObject jsonObject = JObject.Parse(jsonString);

            if (jsonObject.IsValid(schema))
            {
                Welcome    welcome      = Welcome.FromJson(jsonString);
                Specimen[] allSpecimens = welcome.Specimens;

                // populate a dictionary with plant data.
                IDictionary <long, Plant> plants = new Dictionary <long, Plant>();
                foreach (Plant plant in allPlants)
                {
                    plants.Add(plant.Id, plant);
                }

                // iterate over the specimens, to find which ones like water.
                foreach (Specimen specimen in allSpecimens)
                {
                    if (plants.ContainsKey(specimen.PlantId))
                    {
                        waterLovingSpecimens.Add(specimen);
                    }
                }
                if (precip < 1)
                {
                    ViewData["Message"] = "It's dry!  Water these plants.";
                }
            }
            else
            {
                ViewData["Message"] = "Invalid JSON";
            }
            // TODO only show water-loving specimens
            ViewData["allSpecimens"] = waterLovingSpecimens;

            string yearStarted = "2006";
            int    foo         = Convert.ToInt32(yearStarted);
            string name        = "My Plant Diary";

            ViewData["Name"] = name;
        }
Example #2
0
        public void OnGet()
        {
            String myName = "Brandan Jones";
            int    age    = 31;

            ViewData["MyName"] = myName;
            ViewData["age"]    = age;
            long precip = 0;

            // make a collection of ONLY specimens that like water.
            IList <QuickType.Specimen> waterLovingSpecimens = new List <QuickType.Specimen>();

            // get our weather data and key
            string weatherAPIKey = System.IO.File.ReadAllText("WeatherAPIKey.txt");
            string weatherData   = GetData("https://api.weatherbit.io/v2.0/current?&city=Cincinnati&country=USA&key=" + weatherAPIKey);

            // parse to objects
            QuickTypeWeather.Weather weather        = QuickTypeWeather.Weather.FromJson(weatherData);
            QuickTypeWeather.Datum[] allWeatherData = weather.Data;

            foreach (QuickTypeWeather.Datum datum in allWeatherData)
            {
                precip = datum.Precip;
            }

            // get the raw plant metadata
            string plantData = GetData("http://www.plantplaces.com/perl/mobile/viewplantsjsonarray.pl?WetTolerant=on");

            // parse to objects
            QuickTypePlant.Plant[] allPlants = QuickTypePlant.Plant.FromJson(plantData);

            // put our plant definitions in a dictionary.
            IDictionary <long, QuickTypePlant.Plant> plantDictionary = new Dictionary <long, QuickTypePlant.Plant>();

            // populate our dictionary (assign seats in our aircraft)
            foreach (QuickTypePlant.Plant plant in allPlants)
            {
                plantDictionary.Add(plant.Id, plant);
            }

            // get the raw JSON data.
            string jsonData = GetData("https://www.plantplaces.com/perl/mobile/viewspecimenlocations.pl?Lat=39.14455075&Lng=-84.5093939666667&Range=0.5&Source=location&Version=2");

            // get the schema that will validate this JSON.
            string schemaString = System.IO.File.ReadAllText("SpecimenSchema.json");
            // pars the schema into a JSchema object.
            JSchema schema = JSchema.Parse(schemaString);
            // quickly read the JSON data into memory.
            JObject jsonObject = JObject.Parse(jsonData);
            // see if the JSON data is valid, per the schema.
            bool valid = jsonObject.IsValid(schema);

            // Marshall the data into a series of objects.
            QuickType.Welcome welcome = QuickType.Welcome.FromJson(jsonData);
            // get the list (collection) of specimens
            List <QuickType.Specimen> allSpecimens = welcome.Specimens;


            // iterate over the specimens so we can shake hands with them.
            foreach (QuickType.Specimen specimen in allSpecimens)
            {
                // shake hands with one specimen at a time.
                Console.WriteLine(specimen);

                // see if this specimen likes water.
                if (plantDictionary.ContainsKey(specimen.PlantId))
                {
                    // we are only here if the plant dictionary contains the plant that is associated with this specimen.
                    waterLovingSpecimens.Add(specimen);
                }
            }

            if (precip < 1)
            {
                ViewData["WeatherMessage"] = "Not much precip; water these water-loving plants.";
                // make the specimen data available to our web page.
                ViewData["allSpecimens"] = waterLovingSpecimens;
            }
            else
            {
                ViewData["WeatherMessage"] = "Lots of rain, these plants will love it!";
                ViewData["allSpecimens"]   = waterLovingSpecimens;
            }
        }
Example #3
0
        public void OnGet()
        {
            String myName = "Arati Lama";
            int    age    = 31;

            ViewData["MyName"] = myName;
            ViewData["age"]    = age;
            long precip = 0;
            IList <QuickType.Specimen> waterLovingSpecimens = new List <QuickType.Specimen>();

            //download JSON data.
            //a webClient giv es us access to data on internet

            using (WebClient webClient = new WebClient())
            {
                string weatherAPIkey = System.IO.File.ReadAllText("wetherAPI.txt");
                string weatherData   = webClient.DownloadString("https://api.weatherbit.io/v2.0/current?&city=Cincinnati&country=USA&key=" + weatherAPIkey);


                QuickTypeWeather.Weather weather        = QuickTypeWeather.Weather.FromJson(weatherData);
                QuickTypeWeather.Datum[] allWeatherData = weather.Data;

                foreach (QuickTypeWeather.Datum datum in allWeatherData)
                {
                    precip = datum.Precip;
                }

                string plantData = webClient.DownloadString("http://www.plantplaces.com/perl/mobile/viewplantsjsonarray.pl?WetTolerant=on");
                QuickTypePlant.Plant[] allPlants = QuickTypePlant.Plant.FromJson(plantData);

                IDictionary <long, QuickTypePlant.Plant> plantDictionary = new Dictionary <long, QuickTypePlant.Plant>();
                foreach (QuickTypePlant.Plant plant in allPlants)
                {
                    plantDictionary.Add(plant.Id, plant);
                }

                string                    jsonData     = webClient.DownloadString("https://www.plantplaces.com/perl/mobile/viewspecimenlocations.pl?Lat=39.14455075&Lng=-84.5093939666667&Range=0.5&Source=location&Version=2");
                QuickType.Welcome         welcome      = QuickType.Welcome.FromJson(jsonData);
                List <QuickType.Specimen> allSpecimens = welcome.Specimens;



                foreach (QuickType.Specimen specimen in allSpecimens)
                {
                    Console.WriteLine(specimen);
                    if (plantDictionary.ContainsKey(specimen.PlantId))
                    {
                        waterLovingSpecimens.Add(specimen);
                    }
                }



                if (precip < 1)
                {
                    ViewData["WeatherMessage"] = "Not much precip; water these water-loving plants.";
                    // make the specimen data available to our web page.
                    ViewData["allSpecimens"] = waterLovingSpecimens;
                }
                else
                {
                    ViewData["WeatherMessage"] = "Lots of rain, these plants will love it!";
                    ViewData["allSpecimens"]   = waterLovingSpecimens;
                }
            }
        }