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| ## #CcgLogger::LOGGER.info "#{Time.now.iso8601} | Dispatcher: rca dispatching: event_type: #{event.event_type}" ## # 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] } CcgLogger::LOGGER.info "#{Time.now.iso8601} | >> 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 CcgLogger::LOGGER.info "#{Time.now.iso8601} #{my_command.send_command}" else CcgLogger::LOGGER.info "#{Time.now.iso8601} | Device '#{my_station['device']}' not properly initialized in gom? (sender: station #{station})" end else ## #CcgLogger::LOGGER.info "#{Time.now.iso8601} | event '#{event.event_type}' not mappable to RCA command..." ## end end else #CcgLogger::LOGGER.info "#{Time.now.iso8601} | unknown station_id '#{@station_id.inspect}'" end rescue => e "ERROR: #{e.backtrace.join "\n\t"}" end #def dispatch_osc # @events.each do |event| # if event.event_type && @station_id # ## # #CcgLogger::LOGGER.info "#{Time.now.iso8601} | Dispatcher: osc dispatching: event_type: #{event.event_type} - vector: #{event.vector}" # ## # my_osc_package = CanControlsGateway::OscPackage.new event, @station_id # my_ret = my_osc_package.broadcast # nil # end # end #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