lib/artoo/robot.rb in artoo-1.6.7 vs lib/artoo/robot.rb in artoo-1.8.0

- old
+ new

@@ -1,10 +1,8 @@ require 'celluloid/autostart' require 'celluloid/io' require 'multi_json' -require 'artoo/ext/timers' -require 'artoo/ext/actor' require 'artoo/robot_class_methods' require 'artoo/basic' require 'artoo/connection' require 'artoo/adaptors/adaptor' @@ -14,10 +12,13 @@ require 'artoo/exceptions' require 'artoo/api/api' require 'artoo/master' require 'artoo/port' require 'artoo/utility' +require 'artoo/interfaces/interface' +require 'artoo/interfaces/ping' +require 'artoo/interfaces/rover' module Artoo # The most important class used by Artoo is Robot. This represents the primary # interface for interacting with a collection of physical computing capabilities. # @@ -28,11 +29,11 @@ extend Artoo::Basic extend Artoo::Robot::ClassMethods include Artoo::Utility include Artoo::Events - attr_reader :connections, :devices, :name, :commands + attr_reader :connections, :devices, :name, :commands, :interfaces exclusive :execute_startup # Create new robot # @param [Hash] params @@ -40,10 +41,11 @@ # @option params [Collection] :connections # @option params [Collection] :devices 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 # @return [String] Name without spaces and downcased @@ -132,16 +134,18 @@ # @return [String] robot def inspect "#<Robot #{object_id}>" end - def command(method_name, *arguments) - if known_command?(method_name) + # @return [Object] whatever result is passed back from the wrapped robot + def command(method_name, *arguments, &block) + t = interface_for_command(method_name) + if t if arguments.first - self.send(method_name, *arguments) + t.send(method_name, *arguments) else - self.send(method_name) + t.send(method_name) end else "Unknown Command" end rescue Exception => e @@ -149,13 +153,34 @@ Logger.error e.backtrace.inspect return nil end # @return [Boolean] True if command exists - def known_command?(method_name) + def own_command?(method_name) return commands.include?(method_name.intern) end + def add_interface(i) + @interfaces[i.interface_type.intern] = i + end + + # @return [Boolean] True if command exists in any of the robot's interfaces + 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 + def method_missing(method_name, *arguments, &block) + command(method_name, *arguments, &block) + end + + def respond_to_missing?(method_name, include_private = false) + own_command?(method_name)|| interface_for_command(method_name) + end private def initialize_connections(params={})