Class: Artoo::Robot

Inherits:
Object
  • Object
show all
Extended by:
Basic, ClassMethods
Includes:
Events, Utility, Celluloid, Celluloid::Notifications
Defined in:
lib/artoo/robot.rb,
lib/artoo/robot_class_methods.rb

Overview

The most important class used by Artoo is Robot. This represents the primary interface for interacting with a collection of physical computing capabilities.

This module contains the class-level methods used by Artoo::Robot

Direct Known Subclasses

MainRobot

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Basic

Basic::CALLERS_TO_IGNORE

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from ClassMethods

#use_api

Instance Method Summary collapse

Methods included from Basic

caller_files, set

Methods included from ClassMethods

api, begin_working, cli?, connection, device, handle_signals, is_running?, prepare_work, running!, setup_interrupt, start_api, stopped!, test?, work!

Methods included from Events

#create_proxy_method, #on, #proxy_method_name

Methods included from Utility

#classify, #constantize, #current_class, #current_instance, #os, #random_string, #underscore

Constructor Details

#initialize(params = {}) ⇒ Robot

Create new robot

Parameters:

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

Options Hash (params):

  • :name (String)
  • :connections (Collection)
  • :devices (Collection)


42
43
44
45
46
47
# File 'lib/artoo/robot.rb', line 42

def initialize(params={})
  @name = params[:name] || current_class.name || "Robot #{random_string}"
  @commands = params[:commands] || []
  initialize_connections(params[:connections] || {})
  initialize_devices(params[:devices] || {})
end

Class Attribute Details

.connection_typesObject

Returns the value of attribute connection_types.



7
8
9
# File 'lib/artoo/robot_class_methods.rb', line 7

def connection_types
  @connection_types
end

.device_typesObject

Returns the value of attribute device_types.



7
8
9
# File 'lib/artoo/robot_class_methods.rb', line 7

def device_types
  @device_types
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



33
34
35
# File 'lib/artoo/robot.rb', line 33

def commands
  @commands
end

#connectionsObject (readonly)

Returns the value of attribute connections.



33
34
35
# File 'lib/artoo/robot.rb', line 33

def connections
  @connections
end

#devicesObject (readonly)

Returns the value of attribute devices.



33
34
35
# File 'lib/artoo/robot.rb', line 33

def devices
  @devices
end

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/artoo/robot.rb', line 33

def name
  @name
end

Instance Method Details

#api_hostString

Returns Api Host.

Returns:

  • (String)

    Api Host



55
56
57
# File 'lib/artoo/robot.rb', line 55

def api_host
  self.class.api_host
end

#api_portString

Returns Api Port.

Returns:

  • (String)

    Api Port



60
61
62
# File 'lib/artoo/robot.rb', line 60

def api_port
  self.class.api_port
end

#as_jsonJSON

Returns robot.

Returns:

  • (JSON)

    robot



128
129
130
# File 'lib/artoo/robot.rb', line 128

def as_json
  MultiJson.dump(to_hash)
end

#command(method_name, *arguments) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/artoo/robot.rb', line 137

def command(method_name, *arguments)
  if known_command?(method_name)
    if arguments.first
      self.send(method_name, *arguments)
    else
      self.send(method_name)
    end
  else
    "Unknown Command"
  end
rescue Exception => e
  Logger.error e.message
  Logger.error e.backtrace.inspect
  return nil
end

#connection_typesCollection

Returns connection types.

Returns:

  • (Collection)

    connection types



95
96
97
# File 'lib/artoo/robot.rb', line 95

def connection_types
  current_class.connection_types ||= [{:name => :passthru}]
end

#continue_workObject

continue with the work



79
80
81
82
# File 'lib/artoo/robot.rb', line 79

def continue_work
  Logger.info "Continuing work..."
  current_instance.timers.continue
end

#default_connectionConnection

Returns default connection.

Returns:



90
91
92
# File 'lib/artoo/robot.rb', line 90

def default_connection
  connections.values.first
end

#device_typesCollection

Returns device types.

Returns:

  • (Collection)

    device types



100
101
102
103
# File 'lib/artoo/robot.rb', line 100

def device_types
  current_class.device_types ||= []
  current_class.device_types
end

#disconnectObject

Terminate all connections



85
86
87
# File 'lib/artoo/robot.rb', line 85

def disconnect
  connections.each {|k, c| c.async.disconnect}
end

#has_work?(period, interval) ⇒ Boolean

Returns True if there is recurring work for the period and interval.

Parameters:

  • period (Symbol)
  • interval (Numeric)

Returns:

  • (Boolean)

    True if there is recurring work for the period and interval



113
114
115
# File 'lib/artoo/robot.rb', line 113

def has_work?(period, interval)
  current_instance.timers.find {|t| t.recurring == (period == :every) && t.interval == interval}
end

#inspectString

Returns robot.

Returns:

  • (String)

    robot



133
134
135
# File 'lib/artoo/robot.rb', line 133

def inspect
  "#<Robot #{object_id}>"
end

#known_command?(method_name) ⇒ Boolean

Returns True if command exists.

Returns:

  • (Boolean)

    True if command exists



154
155
156
# File 'lib/artoo/robot.rb', line 154

def known_command?(method_name)
  return commands.include?(method_name.intern)
end

#pause_workObject

pause the work



73
74
75
76
# File 'lib/artoo/robot.rb', line 73

def pause_work
  Logger.info "Pausing work..."
  current_instance.timers.pause
end

#safe_nameString

Returns Name without spaces and downcased.

Returns:

  • (String)

    Name without spaces and downcased



50
51
52
# File 'lib/artoo/robot.rb', line 50

def safe_name
  name.gsub(' ', '_').downcase
end

#to_hashHash

Returns robot.

Returns:

  • (Hash)

    robot



118
119
120
121
122
123
124
125
# File 'lib/artoo/robot.rb', line 118

def to_hash
  {
    :name => name,
    :connections => connections.each_value.collect {|c|c.to_hash},
    :devices => devices.each_value.collect {|d|d.to_hash},
    :commands => commands
  }
end

#workObject

start doing the work



65
66
67
68
69
70
# File 'lib/artoo/robot.rb', line 65

def work
  Logger.info "Starting work..."
  execute_startup(connections) {|c| c.future.connect}
  execute_startup(devices) {|d| d.future.start_device}
  execute_working_code
end

#working_codeProc

Returns current working code.

Returns:

  • (Proc)

    current working code



106
107
108
# File 'lib/artoo/robot.rb', line 106

def working_code
  current_class.working_code ||= proc {puts "No work defined."}
end