changeset 118:ffc3ec017bad

geohub: add unit option to logjson
author Lewin Bormann <lbo@spheniscida.de>
date Thu, 31 Dec 2020 19:59:19 +0100
parents 8b7202c3f945
children 08caf18bd9ef
files src/main.rs src/types.rs
diffstat 2 files changed, 12 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs	Thu Dec 31 19:47:08 2020 +0100
+++ b/src/main.rs	Thu Dec 31 19:59:19 2020 +0100
@@ -262,13 +262,14 @@
 }
 
 /// Ingest GeoJSON.
-#[rocket::post("/geo/<name>/logjson?<secret>&<datesecret>", data = "<body>")]
+#[rocket::post("/geo/<name>/logjson?<secret>&<datesecret>&<unit>", data = "<body>")]
 fn log_json(
     db: db::DBConn,
     notify_manager: rocket::State<notifier::NotifyManager>,
     name: String,
     secret: Option<String>,
     datesecret: Option<bool>,
+    unit: Option<String>,
     body: rocket_contrib::json::Json<types::LogLocations>,
 ) -> http::GeoHubResponder {
     // Check that secret and client name are legal.
@@ -298,7 +299,15 @@
     // Due to prepared statements, this isn't as bad as it looks.
     let mut errs = vec![];
     for feat in geofeats {
-        let point = types::geopoint_from_feature(feat);
+        let mut point = types::geopoint_from_feature(feat);
+
+        if let (Some(u), Some(speed)) = (unit.as_ref(), point.spd) {
+            match util::to_kph(u.as_str(), speed) {
+                Ok(speed) => point.spd = Some(speed),
+                Err(e) => return e,
+            }
+        }
+
         if let Err(e) = db.log_geopoint(name.as_str(), &secret, &point) {
             errs.push(e);
         }
--- a/src/types.rs	Thu Dec 31 19:47:08 2020 +0100
+++ b/src/types.rs	Thu Dec 31 19:59:19 2020 +0100
@@ -7,7 +7,7 @@
     pub id: Option<i32>,
     pub lat: f64,
     pub long: f64,
-    pub spd: Option<f64>,
+    pub spd: Option<f64>, // in km/h by convention
     pub ele: Option<f64>,
     pub accuracy: Option<f64>,
     pub time: chrono::DateTime<chrono::Utc>,