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
Public Class Methods
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
# File lib/artoo/robot.rb, line 162 def add_interface(i) @interfaces[i.interface_type.intern] = i end
@return [String] Api
Host
# File lib/artoo/robot.rb, line 57 def api_host self.class.api_host end
@return [JSON] robot
# File lib/artoo/robot.rb, line 130 def as_json MultiJson.dump(to_hash) end
@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
@return [Collection] connection types
# File lib/artoo/robot.rb, line 97 def connection_types current_class.connection_types ||= [{:name => :passthru}] end
continue with the work
# File lib/artoo/robot.rb, line 81 def continue_work Logger.info "Continuing work..." current_instance.timers.continue end
@return [Connection] default connection
# File lib/artoo/robot.rb, line 92 def default_connection connections.values.first end
@return [Collection] device types
# File lib/artoo/robot.rb, line 102 def device_types current_class.device_types ||= [] current_class.device_types end
Terminate all connections
# File lib/artoo/robot.rb, line 87 def disconnect connections.each {|k, c| c.async.disconnect} end
@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
@return [String] robot
# File lib/artoo/robot.rb, line 135 def inspect "#<Robot #{object_id}>" end
@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
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
@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 the work
# File lib/artoo/robot.rb, line 75 def pause_work Logger.info "Pausing work..." current_instance.timers.pause end
# 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
@return [String] Name without spaces and downcased
# File lib/artoo/robot.rb, line 52 def safe_name name.gsub(' ', '_').downcase end
@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
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
@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
# 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
# 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
# File lib/artoo/robot.rb, line 197 def initialize_connection(params, type, connections) connection = initialize_object(Connection, params, type) @connections[type[:name]] = connection end
# File lib/artoo/robot.rb, line 187 def initialize_connections(params={}) @connections = {} connection_types.each {|type| initialize_connection(params, type, @connections)} end
# 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
# File lib/artoo/robot.rb, line 192 def initialize_devices(params={}) @devices = {} device_types.each {|type| initialize_device(params, type, @devices)} end
# 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