summaryrefslogtreecommitdiffstats
path: root/core/parse-xml.c
diff options
context:
space:
mode:
authorGravatar Seppo Takalo <seppo.takalo@iki.fi>2017-09-13 17:46:30 +0300
committerGravatar Dirk Hohndel <dirk@hohndel.org>2017-09-13 07:57:45 -0700
commitba854a8cc5ed51e26144f272e2fa5235f5d00119 (patch)
treed08723fbaa76232cc247352db7ff54f56a664766 /core/parse-xml.c
parent83c9ad35a6c9266c17097d5e3f35837fc62833f6 (diff)
downloadsubsurface-ba854a8cc5ed51e26144f272e2fa5235f5d00119.tar.gz
Fix cylinder changes when importing from Shearwater database
Re-do the logic to use add_gas_switch_event() instead of creating event manually. Fix the SQL query to find the proper dive id from dive log number. Signed-off-by: Seppo Takalo <seppo.takalo@iki.fi>
Diffstat (limited to 'core/parse-xml.c')
-rw-r--r--core/parse-xml.c42
1 files changed, 33 insertions, 9 deletions
diff --git a/core/parse-xml.c b/core/parse-xml.c
index 3dd32c1c8..fa1fc4fdb 100644
--- a/core/parse-xml.c
+++ b/core/parse-xml.c
@@ -2675,15 +2675,39 @@ extern int shearwater_changes(void *handle, int columns, char **data, char **col
(void) columns;
(void) column;
- event_start();
- if (data[0])
- cur_event.time.seconds = atoi(data[0]);
- if (data[1]) {
- strcpy(cur_event.name, "gaschange");
- cur_event.value = lrint(atof(data[1]) * 100);
+ if (columns != 3) {
+ return 1;
+ }
+ if (!data[0] || !data[1] || !data[2]) {
+ return 2;
+ }
+ int o2 = lrint(atof(data[1]) * 1000);
+ int he = lrint(atof(data[2]) * 1000);
+
+ /* Shearwater allows entering only 99%, not 100%
+ * so assume 99% to be pure oxygen */
+ if (o2 == 990 && he == 0)
+ o2 = 1000;
+
+ // Find the cylinder index
+ int i;
+ bool found = false;
+ for (i = 0; i < cur_cylinder_index; ++i) {
+ if (cur_dive->cylinder[i].gasmix.o2.permille == o2 && cur_dive->cylinder[i].gasmix.he.permille == he) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) {
+ // Cylinder not found, creating a new one
+ cylinder_start();
+ cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = o2;
+ cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = he;
+ cylinder_end();
+ i = cur_cylinder_index;
}
- event_end();
+ add_gas_switch_event(cur_dive, get_dc(), atoi(data[0]), i);
return 0;
}
@@ -2788,7 +2812,7 @@ extern int shearwater_dive(void *param, int columns, char **data, char **column)
char get_profile_template[] = "select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling from dive_log_records AS r join dive_logs as l on r.diveLogId=l.diveId where diveLogId = %d";
char get_profile_template_ai[] = "select currentTime,currentDepth,waterTemp,averagePPO2,currentNdl,CNSPercent,decoCeiling,aiSensor0_PressurePSI,aiSensor1_PressurePSI from dive_log_records AS r join dive_logs as l on r.diveLogId=l.diveId where number = %d";
char get_cylinder_template[] = "select fractionO2,fractionHe from dive_log_records as r join dive_logs as l on r.diveLogId=l.diveId where number = %d group by fractionO2,fractionHe";
- char get_changes_template[] = "select a.currentTime,a.fractionO2,a.fractionHe from dive_log_records as a,dive_log_records as b where a.diveLogId = %d and b.diveLogId = %d and (a.id - 1) = b.id and (a.fractionO2 != b.fractionO2 or a.fractionHe != b.fractionHe) union select min(currentTime),fractionO2,fractionHe from dive_log_records where diveLogId = %d";
+ char get_changes_template[] = "select a.currentTime,a.fractionO2,a.fractionHe from dive_log_records as a join dive_logs as l on a.diveLogId=l.diveId,dive_log_records as b where l.number = %d and (a.id - 1) = b.id and (a.fractionO2 != b.fractionO2 or a.fractionHe != b.fractionHe) and a.diveLogId=b.divelogId";
char get_buffer[1024];
dive_start();
@@ -2861,7 +2885,7 @@ extern int shearwater_dive(void *param, int columns, char **data, char **column)
return 1;
}
- snprintf(get_buffer, sizeof(get_buffer) - 1, get_changes_template, cur_dive->number);
+ snprintf(get_buffer, sizeof(get_buffer) - 1, get_changes_template, cur_dive->number, cur_dive->number, cur_dive->number);
retval = sqlite3_exec(handle, get_buffer, &shearwater_changes, 0, &err);
if (retval != SQLITE_OK) {
fprintf(stderr, "%s", "Database query shearwater_changes failed.\n");