Class: Artoo::Device

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

Overview

The Artoo::Device class represents the interface to a specific individual hardware devices. Examples would be a digital thermometer connected to an Arduino, or a Sphero's accelerometer.

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 = {}) ⇒ Device

Create new device

Parameters:

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

Options Hash (params):

  • :name (String)
  • :pin (String)
  • :parent (String)
  • :connection (String)
  • :interval (String)
  • :driver (String)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/artoo/device.rb', line 19

def initialize(params={})
  @name       = params[:name].to_s
  @pin        = params[:pin]
  @parent     = params[:parent]
  @connection = determine_connection(params[:connection]) ||
    default_connection
  @interval   = params[:interval] || 0.5

  @details = remove_keys(params, :name, :parent, :connection,
                         :passthru, :driver)

  require_driver(params[:driver] || :passthru, params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Sends missing methods to command



89
90
91
# File 'lib/artoo/device.rb', line 89

def method_missing(method_name, *arguments, &block)
  command(method_name, *arguments, &block)
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#detailsObject (readonly)

Returns the value of attribute details.



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

def details
  @details
end

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
end

#interfaceObject (readonly)

Returns the value of attribute interface.



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

def interface
  @interface
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#pinObject (readonly)

Returns the value of attribute pin.



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

def pin
  @pin
end

Instance Method Details

#add_interface(i) ⇒ Object



102
103
104
# File 'lib/artoo/device.rb', line 102

def add_interface(i)
  @parent.add_interface(i)  
end

#as_jsonJSON

Returns device.

Returns:

  • (JSON)

    device



74
75
76
# File 'lib/artoo/device.rb', line 74

def as_json
  MultiJson.dump(to_hash)
end

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

Execute driver command



84
85
86
# File 'lib/artoo/device.rb', line 84

def command(method_name, *arguments, &block)
  driver.command(method_name, *arguments)
end

#commandsCollection

Returns commands.

Returns:

  • (Collection)

    commands



79
80
81
# File 'lib/artoo/device.rb', line 79

def commands
  driver.commands
end

#default_connectionConnection

Returns default connection.

Returns:



40
41
42
# File 'lib/artoo/device.rb', line 40

def default_connection
  parent.default_connection
end

#determine_connection(c) ⇒ Object

Retrieve connections from parent

Parameters:

  • c (String)

    connection



35
36
37
# File 'lib/artoo/device.rb', line 35

def determine_connection(c)
  parent.connections[c] unless c.nil?
end

#event_topic_name(event) ⇒ String

Returns event topic name.

Returns:

  • (String)

    event topic name



50
51
52
# File 'lib/artoo/device.rb', line 50

def event_topic_name(event)
  "#{parent.safe_name}_#{name}_#{event}"
end

#inspectString

Returns pretty inspect.

Returns:

  • (String)

    pretty inspect



98
99
100
# File 'lib/artoo/device.rb', line 98

def inspect
  "#<Device @id=#{object_id}, @name='name', @driver='#{driver.class.name}'>"
end

#publish(event, *data) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/artoo/device.rb', line 54

def publish(event, *data)
  if data.first
    driver.publish(event_topic_name(event), *data)
  else
    driver.publish(event_topic_name(event))
  end
end

#require_interface(i) ⇒ Object



106
107
108
109
110
111
# File 'lib/artoo/device.rb', line 106

def require_interface(i)
  Logger.info "Require interface #{i}"
  require "artoo/interfaces/#{i.to_s}"
  @interface = constantize("Artoo::Interfaces::#{classify(i.to_s)}").new(:name => i.to_s, :robot => parent, :device => current_instance)      
  add_interface(@interface)
end

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

Returns:

  • (Boolean)


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

def respond_to_missing?(method_name, include_private = false)
  commands.include?(method_name) || super
end

#start_deviceObject

Starts device driver



45
46
47
# File 'lib/artoo/device.rb', line 45

def start_device
  driver.start_driver
end

#to_hashHash

Returns device.

Returns:

  • (Hash)

    device



63
64
65
66
67
68
69
70
71
# File 'lib/artoo/device.rb', line 63

def to_hash
  {
    :name => name,
    :driver => driver.class.name.to_s.gsub(/^.*::/, ''),
    :connection => connection.name,
    :commands => driver.commands,
    :details => @details
  }
end