class IPWebcamSensor2020
Public Class Methods
new(url, interval: 4, quiet: 150, sound_threshold: 30, debug: false)
click to toggle source
# 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
Public Instance Methods
onmotion_enter(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 20 def onmotion_enter(&blk) @exe_motion[0] = blk if block_given? end
onmotion_leave(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 24 def onmotion_leave(&blk) @exe_motion[1] = blk if block_given? end
onmotionsound_end(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 32 def onmotionsound_end(&blk) @exe_motionsound[1] = blk if block_given? end
onmotionsound_start(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 28 def onmotionsound_start(&blk) @exe_motionsound[0] = blk if block_given? end
onsound_end(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 40 def onsound_end(&blk) @exe_sound[1] = blk if block_given? end
onsound_start(&blk)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 36 def onsound_start(&blk) @exe_sound[0] = blk if block_given? end
start()
click to toggle source
# 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
Private Instance Methods
motion_active(a)
click to toggle source
Handles 1 or more motion_active
results of value 1.0 or 0.0 require url to contain sense param with value motion_active
# File lib/ipwebcam_sensor2020.rb, line 75 def motion_active(a) rawtime, raw_motion = a.last time = Time.at(rawtime.to_s[0..-4].to_i) motion = raw_motion.first.to_i == 1 if @exe_motion[0] and motion and not @prev_motion then @exe_motion[0].call() elsif @exe_motion[1] and not motion and @prev_motion then @exe_motion[1].call() end @prev_motion = motion puts [time, motion] if @debug return motion == true end
sound_event(a)
click to toggle source
# File lib/ipwebcam_sensor2020.rb, line 100 def sound_event(a) soundlevels = if @old_sound.any? then all_sound = (@old_sound + a).uniq timestamp = @old_sound.last[0] all_sound.reverse.take_while {|x,y| x != timestamp}.reverse else a end levels = soundlevels.map do |rawtime, raw_vol| time = Time.at(rawtime.to_s[0..-4].to_i) vol = raw_vol.first vol end threshold = @quiet + @sound_threshold = 30 loud = levels.max > threshold puts loud ? 'noisy' : 'quiet' if @debug if @exe_sound[0] and loud and not @prev_loud then @exe_sound[0].call elsif @exe_sound[1] and not loud and @prev_loud @exe_sound[1].call end @prev_loud = loud @old_sound = a return loud == true end