Module: Artoo::Robot::ClassMethods

Included in:
Artoo::Robot
Defined in:
lib/artoo/robot_class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_hostObject

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_portObject

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_apiObject

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_codeObject

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_workingObject



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.

Returns:

  • (Boolean)

    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

Examples:

connection :arduino, :adaptor => :firmata, :port => '/dev/tty.usbmodemxxxxx'

Parameters:

  • name (String)
  • params (Hash) (defaults to: {})


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

Examples:

device :collision_detect, :driver => :switch, :pin => 3

Parameters:

  • name (String)
  • params (Hash) (defaults to: {})


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.

Returns:

  • (Boolean)

    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_apiObject



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.

Returns:

  • (Boolean)

    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

Examples:

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

Parameters:

  • work (block)


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

Parameters:

  • robot (Robot) (defaults to: nil)


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