ripple-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject incubator-ripple git commit: RIPPLE-91 Fix gpx file implementation in Geolocation panel.
Date Thu, 18 Jun 2015 22:18:29 GMT
Repository: incubator-ripple
Updated Branches:
  refs/heads/master 3d95b7e85 -> 9cdc31fb4


RIPPLE-91 Fix gpx file implementation in Geolocation panel.

Clicking on the gpx file input box raises a file picker dialog. Selecting a file usually leads
to a change event on the input box. This causes loadGpxFile to be called, passing in two null
values and an array containing one element, which is the change event. loadGpxFile then extracts
the change event from the array, extracts the array of files selected by the file picker from
the change event, and reads the file that is the first element of this array.

There are a number of problems with this implementation.
1) if you select a file and then click on the input box again and cancel out of the file picker,
then you select no files but still generate a change event.  This causes loadGpxFile to throw
an unhandled exception.
2) if an error were to occur while reading the file, loadGpxFile would call a null function
value (the "fail" callback).
3) loadGpx has an absurd interface.  This routine is only called in one place.  All you need
is a string which is the file to load.

The fix is to verify that a file was selected before calling loadGpxFile, and to have loadGpxFile
call console.log to report read errors.

In addition to fixing these problems, I also cleaned up the mixture of CR and CRLF line endings.
 This was the only change to geoView/panel.html.

This closes #63


Project: http://git-wip-us.apache.org/repos/asf/incubator-ripple/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ripple/commit/9cdc31fb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ripple/tree/9cdc31fb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ripple/diff/9cdc31fb

Branch: refs/heads/master
Commit: 9cdc31fb4cd1daa2b60635553e3d9965616c0519
Parents: 3d95b7e
Author: Julian Horn <julian.c.horn@intel.com>
Authored: Thu Jun 18 15:52:53 2015 -0400
Committer: Tim Barham <tim.barham@microsoft.com>
Committed: Thu Jun 18 15:18:14 2015 -0700

----------------------------------------------------------------------
 lib/client/ui/plugins/geoView.js         | 295 +++++++++++++-------------
 lib/client/ui/plugins/geoView/panel.html |  36 ++--
 2 files changed, 167 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/9cdc31fb/lib/client/ui/plugins/geoView.js
----------------------------------------------------------------------
diff --git a/lib/client/ui/plugins/geoView.js b/lib/client/ui/plugins/geoView.js
index 6dcafb7..01fbaeb 100644
--- a/lib/client/ui/plugins/geoView.js
+++ b/lib/client/ui/plugins/geoView.js
@@ -220,111 +220,110 @@ module.exports = {
             });
         }
 
