public void GetDataFromWeb(double latValue, double longValue)
{
/*
steps:
* 0.pull the lat and long value stored in the database
* 1.pull the data form web
* 2.Parse the xml data
* 3.insert data
* 4.display data
*/
try
{
//lets pull the vales offline values stored in db...
//string connString = @"Data Source=" + databaseFile + ";Version=3;";
lat_val = latValue;//store_station_list[index_for_station_selected].lat;
lng_val = longValue; //store_station_list[index_for_station_selected].lng;
//display lat lng...
//MessageBox.Show("lat= "+lat_val+" lng = "+lng_val);
using (var wc = new WebClient())
{
// var json = await httpClient.GetStringAsync(api_url);
// double station_id = store_station_list[index_for_station_selected].id;
//MessageBox.Show("lat = " + lat_val + "lng = " + lng_val);
string api_url = "http://api.openweathermap.org/data/2.5/weather?mode=xml&lat=" + lat_val + "&lon=" + lng_val + "&appid=615afd606af791f572a1f92b27a68bcd";
//string api_url = "http://api.openweathermap.org/data/2.5/station?id="+station_id+"&APPID=615afd606af791f572a1f92b27a68bcd";
var data = wc.DownloadString(api_url);
// MessageBox.Show("string apic return =" + data);
string xml_string = data.ToString();
//xml parsing...
XmlDocument xml = new XmlDocument();
xml.LoadXml(xml_string);
XmlNodeList elem_city = xml.GetElementsByTagName("city");
foreach (XmlNode x in elem_city)
{
city_name_pulled = x.Attributes["name"].Value;
// MessageBox.Show("city name = " + city_name_pulled);
}
//for temperature
XmlNodeList temp_list = xml.GetElementsByTagName("temperature");
foreach (XmlNode x in temp_list)
{
temp_pulled = x.Attributes["value"].Value;
// MessageBox.Show("temp = " + temp_pulled);
}
//for humidity
XmlNodeList hum_list = xml.GetElementsByTagName("humidity");
foreach (XmlNode x in hum_list)
{
hum_pulled = x.Attributes["value"].Value;
//MessageBox.Show("hum = " + hum_pulled);
}
//for pressure..
XmlNodeList pressure_list = xml.GetElementsByTagName("pressure");
foreach (XmlNode x in pressure_list)
{
pressure_pulled = x.Attributes["value"].Value;
//MessageBox.Show("press = " + pressure_pulled);
}
//for wind
XmlNodeList wind_list = xml.GetElementsByTagName("speed");
foreach (XmlNode x in wind_list)
{
wind_speed_pulled = x.Attributes["value"].Value;
// MessageBox.Show("wind speed = " + wind_speed_pulled);
}
//for direction..
XmlNodeList direction_list = xml.GetElementsByTagName("direction");
foreach (XmlNode x in direction_list)
{
direction_pulled = x.Attributes["name"].Value;
// MessageBox.Show("direction name = " + direction_pulled);
}
//for lat and long of station...
XmlNodeList coord_list = xml.GetElementsByTagName("coord");
foreach (XmlNode x in coord_list)
{
lat_pulled = x.Attributes["lat"].Value;
long_pulled = x.Attributes["lon"].Value;
// MessageBox.Show("lat = " + lat_pulled +"long" +long_pulled);
}
//for last date update time
XmlNodeList last_update_list = xml.GetElementsByTagName("lastupdate");
foreach (XmlNode x in last_update_list)
{
last_update_pulled = x.Attributes["value"].Value;
// MessageBox.Show("last update date = " + last_update_pulled);
}
//for country..
XmlNodeList country_list = xml.GetElementsByTagName("country");
foreach (XmlNode x in country_list)
{
country_name_pulled = x.InnerText;
// MessageBox.Show("country name = " + country_name_pulled);
}
//step3. insert the values pulled into a database..
string loc_value = "";
double d = 0.0000;
double temp_adjust = double.Parse(temp_pulled.ToString()) - 273.15;
temp_pulled_from_web = temp_adjust;//This is the temperature in deg celcius,while temp_pulled is in kelvin
double R = 6371.0;//radius of earth in Km
double dlon = double.Parse(long_pulled.ToString()) - lng_val;
double dlat = double.Parse(lat_pulled.ToString()) - lat_val;
double a = Math.Pow((Math.Sin(dlat / 2)), 2) + Math.Cos(lat_val) * Math.Cos(double.Parse(lat_pulled.ToString())) * Math.Pow((Math.Sin(dlon / 2)), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
d = R * c;//This is the distance
loc_value = country_name_pulled + "," + city_name_pulled;
//lets check a simple thing if the index is pressent then update else insert the data...
/*steps.. count the number of items in a column
1.if the count is less than the index_selected then update else insert
*/
lb_web_status.Text = "active";
// MessageBox.Show("success !");
}
}
catch (Exception ex)
{
//The device is offline and so need to be made inactive
lb_web_status.Text = "inactive";
return;//also return from the current process
}
} //Close of the main function