Class: PhidgetRfid::RfidHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/phidget_rfid/rfid_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRfidHandler

Returns a new instance of RfidHandler.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/phidget_rfid/rfid_handler.rb', line 8

def initialize()
  @rfid = Phidgets::RFID.new
  @time = Time.new
  @prompt = "at #{@time.strftime("%Y-%m-%d %H:%M:%S")} \033[0m"
  @log = ""
  
  @rfid.on_detach  do |device, obj|
    @log.append "\n#{device.attributes.inspect} removed"
  end
  
  @rfid.on_attach  do |device, obj|
    @rfid.antenna = true
    @rfid.led = false
  end
  
  @rfid.on_error do |device, obj, code, description|
    @log << "\033[31m\nError #{code}: #{description} #{@prompt}" 
  end
  
  @rfid.on_tag  do |device, tag, obj|
    @rfid.led = true
    @log << "\n\033[32m[+] #{protocol} tag detected #{@prompt}" 
  end
  
  @rfid.on_tag_lost do |device, tag, obj|
    @rfid.led = false
    @log << "\n\033[33m[-] #{protocol} tag removed #{@prompt}"
  end
  
  
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



6
7
8
# File 'lib/phidget_rfid/rfid_handler.rb', line 6

def log
  @log
end

Instance Method Details

#deviceObject



68
69
70
71
72
73
74
# File 'lib/phidget_rfid/rfid_handler.rb', line 68

def device
  puts "Library Version: #{Phidgets::FFI.library_version}"
  puts "Class: #{device.device_class}"
  puts "Id: #{device.id}"
  puts "Serial number: #{device.serial_number}"
  puts "# Digital Outputs: #{device.outputs.size}"
end

#protocolObject



40
41
42
43
44
# File 'lib/phidget_rfid/rfid_handler.rb', line 40

def protocol
  if(@rfid.attached?)
    @rfid.last_tag_protocol
  end
end

#readObject



46
47
48
49
50
# File 'lib/phidget_rfid/rfid_handler.rb', line 46

def read
  if(@rfid.attached?)
    @rfid.last_tag
  end
end

#write(value, protocol = @rfid.last_tag_protocol, lock = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/phidget_rfid/rfid_handler.rb', line 52

def write(value, protocol = @rfid.last_tag_protocol, lock = false)
  if(@rfid.attached?)
    p = protocol.to_sym

    if lock
      puts "Data: #{value}"
      puts "Protocol: #{p}"
      puts "Are you sure you want to lock the RFID Tag? This will make it read-only (y/N)"
      r = gets.chomp
      @rfid.write value, p, true if r == "y"

    end

  end
end