dawarich/app/services/overland/params.rb

39 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2024-04-06 13:09:38 -04:00
# frozen_string_literal: true
class Overland::Params
attr_reader :data, :points
def initialize(json)
@data = json.with_indifferent_access
@points = @data[:locations]
end
def call
points.map do |point|
2024-07-08 17:56:08 -04:00
next if point[:geometry].nil? || point.dig(:properties, :timestamp).nil?
2024-04-06 13:09:38 -04:00
{
2025-02-22 16:37:21 -05:00
lonlat: "POINT(#{point[:geometry][:coordinates][0]} #{point[:geometry][:coordinates][1]})",
2024-04-06 13:09:38 -04:00
battery_status: point[:properties][:battery_state],
battery: battery_level(point[:properties][:battery_level]),
timestamp: DateTime.parse(point[:properties][:timestamp]),
altitude: point[:properties][:altitude],
velocity: point[:properties][:speed],
tracker_id: point[:properties][:device_id],
ssid: point[:properties][:wifi],
accuracy: point[:properties][:horizontal_accuracy],
vertical_accuracy: point[:properties][:vertical_accuracy],
raw_data: point
}
2024-07-08 17:56:08 -04:00
end.compact
2024-04-06 13:09:38 -04:00
end
private
def battery_level(level)
value = (level.to_f * 100).to_i
value.positive? ? value : nil
end
end