Module: Artoo::Robot::ClassMethods
- Included in:
- Artoo::Robot
- Defined in:
- lib/artoo/robot_class_methods.rb
Instance Attribute Summary collapse
-
#api_host ⇒ Object
Returns the value of attribute api_host.
-
#api_port ⇒ Object
Returns the value of attribute api_port.
-
#use_api ⇒ Object
Returns the value of attribute use_api.
-
#working_code ⇒ Object
Returns the value of attribute working_code.
Instance Method Summary collapse
-
#api(params = {}) ⇒ Object
Activate RESTful api Example: api :host => '127.0.0.1', :port => '1234'.
- #begin_working ⇒ Object
-
#cli? ⇒ Boolean
True if cli env.
-
#connection(name, params = {}) ⇒ Object
Connection to some hardware that has one or more devices via some specific protocol.
-
#device(name, params = {}) ⇒ Object
Device that uses a connection to communicate.
- #handle_signals(self_read) ⇒ Object
-
#is_running? ⇒ Boolean
True if it's running.
-
#master ⇒ Object
Master actor.
-
#prepare_robots(robot = nil) ⇒ Object
Prepare master robots for work.
- #running! ⇒ Object
- #setup_interrupt(self_write) ⇒ Object
- #start_api ⇒ Object
- #stopped! ⇒ Object
-
#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 Attribute Details
#api_host ⇒ Object
Returns the value of attribute api_host.
10 11 12 |
# File 'lib/artoo/robot_class_methods.rb', line 10 def api_host @api_host end |
#api_port ⇒ Object
Returns the value of attribute api_port.
10 11 12 |
# File 'lib/artoo/robot_class_methods.rb', line 10 def api_port @api_port end |
#use_api ⇒ Object
Returns the value of attribute use_api.
10 11 12 |
# File 'lib/artoo/robot_class_methods.rb', line 10 def use_api @use_api end |
#working_code ⇒ Object
Returns the value of attribute working_code.
10 11 12 |
# File 'lib/artoo/robot_class_methods.rb', line 10 def working_code @working_code end |
Instance Method Details
#api(params = {}) ⇒ Object
Activate RESTful api Example: api :host => '127.0.0.1', :port => '1234'
48 49 50 51 52 53 |
# File 'lib/artoo/robot_class_methods.rb', line 48 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 |
#begin_working ⇒ Object
92 93 94 95 96 |
# File 'lib/artoo/robot_class_methods.rb', line 92 def begin_working self_read, self_write = IO.pipe setup_interrupt(self_write) handle_signals(self_read) end |
#cli? ⇒ Boolean
Returns True if cli env.
126 127 128 |
# File 'lib/artoo/robot_class_methods.rb', line 126 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
16 17 18 19 20 |
# File 'lib/artoo/robot_class_methods.rb', line 16 def connection(name, params = {}) @connection_types ||= [] Celluloid::Logger.info "Registering connection '#{name}'..." @connection_types << {:name => name}.merge(params) end |
#device(name, params = {}) ⇒ Object
Device that uses a connection to communicate
26 27 28 29 30 |
# File 'lib/artoo/robot_class_methods.rb', line 26 def device(name, params = {}) @device_types ||= [] Celluloid::Logger.info "Registering device '#{name}'..." @device_types << {:name => name}.merge(params) end |
#handle_signals(self_read) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/artoo/robot_class_methods.rb', line 104 def handle_signals(self_read) while readable_io = IO.select([self_read]) signal = readable_io.first[0].gets.strip raise Interrupt end end |
#is_running? ⇒ Boolean
Returns True if it's running.
131 132 133 134 |
# File 'lib/artoo/robot_class_methods.rb', line 131 def is_running? @@running ||= false @@running == true end |
#master ⇒ Object
Master actor
116 117 118 |
# File 'lib/artoo/robot_class_methods.rb', line 116 def master Celluloid::Actor[:master] end |
#prepare_robots(robot = nil) ⇒ Object
Prepare master robots for work
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/artoo/robot_class_methods.rb', line 80 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 |
#running! ⇒ Object
136 137 138 |
# File 'lib/artoo/robot_class_methods.rb', line 136 def running! @@running = true end |
#setup_interrupt(self_write) ⇒ Object
98 99 100 101 102 |
# File 'lib/artoo/robot_class_methods.rb', line 98 def setup_interrupt(self_write) Signal.trap("INT") do self_write.puts("INT") end end |
#start_api ⇒ Object
111 112 113 |
# File 'lib/artoo/robot_class_methods.rb', line 111 def start_api Celluloid::Actor[:api] = Api::Server.new(self.api_host, self.api_port) if self.use_api end |
#stopped! ⇒ Object
140 141 142 |
# File 'lib/artoo/robot_class_methods.rb', line 140 def stopped! @@running = false end |
#test? ⇒ Boolean
Returns True if test env.
121 122 123 |
# File 'lib/artoo/robot_class_methods.rb', line 121 def test? ENV["ARTOO_TEST"] == 'true' end |
#work(&block) ⇒ Object
The work that needs to be performed
40 41 42 43 |
# File 'lib/artoo/robot_class_methods.rb', line 40 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/artoo/robot_class_methods.rb', line 60 def work!(robot=nil) return if is_running? prepare_robots(robot) unless cli? begin start_api master.start_work begin_working rescue Interrupt Celluloid::Logger.info 'Shutting down...' master.stop_work if master # Explicitly exit so busy Processor threads can't block # process shutdown... taken from Sidekiq, thanks! exit(0) end end end |