Class: Artoo::Connection
- Inherits:
-
Object
- Object
- Artoo::Connection
- 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
-
#adaptor ⇒ Object
readonly
Returns the value of attribute adaptor.
-
#connection_id ⇒ Object
readonly
Returns the value of attribute connection_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #adaptor_name ⇒ Object
- #as_json ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
-
#initialize(params = {}) ⇒ Connection
constructor
A new instance of Connection.
- #inspect ⇒ Object
- #method_missing(method_name, *arguments, &block) ⇒ Object
- #to_hash ⇒ Object
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #random_string
Methods included from Celluloid
Constructor Details
#initialize(params = {}) ⇒ Connection
Returns a new instance of Connection.
14 15 16 17 18 19 20 21 |
# File 'lib/artoo/connection.rb', line 14 def initialize(params={}) @connection_id = rand(10000) @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
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/artoo/connection.rb', line 61 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. Logger.error e.backtrace.inspect return nil end |
Instance Attribute Details
#adaptor ⇒ Object (readonly)
Returns the value of attribute adaptor.
12 13 14 |
# File 'lib/artoo/connection.rb', line 12 def adaptor @adaptor end |
#connection_id ⇒ Object (readonly)
Returns the value of attribute connection_id.
12 13 14 |
# File 'lib/artoo/connection.rb', line 12 def connection_id @connection_id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/artoo/connection.rb', line 12 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/artoo/connection.rb', line 12 def parent @parent end |
#port ⇒ Object (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_name ⇒ Object
40 41 42 |
# File 'lib/artoo/connection.rb', line 40 def adaptor_name adaptor.class.name end |
#as_json ⇒ Object
53 54 55 |
# File 'lib/artoo/connection.rb', line 53 def as_json MultiJson.dump(to_hash) end |
#connect ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/artoo/connection.rb', line 23 def connect Logger.info "Connecting to '#{name}' on port '#{port}'..." adaptor.connect rescue Exception => e Logger.error e. Logger.error e.backtrace.inspect end |
#connected? ⇒ Boolean
36 37 38 |
# File 'lib/artoo/connection.rb', line 36 def connected? adaptor.connected? end |
#disconnect ⇒ Object
31 32 33 34 |
# File 'lib/artoo/connection.rb', line 31 def disconnect Logger.info "Disconnecting from '#{name}' on port '#{port}'..." adaptor.disconnect end |
#inspect ⇒ Object
57 58 59 |
# File 'lib/artoo/connection.rb', line 57 def inspect "#<Connection @id=#{object_id}, @name='#{name}', @adaptor=#{adaptor_name}>" end |
#to_hash ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/artoo/connection.rb', line 44 def to_hash {:name => name, :connection_id => connection_id, :port => port.to_s, :adaptor => adaptor_name.demodulize, :connected => connected? } end |