class Alien::AlienReader

Public Class Methods

new(methodsfile=File.join(File.dirname(__FILE__) , "readermethods.dat")) click to toggle source

call the AlienConnection initializer and build lots of the Getters/Setters from a reader methods file. Default value for the methods file will be the file, readermethods.txt found in the same directory as this file, alienreader.rb.

Calls superclass method Alien::AlienConnection::new
# File lib/alien/alienreader.rb, line 21
def initialize(methodsfile=File.join(File.dirname(__FILE__) , "readermethods.dat"))
  super()
  if @@methods_needed
    build_methods(methodsfile)
  end
end

Public Instance Methods

alien_tag_list() click to toggle source
# File lib/alien/alienreader.rb, line 164
def alien_tag_list
  tl = AlienTagList.new(self.taglist)
  return tl
end
blinkled(state1, state2, duration, count) click to toggle source

Makes the reader blink its LEDs.

blinkled is a more complicated function call than an attribute. We just put the method in here rather than try to generate it dynamically.

# File lib/alien/alienreader.rb, line 138
def blinkled (state1, state2, duration, count)
  execute('blinkled=' + state1.to_s  + ' ' +  state2.to_s + ' ' + duration.to_s + ' ' +count.to_s)
end
gpio() click to toggle source

Returns the state of the reader's external inputs

# File lib/alien/alienreader.rb, line 143
def gpio
  execute('externalinput?')
end
gpio=(newvalue) click to toggle source

Sets reader's external outputs

# File lib/alien/alienreader.rb, line 148
def gpio=(newvalue)
  execute('externaloutput='+newvalue)
end
service(service_name ="all", command="status") click to toggle source

Implements the Alien service command

# File lib/alien/alienreader.rb, line 160
def service(service_name ="all", command="status")
  sendreceive("service #{service_name} #{command}")
end
upgradenow(opts="") click to toggle source

Executes the Alien UpgradeNow command. The opts parameter correspondst to the standard values passed to the UpgradeNow command. For example

rdr.upgradenow("force http://server.ip/firmware")
# File lib/alien/alienreader.rb, line 155
    def upgradenow(opts="")
  sendreceive("upgradenow #{opts}", :timeout=>120)
end

Private Instance Methods

add_do(cmd) click to toggle source
# File lib/alien/alienreader.rb, line 63
def add_do(cmd)
  method_name = cmd.to_sym
  self.class.instance_eval {
    send :define_method, method_name do |*val|
      if val.nil? || val.to_s.size==0
            sendreceive("#{cmd}")
      else
        sendreceive("#{cmd} #{val.join}")
      end
    end
  }
end
add_do_set(cmd) click to toggle source
# File lib/alien/alienreader.rb, line 76
def add_do_set(cmd)
  method_name = cmd.to_sym
  self.class.instance_eval {
    send :define_method, method_name do |*val|
      if val.nil? || val.to_s.size==0
        execute("#{cmd}")
      else
        execute("#{cmd}=#{val.join}")
      end
    end
  }
end
add_get(cmd) click to toggle source
# File lib/alien/alienreader.rb, line 40
def add_get(cmd)
  method_name = cmd.to_sym
  self.class.instance_eval {
    send :define_method, method_name do |*val|
      if val.nil? || val.to_s.size==0
        execute("#{cmd}?")
      else
        puts "sending val"
        execute("#{cmd} [#{val.join}]?")
      end
    end
  }
end
add_getset(cmd) click to toggle source
# File lib/alien/alienreader.rb, line 89
def add_getset(cmd)
  add_get(cmd)
  add_set(cmd)
end
add_set(cmd) click to toggle source
# File lib/alien/alienreader.rb, line 54
def add_set(cmd)
  method_name = (cmd+"=").to_sym
  self.class.instance_eval {
    send :define_method, method_name do |*val|
      execute("#{cmd}=#{val.join}")
    end
  }
end
build_methods(fn) click to toggle source

Build lots of the simple get/set methods supported by the class from a configuration file…

# File lib/alien/alienreader.rb, line 96
def build_methods(fn)
  #puts "building methods from: #{fn}"
  #open the file and read out the methods to be supported
  if File.file?(fn)
    File.open(fn).each do |line|
      #puts line
      #ignore comment lines...
      if (line[0..0]!="#")
        dat = line.split

        #ignore blank lines
        if dat.size>0
          method_name = dat[0].strip
          use = dat[1].strip

          #puts "Method: #{dat[0]} Use: #{dat[1]}"
          case dat[1].downcase
            when "getset"
              add_getset(dat[0])
            when "get"
              add_get(dat[0])
            when "set"
              add_set(dat[0])
            when "do"
              add_do(dat[0])
            when "doset"
              add_do_set(dat[0])
          end
        end
      end
    end
    @@methods_needed = false
  else
    raise "Error: cannot find method definition file: #{fn}."
  end
end
execute(cmd) click to toggle source

execute a sendreceive and pull off the 'y' payload for messages that return with an 'x = y' style reply

# File lib/alien/alienreader.rb, line 31
    def execute (cmd)
  s = sendreceive(cmd)
  if s.include? '='
    return s.split('=')[1].strip!
  else
    return s
  end
end