Class: Domotics::Core::FileCameraDevice

Inherits:
Device show all
Defined in:
lib/domotics/core/device/file_camera_device.rb

Overview

__as__ :file_camera

Instance Attribute Summary collapse

Attributes inherited from Device

#name, #type

Instance Method Summary collapse

Methods inherited from Device

[], #destroy, #to_s

Constructor Details

#initialize(args = {}) ⇒ FileCameraDevice

Returns a new instance of FileCameraDevice.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#camera_elementObject (readonly)

Returns the value of attribute camera_element.



5
6
7
# File 'lib/domotics/core/device/file_camera_device.rb', line 5

def camera_element
  @camera_element
end

#current_file_nameObject (readonly)

Returns the value of attribute current_file_name.



5
6
7
# File 'lib/domotics/core/device/file_camera_device.rb', line 5

def current_file_name
  @current_file_name
end

#modeObject

Returns the value of attribute mode.



4
5
6
# File 'lib/domotics/core/device/file_camera_device.rb', line 4

def mode
  @mode
end

Instance Method Details

#current_fileObject



60
61
62
# File 'lib/domotics/core/device/file_camera_device.rb', line 60

def current_file
  IO.read @current_file_name if @current_file_name
end


56
57
58
# File 'lib/domotics/core/device/file_camera_device.rb', line 56

def current_link
  "#{@camera_element.room.name}/#{@camera_element.name}/file/#{Time.now.to_i}#{@file_ext}"
end

#event_handler(event) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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) ⇒ Object



31
32
33
# File 'lib/domotics/core/device/file_camera_device.rb', line 31

def register_sensor(sensor)
  @sensors << sensor
end