Class: Artoo::Adaptors::Sphero

Inherits:
Adaptor
  • Object
show all
Defined in:
lib/artoo/adaptors/sphero.rb

Overview

Connect to a Sphero device

Constant Summary collapse

RETRY_COUNT =

Number of retries when connecting

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Uses method missing to call sphero actions



46
47
48
# File 'lib/artoo/adaptors/sphero.rb', line 46

def method_missing(method_name, *arguments, &block)
  sphero.send(method_name, *arguments, &block)
end

Instance Attribute Details

#spheroObject (readonly)

Returns the value of attribute sphero.



9
10
11
# File 'lib/artoo/adaptors/sphero.rb', line 9

def sphero
  @sphero
end

Instance Method Details

#connectBoolean

Creates a connection with Sphero object with retries

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/artoo/adaptors/sphero.rb', line 16

def connect
  @retries_left = RETRY_COUNT
  require 'sphero' unless defined?(::Sphero)
  begin
    @sphero = ::Sphero.new(connect_to)
    super
    return true
  rescue Errno::EBUSY, Errno::ECONNREFUSED => e
    @retries_left -= 1
    if @retries_left > 0
      retry
    else
      Logger.error e.message
      Logger.error e.backtrace.inspect
      return false
    end
  end
end

#disconnectBoolean

Closes connection with device

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/artoo/adaptors/sphero.rb', line 37

def disconnect
  Logger.info "Stopping and disconnecting sphero"
  sphero.close if connected?

  super
end