Class: Artoo::Robot
- Inherits:
-
Object
- Object
- Artoo::Robot
- Extended by:
- Basic, ClassMethods
- Defined in:
- lib/artoo/robot.rb,
lib/artoo/robot_class_methods.rb
Overview
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
Direct Known Subclasses
Defined Under Namespace
Modules: ClassMethods
Constant Summary
Constants included from Basic
Class Attribute Summary collapse
-
.connection_types ⇒ Object
Returns the value of attribute connection_types.
-
.device_types ⇒ Object
Returns the value of attribute device_types.
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#interfaces ⇒ Object
readonly
Returns the value of attribute interfaces.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Attributes included from ClassMethods
Instance Method Summary collapse
- #add_interface(i) ⇒ Object
-
#api_host ⇒ String
Api Host.
-
#api_port ⇒ String
Api Port.
-
#as_json ⇒ JSON
Robot.
-
#command(method_name, *arguments, &block) ⇒ Object
Whatever result is passed back from the wrapped robot.
-
#connection_types ⇒ Collection
Connection types.
-
#continue_work ⇒ Object
continue with the work.
-
#default_connection ⇒ Connection
Default connection.
-
#device_types ⇒ Collection
Device types.
-
#disconnect ⇒ Object
Terminate all connections.
-
#has_work?(period, interval) ⇒ Boolean
True if there is recurring work for the period and interval.
-
#initialize(params = {}) ⇒ Robot
constructor
Create new robot.
-
#inspect ⇒ String
Robot.
-
#interface_for_command(method_name) ⇒ Boolean
True if command exists in any of the robot's interfaces.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Sends missing methods to command.
-
#own_command?(method_name) ⇒ Boolean
True if command exists.
-
#pause_work ⇒ Object
pause the work.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#safe_name ⇒ String
Name without spaces and downcased.
-
#to_hash ⇒ Hash
Robot.
-
#work ⇒ Object
start doing the work.
-
#working_code ⇒ Proc
Current working code.
Methods included from Basic
Methods included from ClassMethods
api, begin_working, cli?, connection, device, handle_signals, is_running?, prepare_work, running!, setup_interrupt, start_api, stopped!, test?, work!
Methods included from Events
#create_proxy_method, #on, #proxy_method_name
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #os, #random_string, #remove_keys, #underscore
Constructor Details
#initialize(params = {}) ⇒ Robot
Create new robot
43 44 45 46 47 48 49 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Sends missing methods to command
176 177 178 |
# File 'lib/artoo/robot.rb', line 176 def method_missing(method_name, *arguments, &block) command(method_name, *arguments, &block) end |
Class Attribute Details
.connection_types ⇒ Object
Returns the value of attribute connection_types.
7 8 9 |
# File 'lib/artoo/robot_class_methods.rb', line 7 def connection_types @connection_types end |
.device_types ⇒ Object
Returns the value of attribute device_types.
7 8 9 |
# File 'lib/artoo/robot_class_methods.rb', line 7 def device_types @device_types end |
Instance Attribute Details
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
34 35 36 |
# File 'lib/artoo/robot.rb', line 34 def commands @commands end |
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
34 35 36 |
# File 'lib/artoo/robot.rb', line 34 def connections @connections end |
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
34 35 36 |
# File 'lib/artoo/robot.rb', line 34 def devices @devices end |
#interfaces ⇒ Object (readonly)
Returns the value of attribute interfaces.
34 35 36 |
# File 'lib/artoo/robot.rb', line 34 def interfaces @interfaces end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
34 35 36 |
# File 'lib/artoo/robot.rb', line 34 def name @name end |
Instance Method Details
#add_interface(i) ⇒ Object
162 163 164 |
# File 'lib/artoo/robot.rb', line 162 def add_interface(i) @interfaces[i.interface_type.intern] = i end |
#api_host ⇒ String
Returns Api Host.
57 58 59 |
# File 'lib/artoo/robot.rb', line 57 def api_host self.class.api_host end |
#api_port ⇒ String
Returns Api Port.
62 63 64 |
# File 'lib/artoo/robot.rb', line 62 def api_port self.class.api_port end |
#as_json ⇒ JSON
Returns robot.
130 131 132 |
# File 'lib/artoo/robot.rb', line 130 def as_json MultiJson.dump(to_hash) end |
#command(method_name, *arguments, &block) ⇒ Object
Returns whatever result is passed back from the wrapped robot.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# 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. Logger.error e.backtrace.inspect return nil end |
#connection_types ⇒ Collection
Returns connection types.
97 98 99 |
# File 'lib/artoo/robot.rb', line 97 def connection_types current_class.connection_types ||= [{:name => :passthru}] end |
#continue_work ⇒ Object
continue with the work
81 82 83 84 |
# File 'lib/artoo/robot.rb', line 81 def continue_work Logger.info "Continuing work..." current_instance.timers.continue end |
#default_connection ⇒ Connection
Returns default connection.
92 93 94 |
# File 'lib/artoo/robot.rb', line 92 def default_connection connections.values.first end |
#device_types ⇒ Collection
Returns device types.
102 103 104 105 |
# File 'lib/artoo/robot.rb', line 102 def device_types current_class.device_types ||= [] current_class.device_types end |
#disconnect ⇒ Object
Terminate all connections
87 88 89 |
# File 'lib/artoo/robot.rb', line 87 def disconnect connections.each {|k, c| c.async.disconnect} end |
#has_work?(period, interval) ⇒ Boolean
Returns True if there is recurring work for the period and interval.
115 116 117 |
# 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 ⇒ String
Returns robot.
135 136 137 |
# File 'lib/artoo/robot.rb', line 135 def inspect "#<Robot #{object_id}>" end |
#interface_for_command(method_name) ⇒ Boolean
Returns True if command exists in any of the robot's interfaces.
167 168 169 170 171 172 173 |
# 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 |
#own_command?(method_name) ⇒ Boolean
Returns True if command exists.
158 159 160 |
# File 'lib/artoo/robot.rb', line 158 def own_command?(method_name) return commands.include?(method_name.intern) end |
#pause_work ⇒ Object
pause the work
75 76 77 78 |
# 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) ⇒ Boolean
180 181 182 |
# 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 ⇒ String
Returns Name without spaces and downcased.
52 53 54 |
# File 'lib/artoo/robot.rb', line 52 def safe_name name.gsub(' ', '_').downcase end |
#to_hash ⇒ Hash
Returns robot.
120 121 122 123 124 125 126 127 |
# 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 ⇒ Object
start doing the work
67 68 69 70 71 72 |
# 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 ⇒ Proc
Returns current working code.
108 109 110 |
# File 'lib/artoo/robot.rb', line 108 def working_code current_class.working_code ||= proc {puts "No work defined."} end |