openhab-scripting 4.34.1 → 4.37.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b5fd36cfc11c1123c6dc979a7d7072ee7aff804deded7d4bb72716be4c2e216
4
- data.tar.gz: f324548ea1075012f58076195a8c25d8cb6bb4afe62344d9752ba4a2415a4394
3
+ metadata.gz: 152174c3766a83b620a0ec7df6e692bb914e187c788d9c5eecfcedd42bfd641c
4
+ data.tar.gz: 44084d3557abd293faf97858470414cf5f3dd65e07faa8f74e8f40e0d6ea8edb
5
5
  SHA512:
6
- metadata.gz: 49b5fab340c77c046ad534568efb939206ab11824f6f08c56622a691d066fd4ce79c308e153d4b45d408cb901f461f5b97c6b14b00deceaba70efbdb46475e2c
7
- data.tar.gz: 70c98aaec888f7c889a3bcac861caf216fee51b71c0f086c624ad4caf51f497f6db56c4b218e808524fa3b903d2c149a4c580a862878158fd988d12eb2ce8659
6
+ metadata.gz: 617deb9b4f75a92a407cf8db6ff695f409ac5f006690751c10100919d414851dc83758ec0560badf3d40097cc896d86de132a215d22a3f505b8e97bff26a8ad2
7
+ data.tar.gz: 5a2c037047ce9db79d57a153e5df94c631144d511ac716b1862a8a6c4fc567d01c6efa15d93583f2bc80a792c47c85e30dda65ec481a1f14f6a588765638d56f
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'imports'
4
+
5
+ OpenHAB::DSL.import_presets
6
+
3
7
  require 'openhab/log/logger'
4
8
 
5
9
  # the order of these is important
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module DSL
5
+ include OpenHAB::Log
6
+ #
7
+ # Import required java classes
8
+ #
9
+ def self.import_presets # rubocop:disable Metrics/AbcSize
10
+ return if Object.const_get(:ZonedDateTime).is_a?(Class)
11
+
12
+ # Fix the import problem in OpenHAB 3.2 addon. Not required in 3.3+
13
+ [java.time.Duration,
14
+ java.time.ZonedDateTime,
15
+ java.time.ZoneId,
16
+ java.time.temporal.ChronoUnit].each do |klass|
17
+ Object.const_set(klass.java_class.simple_name.to_sym, klass)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -76,19 +76,15 @@ module OpenHAB
76
76
  private
77
77
 
78
78
  #
79
- # Convert timestamp to ZonedDateTime if it's a TemporalAmount
79
+ # Convert timestamp to ZonedDateTime with duration negated to indicate a time in the past
80
80
  #
81
- # @param [Object] timestamp to convert
81
+ # @param [Object] timestamp timestamp to convert
82
82
  #
83
83
  # @return [ZonedDateTime]
84
84
  #
85
85
  def to_zdt(timestamp)
86
- if timestamp.is_a? Java::JavaTimeTemporal::TemporalAmount
87
- logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
88
- Java::JavaTime::ZonedDateTime.now.minus(timestamp)
89
- else
90
- timestamp
91
- end
86
+ timestamp = timestamp.negated if timestamp.is_a? Java::JavaTime::Duration
87
+ OpenHAB::DSL.to_zdt(timestamp)
92
88
  end
93
89
 
94
90
  #
@@ -171,6 +171,7 @@ module OpenHAB
171
171
  # rubocop: disable Metrics/AbcSize
172
172
  # There is no feasible way to break this method into smaller components
173
173
  def execute(_mod = nil, inputs = nil)
174
+ OpenHAB::DSL.import_presets
174
175
  @semaphore.synchronize do
175
176
  thread_local(@thread_locals) do
176
177
  logger.trace "Canceling implicit timer #{@timed_command_details.timer} for "\
@@ -3,6 +3,15 @@
3
3
  require 'pathname'
4
4
 
5
5
  module OpenHAB
6
+ #
7
+ # Return the OpenHAB conf directory as a ruby pathname
8
+ #
9
+ # @return [Pathname] OpenHAB conf path
10
+ #
11
+ def self.conf_root
12
+ Pathname.new(ENV['OPENHAB_CONF'])
13
+ end
14
+
6
15
  module DSL
