Class: RfidUsbGomSensor::Node

Inherits:
Gom::Remote::Entry
  • Object
show all
Includes:
OAttr
Defined in:
lib/rfid-usb-gom-sensor/node.rb

Defined Under Namespace

Classes: BrokenRead

Constant Summary collapse

Defaults =
{
  :device => "/dev/ttyUSB0", 
  :baudrate => 9600,
  :legacy => false,
  :hex_id => false,
}
STX =

reader commands

0x02
ETX =
0x03
GetInfo =
[STX, 0x01, 0x47, 0x46, ETX].pack "c*"
GoContinuous =
[STX, 0x01, 0x43, 0x04, 0x46, ETX].pack "c*"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Node

Returns a new instance of Node.



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rfid-usb-gom-sensor/node.rb', line 91

def initialize path, options = {}
  @path = path
  @options = Defaults.merge(gnode @path).merge(options)
  puts "options: #{@options.inspect}"

  @ip = connection.callback_server.host
  @sp = SerialPort.new device, baudrate

  @sensor_tags = {}.extend(MonitorMixin)
  @gom_tags = find_tags
end

Class Method Details

.calc_checksum(blk) ⇒ Object



184
185
186
# File 'lib/rfid-usb-gom-sensor/node.rb', line 184

def calc_checksum blk
  (blk.unpack "C*").inject(0) {|cc, c| cc^c}
end

.decode(blk, opts = { }) ⇒ Object

blk format:

 0 STX  0x02
 1 ADR  0x01
 2 UID7 0xE0
 3 UID6  
 4 UID5  
 5 UID4  
 6 UID3  
 7 UID2  
 8 UID1  
 9 UID0  
10 ERR  0x00
11 BCC  XOR[1..10]
12 ETX  0x03

 :legacy => true -> makes this compatible to the tag id readout code
 in: rfid-readers/app/models/rfid_reader.rb


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rfid-usb-gom-sensor/node.rb', line 163

def decode blk, opts = { }
  opts = ({:hex_id => false, :legacy => false }.merge opts)
  #validate 
  rec = (blk.unpack 'c2Qc*')

  if opts[:legacy]
    id = rec[2]
    rec[2] = Integer("0x#{(([id].pack 'q').unpack 'H*').first}")
  end
  if opts[:hex_id]
    id = rec[2]
    rec[2] = "0x#{([id].pack 'Q').unpack 'H*'}"
  end
  expected = rec[4]
  cc = (calc_checksum blk[1..10]) # (blk[1..10].unpack "C*").inject(0) {|cc, c| cc^c}
  (cc == expected) or (raise_broken_read cc, expected, blk)
  #checksum blk[1..10], rec[4]
  #puts "-#{rec.inspect}-"
  rec
end

.raise_broken_read(cc, expected, buf) ⇒ Object

Raises:



139
140
141
142
# File 'lib/rfid-usb-gom-sensor/node.rb', line 139

def raise_broken_read cc, expected, buf
  msg = "checksum missmatch: #{cc} != #{expected} -- #{buf.unpack 'H*'}"
  raise BrokenRead, msg
end

Instance Method Details

#detectObject



189
190
191
192
193
194
195
196
197
198
# File 'lib/rfid-usb-gom-sensor/node.rb', line 189

def detect
  loop do
    uid = next_tag
    @sensor_tags.synchronize { @sensor_tags[uid] = Time.now }
  end
rescue BrokenRead => e
  (resync_from_broken_read e) and retry
rescue SignalException => e
  :stop # if (e.signm == "INT")
end

#find_tagsObject



107
108
109
110
111
112
113
114
# File 'lib/rfid-usb-gom-sensor/node.rb', line 107

def find_tags
  tag_node = X::Entry.find "#{@path}/tags"
  #puts "-->#{tag_node.inspect}<---"
  #puts "--->\n#{tag_node}\n<---"
  tag_node.entries.inject({}) do |tags, node|
    tags.update node.name => node.mtime
  end
end

#hex_idObject



103
104
105
# File 'lib/rfid-usb-gom-sensor/node.rb', line 103

def hex_id
  @hex_id ||= @options[:hex_id] 
end

#prerollObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rfid-usb-gom-sensor/node.rb', line 122

def preroll
  #info = [0x02, 0x01, 0x47, 0x46, 0x03]
  #info.each {|c| @sp.putc c.to_i}
  @sp.puts GetInfo
  2.times { @sp.getc}
  print "Detected reader: "
  37.times {putc(@sp.getc)}
  print "\n"
  6.times {@sp.getc}

  #continuous = [0x02, 0x01, 0x43, 0x04, 0x46, 0x03]
  #continuous.each {|c| @sp.putc c.to_i}
  @sp.puts GoContinuous
  6.times {@sp.getc}
end

#push_active_tags_to_gomObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rfid-usb-gom-sensor/node.rb', line 200

def push_active_tags_to_gom
  active_tags = []
  @sensor_tags.synchronize do
    active_tags = @sensor_tags.keys
    @sensor_tags.clear
  end
  #puts "active tags: #{active_tags.inspect}"
  #connection.write "#{@path}/tags/#{uid}:sensor_ip", @ip

  # 1. which gom tags are not longer active?
  active_tags.each { |tag| @gom_tags.delete tag }
  (to_be_deleted = @gom_tags.keys).each do |tag_id|
    puts "disappeared: #{@path}/tags/#{tag_id}"
    connection.destroy "#{@path}/tags/#{tag_id}"
  end
  @gom_tags.clear

  # 2. active_tags becomes the new gom_tags
  active_tags.each do |tag_id|
    @gom_tags[tag_id] = Time.now
    puts "     active: #{@path}/tags/#{tag_id}"
    connection.write "#{@path}/tags/#{tag_id}:sensor_ip", @ip
  end
end