class Geppeto::Scout

Attributes

events[R]
hq[R]
id[R]
last_report[RW]
led[R]
logger[RW]
memory[R]
mesh[R]
pin[R]
power[R]
temperature[R]
uptime[R]
wifi[R]

Public Class Methods

new(connection, id = nil) click to toggle source
# File lib/scout.rb, line 18
def initialize(connection, id = nil)
  @id = id
  @conn = connection
  @options = Array.new
  @logger = @conn.logger ? @conn.logger : Logger.new(STDOUT)

  @events = Geppeto::Commands::Events.new(self)
  @hq  = Geppeto::Commands::Hq.new(self)
  @led = Geppeto::Commands::Led.new(self)
  @memory = Geppeto::Commands::Memory.new(self)
  @mesh = Geppeto::Commands::Mesh.new(self)
  @pin = Geppeto::Commands::Pin.new(self)
  @power = Geppeto::Commands::Power.new(self)
  @uptime = Geppeto::Commands::Uptime.new(self)
  @wifi = Geppeto::Commands::Wifi.new(self)
end

Public Instance Methods

all() click to toggle source
# File lib/scout.rb, line 35
def all
  @options << :all
  self
end
boot() click to toggle source
# File lib/scout.rb, line 65
def boot
  request("scout.boot")
end
delay(ms = nil) click to toggle source
# File lib/scout.rb, line 59
def delay(ms = nil)
  @options << :delay
  @_ms = ms
  self
end
lead?() click to toggle source
# File lib/scout.rb, line 49
def lead?
  request("print scout.isleadscout").to_i == 1
end
others() click to toggle source
# File lib/scout.rb, line 40
def others
  @options << :others
  self
end
randomnumber() click to toggle source
# File lib/scout.rb, line 53
def randomnumber
  request("randomnumber")
end
report() click to toggle source
# File lib/scout.rb, line 45
def report
  request("scout.report")
end
request(command, *args, ms: nil) click to toggle source
# File lib/scout.rb, line 69
def request(command, *args, ms: nil)
  args_string = join_args(args)
  command, args_string = process_options(command, args_string)

  if args_string.empty?
    unless @id.nil?
      command = "command.scout(#{@id}, \"#{command}\")"
    else
      command = "#{command}()"
    end
  else
    unless @id.nil?
      command = "command.scout(#{@id}, \"#{command}\", #{args_string})"
    else
      command = "#{command}(#{args_string})"
    end
  end

  @logger.debug("Issuing: #{command}")
  @conn.write(command, ms)
end

Private Instance Methods

join_args(args) click to toggle source
# File lib/scout.rb, line 112
def join_args(args)
  arg_str = String.new
  n = 0
  args.each { |arg|
    if arg.class == String
      arg_str << "\"#{arg}\""
    else
      arg_str << arg.to_s
    end
    n += 1
    arg_str << ", " if n < args.length && args[n].class != NilClass
  }
  arg_str
end
process_options(command, arg_string) click to toggle source
# File lib/scout.rb, line 92
def process_options(command, arg_string)
  arg_string = "\"#{command}(#{arg_string})\"" unless @options.empty?
  case @options.shift
  when :all
    command = "command.all"
    process_options(command, arg_string)
  when :others
    command = "command.others"
    process_options(command, arg_string)
  when :delay
    command = "scout.delay"
    arg_string = "#{@_ms}, #{arg_string}"
    @_ms = false
    process_options(command, arg_string)
  else
    arg_string.gsub(/"/, "\\\"") unless arg_string.empty?
    return command, arg_string
  end
end