Class: Artoo::Connection
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, #random_string
Methods included from Celluloid
#timers
Constructor Details
#initialize(params = {}) ⇒ Connection
13
14
15
16
17
18
19
|
# File 'lib/artoo/connection.rb', line 13
def initialize(params={})
@name = params[:name].to_s
@port = Port.new(params[:port])
@parent = params[:parent]
require_adaptor(params[:adaptor] || :loopback)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/artoo/connection.rb', line 58
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
#adaptor ⇒ Object
Returns the value of attribute adaptor.
11
12
13
|
# File 'lib/artoo/connection.rb', line 11
def adaptor
@adaptor
end
|
#name ⇒ Object
Returns the value of attribute name.
11
12
13
|
# File 'lib/artoo/connection.rb', line 11
def name
@name
end
|
#parent ⇒ Object
Returns the value of attribute parent.
11
12
13
|
# File 'lib/artoo/connection.rb', line 11
def parent
@parent
end
|
#port ⇒ Object
Returns the value of attribute port.
11
12
13
|
# File 'lib/artoo/connection.rb', line 11
def port
@port
end
|
Instance Method Details
#as_json ⇒ Object
46
47
48
|
# File 'lib/artoo/connection.rb', line 46
def as_json
MultiJson.dump(to_hash)
end
|
#connect ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/artoo/connection.rb', line 21
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
34
35
36
|
# File 'lib/artoo/connection.rb', line 34
def connected?
adaptor.connected?
end
|
#disconnect ⇒ Object
29
30
31
32
|
# File 'lib/artoo/connection.rb', line 29
def disconnect
Logger.info "Disconnecting from '#{name}' on port '#{port}'..."
adaptor.disconnect
end
|
#inspect ⇒ Object
54
55
56
|
# File 'lib/artoo/connection.rb', line 54
def inspect
"#<#{to_s}>"
end
|
#to_hash ⇒ Object
38
39
40
41
42
43
44
|
# File 'lib/artoo/connection.rb', line 38
def to_hash
{:name => name,
:port => port.to_s,
:adaptor => adaptor.class.name.demodulize,
:connected => connected?
}
end
|
#to_s ⇒ Object
50
51
52
|
# File 'lib/artoo/connection.rb', line 50
def to_s
"#{self.class}:0x#{self.object_id}"
end
|