diff options
author | stuebinm | 2023-03-11 01:36:35 +0100 |
---|---|---|
committer | stuebinm | 2023-03-11 01:37:54 +0100 |
commit | 7798666c81b390183e2e227232d936abf0cc4a65 (patch) | |
tree | a9ecbe352d7dc28faf7f74720022e27640edea5b /tools/obu-guess-trip | |
parent | 99463395ee9497256b794f4ad2c94b490ca5d0fd (diff) |
simple on-board tools
these are just enough to send train positions to tracktrain with the
current API, but are somewhat brittle (e.g. will fail if not restarted
between trips, etc.)
Diffstat (limited to 'tools/obu-guess-trip')
-rwxr-xr-x | tools/obu-guess-trip | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tools/obu-guess-trip b/tools/obu-guess-trip new file mode 100755 index 0000000..478ba2b --- /dev/null +++ b/tools/obu-guess-trip @@ -0,0 +1,75 @@ +#!/usr/bin/env gosh + +(use rfc.http) +(use rfc.uri) +(use rfc.json) +(use file.util) +(use srfi-19) +(use gauche.collection) +(use gauche.parseopt) +(use gauche.process) + + +(define (main args) + (let-args + (cdr args) + ((adir "d|dir=s") + (baseurl "u|url=s") + (statefile "s|statefile=s") + (help "h|help" => (cut show-help (car args))) + . restargs) + (if (equal? baseurl #f) + (show-help (car args)) + (guesstimate + (if (equal? adir #f) "." adir) + baseurl + (if statefile statefile "./obu-state.edn"))) + (exit 0))) + + +(define (show-help progname) + (display (format +"~a: tracktrain's on-board guesstimater for which trip you're on + +Arguments: + -u --url: base url of the tracktrain server [required] + -s --statefile: state file + -c --config: config file (default is /etc/tracktrain/obu.conf) + -h --help: display this help +" progname)) + (exit 0)) + +(define (guesstimate dir url statefile) + (define stops (fetch-stations url)) + (define pos + (with-input-from-process `(obu-ping -s ,statefile -n 1 -d) read)) + (define trip (assoc-ref (closest-stop-to stops pos) 'trip)) + (display trip)) + +(define (closest-stop-to stops pos) + (define stops-by-station + (apply vector-append (vector->list + (vector-map (lambda (trip) + (vector-map + (lambda (stop) (cons `(trip . ,(assoc-ref trip 'trip)) stop)) + (assoc-ref trip 'stops))) + stops)))) + (find-min + stops-by-station + :key (lambda (stop) + (+ (square (- (assoc-ref pos 'lat) (assoc-ref stop 'lat))) + (square (- (assoc-ref pos 'lon) (assoc-ref stop 'lon))))) + :default (vector-ref stops 0))) + +(define (fetch-stations url) + (define day (date->string (current-date) "~1")) + (define tls + (equal? (uri-ref url 'scheme) 'https)) + (parameterize + ; replace all json keys with symbols; everything else is confusing + ([json-object-handler + (cut map (lambda p `(,(string->symbol (car (car p))) . ,(cdr (car p)))) <>)]) + (parse-json-string + (values-ref (http-get (uri-ref url 'host+port) + (format "/api/timetable/stops/~a" day) + :secure tls) 2)))) |