openhab-scripting 4.35.0 → 4.37.1

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
  SHA256:
3
- metadata.gz: a64a452ca8ff7c31d08ceed61a6e2d466250438365ea71c4de1c968b33198f8c
4
- data.tar.gz: 31e41c724ad7b7871e913044259f5d3736130d5634d2f9301b98c7093b97f804
3
+ metadata.gz: e405e19fe5977dfbe708699abaf9fe3e0e953d2d16450da5b10879b6e0d3494b
4
+ data.tar.gz: c1ed131f668cd5e214f56164ff10a3e9938df4aaa5f6a55efde4ba23db164f82
5
5
  SHA512:
6
- metadata.gz: 1cb0812ede637a43674c260dce7124a38fce48c0940cae506125f9c5d6c3ebc3fa08d229a92155060fc5eb816b9e946874cebbc6e0d9654b3405e75879b1dbfd
7
- data.tar.gz: cf04212fd7483729494be64840f74c9d6d5241c35c87e4c46f7207c8a007a9d0c537a8379481172d8395cf27c74eae5764d2d2e65ef984d7478104778e19c4a0
6
+ metadata.gz: d970bd8b607aad49f59d0746ba56711dcf34a0c199547b76008e3e7f375b6836d06841c8cbdb86f056c4f58704690950583f20b76ae1e641515ea332f594f710
7
+ data.tar.gz: 7cbc491b62553a735e02f25f690a1244801fc2eac7d2a179dd1777d608a34752e5b549ecb41b73df6765c83ef717fe40d030414bb41291d1372872acaa89c9bb
@@ -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
@@ -19,11 +19,26 @@ module OpenHAB
19
19
  def self.included(klass)
20
20
  klass.prepend ItemEquality # make sure this is first
21
21
  klass.extend Forwardable
22
- klass.delegate %i[+ - * / % | to_d to_f to_i to_int] => :state
22
+ klass.delegate %i[+ - * / % to_d to_f to_i to_int] => :state
23
23
  # remove the JRuby default == so that we can inherit the Ruby method
24
24
  klass.remove_method :==
25
25
  end
26
26
 
27
+ #
28
+ # Convert state to a Quantity by calling state (DecimalType)#|
29
+ # Raise a NoMethodError if state is nil (NULL or UNDEF) instead of delegating to it.
30
+ # because nil#| would return true, causing an unexpected result
31
+ #
32
+ # @param [Unit, String] other the unit to convert to
33
+ #
34
+ # @return [QuantityType] the QuantityType in the given unit
35
+ #
36
+ def |(other)
37
+ raise NoMethodError, 'State is nil' unless state?
38
+
39
+ state.|(other)
40
+ end
41
+
27
42
  #
28
43
  # Check if NumericItem is truthy? as per defined by library
29
44
  #
@@ -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')
@@ -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.35.0'
8
+ VERSION = '4.37.1'
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.35.0
4
+ version: 4.37.1
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-29 00:00:00.000000000 Z
11
+ date: 2022-04-09 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