Class: Artoo::Connection
- Inherits:
-
Object
- Object
- Artoo::Connection
- 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
-
#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 ⇒ String
Adaptor class name.
-
#as_json ⇒ JSON
Connection.
-
#connect ⇒ Boolean
Creates adaptor connection.
-
#connected? ⇒ Boolean
Connection status.
-
#disconnect ⇒ Boolean
Closes adaptor connection.
-
#initialize(params = {}) ⇒ Connection
constructor
Create new connection.
-
#inspect ⇒ String
Formated connection.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Redirects missing methods to adaptor, attemps reconnection if adaptor not connected.
-
#to_hash ⇒ Hash
Connection.
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #random_string, #underscore
Constructor Details
#initialize(params = {}) ⇒ Connection
Create new connection
20 21 22 23 24 25 26 27 |
# File 'lib/artoo/connection.rb', line 20 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
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. 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 ⇒ String
Returns Adaptor class name.
52 53 54 |
# File 'lib/artoo/connection.rb', line 52 def adaptor_name adaptor.class.name end |
#as_json ⇒ JSON
Returns connection.
68 69 70 |
# File 'lib/artoo/connection.rb', line 68 def as_json MultiJson.dump(to_hash) end |
#connect ⇒ Boolean
Creates adaptor connection
31 32 33 34 35 36 37 |
# File 'lib/artoo/connection.rb', line 31 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
Returns Connection status.
47 48 49 |
# File 'lib/artoo/connection.rb', line 47 def connected? adaptor.connected? end |
#disconnect ⇒ Boolean
Closes adaptor connection
41 42 43 44 |
# File 'lib/artoo/connection.rb', line 41 def disconnect Logger.info "Disconnecting from '#{name}' on port '#{port}'..." adaptor.disconnect end |
#inspect ⇒ String
Returns Formated connection.
73 74 75 |
# File 'lib/artoo/connection.rb', line 73 def inspect "#<Connection @id=#{object_id}, @name='#{name}', @adaptor=#{adaptor_name}>" end |
#to_hash ⇒ Hash
Returns connection.
57 58 59 60 61 62 63 64 65 |
# File 'lib/artoo/connection.rb', line 57 def to_hash { :name => name, :connection_id => connection_id, :port => port.to_s, :adaptor => adaptor_name.to_s.gsub(/^.*::/, ''), :connected => connected? } end |