lib/artoo/robot.rb in artoo-0.2.0 vs lib/artoo/robot.rb in artoo-0.3.0
- old
+ new
@@ -1,17 +1,21 @@
+require 'celluloid/autostart'
require 'celluloid/io'
require 'multi_json'
+require 'artoo/ext/timers'
+require 'artoo/ext/actor'
require 'artoo/basic'
require 'artoo/connection'
require 'artoo/device'
require 'artoo/events'
require 'artoo/api'
require 'artoo/master'
require 'artoo/port'
require 'artoo/utility'
+
module Artoo
# The most important class used by Artoo is Robot. This represents the primary
# interface for interacting with a collection of physical computing capabilities.
class Robot
include Celluloid
@@ -27,11 +31,11 @@
initialize_connections(params[:connections] || {})
initialize_devices(params[:devices] || {})
end
class << self
- attr_accessor :device_types, :working_code,
+ attr_accessor :device_types, :working_code, :running,
:use_api, :api_host, :api_port
def connection_types
@@connection_types ||= []
end
@@ -78,29 +82,45 @@
# work can be performed by either:
# an existing instance
# an array of existing instances
# or, a new instance can be created
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
+
+ def prepare_robots(robot=nil)
if robot.respond_to?(:work)
robots = [robot]
elsif robot.kind_of?(Array)
robots = robot
else
robots = [self.new]
end
- robots.each {|r| r.async.work}
-
Celluloid::Actor[:master] = Master.new(robots)
- Celluloid::Actor[:api] = Api.new(self.api_host, self.api_port) if self.use_api
-
- sleep # sleep main thread, and let the work commence!
end
def test?
ENV["ARTOO_TEST"] == 'true'
end
+
+ def cli?
+ ENV["ARTOO_CLI"] == 'true'
+ end
+
+ def is_running?
+ self.running ||= false
+ self.running == true
+ end
end
def safe_name
name.gsub(' ', '_').downcase
end
@@ -119,16 +139,26 @@
execute_startup(connections) {|c| c.future.connect}
execute_startup(devices) {|d| d.future.start_device}
execute_working_code
end
+ def pause_work
+ Logger.info "Pausing work..."
+ current_instance.timers.pause
+ end
+
+ def continue_work
+ Logger.info "Continuing work..."
+ current_instance.timers.continue
+ end
+
def disconnect
connections.each {|k, c| c.async.disconnect}
end
def default_connection
- connections[connections.keys.first]
+ connections.values.first
end
def connection_types
current_class.connection_types
end
@@ -149,9 +179,13 @@
}
end
def as_json
MultiJson.dump(to_hash)
+ end
+
+ def inspect
+ "#<Robot #{object_id}>"
end
private
def initialize_connections(params={})