class Artoo::Device
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.
Attributes
Public Class Methods
Create new device @param [Hash] params @option params :name [String] @option params :pin [String] @option params :parent [String] @option params :connection [String] @option params :interval [String] @option params :driver [String]
# 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
Public Instance Methods
# File lib/artoo/device.rb, line 102 def add_interface(i) @parent.add_interface(i) end
@return [JSON] device
# File lib/artoo/device.rb, line 74 def as_json MultiJson.dump(to_hash) end
Execute driver command
# File lib/artoo/device.rb, line 84 def command(method_name, *arguments, &block) driver.command(method_name, *arguments) end
@return [Collection] commands
# File lib/artoo/device.rb, line 79 def commands driver.commands end
@return [Connection] default connection
# File lib/artoo/device.rb, line 40 def default_connection parent.default_connection end
Retrieve connections from parent @param [String] c connection
# File lib/artoo/device.rb, line 35 def determine_connection(c) parent.connections[c] unless c.nil? end
@return [String] event topic name
# File lib/artoo/device.rb, line 50 def event_topic_name(event) "#{parent.safe_name}_#{name}_#{event}" end
@return [String] pretty inspect
# File lib/artoo/device.rb, line 98 def inspect "#<Device @id=#{object_id}, @name='name', @driver='#{driver.class.name}'>" end
Sends missing methods to command
# File lib/artoo/device.rb, line 89 def method_missing(method_name, *arguments, &block) command(method_name, *arguments, &block) end
# 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
# 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
# File lib/artoo/device.rb, line 93 def respond_to_missing?(method_name, include_private = false) commands.include?(method_name) || super end
Starts device driver
# File lib/artoo/device.rb, line 45 def start_device driver.start_driver end
@return [Hash] device
# 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
Private Instance Methods
# File lib/artoo/device.rb, line 115 def require_driver(d, params) if Artoo::Robot.test? original_type = d d = :test end require "artoo/drivers/#{d.to_s}" @driver = constantize("Artoo::Drivers::#{classify(d.to_s)}").new(:parent => current_instance, :additional_params => params) end