Sha256: 1923846e96c96a4cacc00d58e86a0620403407ed2258d23e17589fa746913bdb

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tg-can-controls-gateway-0.1.3 lib/dispatcher.rb