From 46d93829e257e9278bb176e44341b4aae0315524 Mon Sep 17 00:00:00 2001 From: Franzz Date: Tue, 29 Dec 2020 19:39:45 +0100 Subject: [PATCH] Tracks & waypoints creation timestamp --- script/gaia_upload.js | 123 +++++++++++------------------------------- 1 file changed, 31 insertions(+), 92 deletions(-) diff --git a/script/gaia_upload.js b/script/gaia_upload.js index 0ce24fa..11e54a5 100644 --- a/script/gaia_upload.js +++ b/script/gaia_upload.js @@ -75,8 +75,9 @@ gpxParser.prototype.parse = function (gpxstring) { pt.desc = keepThis.getElementValue(wpt, "desc"); pt.sym = keepThis.getElementValue(wpt, "sym"); - let time = keepThis.getElementValue(wpt, "time"); - pt.time = time == null ? null : new Date(time); + //let time = keepThis.getElementValue(wpt, "time"); + //pt.time = time == null ? null : new Date(time); + pt.time = (keepThis.getElementValue(wpt, "time") || keepThis.metadata.time) || null;; keepThis.waypoints.push(pt); } @@ -113,8 +114,9 @@ gpxParser.prototype.parse = function (gpxstring) { pt.lon = parseFloat(rtept.getAttribute("lon")); pt.ele = parseFloat(keepThis.getElementValue(rtept, "ele")) || null; - let time = keepThis.getElementValue(rtept, "time"); - pt.time = time == null ? null : new Date(time); + //let time = keepThis.getElementValue(rtept, "time"); + //pt.time = time == null ? null : new Date(time); + pt.time = (keepThis.getElementValue(rtept, "time") || keepThis.metadata.time) || null; routepoints.push(pt); } @@ -138,6 +140,7 @@ gpxParser.prototype.parse = function (gpxstring) { track.src = keepThis.getElementValue(trk, "src"); track.number = keepThis.getElementValue(trk, "number"); track.color = keepThis.getElementValue(trk, "DisplayColor"); + track.time = keepThis.metadata.time; let type = keepThis.queryDirectSelector(trk, "type"); track.type = type != null ? type.innerHTML : null; @@ -160,8 +163,9 @@ gpxParser.prototype.parse = function (gpxstring) { pt.lon = parseFloat(trkpt.getAttribute("lon")); pt.ele = parseFloat(keepThis.getElementValue(trkpt, "ele")) || null; - let time = keepThis.getElementValue(trkpt, "time"); - pt.time = time == null ? null : new Date(time); + //let time = keepThis.getElementValue(trkpt, "time"); + //pt.time = time == null ? null : new Date(time); + pt.time = (keepThis.getElementValue(trkpt, "time") || keepThis.metadata.time) || null; trackpoints.push(pt); } @@ -583,12 +587,16 @@ class Gaia { } //Build & Upload Track File - uploadTrack() { - this.feedback('info', 'Uploading tracks...'); + uploadTrack(iIndex, bSecondTry) { + iIndex = iIndex || 0; + bSecondTry = bSecondTry || false; - //Convert to geojson - let iPostedTracks = 0; - $.each(this.aoTracks, (iIndex, aoTrack) => { + if(iIndex == 0) this.feedback('info', 'Uploading tracks...'); + + if(iIndex < this.aoTracks.length) { + let aoTrack = this.aoTracks[i]; + + this.feedback('info', 'Uploading track "'+aoTrack.name+'"'); //Set color let sColor = '#4ABD32'; @@ -604,13 +612,13 @@ class Gaia { aoCoords.push([pt.lon, pt.lat, pt.ele, 0]); } - //Upload + //Convert to geojson let sPostedData = JSON.stringify({ type: 'feature', properties: { color: sColor, title: aoTrack.name, - time_created: '2020-12-27T11:34:03.537Z', + time_created: aoTrack.time, routing_mode: null, notes: aoTrack.desc, //distance: 168405.62350073704, @@ -633,7 +641,6 @@ class Gaia { }); var self = this; - this.feedback('info', 'Uploading track "'+aoTrack.name+'"'); $.post({ url: Gaia.API+'/track/', contentType: 'application/json', @@ -650,42 +657,18 @@ class Gaia { data: this.postedData, trackName: this.trackName }).done(function() { - iPostedTracks++; self.feedback('info', 'Track "'+this.trackName+'" uploaded'); - if(iPostedTracks == self.aoTracks.length) { - self.feedback('info', 'All tracks uploaded'); - self.uploadWayPoints(); - } + self.uploadTrack(++iIndex); }).fail(function() { - self.feedback('error', 'Track "'+this.trackName+'" failed to upload'); + self.feedback('error', 'Track "'+this.trackName+'" failed to upload. Retrying...'); + self.uploadTrack(iIndex, true); }); }); - }); - - /* Legacy lethod: through form submit - - //Build track file - this.feedback('info', 'Building consolidated track'); - var oGPX = new gpxParser(); - oGPX.tracks = this.aoTracks; - var sTrack = oGPX.getGPX(this.sFolderName); - - //Send consolidated Tracks as one file - var oForm = new FormData(); - oForm.append('name', this.sFolderName); - oForm.append('files', new Blob([sTrack]), 'Consolidated.gpx'); - - this.feedback('info', 'Uploading consolidated track'); - $.ajax({ - url: Gaia.URL+'/upload/', - type: 'POST', - data: oForm, - enctype: 'multipart/form-data', - processData: false, - contentType: false, - cache: false - }).done(() => {this.checkNotif();}); - */ + } + else { + self.feedback('info', 'All tracks uploaded'); + self.uploadWayPoints(); + } } /* @@ -709,50 +692,6 @@ class Gaia { else setTimeout((() => {this.checkNotif();}), 1000); }); } - - //Convert QMapshack Track Colors into Gaia Colors - setTracksColor() { - this.feedback('info', 'Track Color - Matching Gaia track IDs with GPX tracks'); - var iCount = 0; - - $.get(Gaia.API+'/folder/'+this.sFolderId+'/').done((asFolder) => { - var self = this; - for(var iGaiaIndex in asFolder.properties.tracks) { - for(var iGpxIndex in this.aoTracks) { - if(asFolder.properties.tracks[iGaiaIndex].title == this.aoTracks[iGpxIndex].name) { - this.aoTracks[iGpxIndex].id = asFolder.properties.tracks[iGaiaIndex].id; - this.feedback('info', 'Track Color - Track "'+this.aoTracks[iGpxIndex].name+'" found a match ('+(++iCount)+'/'+this.aoTracks.length+')'); - - $.ajax({ - url: Gaia.API+'/track/'+this.aoTracks[iGpxIndex].id+'/', - color: this.aoTracks[iGpxIndex].color - }).done(function(asGaiaTrackDisplay) { - var asGaiaTrack = asGaiaTrackDisplay.features[0]; - delete asGaiaTrack.id; - delete asGaiaTrack.style; - - //Set color - var sColor = '#ff0000'; - switch(this.color) { - case 'DarkBlue': sColor = '#2D3FC7'; break; - case 'Magenta': sColor = '#B60DC3'; break; - } - asGaiaTrack.properties.color = sColor; - asGaiaTrack.properties.hexcolor = sColor; - - self.feedback('info', 'Track Color - Setting track color "'+sColor+'" ('+this.color+') to "'+asGaiaTrack.properties.title+'"'); - $.ajax({ - url: Gaia.API+'/track/'+asGaiaTrack.properties.id+'/', - type: 'PUT', - contentType: 'application/json', - data: JSON.stringify(asGaiaTrack) - }); - }); - } - } - } - }); - } */ uploadWayPoints(iIndex, bSecondTry) { @@ -776,7 +715,7 @@ class Gaia { }, properties: { title: sWaypointName, - time_created: "2020-06-07T14:01:03.944Z", + time_created: this.aoWaypoints[iIndex].time, icon: Gaia.getIconName(this.aoWaypoints[iIndex].sym), isValid: true, isLatestImport: true, @@ -889,7 +828,7 @@ class Gaia { console.log('Loading GaiaGps Uploader...'); //To be adjusted regularly -$FileInput = $('input[type=file]') +$FileInput = $('input[type=file]'); $FileButton = $('a[href="https://help.gaiagps.com/hc/en-us/articles/360052763513"]').parent().find('button'); let oGaia = new Gaia($FileInput, $FileButton);