domotics-core 0.2.1 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/domotics-core.gemspec +13 -14
- data/lib/domotics/core/device/file_camera_device.rb +12 -0
- data/lib/domotics/core/element.rb +2 -2
- data/lib/domotics/core/element/button.rb +8 -17
- data/lib/domotics/core/element/motion_sensor.rb +3 -3
- data/lib/domotics/core/element/reed_switch.rb +3 -3
- data/lib/domotics/core/element/switch.rb +19 -9
- data/lib/domotics/core/helper/helper.rb +1 -1
- data/lib/domotics/core/room.rb +1 -1
- data/lib/domotics/core/server.rb +2 -4
- data/lib/domotics/core/version.rb +1 -1
- data/lib/domotics/core/ws_server.rb +13 -7
- data/lib/domotics/file_camera/camera_element.rb +1 -1
- data/lib/domotics/file_camera/digital_sensor.rb +7 -0
- data/test/test_devices.rb +1 -0
- data/test/test_elements.rb +11 -1
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aa3e061dd51a3ebe28885793ab969d98f67df951c4fec5ff3e90bd48b1c76f89
|
4
|
+
data.tar.gz: 175278d956f8ccafda95bb89a371172181581c297f3fb9cad6eb9a7e4589027e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67c0ff46d5504c10d22278e300a57047fe45d23310890cb56c2153fd0c6f0e2144efabca02a5f37d7f6b022b45fd61ae387e5fac65cce5cc7443bc86740081f8
|
7
|
+
data.tar.gz: 12f3e5e769cb24e392f48c0c10afe6e9d962381294c74588e2372752b005b5f50498ded8a4ce0a6db82936abe9be61384f30a6bb145cb65bdb9226e33293e449
|
data/domotics-core.gemspec
CHANGED
@@ -1,21 +1,20 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require "domotics/core/version"
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name
|
8
|
-
spec.version
|
9
|
-
spec.authors
|
10
|
-
spec.email
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
14
|
-
spec.license
|
6
|
+
spec.name = "domotics-core"
|
7
|
+
spec.version = Domotics::Core::VERSION
|
8
|
+
spec.authors = ["goredar"]
|
9
|
+
spec.email = ["info@goredar.it"]
|
10
|
+
spec.summary = "Home automation system."
|
11
|
+
spec.description = "Main core elements"
|
12
|
+
spec.homepage = "https://goredar.it"
|
13
|
+
spec.license = "MIT"
|
15
14
|
|
16
|
-
spec.files
|
17
|
-
spec.executables
|
18
|
-
spec.test_files
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
18
|
spec.require_paths = ["lib"]
|
20
19
|
|
21
20
|
spec.add_runtime_dependency "domotics-arduino"
|
@@ -1,8 +1,11 @@
|
|
1
1
|
module Domotics::Core
|
2
2
|
class FileCameraDevice < Device #__as__ :file_camera
|
3
|
+
|
3
4
|
attr_accessor :mode
|
4
5
|
attr_reader :current_file_name, :camera_element
|
6
|
+
|
5
7
|
def initialize(args = {})
|
8
|
+
@sensors = []
|
6
9
|
@current_file_name = nil
|
7
10
|
@mode = args[:mode] || :watch
|
8
11
|
# Emulate element
|
@@ -24,6 +27,11 @@ module Domotics::Core
|
|
24
27
|
end
|
25
28
|
super
|
26
29
|
end
|
30
|
+
|
31
|
+
def register_sensor(sensor)
|
32
|
+
@sensors << sensor
|
33
|
+
end
|
34
|
+
|
27
35
|
def event_handler(event)
|
28
36
|
return if File.extname(event.name) != @file_ext
|
29
37
|
# Wait untill close file and rename it
|
@@ -41,10 +49,14 @@ module Domotics::Core
|
|
41
49
|
@current_file_name = nil
|
42
50
|
FileUtils.rm "#{@path}/#{event.name}"
|
43
51
|
end
|
52
|
+
@sensors.each { |sensor| sensor.state_changed :motion_detection }
|
53
|
+
@camera_element.state_changed :new_image
|
44
54
|
end
|
55
|
+
|
45
56
|
def current_link
|
46
57
|
"#{@camera_element.room.name}/#{@camera_element.name}/file/#{Time.now.to_i}#{@file_ext}"
|
47
58
|
end
|
59
|
+
|
48
60
|
def current_file
|
49
61
|
IO.read @current_file_name if @current_file_name
|
50
62
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Domotics::Core
|
2
2
|
class Element
|
3
3
|
@@data = DataHash.new
|
4
|
-
attr_reader :name, :type, :room
|
4
|
+
attr_reader :name, :type, :room, :device
|
5
5
|
|
6
6
|
def initialize(args = {})
|
7
7
|
@room = args[:room]
|
@@ -57,7 +57,7 @@ module Domotics::Core
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def to_s
|
60
|
-
"
|
60
|
+
"#{@room.name}@#{@name}(id:#{__id__})"
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
@@ -3,35 +3,26 @@ module Domotics::Core
|
|
3
3
|
def initialize(args = {})
|
4
4
|
@type = args[:type] || :button
|
5
5
|
@touch = args[:touch]
|
6
|
-
@
|
7
|
-
#@tap_lock = Mutex.new
|
6
|
+
@last_on = nil
|
8
7
|
args[:driver] = @touch ? "DigitalSensor" : "NOSensor"
|
9
8
|
load_driver args
|
10
9
|
super
|
11
10
|
end
|
12
|
-
|
11
|
+
|
12
|
+
def set_state(*_args)
|
13
13
|
nil
|
14
14
|
end
|
15
15
|
|
16
16
|
def state_changed(value)
|
17
17
|
case value
|
18
18
|
when :on
|
19
|
-
|
19
|
+
@last_on = Time.now
|
20
20
|
when :off
|
21
21
|
case Time.now - (@last_on || Time.now)
|
22
|
-
when 0...0.
|
23
|
-
when 0.
|
24
|
-
when 0.3...1 then super :long_tap
|
25
|
-
|
26
|
-
# if @tap and @tap.alive?
|
27
|
-
# @tap.kill
|
28
|
-
# @tap = nil
|
29
|
-
# super :double_tap
|
30
|
-
# else
|
31
|
-
# @tap = Thread.new { sleep 0.25; super :tap }
|
32
|
-
# end
|
33
|
-
#end
|
34
|
-
else super :long_tap_x2; @taped = true
|
22
|
+
when 0...0.03 then nil # debounce
|
23
|
+
when 0.03...0.3 then super :tap
|
24
|
+
when 0.3...1 then super :long_tap
|
25
|
+
when 1...2 then super :long_tap_x2
|
35
26
|
end
|
36
27
|
end
|
37
28
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Domotics::Core
|
2
2
|
class Switch < Element
|
3
|
-
MINIMUM_LAG =
|
3
|
+
MINIMUM_LAG = 0
|
4
4
|
def initialize(args = {})
|
5
5
|
@type = args[:type] || :switch
|
6
6
|
@lag = nil
|
@@ -11,52 +11,62 @@ module Domotics::Core
|
|
11
11
|
super
|
12
12
|
@initialized = true
|
13
13
|
end
|
14
|
+
|
14
15
|
def set_state(value)
|
15
16
|
@initialized ? (super unless state == value) : super
|
16
17
|
end
|
18
|
+
|
17
19
|
def on(timer = nil)
|
18
20
|
set_state :on
|
19
21
|
lag(:off, timer)
|
20
22
|
end
|
23
|
+
|
21
24
|
def on?
|
22
25
|
state == :on
|
23
26
|
end
|
27
|
+
|
24
28
|
def delay_on(timer)
|
25
29
|
lag(:on, timer)
|
26
30
|
end
|
31
|
+
|
27
32
|
def off(timer = nil)
|
28
33
|
set_state :off
|
29
34
|
lag(:on, timer)
|
30
35
|
end
|
36
|
+
|
31
37
|
def off?
|
32
38
|
state == :off
|
33
39
|
end
|
34
|
-
|
35
|
-
|
40
|
+
|
41
|
+
def delay_off(timer, &block)
|
42
|
+
lag(:off, timer, &block)
|
36
43
|
end
|
44
|
+
|
37
45
|
def toggle(timer = nil)
|
38
46
|
set_state state == :off ? :on : :off
|
39
47
|
lag(:toggle, timer)
|
40
48
|
end
|
49
|
+
|
41
50
|
def delay_toggle(timer)
|
42
51
|
lag(:toggle, timer)
|
43
52
|
end
|
44
53
|
|
45
54
|
private
|
46
55
|
|
47
|
-
def lag(action = nil, timer = nil)
|
56
|
+
def lag(action = nil, timer = nil, &block)
|
48
57
|
# Kill previous action -> out of date
|
49
58
|
@lag_lock.synchronize do
|
50
|
-
if @lag
|
59
|
+
if @lag && @lag.alive?
|
51
60
|
@lag.kill
|
52
61
|
@lag = nil
|
53
62
|
end
|
54
|
-
raise ArgumentError unless
|
63
|
+
raise ArgumentError unless timer.is_a?(Integer) && (timer >= MINIMUM_LAG)
|
64
|
+
|
55
65
|
# Delayed action
|
56
|
-
@lag = Thread.new
|
66
|
+
@lag = Thread.new {
|
57
67
|
sleep timer
|
58
|
-
public_send action
|
59
|
-
|
68
|
+
block_given? ? (public_send(action) if block.call) : public_send(action)
|
69
|
+
}
|
60
70
|
end
|
61
71
|
rescue ArgumentError
|
62
72
|
nil
|
data/lib/domotics/core/room.rb
CHANGED
@@ -57,7 +57,7 @@ module Domotics::Core
|
|
57
57
|
# Default - simple prints event
|
58
58
|
def event_handler(msg = {})
|
59
59
|
event, element = msg[:event], msg[:element]
|
60
|
-
@logger.info { "
|
60
|
+
@logger.info { "[Domotics] event [#{event}] element [#{element}] state [#{element.state}]" }
|
61
61
|
Domotics::Core::WsServer.publish "#{element.room.name}/#{element.name}"
|
62
62
|
end
|
63
63
|
|
data/lib/domotics/core/server.rb
CHANGED
@@ -2,8 +2,6 @@ module Domotics::Core
|
|
2
2
|
class Server
|
3
3
|
def initialize(args = {})
|
4
4
|
@logger = Domotics::Core::Setup.logger || Logger.new(STDERR)
|
5
|
-
@args = {}
|
6
|
-
@args[:Host], @args[:Port] = args[:host], args[:port]
|
7
5
|
end
|
8
6
|
def call(env)
|
9
7
|
# [object]/[action]/[params]
|
@@ -38,8 +36,8 @@ module Domotics::Core
|
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
41
|
-
def run
|
42
|
-
Rack::Handler::Thin.run
|
39
|
+
def self.run(args = {})
|
40
|
+
Rack::Handler::Thin.run self.new, args
|
43
41
|
end
|
44
42
|
|
45
43
|
private
|
@@ -1,28 +1,34 @@
|
|
1
1
|
module Domotics::Core
|
2
2
|
class WsServer
|
3
|
-
@@
|
3
|
+
@@parent = nil
|
4
4
|
def initialize(args = {})
|
5
5
|
@logger = Domotics::Core::Setup.logger || Logger.new(STDERR)
|
6
6
|
@args = args
|
7
|
+
@@parent, @child = Socket.pair(:UNIX, :DGRAM, 0)
|
7
8
|
end
|
8
9
|
def run
|
9
|
-
|
10
|
-
|
10
|
+
@logger.info { "[WebSocket] server [#{@args[:host]}:#{@args[:port]}]" }
|
11
|
+
fork do
|
12
|
+
@@parent.close
|
13
|
+
channel = EventMachine::Channel.new
|
14
|
+
Thread.new { loop { channel.push @child.recv(2**10) }}
|
11
15
|
EventMachine::WebSocket.start(@args) do |ws|
|
12
16
|
ws.onopen do
|
13
|
-
sid =
|
17
|
+
sid = channel.subscribe { |msg| ws.send msg }
|
18
|
+
@logger.info { "[WebSocket] client [#{sid}]" }
|
14
19
|
ws.onmessage do |msg|
|
15
|
-
@logger.info { "WebSocket message [#{msg}] from
|
20
|
+
@logger.info { "[WebSocket] message [#{msg}] from [#{sid}]" }
|
16
21
|
end
|
17
22
|
ws.onclose do
|
18
|
-
|
23
|
+
channel.unsubscribe(sid)
|
19
24
|
end
|
20
25
|
end
|
21
26
|
end
|
22
27
|
end
|
28
|
+
@child.close
|
23
29
|
end
|
24
30
|
def self.publish(msg)
|
25
|
-
@@
|
31
|
+
@@parent.send msg, 0 if @@parent
|
26
32
|
end
|
27
33
|
end
|
28
34
|
end
|
data/test/test_devices.rb
CHANGED
@@ -38,5 +38,6 @@ class DomoticsDevicesTestCase < Test::Unit::TestCase
|
|
38
38
|
%x{echo "[test foto content]" > /tmp/test_foto_4.jpg}
|
39
39
|
sleep 0.5
|
40
40
|
assert_equal "[test foto content]", cam.camera_element.file.chomp
|
41
|
+
assert_equal Domotics::Core::Room[:test].last_event(cam.camera_element.name), :state_changed => :new_image
|
41
42
|
end
|
42
43
|
end
|
data/test/test_elements.rb
CHANGED
@@ -60,4 +60,14 @@ class DomoticsElementsTestCase < Test::Unit::TestCase
|
|
60
60
|
sleep 0.01
|
61
61
|
assert_equal room.last_event(btn.name), :state_changed => :long_tap
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
|
+
def test_camera_motion_sensor
|
65
|
+
room = Domotics::Core::Room[:test]
|
66
|
+
cam_mt = room.cam_motion
|
67
|
+
saved_mode = cam_el.device.mode
|
68
|
+
%x{echo "[test foto content]" > /tmp/test_foto.jpg}
|
69
|
+
sleep 0.5
|
70
|
+
assert_equal room.last_event(cam_mt.name), :state_changed => :motion_detection
|
71
|
+
assert_equal cam_el.device.mode, saved_mode
|
72
|
+
end
|
73
|
+
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.2.
|
4
|
+
version: 0.2.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- goredar
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: domotics-arduino
|
@@ -223,7 +223,7 @@ homepage: https://goredar.it
|
|
223
223
|
licenses:
|
224
224
|
- MIT
|
225
225
|
metadata: {}
|
226
|
-
post_install_message:
|
226
|
+
post_install_message:
|
227
227
|
rdoc_options: []
|
228
228
|
require_paths:
|
229
229
|
- lib
|
@@ -238,9 +238,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
238
238
|
- !ruby/object:Gem::Version
|
239
239
|
version: '0'
|
240
240
|
requirements: []
|
241
|
-
|
242
|
-
|
243
|
-
signing_key:
|
241
|
+
rubygems_version: 3.1.4
|
242
|
+
signing_key:
|
244
243
|
specification_version: 4
|
245
244
|
summary: Home automation system.
|
246
245
|
test_files:
|