module Artoo::Robot::ClassMethods

Attributes

api_host[RW]
api_port[RW]
use_api[RW]
working_code[RW]

Public Instance Methods

api(params = {}) click to toggle source

Activate RESTful api Example:

api :host => '127.0.0.1', :port => '1234'
# 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() click to toggle source
# 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?() click to toggle source

@return [Boolean] True if cli env

# File lib/artoo/robot_class_methods.rb, line 126
def cli?
  ENV["ARTOO_CLI"] == 'true'
end
connection(name, params = {}) click to toggle source

Connection to some hardware that has one or more devices via some specific protocol @example connection :arduino, :adaptor => :firmata, :port => ‘/dev/tty.usbmodemxxxxx’ @param [String] name @param [Hash] params

# 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 = {}) click to toggle source

Device that uses a connection to communicate @example device :collision_detect, :driver => :switch, :pin => 3 @param [String] name @param [Hash] params

# 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) click to toggle source
# 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?() click to toggle source

@return [Boolean] True if it’s running

# File lib/artoo/robot_class_methods.rb, line 131
def is_running?
  @@running ||= false
  @@running == true
end
name(new_name = false) click to toggle source
# 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) click to toggle source

Prepare master robots for work

# 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!() click to toggle source
# File lib/artoo/robot_class_methods.rb, line 136
def running!
  @@running = true
end
setup_interrupt(self_write) click to toggle source
# 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() click to toggle source
# 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!() click to toggle source
# File lib/artoo/robot_class_methods.rb, line 140
def stopped!
  @@running = false
end
test?() click to toggle source

@return [Boolean] True if test env

# File lib/artoo/robot_class_methods.rb, line 121
def test?
  ENV["ARTOO_TEST"] == 'true'
end
work(&block) click to toggle source

The work that needs to be performed @example

work do
  every(10.seconds) do
    puts "hello, world"
  end
end

@param [block] work

# 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) click to toggle source

Work can be performed by either:

an existing instance
an array of existing instances
or, a new instance can be created

@param [Robot] robot

# 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