Esempio n. 1
0
        /// <summary>
        /// Return current weather on given longitude and latitude
        /// </summary>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <returns>Tuple of format(longitude, latitude, tempC, humidity, windspeed, winddirection)</returns>
        public static WeatherReading getWeatherAt(string longitude, string latitude)
        {
            String longitudeStr = longitude;
            String latitudeStr  = latitude;
            String response     = String.Empty;

            //Dictionary<String, String> parameters = new Dictionary<String, String>();
            //parameters.Add("q", String.Format("{0:0.##}", longitude) + "%2C" + String.Format("{0:0.##}", latitude));
            //parameters.Add("format", "xml");
            //parameters.Add("num_of_days", "1");
            //parameters.Add("fx", "no");
            //parameters.Add("cc", "yes");
            //parameters.Add("includelocation", "yes");
            //parameters.Add("show_comments", "no");
            //parameters.Add("key", key);
            response = httpGetRequest(latitudeStr, longitudeStr);

            System.Xml.XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(response);
            WeatherReading cc = new WeatherReading();

            if (xmldoc.GetElementsByTagName("data").Count > 0 &&
                xmldoc.GetElementsByTagName("current_condition").Count > 0 &&
                xmldoc.GetElementsByTagName("error").Count == 0)
            {
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("temp_C").Count > 0)
                {
                    cc.tempC = xmldoc["data"]["current_condition"]["temp_C"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("humidity").Count > 0)
                {
                    cc.humidity = xmldoc["data"]["current_condition"]["humidity"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("windspeedKmph").Count > 0)
                {
                    cc.windspeed = xmldoc["data"]["current_condition"]["windspeedKmph"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("winddirDegree").Count > 0)
                {
                    cc.winddirection = xmldoc["data"]["current_condition"]["winddirDegree"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("visibility").Count > 0)
                {
                    cc.visibility = xmldoc["data"]["current_condition"]["visibility"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("pressure").Count > 0)
                {
                    cc.pressure = xmldoc["data"]["current_condition"]["pressure"].InnerText;
                }
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("cloudcover").Count > 0)
                {
                    cc.cloudcover = xmldoc["data"]["current_condition"]["cloudcover"].InnerText;
                }
            }
            return(cc);
        }
Esempio n. 2
0
        /// <summary>
        /// Return current weather on given longitude and latitude
        /// </summary>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <returns>Tuple of format(longitude, latitude, tempC, humidity, windspeed, winddirection)</returns>
        public static WeatherReading getWeatherAt(string longitude, string latitude)
        {
            String longitudeStr = longitude;
            String latitudeStr = latitude;
            String response = String.Empty;
            //Dictionary<String, String> parameters = new Dictionary<String, String>();
            //parameters.Add("q", String.Format("{0:0.##}", longitude) + "%2C" + String.Format("{0:0.##}", latitude));
            //parameters.Add("format", "xml");
            //parameters.Add("num_of_days", "1");
            //parameters.Add("fx", "no");
            //parameters.Add("cc", "yes");
            //parameters.Add("includelocation", "yes");
            //parameters.Add("show_comments", "no");
            //parameters.Add("key", key);
            response = httpGetRequest(latitudeStr, longitudeStr);

            System.Xml.XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(response);
            WeatherReading cc = new WeatherReading();

            if (xmldoc.GetElementsByTagName("data").Count > 0 &&
                xmldoc.GetElementsByTagName("current_condition").Count> 0 &&
                xmldoc.GetElementsByTagName("error").Count == 0)
            {
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("temp_C").Count > 0)
                    cc.tempC = xmldoc["data"]["current_condition"]["temp_C"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("humidity").Count > 0)
                    cc.humidity = xmldoc["data"]["current_condition"]["humidity"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("windspeedKmph").Count > 0)
                    cc.windspeed = xmldoc["data"]["current_condition"]["windspeedKmph"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("winddirDegree").Count > 0)
                    cc.winddirection = xmldoc["data"]["current_condition"]["winddirDegree"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("visibility").Count > 0)
                    cc.visibility = xmldoc["data"]["current_condition"]["visibility"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("pressure").Count > 0)
                    cc.pressure = xmldoc["data"]["current_condition"]["pressure"].InnerText;
                if (xmldoc["data"]["current_condition"].GetElementsByTagName("cloudcover").Count > 0)
                    cc.cloudcover = xmldoc["data"]["current_condition"]["cloudcover"].InnerText;
            }
            return cc;
        }
Esempio n. 3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //string response = System.IO.File.ReadAllText(MapPath("TestXml.xml"));
            //this.Page.Request.QueryString;
            //this.Response.Write(response);
            try
            {
                if (this.Request.HttpMethod != "GET")
                {
                    this.Response.Write("Request method must be GET");
                    return;
                }
                System.Text.StringBuilder bld = new System.Text.StringBuilder();
                bld.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?> ");
                bld.AppendLine("<Measurements>");
                if (this.Request.QueryString.AllKeys.Contains("lat") & this.Request.QueryString.AllKeys.Contains("lon"))
                {
                    string      lat = this.Request.QueryString["lat"].ToString().Replace(',', '.');
                    string      lon = this.Request.QueryString["lon"].ToString().Replace(',', '.');
                    CityCountry cc  = ReverseGeocoding.GetCityCountry(lat, lon);
                    bld.AppendLine(String.Format("<Country>{0}</Country>", cc.countryName));
                    bld.AppendLine(String.Format("<City>{0}</City>", cc.adminName1));

                    WeatherReading ww = WorldWeather.getWeatherAt(lat, lon);
                    bld.AppendLine(String.Format("<Humidity>{0}</Humidity>", ww.humidity.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Temperature>{0}</Temperature>", ww.tempC.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Windspeed>{0}</Windspeed>", ww.windspeed.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Winddirection>{0}</Winddirection>", ww.winddirection.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Visibility>{0}</Visibility>", ww.visibility.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Pressure>{0}</Pressure>", ww.pressure.Replace(',', '.')));
                    bld.AppendLine(String.Format("<Cloudcover>{0}</Cloudcover>", ww.cloudcover.Replace(',', '.')));
                }
                //windspeed = xmldoc["data"]["current_condition"]["windspeedKmph"].InnerText;
                //winddirection = xmldoc["data"]["current_condition"]["winddirDegree"].InnerText;
                //visibility = xmldoc["data"]["current_condition"]["visibility"].InnerText;
                //pressure = xmldoc["data"]["current_condition"]["pressure"].InnerText;
                //cloudcover = xmldoc["data"]["current_condition"]["cloudcover"].InnerText;

                //using (SmartCityEntities ctx = new SmartCityEntities())
                //{
                //    List<Sample> samples = ctx.Sample.ToList();
                //    if (this.Request.QueryString.AllKeys.Contains("device"))
                //    {
                //        int device = Convert.ToInt32(this.Request.QueryString["device"]);
                //        samples = samples.Where(dev => dev.device_id == device).ToList();
                //    }
                //    foreach (var item in samples)
                //    {
                //        bld.AppendLine("<Measurement>");
                //        bld.AppendLine(String.Format("<id>{0}</id>", item.id_measurement));
                //        bld.AppendLine(String.Format("<deviceId>{0}</deviceId>", item.device_id));
                //        bld.AppendLine(String.Format("<timestamp>{0}</timestamp>", item.sample_time));
                //        bld.AppendLine(String.Format("<longitude>{0}</longitude>", item.lon.ToString().Replace(',', '.')));
                //        bld.AppendLine(String.Format("<latitude>{0}</latitude>", item.lat.ToString().Replace(',', '.')));
                //        bld.AppendLine(String.Format("<humidity>{0}</humidity>", item.humidity.ToString().Replace(',', '.')));
                //        bld.AppendLine(String.Format("<temperature>{0}</temperature>", item.temperature.ToString().Replace(',', '.')));
                //        bld.AppendLine(String.Format("</Measurement>"));
                //    }
                //}
                bld.AppendLine("</Measurements>");
                this.Response.Write(bld.ToString());
            }
            catch (Exception ex)
            {
                this.Response.Write(ex.ToString());
                return;
            }
        }