Trip log: a trip is a library folder (Trips/<date> <name>) with the planned route and its stops; the car's GNSS recorded into its track there (10 m steps, written every minute and at the end), GET/POST/DELETE /api/trip/log; the Trip menu's Start / End and the line really driven (--route-driven); along-route POI mode for the plugins in TODO
This commit is contained in:
1 parent
809e88ab1a
commit
7d72e9eb63
7 files changed
+279
-1
No files matched your search
@@ -0,0 +1,124 @@
|
||||
require "json"
|
||||
require "time"
|
||||
require_relative "track_stats"
|
||||
require_relative "gnss_sim"
|
||||
|
||||
module Maptool
|
||||
# The trip being driven, recorded (docs: TODO "The trip as plan + log"):
|
||||
# a trip is a library folder, Trips/<date> <name>. Start makes it — with
|
||||
# the planned route and its named stops in it — and from then on every
|
||||
# GNSS fix (App.gnss_update) that moved MIN_STEP_M goes into the car's
|
||||
# track there, written into the library every FLUSH_EVERY_S (a power cut
|
||||
# loses that much at most) and at the end. A walk away from the car comes
|
||||
# from another device's track put into the same folder (a FIT, LiveTrack).
|
||||
class TripLog
|
||||
MIN_STEP_M = 10
|
||||
FLUSH_EVERY_S = 60
|
||||
ROOT = "Trips".freeze
|
||||
|
||||
def initialize(path, library:)
|
||||
@path = path
|
||||
@library = library
|
||||
@mutex = Mutex.new
|
||||
@state = begin
|
||||
JSON.parse(File.read(path), symbolize_names: true)
|
||||
rescue Errno::ENOENT, JSON::ParserError
|
||||
{}
|
||||
end
|
||||
@flushed_at = 0
|
||||
end
|
||||
|
||||
def active?
|
||||
@mutex.synchronize { !!@state[:folder] }
|
||||
end
|
||||
|
||||
# { active:, folder:, started_at:, points:, distance_m: } — coordinates
|
||||
# from index `since` on with `since:` (the map draws the line as it grows).
|
||||
def status(since: nil)
|
||||
@mutex.synchronize do
|
||||
points = @state[:points] || []
|
||||
result = { active: !!@state[:folder], folder: @state[:folder], started_at: @state[:started_at], points: points.size,
|
||||
distance_m: TrackStats.compute(points.map { |p| p.first(3) })["distance_meters"] }
|
||||
result[:coordinates] = points[since.to_i..].to_a.map { |p| p.first(2) } if since
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
# plan: the trip's route Feature (or nil) and its points, kept in the
|
||||
# folder as a route and points. Returns the folder.
|
||||
def start(name, plan: nil, stops: [], now: Time.now)
|
||||
@mutex.synchronize do
|
||||
raise ArgumentError, "a trip is already running (#{@state[:folder]})" if @state[:folder]
|
||||
|
||||
folder = "#{ROOT}/#{[now.strftime('%Y-%m-%d'), name.to_s.strip].reject(&:empty?).join(' ')}"
|
||||
@library.create_folder(folder)
|
||||
features = []
|
||||
if plan
|
||||
props = (plan[:properties] || {}).slice(:distance_km, :time_s, :costing).transform_keys(&:to_s)
|
||||
features << { "type" => "Feature", "geometry" => JSON.parse(JSON.generate(plan[:geometry])),
|
||||
"properties" => props.merge("kind" => "route", "name" => "Plan") }
|
||||
end
|
||||
stops.select { |s| s[:name] && s[:name] != "My position" }.each do |s|
|
||||
features << { "type" => "Feature", "geometry" => { "type" => "Point", "coordinates" => s[:lngLat] }, "properties" => { "name" => s[:name] } }
|
||||
end
|
||||
@library.import_features(features, folder: folder) unless features.empty?
|
||||
@state = { folder: folder, started_at: now.to_i, points: [], item: nil }
|
||||
save
|
||||
folder
|
||||
end
|
||||
end
|
||||
|
||||
# A fix: [lon, lat], altitude (m, or nil), at (epoch seconds).
|
||||
def record(lon, lat, alt, at)
|
||||
@mutex.synchronize do
|
||||
return unless @state[:folder]
|
||||
|
||||
last = @state[:points].last
|
||||
return if last && GnssSim.meters(last, [lon, lat]) < MIN_STEP_M
|
||||
|
||||
@state[:points] << [lon, lat, alt, at.to_f.round(1)]
|
||||
flush if at.to_f - @flushed_at >= FLUSH_EVERY_S
|
||||
end
|
||||
end
|
||||
|
||||
# Ends it: the track written a last time. Returns the folder.
|
||||
def stop
|
||||
@mutex.synchronize do
|
||||
folder = @state[:folder] or return nil
|
||||
flush
|
||||
@state = {}
|
||||
save
|
||||
folder
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# The car's track into the library (made on the first flush, replaced
|
||||
# after), the state file with it.
|
||||
def flush
|
||||
points = @state[:points]
|
||||
if points.size >= 2
|
||||
coordinates = points.map { |lon, lat, alt| alt ? [lon, lat, alt] : [lon, lat] }
|
||||
times = points.map { |p| Time.at(p[3]) }
|
||||
name = "Car #{Time.at(@state[:started_at]).strftime('%Y-%m-%d %H:%M')}"
|
||||
feature = { "type" => "Feature", "geometry" => { "type" => "LineString", "coordinates" => coordinates },
|
||||
"properties" => { "kind" => "track", "name" => name, "activity_type" => "driving" }
|
||||
.merge(TrackStats.compute(coordinates, times: times))
|
||||
.merge("profile" => TrackStats.build_profile(coordinates, times: times)) }
|
||||
if @state[:item]
|
||||
@library.update_feature(@state[:item], @state[:item].delete_prefix("item:"), feature)
|
||||
else
|
||||
@state[:item] = @library.import_features([feature], folder: @state[:folder]).find { |key| key.start_with?("item:") }
|
||||
end
|
||||
end
|
||||
@flushed_at = points.last ? points.last[3] : 0
|
||||
save
|
||||
end
|
||||
|
||||
def save
|
||||
File.write("#{@path}.tmp", JSON.generate(@state))
|
||||
File.rename("#{@path}.tmp", @path)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user