summaryrefslogtreecommitdiffstats
path: root/webservice.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2013-01-31 14:09:16 +1100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2013-01-31 14:29:02 +1100
commit635c28923d28335bb7d39f95c17c2edbf16ee2ac (patch)
tree0ae1566c791a505cd8233928c0258629057e83c9 /webservice.c
parent8843ee61560fb3c6a66b7ae9f10367f57eddb109 (diff)
downloadsubsurface-635c28923d28335bb7d39f95c17c2edbf16ee2ac.tar.gz
Better algorithm to merge gps locations & locations names from webservice
This no longer abuses the dive merging code (which would leave stray "dives" behind if a gps fix couldn't be merged with any of the dives) and instead parses the gps fixes into a second table and then walks that table and tries to find matching dives. The code tries to be reasonably smart about this. If we have auto-generated GPS fixes at regular intervals, we look for a fix that is during a dive (that's likely when the boat where the phone is staying dry is more or less above the diver having fun). And if we have named entries (so the user typed in a location name) we try to match them in order to the dives that happened "that day" (where "that day" is about 6h before and after the timestamp of the gps fix). This commit also renames dive_has_location() to dive_has_gps_location() as the difference between if(!dive->location) and if(dives_has_location) is a bit too subtle... Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'webservice.c')
-rw-r--r--webservice.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/webservice.c b/webservice.c
index 9dde771e8..fec2ccf2d 100644
--- a/webservice.c
+++ b/webservice.c
@@ -4,6 +4,7 @@
#include <libxml/tree.h>
#include <libxml/parser.h>
#include "dive.h"
+#include "divelist.h"
#include "display-gtk.h"
struct dive_table gps_location_table;
@@ -144,6 +145,52 @@ static void download_dialog_delete(GtkWidget *w, gpointer data)
download_dialog_release_xml(state);
}
+static gboolean is_automatic_fix(struct dive *gpsfix)
+{
+ if (gpsfix && gpsfix->location && !strcmp(gpsfix->location, "automatic fix"))
+ return TRUE;
+ return FALSE;
+}
+
+#define SAME_GROUP 6 * 3600 // six hours
+
+static void merge_locations_into_dives(void)
+{
+ int i, nr = 0;
+ struct dive *gpsfix, *last_named_fix = NULL, *dive;
+
+ sort_table(&gps_location_table);
+
+ for_each_gps_location(i, gpsfix) {
+ if (is_automatic_fix(gpsfix)) {
+ dive = find_dive_including(gpsfix->when);
+ if (dive && !dive_has_gps_location(dive))
+ copy_gps_location(gpsfix, dive);
+ } else {
+ if (last_named_fix && dive_within_time_range(last_named_fix, gpsfix->when, SAME_GROUP)) {
+ nr++;
+ } else {
+ nr = 1;
+ last_named_fix = gpsfix;
+ }
+ dive = find_dive_n_near(gpsfix->when, nr, SAME_GROUP);
+ if (dive) {
+ if (!dive_has_gps_location(dive))
+ copy_gps_location(gpsfix, dive);
+ if (!dive->location)
+ dive->location = strdup(gpsfix->location);
+ } else {
+ struct tm tm;
+ utc_mkdate(gpsfix->when, &tm);
+ printf("didn't find dive matching gps fix named %s @ %04d-%02d-%02d %02d:%02d:%02d\n",
+ gpsfix->location,
+ tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec);
+ }
+ }
+ }
+}
+
void webservice_download_dialog(void)
{
const guint pad = 6;
@@ -207,7 +254,9 @@ void webservice_download_dialog(void)
/* apply download */
parse_xml_buffer(_("Webservice"), state.xmldata, state.xmldata_len, &gps_location_table, NULL);
/* now merge the data in the gps_location table into the dive_table */
- // TBD
+ merge_locations_into_dives();
+ mark_divelist_changed(TRUE);
+ dive_list_update_dives();
/* store last entered uid in config */
subsurface_set_conf("webservice_uid", gtk_entry_get_text(GTK_ENTRY(uid)));
}