class Alien::AlienReader
Public Class Methods
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.
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
# File lib/alien/alienreader.rb, line 164 def alien_tag_list tl = AlienTagList.new(self.taglist) return tl end
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
Returns the state of the reader's external inputs
# File lib/alien/alienreader.rb, line 143 def gpio execute('externalinput?') end
Sets reader's external outputs
# File lib/alien/alienreader.rb, line 148 def gpio=(newvalue) execute('externaloutput='+newvalue) end
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
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
# 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
# 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
# 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
# File lib/alien/alienreader.rb, line 89 def add_getset(cmd) add_get(cmd) add_set(cmd) end
# 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 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 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