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.
-
#details ⇒ Object
readonly
Returns the value of attribute details.
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#interface ⇒ Object
readonly
Returns the value of attribute interface.
-
#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
- #add_interface(i) ⇒ Object
-
#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.
- #publish(event, *data) ⇒ Object
- #require_interface(i) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#start_device ⇒ Object
Starts device driver.
-
#to_hash ⇒ Hash
Device.
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #os, #random_string, #remove_keys, #underscore
Constructor Details
#initialize(params = {}) ⇒ Device
Create new device
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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def connection @connection end |
#details ⇒ Object (readonly)
Returns the value of attribute details.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def details @details end |
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def driver @driver end |
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
9 10 11 |
# File 'lib/artoo/device.rb', line 9 def interface @interface 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
#add_interface(i) ⇒ Object
102 103 104 |
# File 'lib/artoo/device.rb', line 102 def add_interface(i) @parent.add_interface(i) end |
#as_json ⇒ JSON
Returns 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 |
#commands ⇒ Collection
Returns commands.
79 80 81 |
# File 'lib/artoo/device.rb', line 79 def commands driver.commands end |
#default_connection ⇒ Connection
Returns default connection.
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
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.
50 51 52 |
# File 'lib/artoo/device.rb', line 50 def event_topic_name(event) "#{parent.safe_name}_#{name}_#{event}" end |
#inspect ⇒ String
Returns 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
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_device ⇒ Object
Starts device driver
45 46 47 |
# File 'lib/artoo/device.rb', line 45 def start_device driver.start_driver end |
#to_hash ⇒ Hash
Returns 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 |