openhab-scripting 4.41.0 → 4.42.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: 101fdd6964b1dd5eaefcf5ecaf528ecc01ca12c280440da11ce8646d10fbd67c
4
- data.tar.gz: 40bf18765cc29aee28d9ae486cd7c965435f5cbcf8f47867559bb72ccec7f3d9
3
+ metadata.gz: 62974273127c66d888a44c8871b287dd6cfc90485ad04e9d461b407d19474eb1
4
+ data.tar.gz: d9540eeb7d3df3df1075ff8c79a4bc8eef1ca9e6920860bce2c5a3c64a2931b8
5
5
  SHA512:
6
- metadata.gz: 8a8ce171ac466b46f28e5a5abc59cc2de62a20d16210e58fb7ea33b6ee5a6ad0b9399afb40491142d52396cbbee33b5be444f4d5530176b9ac34764f06a4ec5b
7
- data.tar.gz: 57171fa39cfc71afaa8d8cb7488e184ea031cf6f8b01a135410a97317399e8a478d6cf9691ba81168d023882c488b07de5f47a921e516a05589bd198731da8b8
6
+ metadata.gz: 26fbfcddca4f673fbaf1a2399bb126c06715dfbe966ae1c0a77259bdc5b1debafbbdb9b9103c81d0c8bc31311118b44517504e749611cb0c80cc4ddea1e69e23
7
+ data.tar.gz: 80e903ea1be4ce311c3f5093fa678bd38a22ddc5e97ac24245faaa5ad14cd5cd383b3e82f9cce11da6d7e062608ce5181a8eed3005f5aab09982f0a78f5eb944
@@ -6,10 +6,13 @@ module OpenHAB
6
6
  #
7
7
  # Persistence extension for Items
8
8
  #
9
+ # @note Methods that accept a timestamp can accept a ++ZonedDateTime++, Ruby ++Time++, or a ++Duration++.
10
+ # When given a positive Duration, the timestamp will be calculated as ++now-Duration++
11
+ #
9
12
  module Persistence
10
13
  java_import Java::OrgOpenhabCoreTypesUtil::UnitUtils
11
14
 
12
- # A wrapper for OpenHAB's HistoricItem that returns the state directly
15
+ # A state class with an added timestamp attribute. This is used to hold OpenHAB's HistoricItem.
13
16
  class HistoricState < SimpleDelegator
14
17
  attr_reader :timestamp, :state
15
18
 
@@ -29,14 +32,169 @@ module OpenHAB
29
32
 
30
33
  # All persistence methods that require a timestamp
31
34
  PERSISTENCE_METHODS = (QUANTITY_METHODS +
32
- %i[changed_since
35
+ %i[changed_since?
33
36
  evolution_rate
34
37
  historic_state
35
38
  maximum_since
36
39
  minimum_since
37
- updated_since]).freeze
40
+ updated_since?]).freeze
38
41
  private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
39
42
 
