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.
- #name(new_name = false) ⇒ Object
-
#prepare_work(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'
53 54 55 56 57 58 |
# File 'lib/artoo/robot_class_methods.rb', line 53 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
97 98 99 100 101 |
# File 'lib/artoo/robot_class_methods.rb', line 97 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
109 110 111 112 113 114 |
# File 'lib/artoo/robot_class_methods.rb', line 109 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 |
#name(new_name = false) ⇒ Object
32 33 34 35 |
# File 'lib/artoo/robot_class_methods.rb', line 32 def name(new_name = false) @name = new_name if new_name @name end |
#prepare_work(robot = nil) ⇒ Object
Prepare master robots for work
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/artoo/robot_class_methods.rb', line 85 def prepare_work(robot=nil) if robot.respond_to?(:work) robots = [robot] elsif robot.kind_of?(Array) robots = robot else robots = [self.new] end Artoo::Master.assign(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
103 104 105 106 107 |
# File 'lib/artoo/robot_class_methods.rb', line 103 def setup_interrupt(self_write) Signal.trap("INT") do self_write.puts("INT") end end |
#start_api ⇒ Object
116 117 118 |
# File 'lib/artoo/robot_class_methods.rb', line 116 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
45 46 47 48 |
# File 'lib/artoo/robot_class_methods.rb', line 45 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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/artoo/robot_class_methods.rb', line 65 def work!(robot=nil) prepare_work(robot) return if is_running? unless cli? begin start_api Artoo::Master.start_work begin_working rescue Interrupt Celluloid::Logger.info 'Shutting down...' Artoo::Master.stop_work # Explicitly exit so busy Processor threads can't block # process shutdown... taken from Sidekiq, thanks! exit(0) end end end |