Shared trip: one itinerary per server (data/trip.json, GET/PUT /api/trip, routed server-side), the route panel edits it when logged in and polls other screens' changes in without moving the camera; the GNSS simulation skips trips under 1 km and starts elsewhere when it can't get on from a road island; the car's server updates itself from a topbar button (SELF_UPDATE=1: newer main with CI passed → bin/update-maptool.sh via maptool-update.path); Android Auto and the trip's next steps in TODO
This commit is contained in:
1 parent
5f4c7c6ee8
commit
17891c53b5
16 files changed
+513
-5
No files matched your search
+14
-3
@@ -16,8 +16,10 @@ module Maptool
|
||||
|
||||
attr_reader :error
|
||||
|
||||
def initialize(valhalla_url, vehicle: { id: "car" }, random: Random.new, sleep: ->(s) { Kernel.sleep(s) })
|
||||
def initialize(valhalla_url, vehicle: { id: "car" }, random: Random.new, sleep: ->(s) { Kernel.sleep(s) },
|
||||
router: Routing.method(:route))
|
||||
@valhalla_url = valhalla_url
|
||||
@router = router
|
||||
@vehicle = vehicle
|
||||
@random = random
|
||||
@sleep = sleep
|
||||
@@ -30,6 +32,9 @@ module Maptool
|
||||
end
|
||||
|
||||
RANDOM_START_TRIES = 20 # a random point at sea / off the routing tiles: another one
|
||||
# Shorter: a road island the start snapped to (a lake's middle → a 0 km
|
||||
# route), it'd drive the same few meters again and again — another try.
|
||||
MIN_TRIP_KM = 1.0
|
||||
|
||||
# Drives from [lon, lat] until stopped — or, with bboxes
|
||||
# ([[west, south, east, north], ...], the offline countries) instead,
|
||||
@@ -44,6 +49,10 @@ module Maptool
|
||||
loop do
|
||||
position = drive(position, route, &on_fix)
|
||||
route = nil
|
||||
rescue Routing::RouteError
|
||||
raise unless bboxes
|
||||
|
||||
position, route = random_start(bboxes) # no way on from here: somewhere else
|
||||
end
|
||||
rescue StandardError => e
|
||||
@error = e.message
|
||||
@@ -104,8 +113,10 @@ module Maptool
|
||||
distance = @random.rand(DEST_KM) * 1000
|
||||
lon, lat = position
|
||||
dest = [lon + distance * Math.sin(bearing) / (111_320 * Math.cos(lat * Math::PI / 180)), lat + distance * Math.cos(bearing) / 111_320]
|
||||
route = Routing.route(@valhalla_url, [position, dest], @vehicle)
|
||||
return route if route[:geometry][:coordinates].size >= 2
|
||||
route = @router.call(@valhalla_url, [position, dest], @vehicle)
|
||||
return route if route[:geometry][:coordinates].size >= 2 && route[:properties][:distance_km].to_f >= MIN_TRIP_KM
|
||||
|
||||
last = Routing::RouteError.new("only #{route[:properties][:distance_km]} km")
|
||||
rescue Routing::RouteError => e
|
||||
last = e
|
||||
end
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
require "json"
|
||||
require_relative "routing"
|
||||
|
||||
module Maptool
|
||||
# The trip being driven, one for every screen of this server (the cab's
|
||||
# display, the tablet in the back, the phone): its points, vehicle and
|
||||
# avoids, and the route through them. Any screen edits it (PUT
|
||||
# /api/trip), the server routes it again and bumps the version; every
|
||||
# screen polls GET /api/trip and swaps the route in without touching its
|
||||
# camera. Kept in data/trip.json across restarts.
|
||||
class TripStore
|
||||
EMPTY = { version: 0, points: [], vehicle: nil, avoid: {}, editing: nil, route: nil, error: nil }.freeze
|
||||
|
||||
def initialize(path)
|
||||
@path = path
|
||||
@mutex = Mutex.new
|
||||
@trip = begin
|
||||
EMPTY.merge(JSON.parse(File.read(path), symbolize_names: true))
|
||||
rescue Errno::ENOENT, JSON::ParserError
|
||||
EMPTY.dup
|
||||
end
|
||||
end
|
||||
|
||||
def get
|
||||
@mutex.synchronize { @trip.dup }
|
||||
end
|
||||
|
||||
# points: [{ lngLat: [lon, lat], name:, nav: }]. Routes through them with
|
||||
# the block (locations, vehicle id, avoid → the /api/route Feature, may
|
||||
# raise Routing errors) unless only names/nav changed — the old route
|
||||
# stays then. Last write wins.
|
||||
def update(points:, vehicle:, avoid:, editing:)
|
||||
points = points.map { |p| { lngLat: p.fetch(:lngLat).map { |v| Float(v) }, name: p[:name], nav: !!p[:nav] } }
|
||||
avoid = (avoid || {}).to_h { |k, v| [k.to_sym, !!v] }
|
||||
before = get
|
||||
same_route = before[:points].map { |p| p[:lngLat] } == points.map { |p| p[:lngLat] } &&
|
||||
before[:vehicle] == vehicle && before[:avoid] == avoid && !before[:error]
|
||||
route, error = if same_route then [before[:route], nil]
|
||||
elsif points.size < 2 then [nil, nil]
|
||||
else
|
||||
begin
|
||||
[yield(points.map { |p| p[:lngLat] }, vehicle, avoid), nil]
|
||||
rescue Routing::RouteError, Routing::Unavailable => e
|
||||
[nil, e.message]
|
||||
end
|
||||
end
|
||||
@mutex.synchronize do
|
||||
@trip = { version: @trip[:version] + 1, points: points, vehicle: vehicle, avoid: avoid, editing: editing,
|
||||
route: route, error: error, updated_at: Time.now.to_f }
|
||||
File.write("#{@path}.tmp", JSON.generate(@trip))
|
||||
File.rename("#{@path}.tmp", @path)
|
||||
@trip.dup
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,58 @@
|
||||
require "json"
|
||||
require "fileutils"
|
||||
require "net/http"
|
||||
|
||||
module Maptool
|
||||
# Self-update of the car's server (SELF_UPDATE=1; brick deploys by CI):
|
||||
# is there a newer commit on main that passed CI (Gitea's API, the repo
|
||||
# is public), and asking the host to install it — data/update/request.json,
|
||||
# picked up by maptool-update.path → bin/update-maptool.sh, which reports
|
||||
# back in data/update/status.json.
|
||||
module Updater
|
||||
CHECK_EVERY_S = 600
|
||||
MUTEX = Mutex.new
|
||||
|
||||
def self.dir(data_dir)
|
||||
File.join(data_dir, "update")
|
||||
end
|
||||
|
||||
# { sha:, ci: "success" | "pending" | "failure" | … } of main, cached
|
||||
# CHECK_EVERY_S; nil while Gitea can't be reached (no internet on the road).
|
||||
def self.latest(gitea_url, repo, fetch: method(:get_json))
|
||||
MUTEX.synchronize do
|
||||
return @latest if @checked_at && Time.now.to_f - @checked_at < CHECK_EVERY_S
|
||||
|
||||
@checked_at = Time.now.to_f
|
||||
sha = fetch.call("#{gitea_url}/api/v1/repos/#{repo}/branches/main").dig("commit", "id")
|
||||
@latest = { sha: sha, ci: fetch.call("#{gitea_url}/api/v1/repos/#{repo}/commits/#{sha}/status")["state"] }
|
||||
rescue StandardError
|
||||
@latest = nil
|
||||
end
|
||||
end
|
||||
|
||||
def self.reset!
|
||||
MUTEX.synchronize { @checked_at = @latest = nil }
|
||||
end
|
||||
|
||||
def self.request(data_dir, sha)
|
||||
raise ArgumentError, "not a commit" unless sha.to_s.match?(/\A[0-9a-f]{40}\z/)
|
||||
|
||||
FileUtils.mkdir_p(dir(data_dir))
|
||||
File.write(File.join(dir(data_dir), "request.json"), JSON.generate(sha: sha, at: Time.now.to_i))
|
||||
end
|
||||
|
||||
def self.status(data_dir)
|
||||
JSON.parse(File.read(File.join(dir(data_dir), "status.json")), symbolize_names: true)
|
||||
rescue Errno::ENOENT, JSON::ParserError
|
||||
nil
|
||||
end
|
||||
|
||||
def self.get_json(url)
|
||||
uri = URI(url)
|
||||
response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: 5, read_timeout: 10) { |http| http.get(uri.request_uri) }
|
||||
raise "HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
||||
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user