-        function loadGpxFile(win, fail, args) {
-            var reader = new FileReader(),
-                file = args[0],
-                _xml,
-                t,
-                att,
-                lastAtt,
-                _ele,
-                _timestamp,
-                _lastTimestamp,
-                _useTimestamp = new Date().getTime(),
-                _tempTimestamp,
-                _tempPosition,
-                _lastPosition,
-                _useLastTimestamp,
-                _heading = 0,
-                _speed = 0,
-                _dist = 0,
-                navUtils = new utils.navHelper();
-
-            reader.onload = function (e) {
-                _xml = e.target.result;
-                t = $(_xml).find('trkpt');
-
-                track = [];
-
-                utils.forEach(t, function (p, i) {
-                    if (!isNaN(i)) {
-                        att = t[i].attributes;
-                        lastAtt = t[i - 1] ? t[i - 1].attributes : {};
-                        _ele = $(t[i]).find("ele")[0];
-                        _timestamp = $(t[i]).find("time")[0];
-                        _lastTimestamp = $(t[i - 1]).find("time")[0];
-
-                        if (_timestamp) {
-                            //files recorded with endomondo and others have timestamps, this
is not a route plan but a record of a track
-                            _useTimestamp = new Date(_timestamp.innerHTML).getTime();
-                        }
-
-                        if (t[i - 1]) {
-                            _dist = navUtils.getDistance(att["lat"].value, att["lon"].value,
lastAtt["lat"].value, lastAtt["lon"].value);
-
-                            if (_lastTimestamp) {
-                                _useLastTimestamp = new Date(_lastTimestamp.innerHTML).getTime();
-                            }
-                            else {
-                                //routes from YOURS come in as tracks (rather than routes
under the GPX schema), but with no timestamps.  This is a route.
-                                _useLastTimestamp = _useTimestamp;
-                                _useTimestamp += Math.round(_dist / 22.2222 * 1000);  //80km/h
in m/s
-                            }
-
-                            _heading = navUtils.getHeading(lastAtt["lat"].value, lastAtt["lon"].value,
att["lat"].value, att["lon"].value);
-                            _speed = (_dist / ((_useTimestamp - _useLastTimestamp) / 1000)).toFixed(2);
-
-                            if (!_lastTimestamp) {
-                                //on YOURS routes, make sure we have at least one update
a second
-                                _tempTimestamp = _useLastTimestamp;
-
-                                while (_useTimestamp - _tempTimestamp > 1000) {
-                                    _tempTimestamp += 1000;
-                                    _lastPosition = track[track.length - 1].coords;
-                                    _tempPosition = navUtils.simulateTravel(_lastPosition.latitude,
_lastPosition.longitude, _heading, _speed);
-                                    track.push({
-                                        coords: {
-                                            latitude: _tempPosition.latitude,
-                                            longitude: _tempPosition.longitude,
-                                            altitude: _ele ? _ele.innerHTML : 0,
-                                            accuracy: 150,
-                                            altitudeAccuracy: 80,
-                                            heading: _heading,
-                                            speed: _speed
-                                        },
-                                        timestamp: _tempTimestamp
-                                    });
-                                }
-                            }
-                        }
-
-                        track.push({
-                            coords: {
-                                latitude: att["lat"].value,
-                                longitude: att["lon"].value,
-                                altitude: _ele ? _ele.innerHTML : 0,
-                                accuracy: 150,
-                                altitudeAccuracy: 80,
-                                heading: _heading,
-                                speed: _speed
-                            },
-                            timestamp: _useTimestamp
-                        });
-                    }
-                });
-            };
-            reader.onerror = function (e) {
-                fail(e);
-            };
-            reader.readAsText(file.target.files[0], "UTF-8");
+        function loadGpxFile(filename) {
+            var reader = new FileReader(),
+                _xml,
+                t,
+                att,
+                lastAtt,
+                _ele,
+                _timestamp,
+                _lastTimestamp,
+                _useTimestamp = new Date().getTime(),
+                _tempTimestamp,
+                _tempPosition,
+                _lastPosition,
+                _useLastTimestamp,
+                _heading = 0,
+                _speed = 0,
+                _dist = 0,
+                navUtils = new utils.navHelper();
+
+            reader.onload = function (e) {
+                _xml = e.target.result;
+                t = $(_xml).find('trkpt');
+
+                track = [];
+
+                utils.forEach(t, function (p, i) {
+                    if (!isNaN(i)) {
+                        att = t[i].attributes;
+                        lastAtt = t[i - 1] ? t[i - 1].attributes : {};
+                        _ele = $(t[i]).find("ele")[0];
+                        _timestamp = $(t[i]).find("time")[0];
+                        _lastTimestamp = $(t[i - 1]).find("time")[0];
+
+                        if (_timestamp) {
+                            //files recorded with endomondo and others have timestamps, this
is not a route plan but a record of a track
+                            _useTimestamp = new Date(_timestamp.innerHTML).getTime();
+                        }
+
+                        if (t[i - 1]) {
+                            _dist = navUtils.getDistance(att["lat"].value, att["lon"].value,
lastAtt["lat"].value, lastAtt["lon"].value);
+
+                            if (_lastTimestamp) {
+                                _useLastTimestamp = new Date(_lastTimestamp.innerHTML).getTime();
+                            }
+                            else {
+                                //routes from YOURS come in as tracks (rather than routes
under the GPX schema), but with no timestamps.  This is a route.
+                                _useLastTimestamp = _useTimestamp;
+                                _useTimestamp += Math.round(_dist / 22.2222 * 1000);  //80km/h
in m/s
+                            }
+
+                            _heading = navUtils.getHeading(lastAtt["lat"].value, lastAtt["lon"].value,
att["lat"].value, att["lon"].value);
+                            _speed = (_dist / ((_useTimestamp - _useLastTimestamp) / 1000)).toFixed(2);
+
+                            if (!_lastTimestamp) {
+                                //on YOURS routes, make sure we have at least one update
a second
+                                _tempTimestamp = _useLastTimestamp;
+
+                                while (_useTimestamp - _tempTimestamp > 1000) {
+                                    _tempTimestamp += 1000;
+                                    _lastPosition = track[track.length - 1].coords;
+                                    _tempPosition = navUtils.simulateTravel(_lastPosition.latitude,
_lastPosition.longitude, _heading, _speed);
+                                    track.push({
+                                        coords: {
+                                            latitude: _tempPosition.latitude,
+                                            longitude: _tempPosition.longitude,
+                                            altitude: _ele ? _ele.innerHTML : 0,
+                                            accuracy: 150,
+                                            altitudeAccuracy: 80,
+                                            heading: _heading,
+                                            speed: _speed
+                                        },
+                                        timestamp: _tempTimestamp
+                                    });
+                                }
+                            }
+                        }
+
+                        track.push({
+                            coords: {
+                                latitude: att["lat"].value,
+                                longitude: att["lon"].value,
+                                altitude: _ele ? _ele.innerHTML : 0,
+                                accuracy: 150,
+                                altitudeAccuracy: 80,
+                                heading: _heading,
+                                speed: _speed
+                            },
+                            timestamp: _useTimestamp
+                        });
+                    }
+                });
+            };
+            reader.onerror = function (e) {
+                console.log('Ripple :: error reading gpx file ' + filename + ': ' + e);
+            };
+            reader.readAsText(filename, "UTF-8");
         }
 
