diff --git a/src/Indicator.vala b/src/Indicator.vala index 037add1..62c3742 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -83,10 +83,14 @@ public class Weather.Indicator : Wingpanel.Indicator { } private async void monitor_weather() { - - var result = get_weather("",key); - display_widget.update_state(result.short_discription,result.temperature); - + //Fetch Report every 30 Minutes + //TODO: Should be read from configuration file + get_weather("",key, display_widget); + GLib.Timeout.add_seconds (1800, () => { + get_weather("",key, display_widget); + return true; + },GLib.Priority.DEFAULT); + yield; } public void connections () {} diff --git a/src/lib/Weather.vala b/src/lib/Weather.vala index be23fea..e8b0320 100644 --- a/src/lib/Weather.vala +++ b/src/lib/Weather.vala @@ -16,7 +16,7 @@ string get_ip(Soup.Session session) { } - weatherInfo get_weather(string location, string secret_key) { + async void get_weather(string location, string secret_key, Weather.Widgets.DisplayWidget display_widget) { //Init the session. var session = new Soup.Session (); @@ -27,40 +27,38 @@ string get_ip(Soup.Session session) { string uri = "https://api.darksky.net/forecast/" + secret_key + "/"; - // Get Location. - double lat, lon; + double lat=0, lon=0; var query_location = new Soup.Message ("GET", loc_uri); session.send_message (query_location); try { - var parser = new Json.Parser(); + var parser = new Json.Parser(); - parser.load_from_data((string) query_location.response_body.flatten().data, -1); + parser.load_from_data((string) query_location.response_body.flatten().data, -1); - var root_object = parser.get_root().get_object(); - lat = root_object.get_double_member("latitude"); - lon = root_object.get_double_member("longitude"); + var root_object = parser.get_root().get_object(); + lat = root_object.get_double_member("latitude"); + lon = root_object.get_double_member("longitude"); var city = root_object.get_string_member("city"); stderr.printf("Location : lat : %g, lon : %g \n", lat, lon); stderr.printf("Location : City: %s \n", city); }catch (Error e){ - stderr.printf(" Unable to get location\n"); - return info ; + stderr.printf(" Unable to get location\n"); + // return;// info ; } - - + var weather_uri = uri + lat.to_string() + "," + lon.to_string(); var message = new Soup.Message ("GET", weather_uri); session.send_message (message); - + try { var parser = new Json.Parser (); - + parser.load_from_data ((string) message.response_body.flatten().data, -1); var root_object = parser.get_root ().get_object (); @@ -71,12 +69,12 @@ string get_ip(Soup.Session session) { stderr.printf("current : %s\n", summary); info.short_discription = summary; info.temperature = temp; - + } catch (Error e) { stderr.printf ("I guess something is not working... %s \n", e.message); } - - return info; - + display_widget.update_state(info.short_discription,info.temperature); + + return; }