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
-
.connection(name, params = {}) ⇒ Object
connection to some hardware that has one or more devices via some specific protocol Example: connection :arduino, :adaptor => :firmata, :port => ‘/dev/tty.usbmodemxxxxx’.
- .connection_types ⇒ Object
-
.device(name, params = {}) ⇒ Object
device that uses a connection to communicate Example: device :collision_detect, :driver => :switch, :pin => 3.
- .is_running? ⇒ Boolean
- .prepare_robots(robot = nil) ⇒ Object
- .test? ⇒ Boolean
-
.work(&block) ⇒ Object
the work that needs to be performed Example: work do every(10.seconds) do puts “hello, world” end 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.
Instance Method Summary collapse
- #api_host ⇒ Object
- #api_port ⇒ Object
- #as_json ⇒ Object
- #connection_types ⇒ Object
- #continue_work ⇒ Object
- #default_connection ⇒ Object
- #device_types ⇒ Object
- #disconnect ⇒ Object
-
#initialize(params = {}) ⇒ Robot
constructor
A new instance of Robot.
- #inspect ⇒ Object
- #pause_work ⇒ Object
- #safe_name ⇒ Object
- #to_hash ⇒ Object
-
#work ⇒ Object
start doing the work.
- #working_code ⇒ Object
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
Methods included from Celluloid
Constructor Details
#initialize(params = {}) ⇒ Robot
Returns a new instance of Robot.
29 30 31 32 33 |
# File 'lib/artoo/robot.rb', line 29 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.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 def api_host @api_host end |
.api_port ⇒ Object
Returns the value of attribute api_port.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 def api_port @api_port end |
.device_types ⇒ Object
Returns the value of attribute device_types.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 def device_types @device_types end |
.running ⇒ Object
Returns the value of attribute running.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 def running @running end |
.use_api ⇒ Object
Returns the value of attribute use_api.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 def use_api @use_api end |
.working_code ⇒ Object
Returns the value of attribute working_code.
36 37 38 |
# File 'lib/artoo/robot.rb', line 36 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'
75 76 77 78 79 80 |
# File 'lib/artoo/robot.rb', line 75 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
114 115 116 |
# File 'lib/artoo/robot.rb', line 114 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 Example:
connection :arduino, :adaptor => :firmata, :port => '/dev/tty.usbmodemxxxxx'
46 47 48 49 |
# File 'lib/artoo/robot.rb', line 46 def connection(name, params = {}) Celluloid::Logger.info "Registering connection '#{name}'..." self.connection_types << {:name => name}.merge(params) end |
.connection_types ⇒ Object
39 40 41 |
# File 'lib/artoo/robot.rb', line 39 def connection_types @@connection_types ||= [] end |
.device(name, params = {}) ⇒ Object
device that uses a connection to communicate Example:
device :collision_detect, :driver => :switch, :pin => 3
54 55 56 57 58 |
# File 'lib/artoo/robot.rb', line 54 def device(name, params = {}) Celluloid::Logger.info "Registering device '#{name}'..." self.device_types ||= [] self.device_types << {:name => name}.merge(params) end |
.is_running? ⇒ Boolean
118 119 120 121 |
# File 'lib/artoo/robot.rb', line 118 def is_running? self.running ||= false self.running == true end |
.prepare_robots(robot = nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/artoo/robot.rb', line 98 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
110 111 112 |
# File 'lib/artoo/robot.rb', line 110 def test? ENV["ARTOO_TEST"] == 'true' end |
.work(&block) ⇒ Object
the work that needs to be performed Example:
work do
every(10.seconds) do
puts "hello, world"
end
end
67 68 69 70 |
# File 'lib/artoo/robot.rb', line 67 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
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/artoo/robot.rb', line 86 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 Celluloid::Actor[:master].start_work self.running = true sleep # sleep main thread, and let the work commence! end end |
Instance Method Details
#api_host ⇒ Object
128 129 130 |
# File 'lib/artoo/robot.rb', line 128 def api_host self.class.api_host end |
#api_port ⇒ Object
132 133 134 |
# File 'lib/artoo/robot.rb', line 132 def api_port self.class.api_port end |
#as_json ⇒ Object
182 183 184 |
# File 'lib/artoo/robot.rb', line 182 def as_json MultiJson.dump(to_hash) end |
#connection_types ⇒ Object
162 163 164 |
# File 'lib/artoo/robot.rb', line 162 def connection_types current_class.connection_types end |
#continue_work ⇒ Object
149 150 151 152 |
# File 'lib/artoo/robot.rb', line 149 def continue_work Logger.info "Continuing work..." current_instance.timers.continue end |
#default_connection ⇒ Object
158 159 160 |
# File 'lib/artoo/robot.rb', line 158 def default_connection connections.values.first end |
#device_types ⇒ Object
166 167 168 169 |
# File 'lib/artoo/robot.rb', line 166 def device_types current_class.device_types ||= [] current_class.device_types end |
#disconnect ⇒ Object
154 155 156 |
# File 'lib/artoo/robot.rb', line 154 def disconnect connections.each {|k, c| c.async.disconnect} end |
#inspect ⇒ Object
186 187 188 |
# File 'lib/artoo/robot.rb', line 186 def inspect "#<Robot #{object_id}>" end |
#pause_work ⇒ Object
144 145 146 147 |
# File 'lib/artoo/robot.rb', line 144 def pause_work Logger.info "Pausing work..." current_instance.timers.pause end |
#safe_name ⇒ Object
124 125 126 |
# File 'lib/artoo/robot.rb', line 124 def safe_name name.gsub(' ', '_').downcase end |
#to_hash ⇒ Object
175 176 177 178 179 180 |
# File 'lib/artoo/robot.rb', line 175 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
137 138 139 140 141 142 |
# File 'lib/artoo/robot.rb', line 137 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 ⇒ Object
171 172 173 |
# File 'lib/artoo/robot.rb', line 171 def working_code current_class.working_code ||= proc {puts "No work defined."} end |