-        function replayGpxTrack() {
-            if (_replayingGpxFile) {
-                _haltGpxReplay = true;
-                gpxGo.innerHTML = constants.GEO.GPXGO_LABELS.GO;
-            }
-            else {
+        function replayGpxTrack() {
+            if (_replayingGpxFile) {
+                _haltGpxReplay = true;
+                gpxGo.innerHTML = constants.GEO.GPXGO_LABELS.GO;
+            }
+            else {
                 if (track.length > 0) {
                     _haltGpxReplay = false;
                     gpxGo.innerHTML = constants.GEO.GPXGO_LABELS.STOP;
@@ -341,33 +340,33 @@ module.exports = {
                     updateHeadingValues();
                     _triggerEvent();
 
-                    moveNextGpxTrack(1);
-                }
+                    moveNextGpxTrack(1);
+                }
             }
         }
 
         function moveNextGpxTrack(i)
-        {
-            if (_haltGpxReplay) {
-                _replayingGpxFile = false;
-                _haltGpxReplay = false;
-                console.log("Ripple :: User interrupted replay of GPX file (Aye Captain,
answers All Stop.)");
-            }
-            else {
-                _replayingGpxFile = true;
-                var _timeMultiplier = !isNaN(gpxMultiplier.value) ? gpxMultiplier.value :
1,
-                _step = 0,
-                _interval = 0;
-
-                while (_interval < 250) {
-                    _step++;
-                    if ((i + _step) >= track.length) { break; }
-                    _interval = (track[i + _step].timestamp - track[i].timestamp) / _timeMultiplier;
-                }
-
-                gpxReplayStatus.textContent = (_interval / 1000).toFixed(2) + "s (" + (_interval
/ 1000 * _timeMultiplier).toFixed(2) + "s realtime), " + (i + 1) + " of " + track.length +
" (stepping " + _step + " at " + _timeMultiplier + "x)";
-
-                setTimeout(function () {
+        {
+            if (_haltGpxReplay) {
+                _replayingGpxFile = false;
+                _haltGpxReplay = false;
+                console.log("Ripple :: User interrupted replay of GPX file (Aye Captain,
answers All Stop.)");
+            }
+            else {
+                _replayingGpxFile = true;
+                var _timeMultiplier = !isNaN(gpxMultiplier.value) ? gpxMultiplier.value :
1,
+                _step = 0,
+                _interval = 0;
+
+                while (_interval < 250) {
+                    _step++;
+                    if ((i + _step) >= track.length) { break; }
+                    _interval = (track[i + _step].timestamp - track[i].timestamp) / _timeMultiplier;
+                }
+
+                gpxReplayStatus.textContent = (_interval / 1000).toFixed(2) + "s (" + (_interval
/ 1000 * _timeMultiplier).toFixed(2) + "s realtime), " + (i + 1) + " of " + track.length +
" (stepping " + _step + " at " + _timeMultiplier + "x)";
+
+                setTimeout(function () {
                     latitude.value = track[i].coords.latitude;
                     longitude.value = track[i].coords.longitude;
                     altitude.value = track[i].coords.altitude;
@@ -380,20 +379,20 @@ module.exports = {
                     updateHeadingValues();
                     _triggerEvent();
 
-                    if (track[i + _step]) {
-                        moveNextGpxTrack(i + _step);
+                    if (track[i + _step]) {
+                        moveNextGpxTrack(i + _step);
                     }
-                    else {
-                        if (i < track.length - 1) {
-                            moveNextGpxTrack(track.length - 1);
-                        }
-                        else {
-                            _replayingGpxFile = false;
-                            gpxGo.innerHTML = constants.GEO.GPXGO_LABELS.GO;
-                            console.log("Ripple :: Finished replaying GPX file (Arriving
at our destination, assuming standard orbit Captain.)");
-                        }
-                    }
-                }, _interval);
+                    else {
+                        if (i < track.length - 1) {
+                            moveNextGpxTrack(track.length - 1);
+                        }
+                        else {
+                            _replayingGpxFile = false;
+                            gpxGo.innerHTML = constants.GEO.GPXGO_LABELS.GO;
+                            console.log("Ripple :: Finished replaying GPX file (Arriving
at our destination, assuming standard orbit Captain.)");
+                        }
+                    }
+                }, _interval);
             }
         }
 
@@ -435,10 +434,14 @@ module.exports = {
         jQuery("#" + GEO_OPTIONS.TIMEOUT).bind("click", function () {
             updateGeo();
         });
-        jQuery("#" + GEO_OPTIONS.GPXFILE).bind("change", function (a) {
-            loadGpxFile(null, null, [a]);
+        jQuery("#" + GEO_OPTIONS.GPXFILE).bind("change", function (a) {
+            // It is possible to have no file selected and still get a change event.
+            // You do this by selecting something, then selecting nothing.
+            // You select nothing by cancelling out of the file picker dialog.
+            var selectedFiles = a.target.files;
+            if (selectedFiles.length > 0) loadGpxFile(selectedFiles[0]);
         });
-        jQuery("#" + GEO_OPTIONS.GPXGO).bind("click", function () {
+        jQuery("#" + GEO_OPTIONS.GPXGO).bind("click", function () {
             replayGpxTrack();
         });
 
@@ -464,8 +467,8 @@ module.exports = {
 
         _triggerEvent();
 
-        function _triggerEvent() {
-            event.trigger(positionEvent, [{
+        function _triggerEvent() {
+            event.trigger(positionEvent, [{
                 latitude: latitude.value,
                 longitude: longitude.value,
                 altitude: altitude.value,
@@ -474,8 +477,8 @@ module.exports = {
                 heading: heading ? heading.value : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                 speed: speed ? speed.value : 0, // HACK: see techdebt http://www.pivotaltracker.com/story/show/5478847
                 cellID: cellID.value,
-                timeStamp: new Date()
-            }]);
+                timeStamp: new Date()
+            }]);
         }
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-ripple/blob/9cdc31fb/lib/client/ui/plugins/geoView/panel.html
----------------------------------------------------------------------
diff --git a/lib/client/ui/plugins/geoView/panel.html b/lib/client/ui/plugins/geoView/panel.html
index 86aa367..0573e7b 100644
--- a/lib/client/ui/plugins/geoView/panel.html
+++ b/lib/client/ui/plugins/geoView/panel.html
@@ -88,25 +88,25 @@
                 </td>
             </tr>
             <tr>
-                <td colspan="2">
-                    <input id="geo-gpxfile" type="file" class="ui-state-default ui-corner-all"
/>
-                    <button id="geo-gpx-go" class="ui-button ui-widget ui-state-default
ui-corner-all ui-button-text-only" ><span class="ui-button-text">Go</span></button>
-                    <select id="geo-gpxmultiplier-select" class="ui-state-default ui-corner-all">
-                        <option value="1">1x</option>
-                        <option value="2">2x</option>
-                        <option value="4">4x</option>
-                        <option value="8">8x</option>
-                        <option value="16">16x</option>
-                        <option value="32">32x</option>
-                        <option value="64">64x</option>
-                        <option value="128">128x</option>
-                    </select>
-                </td>
+                <td colspan="2">
+                    <input id="geo-gpxfile" type="file" class="ui-state-default ui-corner-all"
/>
+                    <button id="geo-gpx-go" class="ui-button ui-widget ui-state-default
ui-corner-all ui-button-text-only" ><span class="ui-button-text">Go</span></button>
+                    <select id="geo-gpxmultiplier-select" class="ui-state-default ui-corner-all">
+                        <option value="1">1x</option>
+                        <option value="2">2x</option>
+                        <option value="4">4x</option>
+                        <option value="8">8x</option>
+                        <option value="16">16x</option>
+                        <option value="32">32x</option>
+                        <option value="64">64x</option>
+                        <option value="128">128x</option>
+                    </select>
+                </td>
             </tr>
-            <tr id="geo-gpxreplaystatus-container">
-                <td colspan="2">
-                    <label id="geo-gpxreplaystatus"></label>
-                </td>
+            <tr id="geo-gpxreplaystatus-container">
+                <td colspan="2">
+                    <label id="geo-gpxreplaystatus"></label>
+                </td>
             </tr>
         </table>
 


Mime
View raw message