From e029a031050e08599f9d6d5fa654c17d985039c1 Mon Sep 17 00:00:00 2001
From: stuebinm
Date: Fri, 26 May 2023 21:07:48 +0200
Subject: expose sequence length of trip to onboard unit

---
 tools/obu-ping | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

(limited to 'tools/obu-ping')

diff --git a/tools/obu-ping b/tools/obu-ping
index 2897e23..8d0a84a 100755
--- a/tools/obu-ping
+++ b/tools/obu-ping
@@ -78,11 +78,11 @@ Arguments:
     (while #t
       (ping-pos statefile url token))))
 
-(define (ping-pos dir url token)
+(define (ping-pos statefile url token)
   (let ([pos (try-get-current-pos)])
     (display (format "~a\n" pos))
     (if (not (equal? token #f))
-        (display (format "server: ~a\n" (ping url token pos))))))
+        (display (format "server: ~a\n" (ping url token pos statefile))))))
 
 (define (get-token url trip statefile)
   (define cached?
@@ -109,18 +109,26 @@ Arguments:
                   :secure tls)
                  2))))
 
-(define (ping url token pos)
+(define (ping url token pos statefile)
   (define tls
     (equal? (uri-ref url 'scheme) "https"))
-  (parameterize ([json-object-handler json-symbol-key])
-    (parse-json-string
-     (values-ref (http-post
-                  (uri-ref url 'host+port)
-                  "/api/train/ping"
-                  (construct-json-string `((token . ,token)
-                                           (lat . ,(assoc-ref pos 'lat))
-                                           (long . ,(assoc-ref pos 'lon))
-                                           (timestamp . ,(assoc-ref pos 'time))))
-                  :content-type "application/json"
-                  :secure tls)
-                 2))))
+  (define anchor
+    (parameterize ([json-object-handler json-symbol-key])
+      (parse-json-string
+       (values-ref (http-post
+                    (uri-ref url 'host+port)
+                    "/api/train/ping"
+                    (construct-json-string `((token . ,token)
+                                             (lat . ,(assoc-ref pos 'lat))
+                                             (long . ,(assoc-ref pos 'lon))
+                                             (timestamp . ,(assoc-ref pos 'time))))
+                    :content-type "application/json"
+                    :secure tls)
+                   2))))
+  (define sequencelength
+    (guard [e (else 100000)]
+      (with-input-from-process `(obu-config -s ,statefile sequencelength) read)))
+  (if (> (assoc-ref anchor 'sequence) (sequencelength - 0.2))
+      (do-process `(obu-config -s ,statefile -d token))
+      #f)
+  anchor)
-- 
cgit v1.2.3