Class: Artoo::Device
- Inherits:
-
Object
- Object
- Artoo::Device
- 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
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#pin ⇒ Object
readonly
Returns the value of attribute pin.
Instance Method Summary collapse
-
#as_json ⇒ JSON
Device.
-
#command(method_name, *arguments, &block) ⇒ Object
Execute driver command.
-
#commands ⇒ Collection
Commands.
-
#default_connection ⇒ Connection
Default connection.
-
#determine_connection(c) ⇒ Object
Retrieve connections from parent.
-
#event_topic_name(event) ⇒ String
Event topic name.
-
#initialize(params = {}) ⇒ Device
constructor
Create new device.
-
#inspect ⇒ String
Pretty inspect.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Sends missing methods to command.
-
#start_device ⇒ Object
Starts device driver.
-
#to_hash ⇒ Hash
Device.
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #random_string, #underscore
Constructor Details
#initialize(params = {}) ⇒ Device
Create new device
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) 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
78 79 80 |
# File 'lib/artoo/device.rb', line 78 def method_missing(method_name, *arguments, &block) command(method_name, *arguments, &block) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def connection @connection end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def driver @driver end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def interval @interval end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def parent @parent end |
#pin ⇒ Object (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_json ⇒ JSON
Returns device.
63 64 65 |
# File 'lib/artoo/device.rb', line 63 def as_json MultiJson.dump(to_hash) end |
#command(method_name, *arguments, &block) ⇒ Object
Execute driver command
73 74 75 |
# File 'lib/artoo/device.rb', line 73 def command(method_name, *arguments, &block) driver.command(method_name, *arguments) end |
#commands ⇒ Collection
Returns commands.
68 69 70 |
# File 'lib/artoo/device.rb', line 68 def commands driver.commands end |
#default_connection ⇒ Connection
Returns default connection.
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
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.
46 47 48 |
# File 'lib/artoo/device.rb', line 46 def event_topic_name(event) "#{parent.safe_name}_#{name}_#{event}" end |
#inspect ⇒ String
Returns pretty inspect.
83 84 85 |
# File 'lib/artoo/device.rb', line 83 def inspect "#<Device @id=#{object_id}, @name='name', @driver='driver'>" end |
#start_device ⇒ Object
Starts device driver
41 42 43 |
# File 'lib/artoo/device.rb', line 41 def start_device driver.start_driver end |
#to_hash ⇒ Hash
Returns device.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/artoo/device.rb', line 51 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 |