7
16
  #
8
17
  # Provides access to OpenHAB attributes
@@ -12,13 +21,9 @@ module OpenHAB
12
21
 
13
22
  module_function
14
23
 
15
- #
16
- # Return the OpenHAB conf directory as a ruby pathname
17
- #
18
- # @return [Pathname] OpenHAB conf path
19
- #
24
+ # @deprecated Please use {OpenHAB.conf_root} instead
20
25
  def __conf__
21
- Pathname.new(ENV['OPENHAB_CONF'])
26
+ OpenHAB.conf_root
22
27
  end
23
28
  end
24
29
  end
@@ -23,7 +23,6 @@ module OpenHAB
23
23
  include OpenHAB::Log
24
24
  include OpenHAB::Core::ThreadLocal
25
25
  include OpenHAB::DSL::Between
26
- java_import java.time.ZonedDateTime
27
26
 
28
27
  #
29
28
  # Create a new Rule
@@ -55,6 +54,7 @@ module OpenHAB
55
54
  #
56
55
  #
57
56
  def execute(mod = nil, inputs = nil)
57
+ OpenHAB::DSL.import_presets
58
58
  thread_local(RULE_NAME: name) do
59
59
  logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs.inspect})" }
60
60
  logger.trace { "Event details #{inputs['event'].inspect}" } if inputs&.key?('event')
@@ -28,6 +28,18 @@ module OpenHAB
28
28
  end
29
29
  states
30
30
  end
31
+
32
+ #
33
+ # Check if all the given items have a state (not UNDEF or NULL)
34
+ #
35
+ # @param [Array] items whose state must be non-nil
36
+ # @param [<Type>] check_things when true, also ensures that all linked things are online
37
+ #
38
+ # @return [Boolean] true if all the items have a state, false otherwise
39
+ #
40
+ def state?(*items, things: false)
41
+ items.flatten.all? { |item| (!things || item.things.all?(&:online?)) && item.state? }
42
+ end
31
43
  end
32
44
 
33
45
  module Support
@@ -95,11 +95,24 @@ module OpenHAB
95
95
  class TimerSet < Set
96
96
  #
97
97
  # A shorthand to cancel all the timer objects held within the set
98
- # so that timers[timer_id].cancel_all is equivalent to timers[timer_id].each(&:cancel)
98
+ # so that timers[timer_id].cancel is equivalent to timers[timer_id].each(&:cancel)
99
99
  #
100
- def cancel_all
100
+ def cancel
101
101
  each(&:cancel)
102
102
  end
103
+ # @deprecated Please use {cancel} instead
104
+ alias cancel_all cancel
105
+
106
+ #
107
+ # A shorthand to reschedule all the timer objects held within the set
108
+ #
109
+ # @param [Duration] duration An optional duration to reschedule
110
+ #
111
+ # @return [TimerSet] Set of timers
112
+ #
113
+ def reschedule(duration = nil)
114
+ each { |timer| timer.reschedule duration }
115
+ end
103
116
  end
104
117
  end
105
118
  end
@@ -9,7 +9,6 @@ require 'openhab/core/thread_local'
9
9
  module OpenHAB
10
10
  module DSL
11
11
  java_import org.openhab.core.model.script.actions.ScriptExecution
12
- java_import java.time.ZonedDateTime
13
12
 
14
13
  # Ruby wrapper for OpenHAB Timer
15
14
  # This class implements delegator to delegate methods to the OpenHAB timer
@@ -29,7 +28,6 @@ module OpenHAB
29
28
  # @param [Duration] duration Duration until timer should fire
30
29
  # @param [Block] block Block to execute when timer fires
31
30
  #
32
- # rubocop: disable Metrics/MethodLength
33
31
  def initialize(duration:, thread_locals: {}, &block)
34
32
  @duration = duration
35
33
  @thread_locals = thread_locals
@@ -39,15 +37,12 @@ module OpenHAB
39
37
  semaphore = Mutex.new
40
38
 
41
39
  semaphore.synchronize do
