# frozen_string_literal: true require 'tacview_client/base_processor' require 'time' require_relative 'datastore' module TacScribe # Processes the events emitted by the Ruby Tacview Client class EventProcessor def initialize(datastore:, event_queue:) @datastore = datastore @event_queue = event_queue end def start loop do wrapped_event = @event_queue.events.shift process_event(wrapped_event) rescue StandardError => e puts wrapped_event puts e.inspect next end end def process_event(wrapped_event) case wrapped_event[:type] when :update_object update_object(wrapped_event[:event], wrapped_event[:time]) when :delete_object delete_object(wrapped_event[:object_id]) end end private # Process an update event for an object # # On the first appearance of the object there are usually more fields # including pilot names, object type etc. # # For existing objects these events are almost always lat/lon/alt updates # only # # @param event [Hash] A parsed ACMI line. This hash will always include # the fields listed below but may also include others depending on the # source line. # @option event [String] :object_id The hexadecimal format object ID. # @option event [BigDecimal] :latitude The object latitude in WGS 84 format. # @option event [BigDecimal] :longitude The object latitude in WGS 84 # format. # @option event [BigDecimal] :altitude The object altitude above sea level # in meters to 1 decimal place. def update_object(event, time) if @event_queue.reference_latitude || @event_queue.reference_longitude localize_position(event) end @datastore.write_object(event, time) end # Process a delete event for an object # # @param object_id [String] A hexadecimal number representing the object # ID def delete_object(object_id) @datastore.delete_object(object_id) end # If we have reference lat/long then use that as a base and update the event # position def localize_position(event) # There is no combination of layouts for these lines that doesn' trip # at least one rubocop cop so just ignore the guard clause. The best # one is using an inline if but that breaks the line length. # rubocop:disable Style/GuardClause if event[:latitude] event[:latitude] = @event_queue.reference_latitude + event[:latitude] end if event[:longitude] event[:longitude] = @event_queue.reference_longitude + event[:longitude] end # rubocop:enable Style/GuardClause end end end