openhab-scripting 4.1.4 → 4.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/entity_lookup.rb +1 -57
  3. data/lib/openhab/dsl/dsl.rb +6 -12
  4. data/lib/openhab/dsl/group.rb +1 -5
  5. data/lib/openhab/dsl/items/comparable_item.rb +49 -0
  6. data/lib/openhab/dsl/items/contact_item.rb +41 -0
  7. data/lib/openhab/dsl/items/date_time_item.rb +64 -0
  8. data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
  9. data/lib/openhab/dsl/items/generic_item.rb +197 -0
  10. data/lib/openhab/dsl/items/group_item.rb +56 -92
  11. data/lib/openhab/dsl/items/image_item.rb +5 -41
  12. data/lib/openhab/dsl/items/item_registry.rb +49 -0
  13. data/lib/openhab/dsl/items/items.rb +71 -35
  14. data/lib/openhab/dsl/items/metadata.rb +325 -0
  15. data/lib/openhab/dsl/items/number_item.rb +6 -312
  16. data/lib/openhab/dsl/items/numeric_item.rb +66 -0
  17. data/lib/openhab/dsl/items/persistence.rb +122 -0
  18. data/lib/openhab/dsl/items/player_item.rb +49 -40
  19. data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
  20. data/lib/openhab/dsl/items/string_item.rb +16 -58
  21. data/lib/openhab/dsl/items/switch_item.rb +62 -0
  22. data/lib/openhab/dsl/lazy_array.rb +8 -6
  23. data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
  24. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
  25. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
  26. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
  27. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
  28. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
  29. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
  30. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
  31. data/lib/openhab/dsl/states.rb +1 -1
  32. data/lib/openhab/dsl/time_of_day.rb +3 -5
  33. data/lib/openhab/dsl/types/comparable_type.rb +21 -0
  34. data/lib/openhab/dsl/types/date_time_type.rb +334 -0
  35. data/lib/openhab/dsl/types/decimal_type.rb +187 -0
  36. data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
  37. data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
  38. data/lib/openhab/dsl/types/numeric_type.rb +39 -0
  39. data/lib/openhab/dsl/types/on_off_type.rb +29 -0
  40. data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
  41. data/lib/openhab/dsl/types/percent_type.rb +68 -0
  42. data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
  43. data/lib/openhab/dsl/types/quantity_type.rb +275 -0
  44. data/lib/openhab/dsl/types/refresh_type.rb +18 -0
  45. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
  46. data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
  47. data/lib/openhab/dsl/types/string_type.rb +88 -0
  48. data/lib/openhab/dsl/types/type.rb +72 -0
  49. data/lib/openhab/dsl/types/types.rb +77 -0
  50. data/lib/openhab/dsl/types/un_def_type.rb +22 -0
  51. data/lib/openhab/dsl/types/up_down_type.rb +32 -0
  52. data/lib/openhab/dsl/units.rb +11 -6
  53. data/lib/openhab/version.rb +1 -1
  54. data/lib/openhab.rb +0 -1
  55. metadata +31 -28
  56. data/lib/openhab/dsl/items/datetime_item.rb +0 -75
  57. data/lib/openhab/dsl/items/item_command.rb +0 -90
  58. data/lib/openhab/dsl/items/item_delegate.rb +0 -125
  59. data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
  60. data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
  61. data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
  62. data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
  63. data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
  64. data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
  65. data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
  66. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
  67. data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
  68. data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
  69. data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
  70. data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
  71. data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
  72. data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
  73. data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
  74. data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
  75. data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
  76. data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
  77. data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
  78. data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
  79. data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
  80. data/lib/openhab/dsl/types/datetime.rb +0 -338
  81. data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -1,300 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'forwardable'
