Class: HcitoolsWrapper::Detect

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

Instance Method Summary collapse

Constructor Details

#initialize(bd_address: '', interval: 0, verbose: false) ⇒ Detect

Returns a new instance of Detect.



34
35
36
37
38
39
40
# File 'lib/hcitools_wrapper.rb', line 34

def initialize(bd_address: '', interval: 0, verbose: false)

  @bd_address = bd_address      
  @interval = interval
  @verbose = verbose

end

Instance Method Details

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hcitools_wrapper.rb', line 42

def start()

  id = @bd_address.split(':').reverse.join(' ')
  found, last_found = false, Time.now - 60
  a = []

  t3 = Time.now + @interval

  IO.popen('sudo hcidump --raw').each_line do |x| 
    
    found = if found then

      rssi = (x.split.last.hex - 256)
      a << rssi.to_i unless a.include? rssi.to_i
      h = {bdaddress: @bd_address, rssi: rssi.inspect}

      if t3 <= Time.now then

        avg = a.max + (a.min - a.max) / 2

        if @verbose then
          puts Time.now.inspect + ':  ' + h.inspect
          puts "max: %s, min: %s, average: %s, a: %s" % \
                                  [a.max, a.min, avg, a.sort.inspect]

        end
        
        recent_movement = Time.now - last_found >= 60

        # the RSSI will vary by up to 10 when the device is stationary
        if recent_movement or a.length > 10 or rssi > (a.max + 10) or rssi < (a.min - 10) then

          puts 'movement!' if @verbose
          
          if block_given? then
            yield a, avg
          end

          a = []
        end

        t3 = Time.now + @interval
      end
      
      last_found = Time.now

      false

    else
      x.include?(id)
    end

  end

end