Class: Artoo::Connection

Inherits:
Object
  • Object
show all
Includes:
Utility, Celluloid, Comparable
Defined in:
lib/artoo/connection.rb

Overview

The Connection class represents the interface to a specific group of hardware devices. Examples would be an Arduino, a Sphero, or an ARDrone.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#classify, #constantize, #current_class, #current_instance, #os, #random_string, #remove_keys, #underscore

Constructor Details

#initialize(params = {}) ⇒ Connection

Create new connection

Parameters:

  • params (Hash) (defaults to: {})

Options Hash (params):

  • :id (String)
  • :name (String)
  • :parent (String)
  • :adaptor (String)
  • :port (Integer)


21
22
23
24
25
26
27
28
29
# File 'lib/artoo/connection.rb', line 21

def initialize(params={})
  @connection_id = params[:id] || rand(10000).to_s
  @name = params[:name].to_s
  @port = Port.new(params[:port])
  @parent = params[:parent]
  @details = remove_keys(params, :name, :parent, :id, :loopback)

  require_adaptor(params[:adaptor] || :loopback, params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Redirects missing methods to adaptor, attemps reconnection if adaptor not connected



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/artoo/connection.rb', line 79

def method_missing(method_name, *arguments, &block)
  unless adaptor.connected?
    Logger.warn "Cannot call unconnected adaptor '#{name}', attempting to reconnect..."
    adaptor.reconnect
    return nil
  end
  adaptor.send(method_name, *arguments, &block)
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
  return nil
end

Instance Attribute Details

#adaptorObject (readonly)

Returns the value of attribute adaptor.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def adaptor
  @adaptor
end

#connection_idObject (readonly)

Returns the value of attribute connection_id.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def connection_id
  @connection_id
end

#detailsObject (readonly)

Returns the value of attribute details.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def details
  @details
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def parent
  @parent
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/artoo/connection.rb', line 12

def port
  @port
end

Instance Method Details

#adaptor_nameString

Returns Adaptor class name.

Returns:

  • (String)

    Adaptor class name



54
55
56
# File 'lib/artoo/connection.rb', line 54

def adaptor_name
  adaptor.class.name
end

#as_jsonJSON

Returns connection.

Returns:

  • (JSON)

    connection



68
69
70
# File 'lib/artoo/connection.rb', line 68

def as_json
  MultiJson.dump(to_hash)
end

#connectBoolean

Creates adaptor connection

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/artoo/connection.rb', line 33

def connect
  Logger.info "Connecting to '#{name}' on port '#{port}'..."
  adaptor.connect
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
end

#connected?Boolean

Returns Connection status.

Returns:

  • (Boolean)

    Connection status



49
50
51
# File 'lib/artoo/connection.rb', line 49

def connected?
  adaptor.connected?
end

#disconnectBoolean

Closes adaptor connection

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/artoo/connection.rb', line 43

def disconnect
  Logger.info "Disconnecting from '#{name}' on port '#{port}'..."
  adaptor.disconnect
end

#inspectString

Returns Formated connection.

Returns:

  • (String)

    Formated connection



73
74
75
# File 'lib/artoo/connection.rb', line 73

def inspect
  "#<Connection @id=#{object_id}, @name='#{name}', @adaptor=#{adaptor_name}>"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/artoo/connection.rb', line 92

def respond_to_missing?(method_name, include_private = false)
  # TODO: verify that the adaptor supprts the method we're calling
  true
end

#to_hashHash

Returns connection.

Returns:

  • (Hash)

    connection



59
60
61
62
63
64
65
# File 'lib/artoo/connection.rb', line 59

def to_hash
  {
    :name => name,
    :adaptor => adaptor_name.to_s.gsub(/^.*::/, ''),
    :details => @details
  }
end