Planning (the Design canvas' Plan and GridToday, ideas from azedler/roadplanner): a stop's type as its NPS pictogram (marker and list), notes on the trip, its days and stops, + on a leg inserts the next point there; a parking spot per stop (P marker, the route goes there, the walk shown); a link pasted into the search becomes a place (/api/link: the URL's coordinates, else the page's geo meta / JSON-LD / a map link; public addresses only, logged in), added on the leg it lengthens least; avoids for the trip (+ ferries, unpaved) or a day (routed in pieces, joined); Check and Costs folds in the Route panel (no leave time, long days, arriving after sunset, long walks, missing vignettes; fuel from km × consumption × price, stop costs / paid, the budget); Today in drive mode's Menu (stops left, tonight, sunset, Open-Meteo weather, the day's notes, Skip next, Tomorrow). Plugins along the trip's route (Maptool::Corridor, a corridor width; Overpass around:) and plugin settings saved server-side; p4n via a host Firefox's cookies (docs/deployment.adoc). The recording's days (cut where the car stood ≥ 4 h, or by hand) and their export (days, a piece A–B, the whole trip; GPX with times). Track replay in the GNSS simulator. Step advance 5 m past a maneuver. The level gauge (drive mode, from Terrain): lift per wheel to the ramps' steps from roll, pitch, wheelbase and track
This commit is contained in:
1 parent
5e31dfaba9
commit
87d7bd2463
36 files changed
+2047
-123
No files matched your search
@@ -4,6 +4,7 @@ require "oauth2"
|
||||
require "securerandom"
|
||||
require "zip"
|
||||
require "stringio"
|
||||
require "tempfile"
|
||||
require_relative "lib/maptool/gpx"
|
||||
require_relative "lib/maptool/fit"
|
||||
require_relative "lib/maptool/gpx_store"
|
||||
@@ -17,6 +18,8 @@ require_relative "lib/maptool/can_recorder"
|
||||
require_relative "lib/maptool/gnss_sim"
|
||||
require_relative "lib/maptool/trip"
|
||||
require_relative "lib/maptool/trip_log"
|
||||
require_relative "lib/maptool/trip_days"
|
||||
require_relative "lib/maptool/link_place"
|
||||
require_relative "lib/maptool/updater"
|
||||
require_relative "lib/maptool/sync"
|
||||
require "ipaddr"
|
||||
@@ -25,6 +28,7 @@ require_relative "lib/maptool/livetrack_store"
|
||||
require_relative "lib/maptool/livetrack_client"
|
||||
require_relative "lib/maptool/kiwix"
|
||||
require_relative "lib/maptool/plugins"
|
||||
require_relative "lib/maptool/corridor"
|
||||
require_relative "lib/maptool/pmtiles_builder"
|
||||
require_relative "lib/maptool/search"
|
||||
|
||||
@@ -172,6 +176,7 @@ module Maptool
|
||||
# above can't. Checked after PLUGINS_DIR (see Plugins.list/run), so a
|
||||
# same-named private plugin never shadows the built-in one.
|
||||
PLUGINS_DIRS = [PLUGINS_DIR, File.join(DATA_DIR, "plugins")].freeze
|
||||
PLUGIN_SETTINGS = File.join(DATA_DIR, "plugin_settings.json")
|
||||
|
||||
# Shared secret for the Garmin LiveTrack webhook (a Google Apps Script
|
||||
# watching Gmail for LiveTrack emails, see docs/deployment.adoc) — not a
|
||||
@@ -812,6 +817,87 @@ module Maptool
|
||||
json folder: TRIP_LOG.stop
|
||||
end
|
||||
|
||||
# The recording's days (lib/maptool/trip_days.rb) — the running trip's, or
|
||||
# the last one's: totals, the days with what cut them, and (?track=1) the
|
||||
# line as [lon, lat, t] for picking a piece on the map.
|
||||
get "/api/trip/log/days" do
|
||||
require_login!
|
||||
rec = TRIP_LOG.recording
|
||||
halt 404, json(error: "no trip recorded") unless rec && rec[:points].size >= 2
|
||||
|
||||
points = rec[:points]
|
||||
days = TripDays.days(points, TripDays.cuts(points, hand: rec[:hand], joined: rec[:joined]))
|
||||
body = { folder: rec[:folder], active: rec[:active], started_at: rec[:started_at],
|
||||
totals: TripDays.stats(points).merge(days: days.size, started_at: points.first[3], ended_at: points.last[3]),
|
||||
days: days.map { |d| d.merge(bbox: TripDays.bbox(points[d[:from]...d[:to]])) } }
|
||||
body[:track] = points.map { |lon, lat, _, t| [lon.round(6), lat.round(6), t] } if params[:track]
|
||||
json body
|
||||
end
|
||||
|
||||
# {"cut": t} or {"join": t} (epoch s; a cut with none: now).
|
||||
post "/api/trip/log/days" do
|
||||
require_login!
|
||||
body = JSON.parse(request.body.read)
|
||||
kind = body.key?("join") ? :join : :cut
|
||||
TRIP_LOG.mark(kind, Float(body[kind.to_s] || Time.now.to_f))
|
||||
json ok: true
|
||||
rescue JSON::ParserError, TypeError
|
||||
halt 400, json(error: "invalid JSON")
|
||||
rescue ArgumentError => e
|
||||
halt 422, json(error: e.message)
|
||||
end
|
||||
|
||||
# The recording as GPX: ?days=1,2 (those days), ?from=t&to=t (a piece),
|
||||
# neither: the whole trip — a track per day (?per_day=1: a .gpx per day in
|
||||
# a .zip), with ?stops=1 the trip's stops near the line, ?plan=1 the
|
||||
# planned route, ?pauses=1 the stands as waypoints.
|
||||
get "/api/trip/log/export" do
|
||||
require_login!
|
||||
rec = TRIP_LOG.recording
|
||||
halt 404, json(error: "no trip recorded") unless rec && rec[:points].size >= 2
|
||||
|
||||
points = rec[:points]
|
||||
days = TripDays.days(points, TripDays.cuts(points, hand: rec[:hand], joined: rec[:joined]))
|
||||
name = rec[:folder].to_s.delete_prefix("Trips/")
|
||||
label = ->(d) { "#{name} · Day #{d[:index]} · #{Time.at(d[:started_at]).strftime('%Y-%m-%d')}" }
|
||||
pieces = if params[:from] && params[:to]
|
||||
from, to = [params[:from], params[:to]].map(&:to_f).sort
|
||||
piece = points.select { |p| p[3].between?(from, to) }
|
||||
[["#{name} · #{Time.at(from).strftime('%d. %m. %H:%M')} – #{Time.at(to).strftime('%H:%M')}", piece]]
|
||||
else
|
||||
picked = params[:days].to_s.split(",").map(&:to_i)
|
||||
days.select { |d| picked.empty? || picked.include?(d[:index]) }.map { |d| [label.(d), points[d[:from]...d[:to]]] }
|
||||
end
|
||||
pieces = pieces.reject { |_, piece| piece.size < 2 }
|
||||
halt 422, json(error: "nothing to export") if pieces.empty?
|
||||
|
||||
folder = rec[:folder] ? (LIBRARY.folder_features(rec[:folder]) rescue []) : []
|
||||
extras = lambda do |piece|
|
||||
line = piece.map { |p| p.first(2) }
|
||||
out = []
|
||||
out += folder.select { |f| f.dig("properties", "kind") == "route" } if params[:plan]
|
||||
if params[:stops]
|
||||
out += folder.select { |f| f.dig("geometry", "type") == "Point" && Corridor.distance_km(f["geometry"]["coordinates"], line) <= 0.3 }
|
||||
end
|
||||
out += TripDays.pauses(piece) if params[:pauses]
|
||||
out
|
||||
end
|
||||
if params[:per_day] && pieces.size > 1
|
||||
content_type "application/zip"
|
||||
attachment "#{name}.zip"
|
||||
Zip::OutputStream.write_buffer do |zip|
|
||||
pieces.each do |title, piece|
|
||||
zip.put_next_entry("#{title.tr('/', '-')}.gpx")
|
||||
zip.write(TripDays.gpx([[title, piece]], extras.(piece)))
|
||||
end
|
||||
end.string
|
||||
else
|
||||
content_type "application/gpx+xml"
|
||||
attachment "#{pieces.size == 1 ? pieces.first.first.tr('/', '-') : name}.gpx"
|
||||
TripDays.gpx(pieces, extras.(pieces.flat_map(&:last)))
|
||||
end
|
||||
end
|
||||
|
||||
get "/api/trip" do
|
||||
require_login!
|
||||
trip = TRIP_STORE.get
|
||||
@@ -826,7 +912,8 @@ module Maptool
|
||||
halt 400, json(error: "points must be a list of {lngLat: [lon, lat]}") unless body[:points].is_a?(Array) && body[:points].all? { |p| p.is_a?(Hash) && p[:lngLat].is_a?(Array) }
|
||||
|
||||
json(TRIP_STORE.update(points: body[:points], vehicle: body[:vehicle], avoid: body[:avoid], editing: body[:editing],
|
||||
name: body[:name], start: body[:start], day_start: body[:day_start]) do |locations, vehicle, avoid|
|
||||
name: body[:name], start: body[:start], day_start: body[:day_start], note: body[:note],
|
||||
budget: body[:budget], fuel_price: body[:fuel_price]) do |locations, vehicle, avoid|
|
||||
Routing.route(VALHALLA_URL, locations, Routing.vehicle(DATA_DIR, vehicle), avoid.transform_keys(&:to_s))
|
||||
end)
|
||||
rescue JSON::ParserError
|
||||
@@ -927,6 +1014,17 @@ module Maptool
|
||||
center: center, kiwix_url: KIWIX_URL)
|
||||
end
|
||||
|
||||
# A link pasted into the search → a place (lib/maptool/link_place.rb).
|
||||
# The server fetches the page, so only for a logged-in user.
|
||||
get "/api/link" do
|
||||
require_login!
|
||||
place = LinkPlace.resolve(params[:url].to_s)
|
||||
halt 404, json(error: "no coordinates in that link or its page") unless place
|
||||
json place
|
||||
rescue LinkPlace::Error => e
|
||||
halt 422, json(error: e.message)
|
||||
end
|
||||
|
||||
# Pinged once per page load (public/js/map.js) — url is a same-origin
|
||||
# path (not kiwix-serve's own address, which the browser can't reach
|
||||
# directly behind the reverse proxy in front of this app), proxied by
|
||||
@@ -1262,32 +1360,65 @@ module Maptool
|
||||
# for the contract) — read-only, doesn't need require_login! the way
|
||||
# actually running one does below.
|
||||
get "/api/plugins" do
|
||||
json Plugins.list(PLUGINS_DIRS)
|
||||
saved = Plugins.saved_settings(PLUGIN_SETTINGS)
|
||||
json(Plugins.list(PLUGINS_DIRS).map { |p| p.merge("set" => (saved[p["name"]] || {}).keys) })
|
||||
end
|
||||
|
||||
# A plugin's settings (its plugin.json "settings": a login cookie, a key):
|
||||
# {KEY: value}, an empty value clears it. Kept in data/, never sent back.
|
||||
put "/api/plugins/:name/settings" do
|
||||
require_login!
|
||||
json set: Plugins.save_settings(PLUGIN_SETTINGS, PLUGINS_DIRS, params[:name], JSON.parse(request.body.read))
|
||||
rescue Plugins::RunError => e
|
||||
halt 404, json(error: e.message)
|
||||
rescue JSON::ParserError
|
||||
halt 400, json(error: "invalid JSON")
|
||||
end
|
||||
|
||||
# Runs one plugin for the given bbox (the current map viewport, from
|
||||
# the frontend's "Run for this area" button) and imports its GeoJSON
|
||||
# output into a folder of its own, named after the area's center and
|
||||
# the run's time — so one run's POIs stay apart from another's.
|
||||
#
|
||||
# route=1&corridor_km=N instead: along the shared trip's route, N km
|
||||
# either side (a plugin with "route": true covers just that; the points
|
||||
# further off are dropped either way), into the recorded trip's folder
|
||||
# when one is running.
|
||||
post "/api/plugins/:name/run" do
|
||||
require_login!
|
||||
bbox = %w[south west north east].map { |key| params[key].to_s }
|
||||
halt 400, json(error: "south, west, north, east are required") if bbox.any?(&:empty?)
|
||||
timestamp = Time.now.strftime("%y%m%d_%H%M")
|
||||
env = Plugins.saved_settings(PLUGIN_SETTINGS)[params[:name]] || {}
|
||||
if params[:route]
|
||||
coords = TRIP_STORE.get[:route]&.dig(:geometry, :coordinates)
|
||||
halt 422, json(error: "the trip has no route") unless coords && coords.size >= 2
|
||||
corridor = params[:corridor_km].to_f
|
||||
halt 400, json(error: "corridor_km must be 0.5 to 50") unless (0.5..50).cover?(corridor)
|
||||
|
||||
south, west, north, east = bbox.map(&:to_f)
|
||||
begin
|
||||
geojson = Plugins.run(PLUGINS_DIRS, params[:name], [south, west, north, east])
|
||||
rescue Plugins::RunError => e
|
||||
halt 502, json(error: e.message)
|
||||
geojson = Tempfile.create(["route", ".geojson"]) do |file|
|
||||
file.write(JSON.generate(type: "LineString", coordinates: coords))
|
||||
file.flush
|
||||
Plugins.run(PLUGINS_DIRS, params[:name], Corridor.bbox(coords, corridor), route: file.path, corridor_km: corridor, env: env)
|
||||
end
|
||||
geojson["features"] = geojson["features"].select do |f|
|
||||
f.dig("geometry", "type") != "Point" || Corridor.distance_km(f["geometry"]["coordinates"], coords) <= corridor
|
||||
end
|
||||
name = "#{params[:name]} along the route #{format('%g', corridor)} km #{timestamp}"
|
||||
folder = TRIP_LOG.status[:folder] || name
|
||||
else
|
||||
bbox = %w[south west north east].map { |key| params[key].to_s }
|
||||
halt 400, json(error: "south, west, north, east are required") if bbox.any?(&:empty?)
|
||||
|
||||
south, west, north, east = bbox.map(&:to_f)
|
||||
geojson = Plugins.run(PLUGINS_DIRS, params[:name], [south, west, north, east], env: env)
|
||||
center_lat = format("%.2f", (south + north) / 2)
|
||||
center_lon = format("%.2f", (west + east) / 2)
|
||||
folder = name = "#{params[:name]}_#{center_lat}_#{center_lon}_#{timestamp}"
|
||||
end
|
||||
|
||||
center_lat = format("%.2f", (south + north) / 2)
|
||||
center_lon = format("%.2f", (west + east) / 2)
|
||||
timestamp = Time.now.strftime("%y%m%d_%H%M")
|
||||
name = "#{params[:name]}_#{center_lat}_#{center_lon}_#{timestamp}"
|
||||
|
||||
keys = LIBRARY.import(folder: name, filename: "#{name}.geojson", io: StringIO.new(JSON.generate(geojson)))
|
||||
json path: keys.first, tree: LIBRARY.tree
|
||||
keys = LIBRARY.import(folder: folder, filename: "#{name}.geojson", io: StringIO.new(JSON.generate(geojson)))
|
||||
json path: keys.first, tree: LIBRARY.tree, count: geojson["features"].size
|
||||
rescue Plugins::RunError => e
|
||||
halt 502, json(error: e.message)
|
||||
end
|
||||
|
||||
# Called by the Apps Script watching Gmail for Garmin LiveTrack emails —
|
||||
@@ -1487,7 +1618,17 @@ module Maptool
|
||||
# {"trip": true}: along the shared trip's route (to try the navigation)
|
||||
route = TRIP_STORE.get[:route] if body["trip"]
|
||||
halt 422, json(error: "the trip has no route") if body["trip"] && !route
|
||||
if route then nil
|
||||
# {"track": "item:<id>"}: a library track replayed at its own speed
|
||||
if body["track"]
|
||||
begin
|
||||
line = JSON.parse(LIBRARY.geojson(body["track"].to_s))["features"].find { |f| f.dig("geometry", "type") == "LineString" }
|
||||
rescue Library::NotFound
|
||||
halt 404, json(error: "no such track")
|
||||
end
|
||||
track = line&.dig("properties", "profile")&.filter_map { |p| [p["lon"], p["lat"], p["elapsed_secs"]] if p["elapsed_secs"] }
|
||||
halt 422, json(error: "the track has no times") if track.nil? || track.size < 2
|
||||
end
|
||||
if route || track then nil
|
||||
elsif lon.nil?
|
||||
codes = (PmtilesBuilder.offline_meta(DATA_DIR) || {}).fetch("sources", []).map { |source| source["code"] }
|
||||
bboxes = codes.filter_map { |code| PmtilesBuilder.country_bbox(code) }
|
||||
@@ -1497,7 +1638,7 @@ module Maptool
|
||||
end
|
||||
|
||||
CAN_STORE.override_prefix = "gnss."
|
||||
GNSS_SIM.start(lon, lat, bboxes: bboxes, route: route) do |fix|
|
||||
GNSS_SIM.start(lon, lat, bboxes: bboxes, route: route, track: track) do |fix|
|
||||
CAN_STORE.put(fix.except(:time).transform_keys { |key| "gnss.#{key}" })
|
||||
App.gnss_update(fix)
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user