class Domotics::Core::FileCameraDevice

Attributes

camera_element[R]
current_file_name[R]
mode[RW]

Public Class Methods

new(args = {}) click to toggle source
Calls superclass method Domotics::Core::Device::new
# File lib/domotics/core/device/file_camera_device.rb, line 7
def initialize(args = {})
  @sensors = []
  @current_file_name = nil
  @mode = args[:mode] || :watch
  # Emulate element
  args[:device] = self
  @camera_element = Domotics::FileCamera::CameraElement.new args
  # Path to shots
  @path = args[:path] || "/tmp"
  @path.chop! if @path[-1] == "/"
  # Shots file extension
  @file_ext = args[:file_ext] || ".jpg"
  @file_ext = ".#{@file_ext}" unless @file_ext[0] == "."
  # Remove old shots
  FileUtils.rm Dir.glob("#{@path}/*#{@file_ext}")
  # Watch for new shots
  Thread.new do
    INotify::Notifier.new.tap do |x|
      x.watch(@path, :create) { |event| event_handler event }
    end.run
  end
  super
end

Public Instance Methods

current_file() click to toggle source
# File lib/domotics/core/device/file_camera_device.rb, line 60
def current_file
  IO.read @current_file_name if @current_file_name
end
event_handler(event) click to toggle source
# File lib/domotics/core/device/file_camera_device.rb, line 35
def event_handler(event)
  return if File.extname(event.name) != @file_ext
  # Wait untill close file and rename it
  sleep 0.25
  case @mode
  when :save
    dir_name = Time.now.strftime("%Y%m%d")
    FileUtils.mkdir_p "#{@path}/#{dir_name}"
    @current_file_name = "#{@path}/#{dir_name}/#{Time.now.to_i}#{@file_ext}"
    File.rename "#{@path}/#{event.name}", @current_file_name
  when :watch
    @current_file_name = "#{@path}/current#{@file_ext}"
    File.rename "#{@path}/#{event.name}", @current_file_name
  else #:delete
    @current_file_name = nil
    FileUtils.rm "#{@path}/#{event.name}"
  end
  @sensors.each { |sensor| sensor.state_changed :motion_detection }
  @camera_element.state_changed :new_image
end
register_sensor(sensor) click to toggle source
# File lib/domotics/core/device/file_camera_device.rb, line 31
def register_sensor(sensor)
  @sensors << sensor
end