require 'rexml/document' require 'rexml/xpath' ## require 'osc_package' require 'rca_command' require 'raw_event' module CanControlsGateway class Dispatcher attr_reader :raw_data, :events, :station_id def initialize data # TODO remove REXML usage - REXML is horribly slow @raw_data = REXML::Document.new("#{data}") @events = get_raw_events @station_id = REXML::XPath.first(@raw_data, "/trackingdata/attribute::station_id") @station_id = @station_id.value if @station_id end def dispatch_rca my_station = STATIONS[@station_id] # Do we know this station? if my_station @events.each do |event| # If it is a RCA mappable event send it as RCA command... if EVENTS[event.event_type] if my_station['rci_uri'] my_args = { 'action' => EVENTS[event.event_type] } Ccg.logger.info "Dispatcher: sending command : rci_uri: #{my_station['rci_uri']}, target: #{my_station['target']}, args: #{my_args.inspect}" my_command = CanControlsGateway::RcaCommand.new my_station['rci_uri'], my_station['target'], my_args Ccg.logger.info my_command.send_command else Ccg.logger.info "Device '#{my_station['device']}' not properly initialized in gom? (sender: station #{station})" end else # ???? end end else # ???? end rescue => e "ERROR: #{e.backtrace.join "\n\t"}" end private def get_raw_events my_events = [] my_event_nodes = REXML::XPath.each(@raw_data, "/trackingdata/event") { |event| my_events << CanControlsGateway::RawEvent.new(event) } my_events end end end