Class: TagIt::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/tag_it/monitor.rb

Direct Known Subclasses

TagSnapshot, TagTracker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local_port) ⇒ Monitor

Returns a new instance of Monitor.



5
6
7
8
# File 'lib/tag_it/monitor.rb', line 5

def initialize(local_port)
  self.port = local_port
  @last_pulse = Time.now
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



3
4
5
# File 'lib/tag_it/monitor.rb', line 3

def port
  @port
end

Instance Method Details

#monitor_tagsObject



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
# File 'lib/tag_it/monitor.rb', line 10

def monitor_tags
  char = nil
  tag_name = ""
  #dont start reporting until after the first space, 
  #so we don't have to deal with partial tagnames
  clean_start_flag = false 
  
  #128 is the stop character we're adding so the tests can cutoff the loop.  
  while char != 128  
    # Don't take longer than 3 seconds to find a char, or there aren't any
    Timeout::timeout(3) do
      char = @port.getc
    end
    if char == 32 
      name,strength = split_tag_data(tag_name)
      yield(name,strength) if clean_start_flag
      tag_name = ""
      clean_start_flag = true #here's a space, start sending tags
    else
      tag_name = "#{tag_name}#{char.chr}"
    end
    if ((Time.now - 180) > @last_pulse)
      self.pulse!
    end
  end
end

#pulse!Object



41
42
43
# File 'lib/tag_it/monitor.rb', line 41

def pulse!
  #do nothing, you can override in a subclass if you want to fire an event or something
end

#split_tag_data(tag_name) ⇒ Object



37
38
39
# File 'lib/tag_it/monitor.rb', line 37

def split_tag_data(tag_name)
  [tag_name[0,4],tag_name[4,tag_name.size - 4].to_i]
end