5
-
6
- module OpenHAB
7
- module DSL
8
- #
9
- # Ruby implementation of OpenHAB Types
10
- #
11
- module Types
12
- #
13
- # Ruby implementation for OpenHAB quantities
14
- #
15
- # rubocop: disable Metrics/ClassLength
16
- # Disabled because this class has a single responsibility, there does not appear a logical
17
- # way of breaking it up into multiple classes
18
- class Quantity < Numeric
19
- extend Forwardable
20
- include OpenHAB::Log
21
-
22
- def_delegator :@quantity, :to_s
23
- def_delegators '@quantity.double_value', :positive?, :negative?, :zero?
24
-
25
- java_import org.openhab.core.library.types.QuantityType
26
- java_import org.openhab.core.library.types.DecimalType
27
- java_import org.openhab.core.types.util.UnitUtils
28
- java_import org.openhab.core.library.unit.Units
29
-
30
- # @return [Hash] Mapping of operation symbols to BigDecimal methods
31
- OPERATIONS = {
32
- '+' => 'add',
33
- '-' => 'subtract',
34
- '*' => 'multiply',
35
- '/' => 'divide'
36
- }.freeze
37
-
38
- private_constant :OPERATIONS
39
-
40
- attr_reader :quantity
41
-
42
- #
43
- # Create a new Quantity
44
- #
45
- # @param [object] quantity String,QuantityType or Numeric to be this quantity
46
- #
47
- # Cop disabled, case statement is compact and idiomatic
48
- def initialize(quantity)
49
- @quantity = case quantity
50
- when String then QuantityType.new(quantity)
51
- when QuantityType then quantity
52
- when NumberItem, Numeric then QuantityType.new(quantity.to_d.to_java, Units::ONE)
53
- else raise ArgumentError, "Unexpected type #{quantity.class} provided to Quantity initializer"
54
- end
55
- super()
56
- end
57
-
58
- #
59
- # Convert this quantity into a another unit
60
- #
61
- # @param [Object] other String or Unit to convert to
62
- #
63
- # @return [Quantity] This quantity converted to another unit
64
- #
65
- def |(other)
66
- other = UnitUtils.parse_unit(other) if other.is_a? String
67
-
68
- Quantity.new(quantity.to_unit(other))
69
- end
70
-
71
- #
72
- # Compare this quantity
73
- #
74
- # @param [Object] other object to compare to
75
- #
76
- # @return [Integer] -1,0,1 if this object is less than, equal to, or greater than the supplied object,
77
- # nil if it cannot be compared
78
- #
79
- def <=>(other)
80
- logger.trace("Comparing #{self} to #{other}")
81
- my_qt, other_qt = unitize(*to_qt(coerce(other).reverse))
82
- my_qt.compare_to(other_qt)
83
- end
84
-
85
- #
86
- # Coerce other object into a Quantity
87
- #
88
- # @param [Object] other object to convert to Quantity
89
- #
90
- # @return [Array] of self and other object as Quantity types, nil if object cannot be coerced
91
- #
92
- def coerce(other)
93
- logger.trace("Coercing #{self} as a request from #{other.class}")
94
- case other
95
- when Quantity then [other.quantity, quantity]
96
- when QuantityType then [other, quantity]
97
- when DecimalType then [Quantity.new(other.to_big_decimal.to_d), quantity]
98
- when NumberItem then [other.to_qt.quantity, quantity]
99
- when Numeric, String then [Quantity.new(other), self]
100
- end
101
- end
102
-
103
- #
104
- # Forward missing methods to Openhab Quantity Item if they are defined
105
- #
106
- # @param [String] meth name of method invoked
107
- # @param [Array] args arguments to invoked method
108
- # @param [Proc] block block passed ot method
109
- #
110
- # @return [Object] result of delegation
111
- #
112
- def method_missing(meth, *args, &block)
113
- logger.trace("Method missing, performing dynamic lookup for: #{meth}")
114
- if quantity.respond_to?(meth)
115
- quantity.__send__(meth, *args, &block)
116
- elsif ::Kernel.method_defined?(meth) || ::Kernel.private_method_defined?(meth)
117
- ::Kernel.instance_method(meth).bind_call(self, *args, &block)
118
- else
119
- super(meth, *args, &block)
120
- end
121
- end
122
-
123
- #
124
- # Checks if this method responds to the missing method
125
- #
126
- # @param [String] method_name Name of the method to check
127
- # @param [Boolean] _include_private boolean if private methods should be checked
128
- #
129
- # @return [Boolean] true if this object will respond to the supplied method, false otherwise
130
- #
131
- def respond_to_missing?(method_name, _include_private = false)
132
- quantity.respond_to?(method_name) ||
133
- ::Kernel.method_defined?(method_name) ||
134
- ::Kernel.private_method_defined?(method_name)
135
- end
136
-
137
- #
138
- # Negate the quantity
139
- #
140
- # @return [Quantity] This quantity negated
141
- #
142
- def -@
143
- Quantity.new(quantity.negate)
144
- end
145
-
146
- OPERATIONS.each do |operation, method|
147
- define_method(operation) do |other|
148
- logger.trace do
149
- "Executing math operation '#{operation}' on quantity #{inspect} "\
150
- "with other type #{other.class} and value #{other.inspect}"
151
- end
152
-
153
- a, b = to_qt(coerce(other).reverse)
154
- logger.trace("Coerced a='#{a}' with b='#{b}'")
155
- a, b = unitize(a, b, operation)
156
- logger.trace("Unitized a='#{a}' b='#{b}'")
157
- logger.trace("Performing operation '#{operation}' with method '#{method}' on a='#{a}' with b='#{b}'")
158
- Quantity.new(a.public_send(method, b))
159
- end
160
- end
161
-
162
- #
163
- # Provide details about quantity object
164
- #
165
- # @return [String] Representing details about the quantity object
166
- #
167
- def inspect
168
- if @quantity.unit == Units::ONE
169
- "unit=#{@quantity.unit}, value=#{@quantity.to_string}"
170
- else
171
- @quantity.to_string
172
- end
173
- end
174
-
175
- private
176
-
177
- # @return [Array] Array of strings for operations for which the operands will not be unitized
178
- DIMENSIONLESS_NON_UNITIZED_OPERATIONS = %w[* /].freeze
179
-
180
- # Dimensionless numbers should only be unitzed for addition and subtraction
181
-
182
- #
183
- # Convert one or more Quantity obects to the underlying quantitytypes
184
- #
185
- # @param [Array] quanities Array of either Quantity or QuantityType objects
186
- #
187
- # @return [Array] Array of QuantityType objects
188
- #
189
- def to_qt(*quantities)
190
- [quantities].flatten.compact.map { |item| item.is_a?(Quantity) ? item.quantity : item }
191
- end
192
-
193
- #
194
- # Checks if an item should be unitized
195
- #
196
- # @param [Quantity] quantity to check
197
- # @param [String] operation quantity is being used with
198
- #
199
- # @return [Boolean] True if the quantity should be unitzed based on the unit and operation, false otherwise
200
- #
201
- def unitize?(quantity, operation)
202
- !(quantity.unit == Units::ONE && DIMENSIONLESS_NON_UNITIZED_OPERATIONS.include?(operation))
203
- end
204
-
205
- #
206
- # Convert the unit for the quantity
207
- #
208
- # @param [Quantity] quantity being converted
209
- #
210
- # @return [Quantity] Quantity coverted to unit set by unit block
211
- #
212
- def convert_unit(quantity)
213
- return quantity unless unit?
214
-
215
- case quantity.unit
216
- when unit
217
- quantity
218
- when Units::ONE
219
- convert_unit_from_dimensionless(quantity, unit)
220
- else
221
- convert_unit_from_dimensioned(quantity, unit)
222
- end
223
- end
224
-
225
- #
226
- # Converts a dimensioned quantity to a specific unit
227
- #
228
- # @param [Quantity] quantity to convert
229
- # @param [Unit] unit to convert to
230
- #
231
- # @return [Java::org::openhab::core::library::types::QuantityType] converted quantity
232
- #
233
- def convert_unit_from_dimensioned(quantity, unit)
234
- logger.trace("Converting dimensioned item #{inspect} to #{unit}")
235
- quantity.to_unit(unit).tap do |converted|
236
- raise "Conversion from #{quantity.unit} to #{unit} failed" unless converted
237
- end
238
- end
239
-
240
- #
241
- # Converts a dimensionless quantity to a unit
242
- #
243
- # @param [Quantity] quantity to convert
244
- # @param [Unit] unit to convert to
245
- #
246
- # @return [Java::org::openhab::core::library::types::QuantityType] converted quantity
247
- #
248
- def convert_unit_from_dimensionless(quantity, unit)
249
- logger.trace("Converting dimensionless #{quantity} to #{unit}")
250
- QuantityType.new(quantity.to_big_decimal, unit)
251
- end
252
-
253
- #
254
- # Convert quantities to appropriate units
255
- #
256
- # @param [Quantity] quantity_a Quantity on left side of operation
257
- # @param [Quantity] quantity_b Quantity on right side of operation
258
- # @param [String] operation Math operation
259
- # @yield [quantity_a, quantity_b] yields unitized versions of supplied quantities
260
- #
261
- # @return [Array, Object] of quantites in correct units for the supplied operation and the unit
262
- # or the result of the block if a block is given
263
- #
264
- def unitize(quantity_a, quantity_b, operation = nil)
265
- logger.trace("Unitizing (#{quantity_a}) and (#{quantity_b})")
266
- quantity_a, quantity_b = [quantity_a, quantity_b].map do |qt|
267
- unitize?(qt, operation) ? convert_unit(qt) : qt.to_big_decimal
268
- end
269
-
270
- # Make sure the operation is called on the QuantityType
271
- if quantity_a.is_a?(Java::JavaMath::BigDecimal) && quantity_b.is_a?(QuantityType) && operation == '*'
272
- quantity_a, quantity_b = [quantity_b, quantity_a]
273
- end
274
- return yield quantity_a, quantity_b if block_given?
275
-
276
- [quantity_a, quantity_b]
277
- end
278
-
279
- #
280
- # Get the unit from the current thread local variable
281
- #
282
- # @return [Object] Unit or string representation of Unit, or nil if not set
283
- #
284
- def unit
285
- Thread.current.thread_variable_get(:unit)
286
- end
287
-
288
- #
289
- # Is a unit set for this thread
290
- #
291
- # @return [boolean] true if a unit is set by this thread, false otherwise
292
- #
293
- def unit?
294
- unit != nil
295
- end
296
- end
297
- end
298
- end
299
- end
300
- # rubocop: enable Metrics/ClassLength