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
@@ -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
|
||||
Reference in new issue
Block a user