43
+ # @!method persist(service = nil)
44
+ # Persist the state of the item
45
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
46
+ # @return [void]
47
+
48
+ # @!method last_update(service = nil)
49
+ # Return the time the item was last updated.
50
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
51
+ # @return [ZonedDateTime] The timestamp of the last update
52
+
53
+ # @!method average_since(timestamp, service = nil)
54
+ # Return the average value of the item's state since the given time
55
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
56
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
57
+ # @return [DecimalType, QuantityType, nil] The average value since ++timestamp++,
58
+ # or nil if no previous state could be found.
59
+
60
+ # @!method average_between(start, finish, service = nil)
61
+ # Return the average value of the item's state between two points in time
62
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
63
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
64
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
65
+ # @return [DecimalType, QuantityType, nil] The average value between ++start++ and ++finish++,
66
+ # or nil if no previous state could be found.
67
+
68
+ # @!method delta_since(timestamp, service = nil)
69
+ # Return the difference value of the item's state since the given time
70
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
71
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
72
+ # @return [DecimalType, QuantityType, nil] The difference value since ++timestamp++,
73
+ # or nil if no previous state could be found.
74
+
75
+ # @!method delta_between(start, finish, service = nil)
76
+ # Return the difference value of the item's state between two points in time
77
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
78
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
79
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
80
+ # @return [DecimalType, QuantityType, nil] The difference value between ++start++ and ++finish++,
81
+ # or nil if no previous state could be found.
82
+
83
+ # @!method deviation_since(timestamp, service = nil)
84
+ # Return the standard deviation of the item's state since the given time
85
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
86
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
87
+ # @return [DecimalType, QuantityType, nil] The standard deviation since ++timestamp++,
88
+ # or nil if no previous state could be found.
89
+
90
+ # @!method deviation_between(start, finish, service = nil)
91
+ # Return the standard deviation of the item's state between two points in time
92
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
93
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
94
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
95
+ # @return [DecimalType, QuantityType, nil] The standard deviation between ++start++ and ++finish++,
96
+ # or nil if no previous state could be found.
97
+
98
+ # @!method sum_since(timestamp, service = nil)
99
+ # Return the sum of the item's state since the given time
100
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
101
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
102
+ # @return [DecimalType, QuantityType, nil] The sum since ++timestamp++,
103
+ # or nil if no previous state could be found.
104
+
105
+ # @!method sum_between(start, finish, service = nil)
106
+ # Return the sum of the item's state between two points in time
107
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
108
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
109
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
110
+ # @return [DecimalType, QuantityType, nil] The sum between ++start++ and ++finish++,
111
+ # or nil if no previous state could be found.
112
+
113
+ # @!method variance_since(timestamp, service = nil)
114
+ # Return the variance of the item's state since the given time
115
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
116
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
117
+ # @return [DecimalType, QuantityType, nil] The variance since ++timestamp++,
118
+ # or nil if no previous state could be found.
119
+
120
+ # @!method variance_between(start, finish, service = nil)
121
+ # Return the variance of the item's state between two points in time
122
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
123
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
124
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
125
+ # @return [DecimalType, QuantityType, nil] The variance between ++start++ and ++finish++,
126
+ # or nil if no previous state could be found.
127
+
128
+ # @!method changed_since?(timestamp, service = nil)
129
+ # Whether the item's state has changed since the given time
130
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
131
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
132
+ # @return [Boolean] True if the item's state has changed since the given ++timestamp++, False otherwise.
133
+
134
+ # @!method changed_between?(start, finish, service = nil)
135
+ # Whether the item's state changed between two points in time
136
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
137
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
138
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
139
+ # @return [Boolean] True if the item's state changed between ++start++ and ++finish++, False otherwise.
140
+
141
+ # @!method evolution_rate(timestamp, service = nil)
142
+ # Return the evolution rate of the item's state since the given time
143
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
144
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
145
+ # @return [DecimalType, QuantityType, nil] The evolution rate since ++timestamp++,
146
+ # or nil if no previous state could be found.
147
+
148
+ # @!method historic_state(timestamp, service = nil)
149
+ # Return the the item's state at the given time
150
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time at which to search
151
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
152
+ # @return [HistoricState, nil] The item's state at ++timestamp++,
153
+ # or nil if no previous state could be found.
154
+
155
+ # @!method maximum_since(timestamp, service = nil)
156
+ # Return the maximum value of the item's state since the given time
157
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
158
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
159
+ # @return [HistoricState, nil] The maximum value since ++timestamp++,
160
+ # or nil if no previous state could be found.
161
+
162
+ # @!method maximum_between(start, finish, service = nil)
163
+ # Return the maximum value of the item's state between two points in time
164
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
165
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
166
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
167
+ # @return [HistoricState, nil] The maximum value between ++start++ and ++finish++,
168
+ # or nil if no previous state could be found.
169
+
170
+ # @!method minimum_since(timestamp, service = nil)
171
+ # Return the minimum value of the item's state since the given time
172
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
173
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
174
+ # @return [HistoricState, nil] The minimum value since ++timestamp++,
175
+ # or nil if no previous state could be found.
176
+
177
+ # @!method minimum_between(start, finish, service = nil)
178
+ # Return the minimum value of the item's state between two points in time
179
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
180
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
181
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
182
+ # @return [HistoricState, nil] The minimum value between ++start++ and ++finish++,
183
+ # or nil if no previous state could be found.
184
+
185
+ # @!method updated_since?(timestamp, service = nil)
186
+ # Whether the item's state has been updated since the given time
187
+ # @param [ZonedDateTime, Time, Duration] timestamp The point in time from which to search
188
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
189
+ # @return [Boolean] True if the item's state has been updated since the given ++timestamp++, False otherwise.
190
+
191
+ # @!method updated_between?(start, finish, service = nil)
192
+ # Whether the item's state was updated between two points in time
193
+ # @param [ZonedDateTime, Time, Duration] start The point in time from which to search
194
+ # @param [ZonedDateTime, Time, Duration] finish The point in time to which to search
195
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
196
+ # @return [Boolean] True if the item's state was updated between ++start++ and ++finish++, False otherwise.
197
+
40
198
  %i[persist last_update].each do |method|
