class Artoo::Robot

The most important class used by Artoo is Robot. This represents the primary interface for interacting with a collection of physical computing capabilities.

This file contains the instance-level methods used by Artoo::Robot

The most important class used by Artoo is Robot. This represents the primary interface for interacting with a collection of physical computing capabilities.

This module contains the class-level methods used by Artoo::Robot

Attributes

connection_types[RW]
device_types[RW]
commands[R]
connections[R]
devices[R]
interfaces[R]
name[R]

Public Class Methods

new(params={}) click to toggle source

Create new robot @param [Hash] params @option params [String] :name @option params [Collection] :connections @option params [Collection] :devices

# File lib/artoo/robot.rb, line 43
def initialize(params={})
  @name = params[:name] || current_class.name || "Robot #{random_string}"
  @commands = params[:commands] || []
  @interfaces = {}
  initialize_connections(params[:connections] || {})
  initialize_devices(params[:devices] || {})
end

Public Instance Methods

add_interface(i) click to toggle source
# File lib/artoo/robot.rb, line 162
def add_interface(i)
  @interfaces[i.interface_type.intern] = i
end
api_host() click to toggle source

@return [String] Api Host

# File lib/artoo/robot.rb, line 57
def api_host
  self.class.api_host
end
api_port() click to toggle source

@return [String] Api Port

# File lib/artoo/robot.rb, line 62
def api_port
  self.class.api_port
end
as_json() click to toggle source

@return [JSON] robot

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

@return [Object] whatever result is passed back from the wrapped robot

# File lib/artoo/robot.rb, line 140
def command(method_name, *arguments, &block)
  t = interface_for_command(method_name)
  if t
    if arguments.first
      t.send(method_name, *arguments)
    else
      t.send(method_name)
    end
  else
    "Unknown Command"
  end
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
  return nil
end
connection_types() click to toggle source

@return [Collection] connection types

# File lib/artoo/robot.rb, line 97
def connection_types
  current_class.connection_types ||= [{:name => :passthru}]
end
continue_work() click to toggle source

continue with the work

# File lib/artoo/robot.rb, line 81
def continue_work
  Logger.info "Continuing work..."
  current_instance.timers.continue
end
default_connection() click to toggle source

@return [Connection] default connection

# File lib/artoo/robot.rb, line 92
def default_connection
  connections.values.first
end
device_types() click to toggle source

@return [Collection] device types

# File lib/artoo/robot.rb, line 102
def device_types
  current_class.device_types ||= []
  current_class.device_types
end
disconnect() click to toggle source

Terminate all connections

# File lib/artoo/robot.rb, line 87
def disconnect
  connections.each {|k, c| c.async.disconnect}
end
has_work?(period, interval) click to toggle source

@param [Symbol] period @param [Numeric] interval @return [Boolean] True if there is recurring work for the period and interval

# File lib/artoo/robot.rb, line 115
def has_work?(period, interval)
  current_instance.timers.find {|t| t.recurring == (period == :every) && t.interval == interval}
end
inspect() click to toggle source

@return [String] robot

# File lib/artoo/robot.rb, line 135
def inspect
  "#<Robot #{object_id}>"
end
interface_for_command(method_name) click to toggle source

@return [Boolean] True if command exists in any of the robot’s interfaces

# File lib/artoo/robot.rb, line 167
def interface_for_command(method_name)
  return self if own_command?(method_name)
  @interfaces.each_value {|i|
    return i if i.commands.include?(method_name.intern)
  }
  return nil
end
method_missing(method_name, *arguments, &block) click to toggle source

Sends missing methods to command

# File lib/artoo/robot.rb, line 176
def method_missing(method_name, *arguments, &block)
  command(method_name, *arguments, &block)
end
own_command?(method_name) click to toggle source

@return [Boolean] True if command exists

# File lib/artoo/robot.rb, line 158
def own_command?(method_name)
  return commands.include?(method_name.intern)
end
pause_work() click to toggle source

pause the work

# File lib/artoo/robot.rb, line 75
def pause_work
  Logger.info "Pausing work..."
  current_instance.timers.pause
end
respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/artoo/robot.rb, line 180
def respond_to_missing?(method_name, include_private = false)
  own_command?(method_name)|| interface_for_command(method_name)
end
safe_name() click to toggle source

@return [String] Name without spaces and downcased

# File lib/artoo/robot.rb, line 52
def safe_name
  name.gsub(' ', '_').downcase
end
to_hash() click to toggle source

@return [Hash] robot

# File lib/artoo/robot.rb, line 120
def to_hash
  {
    :name => name,
    :connections => connections.each_value.collect {|c|c.to_hash},
    :devices => devices.each_value.collect {|d|d.to_hash},
    :commands => commands
  }
end
work() click to toggle source

start doing the work

# File lib/artoo/robot.rb, line 67
def work
  Logger.info "Starting work..."
  execute_startup(connections) {|c| c.future.connect}
  execute_startup(devices) {|d| d.future.start_device}
  execute_working_code
end
working_code() click to toggle source

@return [Proc] current working code

# File lib/artoo/robot.rb, line 108
def working_code
  current_class.working_code ||= proc {puts "No work defined."}
end

Private Instance Methods

execute_startup(things) { |t| ... } click to toggle source
# File lib/artoo/robot.rb, line 214
def execute_startup(things, &block)
  future_things = things.each_value.collect {|t| yield(t)}
  future_things.each {|v| result = v.value}
end
execute_working_code() click to toggle source
# File lib/artoo/robot.rb, line 219
def execute_working_code
  current_instance.instance_eval(&working_code)
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
end
initialize_connection(params, type, connections) click to toggle source
# File lib/artoo/robot.rb, line 197
def initialize_connection(params, type, connections)
  connection = initialize_object(Connection, params, type)
  @connections[type[:name]] = connection
end
initialize_connections(params={}) click to toggle source
# File lib/artoo/robot.rb, line 187
def initialize_connections(params={})
  @connections = {}
  connection_types.each {|type| initialize_connection(params, type, @connections)}
end
initialize_device(params, type, devices) click to toggle source
# File lib/artoo/robot.rb, line 202
def initialize_device(params, type, devices)
  type = initialize_object(Device, params, type)
  instance_eval("def #{type.name}; return devices[:#{type.name}]; end")
  @devices[type.name.intern] = type
end
initialize_devices(params={}) click to toggle source
# File lib/artoo/robot.rb, line 192
def initialize_devices(params={})
  @devices = {}
  device_types.each {|type| initialize_device(params, type, @devices)}
end
initialize_object(klass, params, type) click to toggle source
# File lib/artoo/robot.rb, line 208
def initialize_object(klass, params, type)
  Logger.info "Initializing #{klass.to_s.downcase} #{type[:name].to_s}..."
  param = params[type[:name]] || {}
  klass.new(type.merge(param).merge(:parent => current_instance))
end