Class: IPWebcamSensor2020

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

Instance Method Summary collapse

Constructor Details

#initialize(url, interval: 4, quiet: 150, sound_threshold: 30, debug: false) ⇒ IPWebcamSensor2020

Returns a new instance of IPWebcamSensor2020.



11
12
13
14
15
16
17
18
# File 'lib/ipwebcam_sensor2020.rb', line 11

def initialize(url, interval: 4, quiet: 150, sound_threshold: 30, 
               debug: false)
  
  @url, @interval, @debug = url, interval, debug
  @quiet, @sound_threshold = quiet, sound_threshold
  @exe_motion, @exe_sound, @exe_motionsound = [], [], []
  
end

Instance Method Details

#onmotion_enter(&blk) ⇒ Object



20
21
22
# File 'lib/ipwebcam_sensor2020.rb', line 20

def onmotion_enter(&blk)
  @exe_motion[0] = blk if block_given?
end

#onmotion_leave(&blk) ⇒ Object



24
25
26
# File 'lib/ipwebcam_sensor2020.rb', line 24

def onmotion_leave(&blk)
  @exe_motion[1] = blk if block_given?
end

#onmotionsound_end(&blk) ⇒ Object



32
33
34
# File 'lib/ipwebcam_sensor2020.rb', line 32

def onmotionsound_end(&blk)
  @exe_motionsound[1] = blk if block_given?
end

#onmotionsound_start(&blk) ⇒ Object



28
29
30
# File 'lib/ipwebcam_sensor2020.rb', line 28

def onmotionsound_start(&blk)
  @exe_motionsound[0] = blk if block_given?
end

#onsound_end(&blk) ⇒ Object



40
41
42
# File 'lib/ipwebcam_sensor2020.rb', line 40

def onsound_end(&blk)
  @exe_sound[1] = blk if block_given?
end

#onsound_start(&blk) ⇒ Object



36
37
38
# File 'lib/ipwebcam_sensor2020.rb', line 36

def onsound_start(&blk)
  @exe_sound[0] = blk if block_given?
end

#startObject



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
# File 'lib/ipwebcam_sensor2020.rb', line 44

def start()

  @prev_motion, @prev_loud, @prev_motionsound = false, false, false
  @old_sound = []

  loop do 

    h = JSON.parse(open(@url).read, symbolize_names: true )

    motion = motion_active(h[:motion_active][:data])      
    sound = sound_event(h[:sound][:data])
    
    if @exe_motionsound[0] and motion and sound and not @prev_motionsound then
      @exe_motionsound[0].call
      @prev_motionsound = true
    elsif @exe_motionsound[1] and @prev_motionsound and not motion
      @exe_motionsound[1].call
      @prev_motionsound = false
    end
          
    puts h.inspect if @debug       

    sleep @interval
  end
end