openhab-scripting 4.35.0 → 4.36.0

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: 0f8ae7a88299903d5fe44ecc1a1bf2f16bdeb659545a567d1e86dfc908367694
4
+ data.tar.gz: 94db08cd7ee03e382db6d7b4ebed8b240ae4e354a7b62073b0a81cc1b7228cdd
5
5
  SHA512:
6
- metadata.gz: 1cb0812ede637a43674c260dce7124a38fce48c0940cae506125f9c5d6c3ebc3fa08d229a92155060fc5eb816b9e946874cebbc6e0d9654b3405e75879b1dbfd
7
- data.tar.gz: cf04212fd7483729494be64840f74c9d6d5241c35c87e4c46f7207c8a007a9d0c537a8379481172d8395cf27c74eae5764d2d2e65ef984d7478104778e19c4a0
6
+ metadata.gz: 6f8ea8b5f0ab16e602f002cf749102c4250bb3525fc6f7e18f11eafe294125d198d6a85c5565bfc29629024dad0f5823ef0a298e7e90f7aa776030b84df4a25f
7
+ data.tar.gz: 9a04fb5c20858fbe6b1e63dbdff39207f8de7a7809b26000eef663cc3ff146e75e9e6717df1e2485b087fe92736af248f9352085b472d64e931008d60b7eb89c
@@ -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
  #
@@ -29,7 +29,6 @@ module OpenHAB
29
29
  # @param [Duration] duration Duration until timer should fire
30
30
  # @param [Block] block Block to execute when timer fires
31
31
  #
32
- # rubocop: disable Metrics/MethodLength
33
32
  def initialize(duration:, thread_locals: {}, &block)
34
33
  @duration = duration
35
34
  @thread_locals = thread_locals
@@ -39,15 +38,12 @@ module OpenHAB
39
38
  semaphore = Mutex.new
40
39
 
41
40
  semaphore.synchronize do
42
- @timer = ScriptExecution.createTimer(
43
- ZonedDateTime.now.plus(to_duration(@duration)), timer_block(semaphore, &block)
44
- )
41
+ @timer = ScriptExecution.createTimer(OpenHAB::DSL.to_zdt(@duration), timer_block(semaphore, &block))
45
42
  @rule_timers = Thread.current[:rule_timers]
46
43
  super(@timer)
47
44
  Timers.timer_manager.add(self)
48
45
  end
49
46
  end
50
- # rubocop: enable Metrics/MethodLength
51
47
 
52
48
  #
53
49
  # Reschedule timer
@@ -60,7 +56,7 @@ module OpenHAB
60
56
  duration ||= @duration
61
57
 
62
58
  Timers.timer_manager.add(self)
63
- @timer.reschedule(ZonedDateTime.now.plus(to_duration(duration)))
59
+ @timer.reschedule(OpenHAB::DSL.to_zdt(duration))
64
60
  end
65
61
 
66
62
  #
@@ -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
@@ -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.36.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.35.0
4
+ version: 4.36.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-29 00:00:00.000000000 Z
11
+ date: 2022-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler