Class: RfidUsbGomSensor::Node
- Inherits:
-
Gom::Remote::Entry
- Object
- Gom::Remote::Entry
- RfidUsbGomSensor::Node
- 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
- .calc_checksum(blk) ⇒ Object
-
.decode(blk, opts = { }) ⇒ Object
blk format:.
- .raise_broken_read(cc, expected, buf) ⇒ Object
Instance Method Summary collapse
- #detect ⇒ Object
- #find_tags ⇒ Object
- #hex_id ⇒ Object
-
#initialize(path, options = {}) ⇒ Node
constructor
A new instance of Node.
- #preroll ⇒ Object
- #push_active_tags_to_gom ⇒ Object
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, = {} @path = path @options = Defaults.merge(gnode @path).merge() puts "options: #{@options.inspect}" @ip = connection.callback_server.host @sp = SerialPort.new device, baudrate @sensor_tags = {}.extend(MonitorMixin) @gom_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
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
#detect ⇒ Object
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_tags ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/rfid-usb-gom-sensor/node.rb', line 107 def tag_node = X::Entry.find "#{@path}/tags" #puts "-->#{tag_node.inspect}<---" #puts "--->\n#{tag_node}\n<---" tag_node.entries.inject({}) do |, node| .update node.name => node.mtime end end |
#hex_id ⇒ Object
103 104 105 |
# File 'lib/rfid-usb-gom-sensor/node.rb', line 103 def hex_id @hex_id ||= @options[:hex_id] end |
#preroll ⇒ Object
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_gom ⇒ Object
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 = [] @sensor_tags.synchronize do = @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? .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 .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 |