diff options
author | Bastien Nocera <hadess@hadess.net> | 2019-08-26 15:01:24 +0300 |
---|---|---|
committer | Bastien Nocera <hadess@hadess.net> | 2019-08-26 15:59:45 +0300 |
commit | ad783ae5fbbb059a104192f493ffc836c0a4dd6d (patch) | |
tree | e4f307425aa087c4f4476da4b0149ae06d895827 /libgweather | |
parent | 9608cdc8b9b6e6688acb1ab4f18ab894cf6ade70 (diff) | |
download | libgweather-ad783ae5fbbb059a104192f493ffc836c0a4dd6d.tar.gz |
tests: Add unfinished weather test
Could do with options being implemented
Diffstat (limited to 'libgweather')
-rw-r--r-- | libgweather/meson.build | 5 | ||||
-rw-r--r-- | libgweather/test_weather.c | 134 |
2 files changed, 139 insertions, 0 deletions
diff --git a/libgweather/meson.build b/libgweather/meson.build index 6688807..7ca614e 100644 --- a/libgweather/meson.build +++ b/libgweather/meson.build @@ -131,3 +131,8 @@ executable('test_sun_moon', c_args: test_cargs, dependencies: libgweather_dep, install: false) +executable('test_weather', + ['test_weather.c'], + c_args: test_cargs, + dependencies: libgweather_dep, + install: false) diff --git a/libgweather/test_weather.c b/libgweather/test_weather.c new file mode 100644 index 0000000..24e02d7 --- /dev/null +++ b/libgweather/test_weather.c @@ -0,0 +1,134 @@ + +#include <gweather-version.h> +#include "gweather-weather.h" + +#include <locale.h> + +static char *search_str; +// gnome-weather uses: +static GWeatherProvider providers = GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_YR_NO | GWEATHER_PROVIDER_OWM; + +// default: +//static GWeatherProvider providers = GWEATHER_PROVIDER_METAR | GWEATHER_PROVIDER_IWIN; + +static gboolean +find_loc_children (GWeatherLocation *location, + const char *search_str, + GWeatherLocation **ret) +{ + GWeatherLocation **children; + guint i; + + children = gweather_location_get_children (location); + for (i = 0; children[i] != NULL; i++) { + if (gweather_location_get_level (children[i]) == GWEATHER_LOCATION_WEATHER_STATION) { + const char *code; + + code = gweather_location_get_code (children[i]); + if (g_strcmp0 (search_str, code) == 0) { + *ret = gweather_location_ref (children[i]); + return TRUE; + } + } else { + if (find_loc_children (children[i], search_str, ret)) + return TRUE; + } + } + + return FALSE; +} + +static GWeatherLocation * +find_loc (GWeatherLocation *world, + const char *search_str) +{ + GWeatherLocation *loc = NULL; + + find_loc_children (world, search_str, &loc); + return loc; +} + +static void +weather_updated (GWeatherInfo *info, + GMainLoop *loop) +{ + GSList *forecasts, *l; + time_t val; + static gboolean weather_printed = FALSE; + static gboolean forecast_printed = FALSE; + + if (gweather_info_get_value_update (info, &val)) { + g_message ("Weather now: %s", gweather_info_get_temp_summary (info)); + weather_printed = TRUE; + } + + forecasts = gweather_info_get_forecast_list (info); + if (!forecasts) + return; + + for (l = forecasts; l != NULL; l = l->next) { + GWeatherInfo *i = l->data; + + if (gweather_info_get_value_update (i, &val)) { + GDateTime *d; + char *date_str; + + d = g_date_time_new_from_unix_utc (val); + date_str = g_date_time_format (d, "%c"); + g_message ("Weather for %s: %s", date_str, gweather_info_get_temp_summary (i)); + g_free (date_str); + g_date_time_unref (d); + + /* One will be enough... */ + forecast_printed = TRUE; + } + } + + if (weather_printed && + forecast_printed) + g_main_loop_quit (loop); +} + +static void +set_providers (GWeatherInfo *info) +{ + //FIXME print the current providers + gweather_info_set_enabled_providers (info, providers); +} + +int +main (int argc, char **argv) +{ + GWeatherLocation *world, *loc; + GWeatherInfo *info; + GMainLoop *loop; + + setlocale (LC_ALL, ""); + + // FIXME add options + if (argc != 2) + return 1; + search_str = argv[1]; + + world = gweather_location_get_world (); + loc = find_loc (world, search_str); + + if (!loc) { + g_message ("Could not find station for %s", search_str); + return 1; + } + + g_message ("Found station %s for '%s'", gweather_location_get_name (loc), search_str); + + loop = g_main_loop_new (NULL, TRUE); + info = gweather_info_new (loc); + set_providers (info); + g_signal_connect (G_OBJECT (info), "updated", + G_CALLBACK (weather_updated), loop); + gweather_info_update (info); + + g_main_loop_run (loop); + g_main_loop_unref (loop); + + return 0; +} |