openhab-scripting 4.32.7 → 4.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/openhab/dsl/items/ensure.rb +9 -7
- data/lib/openhab/dsl/timers/reentrant_timer.rb +1 -1
- data/lib/openhab/dsl/timers/timer.rb +90 -95
- data/lib/openhab/dsl/timers.rb +3 -1
- data/lib/openhab/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 806613696649b2b84a4dd23339f58c9e25dff4a9834c5fa952b50effd915307a
|
4
|
+
data.tar.gz: 6faf4c72cae568ae889d188b73040ae74c73f57864b30588d57fdce097f37420
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '026014989011c7cc84fe58d783766a8a48ee9d4da002ca6724010bbcd873ed26484934c9a6bfc232fe71f073d51e5589fa583e1e621fa29d9c1b4601dcdceedd'
|
7
|
+
data.tar.gz: 9a75b467b814a87bb0efcb10baeed5700a28fd81423f0d87124e072f102280e53688fc2ef8cdea06820dfb4008d0bffefa1c3d250b759c177c8f1ad591342491
|
@@ -50,15 +50,17 @@ module OpenHAB
|
|
50
50
|
# If +ensure_states+ is active (by block or chained method), then
|
51
51
|
# check if this item is in the command's state before actually
|
52
52
|
# sending the command
|
53
|
-
|
54
|
-
|
53
|
+
%i[command update].each do |ensured_method|
|
54
|
+
define_method(ensured_method) do |command|
|
55
|
+
return super(command) unless Thread.current[:ensure_states]
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
logger.trace do
|
58
|
+
"#{name} ensure #{command}, format_type_pre: #{format_type_pre(command)}, current state: #{state}"
|
59
|
+
end
|
60
|
+
return if state == format_type_pre(command)
|
60
61
|
|
61
|
-
|
62
|
+
super(command)
|
63
|
+
end
|
62
64
|
end
|
63
65
|
alias << command
|
64
66
|
end
|
@@ -8,114 +8,109 @@ require 'openhab/core/thread_local'
|
|
8
8
|
|
9
9
|
module OpenHAB
|
10
10
|
module DSL
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
module Timers
|
15
|
-
java_import org.openhab.core.model.script.actions.ScriptExecution
|
16
|
-
java_import java.time.ZonedDateTime
|
11
|
+
java_import org.openhab.core.model.script.actions.ScriptExecution
|
12
|
+
java_import java.time.ZonedDateTime
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
14
|
+
# Ruby wrapper for OpenHAB Timer
|
15
|
+
# This class implements delegator to delegate methods to the OpenHAB timer
|
16
|
+
#
|
17
|
+
# @author Brian O'Connell
|
18
|
+
# @since 2.0.0
|
19
|
+
class Timer < SimpleDelegator
|
20
|
+
include OpenHAB::Log
|
21
|
+
include OpenHAB::Core::ThreadLocal
|
22
|
+
extend Forwardable
|
27
23
|
|
28
|
-
|
24
|
+
def_delegator :@timer, :has_terminated, :terminated?
|
29
25
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
26
|
+
#
|
27
|
+
# Create a new Timer Object
|
28
|
+
#
|
29
|
+
# @param [Duration] duration Duration until timer should fire
|
30
|
+
# @param [Block] block Block to execute when timer fires
|
31
|
+
#
|
32
|
+
# rubocop: disable Metrics/MethodLength
|
33
|
+
def initialize(duration:, thread_locals: {}, &block)
|
34
|
+
@duration = duration
|
35
|
+
@thread_locals = thread_locals
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
37
|
+
# A semaphore is used to prevent a race condition in which calling the block from the timer thread
|
38
|
+
# occurs before the @timer variable can be set resulting in @timer being nil
|
39
|
+
semaphore = Mutex.new
|
44
40
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
41
|
+
semaphore.synchronize do
|
42
|
+
@timer = ScriptExecution.createTimer(
|
43
|
+
ZonedDateTime.now.plus(to_duration(@duration)), timer_block(semaphore, &block)
|
44
|
+
)
|
45
|
+
@rule_timers = Thread.current[:rule_timers]
|
46
|
+
super(@timer)
|
47
|
+
Timers.timer_manager.add(self)
|
53
48
|
end
|
54
|
-
|
49
|
+
end
|
50
|
+
# rubocop: enable Metrics/MethodLength
|
55
51
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
#
|
53
|
+
# Reschedule timer
|
54
|
+
#
|
55
|
+
# @param [Duration] duration
|
56
|
+
#
|
57
|
+
# @return [Timer] Rescheduled timer instances
|
58
|
+
#
|
59
|
+
def reschedule(duration = nil)
|
60
|
+
duration ||= @duration
|
65
61
|
|
66
|
-
|
67
|
-
|
68
|
-
|
62
|
+
Timers.timer_manager.add(self)
|
63
|
+
@timer.reschedule(ZonedDateTime.now.plus(to_duration(duration)))
|
64
|
+
end
|
69
65
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
66
|
+
#
|
67
|
+
# Cancel timer
|
68
|
+
#
|
69
|
+
# @return [Boolean] True if cancel was successful, false otherwise
|
70
|
+
#
|
71
|
+
def cancel
|
72
|
+
Timers.timer_manager.delete(self)
|
73
|
+
@timer.cancel
|
74
|
+
end
|
79
75
|
|
80
|
-
|
76
|
+
private
|
81
77
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
78
|
+
#
|
79
|
+
# Constructs a block to execute timer within
|
80
|
+
#
|
81
|
+
# @param [Semaphore] semaphore to obtain before executing
|
82
|
+
#
|
83
|
+
# @return [Proc] Block for timer to execute
|
84
|
+
#
|
85
|
+
def timer_block(semaphore)
|
86
|
+
proc {
|
87
|
+
semaphore.synchronize do
|
88
|
+
Timers.timer_manager.delete(self)
|
89
|
+
thread_local(@thread_locals) do
|
90
|
+
yield(self)
|
96
91
|
end
|
97
|
-
}
|
98
|
-
end
|
99
|
-
|
100
|
-
#
|
101
|
-
# Convert argument to a duration
|
102
|
-
#
|
103
|
-
# @param [Java::JavaTimeTemporal::TemporalAmount, #to_f, #to_i, nil] duration Duration
|
104
|
-
#
|
105
|
-
# @raise if duration cannot be used for a timer
|
106
|
-
#
|
107
|
-
# @return Argument converted to seconds if it responds to #to_f or #to_i, otherwise duration unchanged
|
108
|
-
#
|
109
|
-
def to_duration(duration)
|
110
|
-
if duration.nil? || duration.is_a?(Java::JavaTimeTemporal::TemporalAmount)
|
111
|
-
duration
|
112
|
-
elsif duration.respond_to?(:to_f)
|
113
|
-
duration.to_f.seconds
|
114
|
-
elsif duration.respond_to?(:to_i)
|
115
|
-
duration.to_i.seconds
|
116
|
-
else
|
117
|
-
raise ArgumentError, "Supplied argument '#{duration}' cannot be converted to a duration"
|
118
92
|
end
|
93
|
+
}
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
# Convert argument to a duration
|
98
|
+
#
|
99
|
+
# @param [Java::JavaTimeTemporal::TemporalAmount, #to_f, #to_i, nil] duration Duration
|
100
|
+
#
|
101
|
+
# @raise if duration cannot be used for a timer
|
102
|
+
#
|
103
|
+
# @return Argument converted to seconds if it responds to #to_f or #to_i, otherwise duration unchanged
|
104
|
+
#
|
105
|
+
def to_duration(duration)
|
106
|
+
if duration.nil? || duration.is_a?(Java::JavaTimeTemporal::TemporalAmount)
|
107
|
+
duration
|
108
|
+
elsif duration.respond_to?(:to_f)
|
109
|
+
duration.to_f.seconds
|
110
|
+
elsif duration.respond_to?(:to_i)
|
111
|
+
duration.to_i.seconds
|
112
|
+
else
|
113
|
+
raise ArgumentError, "Supplied argument '#{duration}' cannot be converted to a duration"
|
119
114
|
end
|
120
115
|
end
|
121
116
|
end
|
data/lib/openhab/dsl/timers.rb
CHANGED
@@ -36,8 +36,10 @@ module OpenHAB
|
|
36
36
|
thread_locals ||= {}
|
37
37
|
return Timers.reentrant_timer(duration: duration, thread_locals: thread_locals, id: id, &block) if id
|
38
38
|
|
39
|
-
Timer.new(duration: duration, thread_locals: thread_locals, &block)
|
39
|
+
OpenHAB::DSL::Timer.new(duration: duration, thread_locals: thread_locals, &block)
|
40
40
|
end
|
41
|
+
# An alias for +after+
|
42
|
+
alias create_timer after
|
41
43
|
|
42
44
|
#
|
43
45
|
# Provdes access to the hash for mapping timer ids to the set of active timers associated with that id
|
data/lib/openhab/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openhab-scripting
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.33.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian O'Connell
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|