openhab-scripting 4.40.0 → 4.42.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: 7aaa16073cb2b4d71edb5e80debef49282a864bad1e057fb307b5cd8b9ca7844
4
- data.tar.gz: 641502dca198f6cf42d5a489153f6c04fd3bf70900d4a0873bfb01d686bea6b7
3
+ metadata.gz: 5e41958655d441761618cb21164c7954bcb05c037023a080d2a70a741637d951
4
+ data.tar.gz: ee4252bafef8e2a430f98c80bc3277e5c45ef9cd0c06b645f48da848031f0abf
5
5
  SHA512:
6
- metadata.gz: 978fe9d74ac7bc6481bb59cfb32a5c917f7707933e68d686be6119096f64daf78a49af1c9e4870abaecc0eec234e50314fae8093f1f04d1c38f6e15cdbe89bd9
7
- data.tar.gz: b959d2aef01f436f90a56142e4e2c9a4a532de091bd846b97b799a609e16d849d5df2e3de77d91b0a2de588f0da6cf3e6904205b9236538c123484552484565f
6
+ metadata.gz: c1dcd8ab59e1732eb76acd9c707d4dbd18dcae94d3fd6236d6c53362958b7fd0c977ef001c109f147df596321a99ee7640aceb25f92530f7cb8f59bd7733de9f
7
+ data.tar.gz: 70d92d8df318f317c42843cf8eaae67b33c9cdb24f119074f7e59e117ec29f53f1594df05e8fd35059a666a600845375048f991c9cc15b35772fb34b35d83a4f
@@ -9,9 +9,12 @@ module OpenHAB
9
9
  #
10
10
  # Comparison
11
11
  #
12
- # @param [GenericItem, Types::Type, Object] other object to
12
+ # @param [GenericItem, Types::Type, Object, GenericItemObject] other object to
13
13
  # compare to
14
14
  #
15
+ # When comparing GenericItemObject on either side, perform object comparison
16
+ # return 0 if the object is equal, or nil otherwise.
17
+ #
15
18
  # If this item is +NULL+ or +UNDEF+, and +other+ is nil, they are
16
19
  # considered equal
17
20
  #
@@ -28,21 +31,30 @@ module OpenHAB
28
31
  #
29
32
  def <=>(other)
30
33
  logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
31
- # if we're NULL or UNDEF, implement special logic
32
- unless state?
33
- # if comparing to nil, consider ourselves equal
34
- return 0 if other.nil?
35
- # if the other object is an Item, only consider equal if we're
36
- # in the same _kind_ of UnDefType state
37
- return raw_state == other.raw_state if other.is_a?(GenericItem) && !other.state?
38
34
 
39
- # otherwise, it's a non-nil thing comparing to nil, which is undefined
40
- return nil
35
+ if is_a?(OpenHAB::DSL::GenericItemObject) || other.is_a?(OpenHAB::DSL::GenericItemObject)
36
+ return eql?(other) ? 0 : nil
41
37
  end
42
38
 
39
+ # if we're NULL or UNDEF, implement special logic
40
+ return nil_comparison unless state?
41
+
43
42
  # delegate to how the state compares to the other object
44
43
  state <=> other
45
44
  end
45
+
46
+ # Special logic for NULL/UNDEF state comparison
47
+ # @!visibility private
48
+ def nil_comparison
49
+ # if comparing to nil, consider ourselves equal
50
+ return 0 if other.nil?
51
+ # if the other object is an Item, only consider equal if we're
52
+ # in the same _kind_ of UnDefType state
53
+ return raw_state == other.raw_state if other.is_a?(GenericItem) && !other.state?
54
+
55
+ # otherwise, it's a non-nil thing comparing to nil, which is undefined
56
+ nil
57
+ end
46
58
  end
47
59
  end
48
60
  end
@@ -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
  #
@@ -64,15 +222,27 @@ module OpenHAB
64
222
  PERSISTENCE_METHODS.each do |method|
65
223
  define_method(method) do |timestamp, service = nil|
66
224
  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
