コード例 #1
0
        public static WeatherEntity GetWeatherCondition(string apiKey, string city)
        {
            var weatherEntity = new WeatherEntity();

            using (var weatherXmlReader = new XmlTextReader($"http://api.previmeteo.com/{apiKey}/ig/api?weather={city}"))
            {
                var doc = new XmlDocument();
                doc.Load(weatherXmlReader);

                if (doc.SelectSingleNode("xml_api_reply/weather/problem_cause") != null)
                {
                    weatherEntity = null;
                }
                else
                {
                    weatherEntity.City             = doc.SelectSingleNode("/xml_api_reply/weather/forecast_information/city").Attributes["data"].InnerText;
                    weatherEntity.Condition        = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/condition").Attributes["data"].InnerText;
                    weatherEntity.TempInCentigrade = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_c").Attributes["data"].InnerText;
                    weatherEntity.TempInFahrenheit = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/temp_f").Attributes["data"].InnerText;
                    weatherEntity.Humidity         = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/humidity").Attributes["data"].InnerText;
                    weatherEntity.Wind             = doc.SelectSingleNode("/xml_api_reply/weather/current_conditions/wind_condition").Attributes["data"].InnerText;
                }
            }
            return(weatherEntity);
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            // Register yourself with the Weather Service to get the API KEY
            string weatherApiKey = ConfigurationManager.AppSettings["WeatherAPIKey"];
            string kandyDeviceId = ConfigurationManager.AppSettings["DeviceId"];

            if (weatherApiKey.Equals("<APIKEY>"))
            {
                Console.WriteLine("Please key in the API KEY to the fetch weather information");
                weatherApiKey = Console.ReadLine();
            }

            Console.WriteLine("Enter city name: ");
            var           city    = Console.ReadLine();
            WeatherEntity weather = null;

            try
            {
                weather = GetWeatherCondition(weatherApiKey, city);
                if (weather != null)
                {
                    Console.WriteLine("************************************************** ");
                    Console.WriteLine("TempC: " + weather.TempInCentigrade);
                    Console.WriteLine("TempF: " + weather.TempInFahrenheit);
                    Console.WriteLine("Condition: " + weather.Condition);
                    Console.WriteLine(weather.Humidity);
                    Console.WriteLine(weather.Wind);
                    Console.WriteLine("************************************************** ");
                }
                else
                {
                    Console.WriteLine("Problem in fetching in the weather information.");
                }
            }
            catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }

            // Build Hypercat Catalouge
            var hypercatCatalouge = "";

            if (weather != null)
            {
                hypercatCatalouge = BuildHypercatCatalouge(city, weather);
                Console.WriteLine(hypercatCatalouge);
            }

            // Send IM using KandyCSharp Library
            SendMessageToDevice(hypercatCatalouge, kandyDeviceId);

            Console.WriteLine("Press any key to exit!");
            Console.ReadLine();
        }
コード例 #3
0
        private static string BuildHypercatCatalouge(string cityName, WeatherEntity weatherEntity)
        {
            var items = new ItemCollection
            {
                new Item
                {
                    ItemMetadata = HypercatWeatherDataHelper.GetItemMetaDataCollection(cityName, weatherEntity),
                    Href         = "/" + cityName
                }
            };

            var catelouge = new Catalogue
            {
                CatalogueMetaData = HypercatWeatherDataHelper.GetCatalogueMetaDataCollection(),
                Items             = items
            };

            var hyperCatBuilder = new HyperCatBuilder();

            hyperCatBuilder.AddCatalogueMetaData(catelouge.CatalogueMetaData);
            hyperCatBuilder.AddCatalogueItem(items);
            return(hyperCatBuilder.ToString());
        }
コード例 #4
0
 public static ItemMetaDataCollection GetItemMetaDataCollection(string cityName, WeatherEntity weatherEntity)
 {
     return(new ItemMetaDataCollection
     {
         new ItemMetaData {
             val = cityName,
             rel = "urn:X-hypercat:rels:hasDescription:en"
         },
         new ItemMetaData {
             val = weatherEntity.TempInCentigrade,
             rel = "TemperatureInCentigrade"
         },
         new ItemMetaData {
             val = weatherEntity.TempInFahrenheit,
             rel = "TemperatureInFahrenheit"
         },
         new ItemMetaData {
             val = weatherEntity.Humidity,
             rel = "Humidity"
         },
         new ItemMetaData {
             val = weatherEntity.Wind,
             rel = "Wind"
         },
         new ItemMetaData {
             val = weatherEntity.Condition,
             rel = "Condition"
         },
         new ItemMetaData {
             val = weatherEntity.High,
             rel = "High"
         },
         new ItemMetaData {
             val = weatherEntity.Low,
             rel = "Low"
         }
     });
 }