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

connection[R]
details[R]
driver[R]
interface[R]
interval[R]
name[R]
parent[R]
pin[R]

Public Class Methods

new(params={}) click to toggle source

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

add_interface(i) click to toggle source
# File lib/artoo/device.rb, line 102
def add_interface(i)
  @parent.add_interface(i)  
end
as_json() click to toggle source

@return [JSON] device

# File lib/artoo/device.rb, line 74
def as_json
  MultiJson.dump(to_hash)
end
command(method_name, *arguments, &block) click to toggle source

Execute driver command

# File lib/artoo/device.rb, line 84
def command(method_name, *arguments, &block)
  driver.command(method_name, *arguments)
end
commands() click to toggle source

@return [Collection] commands

# File lib/artoo/device.rb, line 79
def commands
  driver.commands
end
default_connection() click to toggle source

@return [Connection] default connection

# File lib/artoo/device.rb, line 40
def default_connection
  parent.default_connection
end
determine_connection(c) click to toggle source

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
event_topic_name(event) click to toggle source

@return [String] event topic name

# File lib/artoo/device.rb, line 50
def event_topic_name(event)
  "#{parent.safe_name}_#{name}_#{event}"
end
inspect() click to toggle source

@return [String] pretty inspect

# File lib/artoo/device.rb, line 98
def inspect
  "#<Device @id=#{object_id}, @name='name', @driver='#{driver.class.name}'>"
end
method_missing(method_name, *arguments, &block) click to toggle source

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
publish(event, *data) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/artoo/device.rb, line 93
def respond_to_missing?(method_name, include_private = false)
  commands.include?(method_name) || super
end
start_device() click to toggle source

Starts device driver

# File lib/artoo/device.rb, line 45
def start_device
  driver.start_driver
end
to_hash() click to toggle source

@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

require_driver(d, params) click to toggle source
# 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