changeset 17:94fe2c70b4b4

Always display last known location
author Lewin Bormann <lbo@spheniscida.de>
date Wed, 02 Dec 2020 19:52:57 +0100
parents 3dd4298eae59
children eafb0bca76fc
files assets/livemap.html src/main.rs
diffstat 2 files changed, 13 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/assets/livemap.html	Wed Dec 02 19:40:32 2020 +0100
+++ b/assets/livemap.html	Wed Dec 02 19:52:57 2020 +0100
@@ -35,19 +35,21 @@
         L.circle([oldCoord.lat, oldCoord.lng], circleProps).addTo(mymap);
     }
 
-    var lastID = -1;
+    var lastID = 1;
     xhrcallback = function(xhr) {
         console.log('xhrcallback called.');
         if (xhr.readyState === 4 && xhr.status == 200) {
             const response = xhr.response;
-            const features = response['geo']['features'];
-            const newLastID = response['last'];
-            const lastfeature = features[features.length-1];
-            const coords = lastfeature['geometry']['coordinates'];
+            if (response['geo']) {
+                const features = response['geo']['features'];
+                const newLastID = response['last'];
+                const lastfeature = features[0];
+                const coords = lastfeature['geometry']['coordinates'];
 
-            console.log('Received update:', coords, 'last:', response);
-            setCurrentLocation(coords[1], coords[0]);
-            window.lastID = newLastID;
+                console.log('Received update:', coords, 'last:', response);
+                setCurrentLocation(coords[1], coords[0]);
+                window.lastID = newLastID;
+            }
 
             // Install next XHR.
             waitforupdate();
--- a/src/main.rs	Wed Dec 02 19:40:32 2020 +0100
+++ b/src/main.rs	Wed Dec 02 19:52:57 2020 +0100
@@ -109,7 +109,7 @@
     let check_for_new = db.0.prepare_cached(
         r"SELECT id, t, lat, long, spd, ele FROM geohub.geodata
         WHERE (client = $1) and (id > $2) AND (secret = public.digest($3, 'sha256') or secret is null)
-        ORDER BY t DESC
+        ORDER BY id DESC
         LIMIT $4").unwrap(); // Must succeed.
 
     let last = last.unwrap_or(0);
@@ -157,6 +157,8 @@
 }
 
 /// Wait for an update.
+///
+/// Points are returned in descending order of time.
 #[rocket::get("/geo/<name>/retrieve/live?<secret>&<last>&<timeout>")]
 fn retrieve_live(
     db: DBConn,