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, #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
# 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

  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



86
87
88
# File 'lib/artoo/device.rb', line 86

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

#driverObject (readonly)

Returns the value of attribute driver.



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

def driver
  @driver
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

#as_jsonJSON

Returns device.

Returns:

  • (JSON)

    device



71
72
73
# File 'lib/artoo/device.rb', line 71

def as_json
  MultiJson.dump(to_hash)
end

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

Execute driver command



81
82
83
# File 'lib/artoo/device.rb', line 81

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

#commandsCollection

Returns commands.

Returns:

  • (Collection)

    commands



76
77
78
# File 'lib/artoo/device.rb', line 76

def commands
  driver.commands
end

#default_connectionConnection

Returns default connection.

Returns:



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

def default_connection
  parent.default_connection
end

#determine_connection(c) ⇒ Object

Retrieve connections from parent

Parameters:

  • c (String)

    connection



31
32
33
# File 'lib/artoo/device.rb', line 31

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



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

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

#inspectString

Returns pretty inspect.

Returns:

  • (String)

    pretty inspect



91
92
93
# File 'lib/artoo/device.rb', line 91

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

#publish(event, *data) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/artoo/device.rb', line 50

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

#start_deviceObject

Starts device driver



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

def start_device
  driver.start_driver
end

#to_hashHash

Returns device.

Returns:

  • (Hash)

    device



59
60
61
62
63
64
65
66
67
68
# File 'lib/artoo/device.rb', line 59

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