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

Methods included from Celluloid

#timers

Constructor Details

#initialize(params = {}) ⇒ Device

Returns a new instance of Device.



11
12
13
14
15
16
17
18
19
# File 'lib/artoo/device.rb', line 11

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)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(method_name, *arguments, &block)
  driver.send(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_jsonObject



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

def as_json
  MultiJson.dump(to_hash)
end

#default_connectionObject



25
26
27
# File 'lib/artoo/device.rb', line 25

def default_connection
  parent.default_connection
end

#determine_connection(c) ⇒ Object



21
22
23
# File 'lib/artoo/device.rb', line 21

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

#event_topic_name(event) ⇒ Object



33
34
35
# File 'lib/artoo/device.rb', line 33

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

#inspectObject



54
55
56
# File 'lib/artoo/device.rb', line 54

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

#start_deviceObject



29
30
31
# File 'lib/artoo/device.rb', line 29

def start_device
  driver.start_driver
end

#to_hashObject



37
38
39
40
41
42
43
44
# File 'lib/artoo/device.rb', line 37

def to_hash
  {:name => name,
   :driver => driver.class.name.demodulize,
   :pin => pin.to_s,
   :connection => connection.to_hash,
   :interval => interval
  }
end