41
199
  define_method(method) do |service = nil|
42
200
  service ||= persistence_service
@@ -51,7 +209,7 @@ module OpenHAB
51
209
  # searches the first state not equal the current state
52
210
  # @param service [String] the name of the PersistenceService to use
53
211
  #
54
- # @return the previous state or nil if no previous state could be found,
212
+ # @return [HistoricState, nil] the previous state or nil if no previous state could be found,
55
213
  # or if the default persistence service is not configured or
56
214
  # does not refer to a valid service
57
215
  #
@@ -62,17 +220,29 @@ module OpenHAB
62
220
  end
63
221
 
64
222
  PERSISTENCE_METHODS.each do |method|
223
+ public_method = method.to_s.delete_suffix('?') # For some reason, the boolean methods with '?' are missing
65
224
  define_method(method) do |timestamp, service = nil|
66
225
  service ||= persistence_service
67
- result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
68
- if result.is_a? Java::OrgOpenhabCorePersistence::HistoricItem
69
- return HistoricState.new(quantify(result.state), result.timestamp)
70
- end
226
+ result = PersistenceExtensions.public_send(public_method, self, to_zdt(timestamp), service&.to_s)
227
+ wrap_result(result, method)
228
+ end
229
+
230
+ next unless /_since\??$/.match?(method.to_s)
71
231
 
72
- QUANTITY_METHODS.include?(method) ? quantify(result) : result
232
+ between_method = method.to_s.sub('_since', '_between').to_sym
233
+ define_method(between_method) do |start, finish, service = nil|
234
+ service ||= persistence_service
235
+ result = PersistenceExtensions.public_send(public_method, self, to_zdt(start), to_zdt(finish),
236
+ service&.to_s)
237
+ wrap_result(result, method)
73
238
  end
74
239
  end
75
240
 
241
+ alias changed_since changed_since?
242
+ alias changed_between changed_between?
243
+ alias updated_since updated_since?
244
+ alias updated_between updated_between?
245
+
76
246
  private
77
247
 
78
248
  #
@@ -103,6 +273,26 @@ module OpenHAB
103
273
  end
104
274
  end
105
275
 
276
+ #
277
+ # Wrap the result into a more convenient object type depending on the method and result.
278
+ #
279
+ # @param [Object] result the raw result type to be wrapped
280
+ # @param [Symbol] method the name of the called method
281
+ #
282
+ # @return [HistoricState] a {HistoricState} object if the result was a HistoricItem
283
+ # @return [QuantityType] a ++QuantityType++ object if the result was an average, delta, deviation,
284
+ # sum, or variance.
285
+ # @return [Object] the original result object otherwise.
286
+ #
287
+ def wrap_result(result, method)
288
+ java_import org.openhab.core.persistence.HistoricItem
289
+
290
+ return HistoricState.new(quantify(result.state), result.timestamp) if result.is_a?(HistoricItem)
291
+ return quantify(result) if QUANTITY_METHODS.include?(method)
292
+
293
+ result
294
+ end
295
+
106
296
  #
107
297
  # Get the specified persistence service from the current thread local variable
108
298
  #
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.41.0'
8
+ VERSION = '4.42.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.41.0
4
+ version: 4.42.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-04-28 00:00:00.000000000 Z
11
+ date: 2022-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler