domotics-core 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eca10e6f705a187186a2656342714b990f23eeee
4
- data.tar.gz: 261c537133b2ac6ee8d15a2de0632785cc5cfa33
3
+ metadata.gz: 74cbeb6f3ebcc02fddd4fdd6a4b980aad204da2a
4
+ data.tar.gz: 9d7c8c5d0fbeb9bdb9c8f4e7e4042c420fca3af2
5
5
  SHA512:
6
- metadata.gz: f403cc9cd6974724d3e23b94c9473acf632280cd34c266065a1fe0cf56ca2ece20375715566a81c5e3e7039c99e7c2b34418b3e1a9e93ff3171233f555987e12
7
- data.tar.gz: 82762b63df335fb078dc48b03c6017468c86592b33a693be8df4a8bf7c57f196e525b96c12e91f0938d82580c1d1b833f4279c4aad1eac9cedabc7a3c6dfcb30
6
+ metadata.gz: d210cd402ee0601e23168fb037b19650e5a570737aa82f5ec1fcbcb315fc40d6978478fa7ad1a1b1b74b6adaddde42770cfb54637bd4f682447842d6e1a9686d
7
+ data.tar.gz: 0ade937876a477a4a3594875766b19a2f32a0ce01e09004f1bf822f0f4efc1b17a0725dc2004410eda8ef61a7f134364c1ef714e19b69037d3acf5bc04c82e2d
@@ -1,14 +1,13 @@
1
1
  module Domotics::Core
2
2
  class FileCameraDevice < Device #__as__ :file_camera
3
+ attr_accessor :mode
4
+ attr_reader :current_file_name, :camera_element
3
5
  def initialize(args = {})
4
- @current_name = nil
6
+ @current_file_name = nil
7
+ @mode = args[:mode] || :watch
5
8
  # Emulate element
6
- @camera_element = Element.new args
7
- s = self
8
- image_lambda = lambda { s.current_link }
9
- file_lambda = lambda { |*args| s.current_file }
10
- @camera_element.eigenclass.send :define_method, :image, image_lambda
11
- @camera_element.eigenclass.send :define_method, :file, file_lambda
9
+ args[:device] = self
10
+ @camera_element = Domotics::FileCamera::CameraElement.new args
12
11
  # Path to shots
13
12
  @path = args[:path] || "/tmp"
14
13
  @path.chop! if @path[-1] == "/"
@@ -26,25 +25,28 @@ module Domotics::Core
26
25
  super
27
26
  end
28
27
  def event_handler(event)
29
- filename = "#{@path}/#{event.name}"
30
- @current_name = "#{@path}/#{Time.now.to_i}#{@file_ext}"
28
+ return if File.extname(event.name) != @file_ext
31
29
  # Wait untill close file and rename it
32
- #inot = INotify::Notifier.new
33
- #inot.watch(filename, :close_write) do |file|
34
- # @current_name = "#{@path}/#{Time.now.to_i}#{@file_ext}"
35
- # sleep 0.1
36
- # File.rename filename, @current_name
37
- #end
38
- #inot.process
39
- #inot.close
40
- sleep 0.5
41
- File.rename filename, @current_name
30
+ sleep 0.25
31
+ case @mode
32
+ when :save
33
+ dir_name = Time.now.strftime("%Y%m%d")
34
+ FileUtils.mkdir_p "#{@path}/#{dir_name}"
35
+ @current_file_name = "#{@path}/#{dir_name}/#{Time.now.to_i}#{@file_ext}"
36
+ File.rename "#{@path}/#{event.name}", @current_file_name
37
+ when :watch
38
+ @current_file_name = "#{@path}/current#{@file_ext}"
39
+ File.rename "#{@path}/#{event.name}", @current_file_name
40
+ else #:delete
41
+ @current_file_name = nil
42
+ FileUtils.rm "#{@path}/#{event.name}"
43
+ end
42
44
  end
43
45
  def current_link
44
46
  "#{@camera_element.room.name}/#{@camera_element.name}/file/#{Time.now.to_i}#{@file_ext}"
45
47
  end
46
48
  def current_file
47
- IO.read @current_name if @current_name
49
+ IO.read @current_file_name if @current_file_name
48
50
  end
49
51
  end
50
52
  end
@@ -3,8 +3,9 @@ module Domotics::Core
3
3
  @@devices = {}
4
4
  attr_reader :name, :type
5
5
  def initialize(args = {})
6
- @@devices[@name = args[:name]] = self
7
- @type = args[:type]
6
+ @name = args[:name] || SecureRandom.hex
7
+ @@devices[@name] = self
8
+ @type = args[:type] || "undefined"
8
9
  end
9
10
 
10
11
  def self.[](symbol = nil)
@@ -5,6 +5,7 @@ module Domotics::Core
5
5
 
6
6
  def initialize(args = {})
7
7
  @room = args[:room]
8
+ @device = args[:device]
8
9
  @room.register_element self, @name = args[:name]
9
10
  @type ||= :element
10
11
  set_state(self.state || :off)
@@ -1,5 +1,5 @@
1
1
  module Domotics
2
2
  module Core
3
- VERSION = "0.1.8"
3
+ VERSION = "0.1.9"
4
4
  end
5
5
  end
data/lib/domotics/core.rb CHANGED
@@ -1,16 +1,18 @@
1
1
  require 'bundler/setup'
2
+ # From arduino_board
3
+ #require "../domotics-arduino/lib/domotics/arduino"
4
+ require 'domotics/arduino'
2
5
  # From data_mongo
3
6
  require 'mongo'
4
7
  # From data_redis
5
8
  require 'redis'
6
9
  require 'hiredis'
7
- # From arduino_board
8
- #require "../domotics-arduino/lib/domotics/arduino"
9
- require 'domotics/arduino'
10
- # From server
11
- require 'json'
10
+ # From device
11
+ require 'securerandom'
12
12
  # From file_camera_device
13
13
  require 'rb-inotify'
14
+ # From server
15
+ require 'json'
14
16
 
15
17
  module Domotics
16
18
  module Core
@@ -0,0 +1,16 @@
1
+ module Domotics
2
+ module FileCamera
3
+ class CameraElement < Domotics::Core::Element
4
+ def image
5
+ @device.current_link
6
+ end
7
+ def file
8
+ @device.current_file
9
+ end
10
+ def mode(param)
11
+ @device.mode = param
12
+ set_state param
13
+ end
14
+ end
15
+ end
16
+ end
data/test/test_devices.rb CHANGED
@@ -7,5 +7,36 @@ Domotics::Core::TestHelper.init
7
7
 
8
8
  class DomoticsDevicesTestCase < Test::Unit::TestCase
9
9
  def test_file_camera
10
+ # General
11
+ cam = Domotics::Core::Device[:cam]
12
+ assert_equal "cam", cam.name.to_s
13
+ assert_equal "test/cam/file/#{Time.now.to_i}.jpg", cam.current_link
14
+ # Watch mode (default)
15
+ %x{echo "[test foto content]" > /tmp/test_foto_1.jpg}
16
+ sleep 0.5
17
+ assert_equal "/tmp/current.jpg", cam.current_file_name
18
+ assert_equal "[test foto content]", cam.current_file.chomp
19
+ # Save mode
20
+ cam.mode = :save
21
+ dir_name = Time.now.strftime("%Y%m%d")
22
+ %x{rm -rf /tmp/#{dir_name}}
23
+ %x{echo "[test foto content]" > /tmp/test_foto_2.jpg}
24
+ name = Time.now.to_i
25
+ sleep 0.5
26
+ assert_match /\/tmp\/#{dir_name}\/#{name/100}\d{2}.jpg/, cam.current_file_name
27
+ assert_equal "[test foto content]", cam.current_file.chomp
28
+ # Delete mode
29
+ cam.mode = :delete
30
+ %x{echo "[test foto content]" > /tmp/test_foto_3.jpg}
31
+ sleep 0.5
32
+ assert_nil cam.current_file_name
33
+ assert_nil cam.current_file
34
+ # Test CameraElement
35
+ assert_equal "test/cam/file/#{Time.now.to_i}.jpg", cam.camera_element.image
36
+ cam.camera_element.mode :watch
37
+ assert_equal :watch, cam.camera_element.state
38
+ %x{echo "[test foto content]" > /tmp/test_foto_4.jpg}
39
+ sleep 0.5
40
+ assert_equal "[test foto content]", cam.camera_element.file.chomp
10
41
  end
11
- end
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domotics-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - goredar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-31 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: domotics-arduino
@@ -197,6 +197,7 @@ files:
197
197
  - lib/domotics/core/server.rb
198
198
  - lib/domotics/core/setup.rb
199
199
  - lib/domotics/core/version.rb
200
+ - lib/domotics/file_camera/camera_element.rb
200
201
  - lib/domotics/file_camera/digital_sensor.rb
201
202
  - test/config.test.rb
202
203
  - test/test_devices.rb