42
- @timer = ScriptExecution.createTimer(
43
- ZonedDateTime.now.plus(to_duration(@duration)), timer_block(semaphore, &block)
44
- )
40
+ @timer = ScriptExecution.createTimer(OpenHAB::DSL.to_zdt(@duration), timer_block(semaphore, &block))
45
41
  @rule_timers = Thread.current[:rule_timers]
46
42
  super(@timer)
47
43
  Timers.timer_manager.add(self)
48
44
  end
49
45
  end
50
- # rubocop: enable Metrics/MethodLength
51
46
 
52
47
  #
53
48
  # Reschedule timer
@@ -60,7 +55,7 @@ module OpenHAB
60
55
  duration ||= @duration
61
56
 
62
57
  Timers.timer_manager.add(self)
63
- @timer.reschedule(ZonedDateTime.now.plus(to_duration(duration)))
58
+ @timer.reschedule(OpenHAB::DSL.to_zdt(duration))
64
59
  end
65
60
 
66
61
  #
@@ -84,6 +79,7 @@ module OpenHAB
84
79
  #
85
80
  def timer_block(semaphore)
86
81
  proc {
82
+ OpenHAB::DSL.import_presets
87
83
  semaphore.synchronize do
88
84
  Timers.timer_manager.delete(self)
89
85
  thread_local(@thread_locals) do
@@ -92,26 +88,44 @@ module OpenHAB
92
88
  end
93
89
  }
94
90
  end
91
+ end
95
92
 
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"
114
- end
93
+ #
94
+ # Convert TemporalAmount (Duration), seconds (float, integer), and Ruby Time to ZonedDateTime
95
+ # Note: TemporalAmount is added to now
96
+ #
97
+ # @param [Object] timestamp to convert
98
+ #
99
+ # @return [ZonedDateTime]
100
+ #
101
+ def self.to_zdt(timestamp)
102
+ logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
103
+ return unless timestamp
104
+
105
+ case timestamp
106
+ when Java::JavaTimeTemporal::TemporalAmount then ZonedDateTime.now.plus(timestamp)
107
+ when ZonedDateTime then timestamp
108
+ when Time then timestamp.to_java(ZonedDateTime)
109
+ else
110
+ to_zdt(seconds_to_duration(timestamp)) ||
111
+ raise(ArgumentError, 'Timestamp must be a ZonedDateTime, a Duration, a Numeric, or a Time object')
112
+ end
113
+ end
114
+
115
+ #
116
+ # Convert numeric seconds to a Duration object
117
+ #
118
+ # @param [Float, Integer] secs The number of seconds in integer or float
119
+ #
120
+ # @return [Duration]
121
+ #
122
+ def self.seconds_to_duration(secs)
123
+ return unless secs
124
+
125
+ if secs.respond_to?(:to_f)
126
+ secs.to_f.seconds
127
+ elsif secs.respond_to?(:to_i)
128
+ secs.to_i.seconds
115
129
  end
116
130
  end
117
131
  end
@@ -8,7 +8,6 @@ module OpenHAB
8
8
  module DSL
9
9
  module Types
10
10
  DateTimeType = org.openhab.core.library.types.DateTimeType
11
- java_import java.time.ZonedDateTime # This is needed for the addon prior to ruby_class fix (OH 3.2.0)
12
11
 
13
12
  # global alias - required for jrubyscripting addon <= OH3.2.0
14
13
  ::DateTimeType = DateTimeType if ::DateTimeType.is_a?(Java::JavaLang::Class)
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.34.1'
8
+ VERSION = '4.37.0'
9
9
  end
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.34.1
4
+ version: 4.37.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-16 00:00:00.000000000 Z
11
+ date: 2022-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ files:
60
60
  - lib/openhab/dsl/dsl.rb
61
61
  - lib/openhab/dsl/gems.rb
62
62
  - lib/openhab/dsl/group.rb
63
+ - lib/openhab/dsl/imports.rb
63
64
  - lib/openhab/dsl/items/color_item.rb
64
65
  - lib/openhab/dsl/items/comparable_item.rb
65
66
  - lib/openhab/dsl/items/contact_item.rb