225
+ result = PersistenceExtensions.public_send(method.to_s.delete_suffix('?'), self, to_zdt(timestamp),
226
+ 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(between_method.to_s.delete_suffix('?'), self, to_zdt(start),
236
+ to_zdt(finish), 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
  #
@@ -73,8 +73,7 @@ module OpenHAB
73
73
 
74
74
  # Gets the related Location Item of this Item.
75
75
  #
76
- # Returns +self+ if this Item is a Location. Otherwise, checks ancestor
77
- # groups one level at a time, returning the first Location Item found.
76
+ # Checks ancestor groups one level at a time, returning the first Location Item found.
78
77
  #
79
78
  # @return [GenericItem, nil]
80
79
  def location
@@ -92,8 +91,7 @@ module OpenHAB
92
91
 
93
92
  # Gets the related Equipment Item of this Item.
94
93
  #
95
- # Returns +self+ if this Item is an Equipment. Otherwise, checks ancestor
96
- # groups one level at a time, returning the first Equipment Item found.
94
+ # Checks ancestor groups one level at a time, returning the first Equipment Item found.
97
95
  #
98
96
  # @return [GenericItem, nil]
99
97
  def equipment
@@ -173,7 +171,7 @@ end
173
171
  # Additions to Enumerable to allow easily filtering groups of items based on the semantic model
174
172
  module Enumerable
175
173
  # Returns a new array of items that are a semantics Location (optionally of the given type)
176
- def sublocations(type = nil)
174
+ def locations(type = nil)
177
175
  if type && !(type < OpenHAB::DSL::Items::Semantics::Location)
178
176
  raise ArgumentError, 'type must be a subclass of Location'
179
177
  end
@@ -183,6 +181,8 @@ module Enumerable
183
181
 
184
182
  result
185
183
  end
184
+ # @deprecated Please use {#locations}
185
+ alias sublocations locations
186
186
 
187
187
  # Returns a new array of items that are a semantics equipment (optionally of the given type)
188
188
  #
@@ -54,7 +54,9 @@ module OpenHAB
54
54
  define_method(level) do |msg = nil, &block|
55
55
  log(severity: level, msg: msg, &block)
56
56
  end
57
- define_method("#{level}_enabled?") { @sl4fj_logger.send("is_#{level}_enabled") }
57
+ define_method("#{level}?") { @sl4fj_logger.send("is_#{level}_enabled") }
58
+ # @deprecated
59
+ alias_method "#{level}_enabled?", "#{level}?"
58
60
  end
59
61
 
60
62
  #
@@ -62,7 +64,7 @@ module OpenHAB
62
64
  # @param [String] preamble to put at start of log message
63
65
  # @param [Hash] kwargs key and values to log
64
66
  def state(preamble = 'State:', **kwargs)
65
- return unless trace_enabled?
67
+ return unless trace?
66
68
 
67
69
  states = kwargs.transform_keys(&:to_s)
68
70
  .transform_keys(&:capitalize)
@@ -82,7 +84,7 @@ module OpenHAB
82
84
  # @return [Exception] the exception, potentially with a cleaned backtrace.
83
85
  #
84
86
  def clean_backtrace(error)
85
- return error if debug_enabled?
87
+ return error if debug?
86
88
 
87
89
  if error.respond_to? :backtrace_locations
88
90
  backtrace = error.backtrace_locations.map(&:to_s).grep_v(INTERNAL_CALL_REGEX)
@@ -122,7 +124,7 @@ module OpenHAB
122
124
  raise ArgumentError, "Unknown Severity #{severity}" unless LEVELS.include? severity
123
125
 
124
126
  # Dynamically check enablement of underlying logger, this expands to "is_<level>_enabled"
125
- return unless send("#{severity}_enabled?")
127
+ return unless send("#{severity}?")
126
128
 
127
129
  # Process block if no message provided
128
130
  msg = yield if msg.nil? && block_given?
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.40.0'
8
+ VERSION = '4.42.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.40.0
4
+ version: 4.42.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-04-24 00:00:00.000000000 Z
11
+ date: 2022-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler