domotics-core 0.1.3 → 0.1.4

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: 108fa6b03de1ea64aa02223d5a5066b1c817c215
4
- data.tar.gz: 5f49ff8821f7fa5a5a04f64e58edfd5e0aa18edb
3
+ metadata.gz: 885324956d9dd4c0e6da8465153c495b9584ec27
4
+ data.tar.gz: 97a20a0f02fbba7c1c7d4c1556c1e5ab638476d5
5
5
  SHA512:
6
- metadata.gz: b7e679fda2c5a15aa04b8b689f3fbbddba0633cc043358b4cf0b898af59daec328fbf072ad514986a9811259e86e9101e602666bf24bc5dfb5a15bfb3e389954
7
- data.tar.gz: 6e10403b7a3ce8cfdc42e71f6817d2dfe2f17fbe19acff6ded800d91d435e8fd42cba990018688ed306ab77400f4b4d13eb19e85e0c65e5549f82014cdecfa89
6
+ metadata.gz: 737f3b1537896b46d54f41fe430845964991d4fdc60a757c4cb1714207bcd811dc85ad46d78008c7ce57e3425787b31f8e376bd3cb34c5ee6e5dd577fe5efdf8
7
+ data.tar.gz: bf230dfc73db18a385c6e7c1775d3bd2c90bcc9fd1926697917e08bdfa4f66d8d8ca50e10bee08fc81e12b2cceea0fb882c1f8ad387e72663c578b0255d732c3
@@ -23,8 +23,8 @@ Gem::Specification.new do |spec|
23
23
  spec.add_runtime_dependency "hiredis"
24
24
  spec.add_runtime_dependency "mongo"
25
25
  spec.add_runtime_dependency "bson_ext"
26
-
27
26
  spec.add_runtime_dependency "rack"
27
+ spec.add_runtime_dependency "rb-inotify"
28
28
 
29
29
  spec.add_development_dependency "bundler"
30
30
  spec.add_development_dependency "rake"
@@ -1,12 +1,16 @@
1
+ require 'bundler/setup'
1
2
  # From data_mongo
2
3
  require 'mongo'
3
4
  # From data_redis
4
5
  require 'redis'
5
6
  require 'hiredis'
6
7
  # From arduino_board
8
+ #require "../domotics-arduino/lib/domotics/arduino"
7
9
  require 'domotics/arduino'
8
10
  # From server
9
11
  require 'json'
12
+ # From file_camera_device
13
+ require 'rb-inotify'
10
14
 
11
15
  module Domotics
12
16
  module Core
@@ -37,9 +41,9 @@ end
37
41
 
38
42
  gem_path = File.dirname(__FILE__)
39
43
  #require all
40
- Dir["#{gem_path}/core/data/*.rb"].each {|file| require file}
41
- Dir["#{gem_path}/core/*.rb"].each {|file| require file}
42
- Dir["#{gem_path}/core/helper/*.rb"].each {|file| require file}
44
+ %w(core/data/*.rb core/*.rb core/helper/*.rb file_camera/*.rb).each do |path|
45
+ Dir["#{gem_path}/#{path}"].each {|file| require file}
46
+ end
43
47
  # scan all devices and elements and populate class map
44
48
  [:device, :room, :element].each do |type|
45
49
  Dir["#{gem_path}/core/#{type}/*.rb"].each do |file|
@@ -0,0 +1,47 @@
1
+ module Domotics::Core
2
+ class FileCameraDevice < Device #__as__ :file_camera
3
+ def initialize(args = {})
4
+ # Emulate element
5
+ @current_link = "xxx"
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
12
+ # Path to shots
13
+ @path = args[:path] || "/tmp"
14
+ @path.chop! if @path[-1] == "/"
15
+ # Shots file extension
16
+ @file_ext = args[:file_ext] || ".jpg"
17
+ @file_ext = ".#{@file_ext}" unless @file_ext[0] == "."
18
+ # Remove old shots
19
+ FileUtils.rm Dir.glob("#{@path}/*#{@file_ext}")
20
+ # Watch for new shots
21
+ Thread.new do
22
+ INotify::Notifier.new.tap do |x|
23
+ x.watch(@path, :create) { |event| event_handler event }
24
+ end.run
25
+ end
26
+ super
27
+ end
28
+ def event_handler(event)
29
+ filename = "#{@path}/#{event.name}"
30
+ # Wait untill close file and rename it
31
+ inot = INotify::Notifier.new
32
+ inot.watch(filename, :close_write) do |file|
33
+ @current_name = "#{@path}/#{Time.now.to_i}#{@file_ext}"
34
+ sleep 0.1
35
+ File.rename filename, @current_name
36
+ end
37
+ inot.process
38
+ inot.close
39
+ end
40
+ def current_link
41
+ "#{@camera_element.room.name}/#{@camera_element.name}/file/#{Time.now.to_i}#{@file_ext}"
42
+ end
43
+ def current_file(*args)
44
+ IO.read @current_name
45
+ end
46
+ end
47
+ end
@@ -10,6 +10,12 @@ module Domotics::Core
10
10
  set_state(self.state || :off)
11
11
  end
12
12
 
13
+ def load_driver(args = {})
14
+ return unless args[:device_type]
15
+ device_space = args[:device_type].to_s.split("_").map{ |x| x.capitalize }.join
16
+ self.class.class_eval(%(include Domotics::#{device_space}::#{args[:driver]}), __FILE__, __LINE__)
17
+ end
18
+
13
19
  def state
14
20
  @@data[self].state
15
21
  end
@@ -19,13 +25,22 @@ module Domotics::Core
19
25
  { :elements =>
20
26
  { @name =>
21
27
  { :state => state,
22
- :info => (info if respond_to? :info)
28
+ :info => info,
29
+ :img => image,
23
30
  }
24
31
  }
25
32
  }
26
33
  }
27
34
  end
28
35
 
36
+ def info
37
+ nil
38
+ end
39
+
40
+ def image
41
+ nil
42
+ end
43
+
29
44
  def set_state(value)
30
45
  @@data[self].state = value
31
46
  @room.notify({ event: :state_set, element: self }) unless @type == :dimmer
@@ -5,10 +5,8 @@ module Domotics::Core
5
5
  @touch = args[:touch]
6
6
  @taped = true
7
7
  #@tap_lock = Mutex.new
8
- if args[:device_type]
9
- eval_str = %(include Domotics::#{args[:device_type].capitalize}::#{@touch ? 'DigitalSensor' : 'NOSensor'})
10
- self.class.class_eval(eval_str, __FILE__, __LINE__)
11
- end
8
+ args[:driver] = @touch ? "DigitalSensor" : "NOSensor"
9
+ load_driver args
12
10
  super
13
11
  end
14
12
  def set_state(*args)
@@ -12,10 +12,8 @@ module Domotics::Core
12
12
  @type = args[:type] || :dimmer
13
13
  @fade_lock = Mutex.new
14
14
  @fade_thread = nil
15
- if args[:device_type]
16
- eval_str = %(include Domotics::#{args[:device_type].capitalize}::PWMPin)
17
- self.class.class_eval(eval_str, __FILE__, __LINE__)
18
- end
15
+ args[:driver] = "PWMPin"
16
+ load_driver args
19
17
  super
20
18
  end
21
19
 
@@ -2,10 +2,8 @@ module Domotics::Core
2
2
  class MotionSensor < Element
3
3
  def initialize(args = {})
4
4
  @type = args[:type] || :motion_sensor
5
- if args[:device_type]
6
- eval_str = %(include Domotics::#{args[:device_type].capitalize}::DigitalSensor)
7
- self.class.class_eval(eval_str, __FILE__, __LINE__)
8
- end
5
+ args[:driver] = "DigitalSensor"
6
+ load_driver args
9
7
  super
10
8
  end
11
9
  #def to_hls(state)
@@ -2,10 +2,8 @@ module Domotics::Core
2
2
  class ReedSwitch < Element
3
3
  def initialize(args = {})
4
4
  @type = args[:type] || :reed_switch
5
- if args[:device_type]
6
- eval_str = %(include Domotics::#{args[:device_type].capitalize}::NCSensor)
7
- self.class.class_eval(eval_str, __FILE__, __LINE__)
8
- end
5
+ args[:driver] = "NCSensor"
6
+ load_driver args
9
7
  super
10
8
  end
11
9
  #def to_hls(state)
@@ -5,10 +5,8 @@ module Domotics::Core
5
5
  @type = args[:type] || :switch
6
6
  @lag = nil
7
7
  @lag_lock = Mutex.new
8
- if args[:device_type]
9
- eval_str = %(include Domotics::#{args[:device_type].capitalize}::DigitalPin)
10
- self.class.class_eval(eval_str, __FILE__, __LINE__)
11
- end
8
+ args[:driver] = "DigitalPin"
9
+ load_driver args
12
10
  @initialized = false
13
11
  super
14
12
  @initialized = true
@@ -65,7 +65,7 @@ module Domotics::Core
65
65
  end
66
66
 
67
67
  class Object
68
- def eigenclass
68
+ def eigenclass
69
69
  class << self
70
70
  self
71
71
  end
@@ -5,7 +5,9 @@ module Domotics::Core
5
5
  end
6
6
  def call(env)
7
7
  # [object]/[action]/[params]
8
+ p env['PATH_INFO']
8
9
  request = env['PATH_INFO'][1..-1].split('/')
10
+ request[-1], form = request.last.split(".")
9
11
  object = request.shift
10
12
  return invalid 'room' unless object and object = Room[object.to_sym]
11
13
  return invalid 'element or action' unless object_action = request.shift
@@ -18,13 +20,20 @@ module Domotics::Core
18
20
  end
19
21
  return invalid 'action' unless action and object.respond_to? action
20
22
  begin
21
- object.public_send(action, *request.map { |param| param.to_isym })
23
+ result = object.public_send(action, *request.map { |param| param.to_isym })
22
24
  rescue Exception => e
23
25
  @logger.error { e.message }
24
26
  @logger.debug { e }
25
27
  return invalid 'request'
26
28
  end
27
- return ok object.verbose_state.to_json
29
+ case form
30
+ when "json"
31
+ return ok object.verbose_state.to_json
32
+ when "jpg"
33
+ return jpg result
34
+ else
35
+ return ok object.verbose_state
36
+ end
28
37
  end
29
38
 
30
39
  private
@@ -35,5 +44,8 @@ module Domotics::Core
35
44
  def ok(param)
36
45
  [200, {"Content-Type" => "text/html"}, [param]]
37
46
  end
47
+ def jpg(param)
48
+ [200, {"Content-Type" => "image/jpeg"}, [param]]
49
+ end
38
50
  end
39
51
  end
@@ -43,6 +43,7 @@ module Domotics::Core
43
43
  def device(klass, args = {})
44
44
  @current_device[:name] = args[:name]
45
45
  @current_device[:type] = args[:type]
46
+ args[:room] = Room[@current_room[:name]]
46
47
  klass.new(args) unless Device[args[:name]]
47
48
  yield if ::Kernel.block_given?
48
49
  @current_device.clear
@@ -1,5 +1,5 @@
1
1
  module Domotics
2
2
  module Core
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -0,0 +1,6 @@
1
+ module Domotics
2
+ module FileCamera
3
+ module DigitalSensor
4
+ end
5
+ end
6
+ end
@@ -1,4 +1,7 @@
1
1
  test_room :test do
2
+ file_camera :cam do
3
+ motion_sensor :cam_motion
4
+ end
2
5
  button :button_no_dev
3
6
  dimmer :dimmer_no_dev
4
7
  motion_sensor :ms_no_dev
@@ -13,4 +16,4 @@ test_room :test do
13
16
  rgb_strip :rgb, r: 9, g: 10, b: 11
14
17
  switch :light_1, pin: 13
15
18
  end
16
- end
19
+ end
@@ -6,4 +6,6 @@ require "domotics/core"
6
6
  Domotics::Core::TestHelper.init
7
7
 
8
8
  class DomoticsDevicesTestCase < Test::Unit::TestCase
9
+ def test_file_camera
10
+ end
9
11
  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.3
4
+ version: 0.1.4
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-03 00:00:00.000000000 Z
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: domotics-arduino
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rb-inotify
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: bundler
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -169,6 +183,7 @@ files:
169
183
  - lib/domotics/core/data/data_redis.rb
170
184
  - lib/domotics/core/device.rb
171
185
  - lib/domotics/core/device/arduino_board.rb
186
+ - lib/domotics/core/device/file_camera_device.rb
172
187
  - lib/domotics/core/element.rb
173
188
  - lib/domotics/core/element/button.rb
174
189
  - lib/domotics/core/element/dimmer.rb
@@ -182,6 +197,7 @@ files:
182
197
  - lib/domotics/core/server.rb
183
198
  - lib/domotics/core/setup.rb
184
199
  - lib/domotics/core/version.rb
200
+ - lib/domotics/file_camera/digital_sensor.rb
185
201
  - test/config.test.rb
186
202
  - test/test_devices.rb
187
203
  - test/test_elements.rb