Class: Artoo::Device
- Inherits:
-
Object
- Object
- Artoo::Device
- 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 ⇒ Object
- #default_connection ⇒ Object
- #determine_connection(c) ⇒ Object
- #event_topic_name(event) ⇒ Object
-
#initialize(params = {}) ⇒ Device
constructor
A new instance of Device.
- #inspect ⇒ Object
- #method_missing(method_name, *arguments, &block) ⇒ Object
- #start_device ⇒ Object
- #to_hash ⇒ Object
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #random_string
Methods included from Celluloid
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
#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 ⇒ Object
46 47 48 |
# File 'lib/artoo/device.rb', line 46 def as_json MultiJson.dump(to_hash) end |
#default_connection ⇒ Object
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 |
#inspect ⇒ Object
54 55 56 |
# File 'lib/artoo/device.rb', line 54 def inspect "#<Device @id=#{object_id}, @name='name', @driver='driver'>" end |
#start_device ⇒ Object
29 30 31 |
# File 'lib/artoo/device.rb', line 29 def start_device driver.start_driver end |
#to_hash ⇒ Object
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 |