Class: Artoo::Robot
Overview
The most important class used by Artoo is Robot. This represents the primary interface for interacting with a collection of physical computing capabilities.
Direct Known Subclasses
Constant Summary
Constants included from Basic
Class Attribute Summary collapse
-
.api_host ⇒ Object
Returns the value of attribute api_host.
-
.api_port ⇒ Object
Returns the value of attribute api_port.
-
.device_types ⇒ Object
Returns the value of attribute device_types.
-
.running ⇒ Object
Returns the value of attribute running.
-
.use_api ⇒ Object
Returns the value of attribute use_api.
-
.working_code ⇒ Object
Returns the value of attribute working_code.
Instance Attribute Summary collapse
-
#connections ⇒ Object
readonly
Returns the value of attribute connections.
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.api(params = {}) ⇒ Object
Activate RESTful api Example: api :host => '127.0.0.1', :port => '1234'.
-
.cli? ⇒ Boolean
True if cli env.
-
.connection(name, params = {}) ⇒ Object
Connection to some hardware that has one or more devices via some specific protocol.
- .connection_types ⇒ Object
-
.device(name, params = {}) ⇒ Object
Device that uses a connection to communicate.
-
.is_running? ⇒ Boolean
True if it's running.
-
.master ⇒ Object
Master actor.
-
.prepare_robots(robot = nil) ⇒ Object
Prepare master robots for work.
-
.test? ⇒ Boolean
True if test env.
-
.work(&block) ⇒ Object
The work that needs to be performed.
-
.work!(robot = nil) ⇒ Object
Work can be performed by either: an existing instance an array of existing instances or, a new instance can be created.
Instance Method Summary collapse
-
#api_host ⇒ String
Api Host.
-
#api_port ⇒ String
Api Port.
-
#as_json ⇒ JSON
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.
-
#initialize(params = {}) ⇒ Robot
constructor
Create new robot.
-
#inspect ⇒ String
Robot.
-
#pause_work ⇒ Object
pause the work.
-
#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 Events
#create_proxy_method, #on, #proxy_method_name
Methods included from Utility
#classify, #constantize, #current_class, #current_instance, #random_string, #underscore
Constructor Details
#initialize(params = {}) ⇒ Robot
Create new robot
34 35 36 37 38 |
# File 'lib/artoo/robot.rb', line 34 def initialize(params={}) @name = params[:name] || "Robot #{random_string}" initialize_connections(params[:connections] || {}) initialize_devices(params[:devices] || {}) end |
Class Attribute Details
.api_host ⇒ Object
Returns the value of attribute api_host.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def api_host @api_host end |
.api_port ⇒ Object
Returns the value of attribute api_port.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def api_port @api_port end |
.device_types ⇒ Object
Returns the value of attribute device_types.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def device_types @device_types end |
.running ⇒ Object
Returns the value of attribute running.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def running @running end |
.use_api ⇒ Object
Returns the value of attribute use_api.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def use_api @use_api end |
.working_code ⇒ Object
Returns the value of attribute working_code.
41 42 43 |
# File 'lib/artoo/robot.rb', line 41 def working_code @working_code end |
Instance Attribute Details
#connections ⇒ Object (readonly)
Returns the value of attribute connections.
27 28 29 |
# File 'lib/artoo/robot.rb', line 27 def connections @connections end |
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
27 28 29 |
# File 'lib/artoo/robot.rb', line 27 def devices @devices end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
27 28 29 |
# File 'lib/artoo/robot.rb', line 27 def name @name end |
Class Method Details
.api(params = {}) ⇒ Object
Activate RESTful api Example: api :host => '127.0.0.1', :port => '1234'
83 84 85 86 87 88 |
# File 'lib/artoo/robot.rb', line 83 def api(params = {}) Celluloid::Logger.info "Registering api..." self.use_api = true self.api_host = params[:host] || '127.0.0.1' self.api_port = params[:port] || '4321' end |
.cli? ⇒ Boolean
Returns True if cli env.
131 132 133 |
# File 'lib/artoo/robot.rb', line 131 def cli? ENV["ARTOO_CLI"] == 'true' end |
.connection(name, params = {}) ⇒ Object
Connection to some hardware that has one or more devices via some specific protocol
52 53 54 55 |
# File 'lib/artoo/robot.rb', line 52 def connection(name, params = {}) Celluloid::Logger.info "Registering connection '#{name}'..." self.connection_types << {:name => name}.merge(params) end |
.connection_types ⇒ Object
44 45 46 |
# File 'lib/artoo/robot.rb', line 44 def connection_types @@connection_types ||= [] end |
.device(name, params = {}) ⇒ Object
Device that uses a connection to communicate
61 62 63 64 65 |
# File 'lib/artoo/robot.rb', line 61 def device(name, params = {}) Celluloid::Logger.info "Registering device '#{name}'..." self.device_types ||= [] self.device_types << {:name => name}.merge(params) end |
.is_running? ⇒ Boolean
Returns True if it's running.
136 137 138 139 |
# File 'lib/artoo/robot.rb', line 136 def is_running? self.running ||= false self.running == true end |
.master ⇒ Object
Master actor
121 122 123 |
# File 'lib/artoo/robot.rb', line 121 def master Celluloid::Actor[:master] end |
.prepare_robots(robot = nil) ⇒ Object
Prepare master robots for work
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/artoo/robot.rb', line 108 def prepare_robots(robot=nil) if robot.respond_to?(:work) robots = [robot] elsif robot.kind_of?(Array) robots = robot else robots = [self.new] end Celluloid::Actor[:master] = Master.new(robots) end |
.test? ⇒ Boolean
Returns True if test env.
126 127 128 |
# File 'lib/artoo/robot.rb', line 126 def test? ENV["ARTOO_TEST"] == 'true' end |
.work(&block) ⇒ Object
The work that needs to be performed
75 76 77 78 |
# File 'lib/artoo/robot.rb', line 75 def work(&block) Celluloid::Logger.info "Preparing work..." self.working_code = block if block_given? end |
.work!(robot = nil) ⇒ Object
Work can be performed by either: an existing instance an array of existing instances or, a new instance can be created
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/artoo/robot.rb', line 95 def work!(robot=nil) return if !test? && is_running? prepare_robots(robot) unless cli? Celluloid::Actor[:api] = Api.new(self.api_host, self.api_port) if self.use_api master.start_work self.running = true sleep # sleep main thread, and let the work commence! end end |
Instance Method Details
#api_host ⇒ String
Returns Api Host.
148 149 150 |
# File 'lib/artoo/robot.rb', line 148 def api_host self.class.api_host end |
#api_port ⇒ String
Returns Api Port.
153 154 155 |
# File 'lib/artoo/robot.rb', line 153 def api_port self.class.api_port end |
#as_json ⇒ JSON
Returns robot.
213 214 215 |
# File 'lib/artoo/robot.rb', line 213 def as_json MultiJson.dump(to_hash) end |
#connection_types ⇒ Collection
Returns connection types.
188 189 190 |
# File 'lib/artoo/robot.rb', line 188 def connection_types current_class.connection_types end |
#continue_work ⇒ Object
continue with the work
172 173 174 175 |
# File 'lib/artoo/robot.rb', line 172 def continue_work Logger.info "Continuing work..." current_instance.timers.continue end |
#default_connection ⇒ Connection
Returns default connection.
183 184 185 |
# File 'lib/artoo/robot.rb', line 183 def default_connection connections.values.first end |
#device_types ⇒ Collection
Returns device types.
193 194 195 196 |
# File 'lib/artoo/robot.rb', line 193 def device_types current_class.device_types ||= [] current_class.device_types end |
#disconnect ⇒ Object
Terminate all connections
178 179 180 |
# File 'lib/artoo/robot.rb', line 178 def disconnect connections.each {|k, c| c.async.disconnect} end |
#inspect ⇒ String
Returns robot.
218 219 220 |
# File 'lib/artoo/robot.rb', line 218 def inspect "#<Robot #{object_id}>" end |
#pause_work ⇒ Object
pause the work
166 167 168 169 |
# File 'lib/artoo/robot.rb', line 166 def pause_work Logger.info "Pausing work..." current_instance.timers.pause end |
#safe_name ⇒ String
Returns Name without spaces and downcased.
143 144 145 |
# File 'lib/artoo/robot.rb', line 143 def safe_name name.gsub(' ', '_').downcase end |
#to_hash ⇒ Hash
Returns robot.
204 205 206 207 208 209 210 |
# File 'lib/artoo/robot.rb', line 204 def to_hash { :name => name, :connections => connections.each_value.collect {|c|c.to_hash}, :devices => devices.each_value.collect {|d|d.to_hash} } end |
#work ⇒ Object
start doing the work
158 159 160 161 162 163 |
# File 'lib/artoo/robot.rb', line 158 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.
199 200 201 |
# File 'lib/artoo/robot.rb', line 199 def working_code current_class.working_code ||= proc {puts "No work defined."} end |