openhab-scripting 3.4.1 → 3.6.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: 3dc9b053a3ac139909642f0e790a231dbd4e0b370a94836b740af22283f60315
4
- data.tar.gz: 9debccaf47bdf3d8b4c80e05d861d3e43f658f22ad569a9f60acf7a214bc0f37
3
+ metadata.gz: 7f22a9381b70e7ea6075cb06cf2ba5e576068e7b71235e3d1693747e8aba2048
4
+ data.tar.gz: e5b2f533163f3e4de0b9b45ac55e64bd61d70be991a44a17b0fbfdf09b13297a
5
5
  SHA512:
6
- metadata.gz: f04e70a4126f41f204b803617e757d66fe8e03f50543cc0a7a5c6c046b3567178f32d6cc6b97fbd9f0b0ca8b90c8c1fceb75b855bfe5565d4b8b7c8732e2efb8
7
- data.tar.gz: 5b2092565032d2431b488250d6dc442178ecab80642f86495f56f2f199f5b05cae2f48a78b53dea118043f96c174811a0705af5596b26c9b784d315cd0bb720d
6
+ metadata.gz: ded957fa16cf005bb2e5182a544cd85ee1e52765834dc427530000fe2939e443a5a25df610674b7f3f195123b51be0539fc9d821a3b92c0fa3775efc27908afa
7
+ data.tar.gz: e0e20addfadad2c22b7af3e999aee3ce2817bb0a7d0a42fdf16521f1a7e6d5d8730c578826f7879fe0e0b34394e7fc6b942eddfd0cc77aa64bd239b9792f5a91
@@ -41,6 +41,7 @@ module OpenHAB
41
41
  logger.trace("Creating delegate for (#{method}) to #{item}) for #{self.class}")
42
42
  def_delegator item, method
43
43
  end
44
+ define_method(:oh_item) { instance_variable_get(item) }
44
45
  end
45
46
 
46
47
  #
@@ -23,8 +23,8 @@ module OpenHAB
23
23
 
24
24
  java_import org.openhab.core.library.types.DecimalType
25
25
  java_import org.openhab.core.library.types.QuantityType
26
- java_import 'tec.uom.se.format.SimpleUnitFormat'
27
- java_import 'tec.uom.se.AbstractUnit'
26
+ java_import org.openhab.core.types.util.UnitUtils
27
+ java_import org.openhab.core.library.unit.Units
28
28
 
29
29
  item_type Java::OrgOpenhabCoreLibraryItems::NumberItem
30
30
 
@@ -93,7 +93,7 @@ module OpenHAB
93
93
  # @return [OpenHAB::DSL::Types::Quantity] NumberItem converted to supplied Unit
94
94
  #
95
95
  def |(other)
96
- other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
96
+ other = UnitUtils.parse_unit(other) if other.is_a? String
97
97
 
98
98
  if dimension
99
99
  to_qt | other
@@ -111,7 +111,7 @@ module OpenHAB
111
111
  if dimension
112
112
  Quantity.new(@number_item.get_state_as(QuantityType))
113
113
  else
114
- Quantity.new(QuantityType.new(to_d.to_java, AbstractUnit::ONE))
114
+ Quantity.new(QuantityType.new(to_d.to_java, Units::ONE))
115
115
  end
116
116
  end
117
117
 
@@ -33,7 +33,7 @@ module OpenHAB
33
33
  #
34
34
  #
35
35
  def command(command)
36
- command = command.to_java.strip_trailing_zeros if command.is_a? BigDecimal
36
+ command = command.to_java.strip_trailing_zeros.to_plain_string if command.is_a? BigDecimal
37
37
  logger.trace "Sending Command #{command} to #{id}"
38
38
  BusEvent.sendCommand(self, command.to_s)
39
39
  end
@@ -30,7 +30,7 @@ module OpenHAB
30
30
 
31
31
  def initialize(metadata: nil, key: nil, value: nil, config: nil)
32
32
  @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
33
- super(@metadata&.configuration)
33
+ super(to_ruby(@metadata&.configuration))
34
34
  end
35
35
 
36
36
  #
@@ -85,6 +85,44 @@ module OpenHAB
85
85
  def to_a
86
86
  [@metadata&.value, @metadata&.configuration || {}]
87
87
  end
88
+
89
+ private
90
+
91
+ #
92
+ # Recursively convert the supplied Hash object into a Ruby Hash and recreate the keys and values
93
+ #
94
+ # @param [Hash] Hash to convert
95
+ #
96
+ # @return [Hash] The converted hash
97
+ #
98
+ def to_ruby_hash(hash)
99
+ return unless hash.respond_to? :each_with_object
100
+
101
+ hash.each_with_object({}) { |(key, value), ruby_hash| ruby_hash[to_ruby(key)] = to_ruby(value) }
102
+ end
103
+
104
+ #
105
+ # Recursively convert the supplied array to a Ruby array and recreate all String values
106
+ #
107
+ # @param [Object] array to convert
108
+ #
109
+ # @return [Array] The converted array
110
+ #
111
+ def to_ruby_array(array)
112
+ return unless array.respond_to? :each_with_object
113
+
114
+ array.each_with_object([]) { |value, ruby_array| ruby_array << to_ruby(value) }
115
+ end
116
+
117
+ # Convert the given object to Ruby equivalent
118
+ def to_ruby(value)
119
+ case value
120
+ when Hash, Java::JavaUtil::Map then to_ruby_hash(value)
121
+ when Array, Java::JavaUtil::List then to_ruby_array(value)
122
+ when String then String.new(value)
123
+ else value
124
+ end
125
+ end
88
126
  end
89
127
 
90
128
  #
@@ -10,6 +10,7 @@ module OpenHAB
10
10
  #
11
11
  module Types
12
12
  java_import Java::OrgOpenhabCoreLibraryTypes::DecimalType
13
+ java_import org.openhab.core.types.util.UnitUtils
13
14
 
14
15
  #
15
16
  # MonkeyPatching Decimal Type
@@ -72,7 +73,7 @@ module OpenHAB
72
73
  # @return [OpenHAB::Core::DSL::Types::Quantity] NumberItem converted to supplied Unit
73
74
  #
74
75
  def |(other)
75
- other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
76
+ other = UnitUtils.parse_unit(other) if other.is_a? String
76
77
  Quantity.new(QuantityType.new(to_big_decimal, other))
77
78
  end
78
79
 
@@ -48,7 +48,8 @@ module OpenHAB
48
48
  # @return [StateStorage] item states
49
49
  #
50
50
  def store_states(*items)
51
- states = StateStorage.new(BusEvent.storeStates(*items.flatten).to_h)
51
+ items = items.flatten.map { |item| item.respond_to?(:oh_item) ? item.oh_item : item }
52
+ states = StateStorage.new(BusEvent.storeStates(*items).to_h)
52
53
  if block_given?
53
54
  yield
54
55
  states.restore
@@ -23,8 +23,8 @@ module OpenHAB
23
23
 
24
24
  java_import org.openhab.core.library.types.QuantityType
25
25
  java_import org.openhab.core.library.types.DecimalType
26
- java_import 'tec.uom.se.format.SimpleUnitFormat'
27
- java_import 'tec.uom.se.AbstractUnit'
26
+ java_import org.openhab.core.types.util.UnitUtils
27
+ java_import org.openhab.core.library.unit.Units
28
28
 
29
29
  # @return [Hash] Mapping of operation symbols to BigDecimal methods
30
30
  OPERATIONS = {
@@ -48,7 +48,7 @@ module OpenHAB
48
48
  @quantity = case quantity
49
49
  when String then QuantityType.new(quantity)
50
50
  when QuantityType then quantity
51
- when NumberItem, Numeric then QuantityType.new(quantity.to_d.to_java, AbstractUnit::ONE)
51
+ when NumberItem, Numeric then QuantityType.new(quantity.to_d.to_java, Units::ONE)
52
52
  else raise ArgumentError, "Unexpected type #{quantity.class} provided to Quantity initializer"
53
53
  end
54
54
  super()
@@ -62,7 +62,7 @@ module OpenHAB
62
62
  # @return [Quantity] This quantity converted to another unit
63
63
  #
64
64
  def |(other)
65
- other = SimpleUnitFormat.instance.unitFor(other) if other.is_a? String
65
+ other = UnitUtils.parse_unit(other) if other.is_a? String
66
66
 
67
67
  Quantity.new(quantity.to_unit(other))
68
68
  end
@@ -164,7 +164,7 @@ module OpenHAB
164
164
  # @return [String] Representing details about the quantity object
165
165
  #
166
166
  def inspect
167
- if @quantity.unit == AbstractUnit::ONE
167
+ if @quantity.unit == Units::ONE
168
168
  "unit=#{@quantity.unit}, value=#{@quantity.to_string}"
169
169
  else
170
170
  @quantity.to_string
@@ -198,7 +198,7 @@ module OpenHAB
198
198
  # @return [Boolean] True if the quantity should be unitzed based on the unit and operation, false otherwise
199
199
  #
200
200
  def unitize?(quantity, operation)
201
- !(quantity.unit == AbstractUnit::ONE && DIMENSIONLESS_NON_UNITIZED_OPERATIONS.include?(operation))
201
+ !(quantity.unit == Units::ONE && DIMENSIONLESS_NON_UNITIZED_OPERATIONS.include?(operation))
202
202
  end
203
203
 
204
204
  #
@@ -214,7 +214,7 @@ module OpenHAB
214
214
  case quantity.unit
215
215
  when unit
216
216
  quantity
217
- when AbstractUnit::ONE
217
+ when Units::ONE
218
218
  convert_unit_from_dimensionless(quantity, unit)
219
219
  else
220
220
  convert_unit_from_dimensioned(quantity, unit)
@@ -263,7 +263,12 @@ module OpenHAB
263
263
  def unitize(quantity_a, quantity_b, operation = nil)
264
264
  logger.trace("Unitizing (#{quantity_a}) and (#{quantity_b})")
265
265
  quantity_a, quantity_b = [quantity_a, quantity_b].map do |qt|
266
- unitize?(qt, operation) ? convert_unit(qt) : qt
266
+ unitize?(qt, operation) ? convert_unit(qt) : qt.to_big_decimal
267
+ end
268
+
269
+ # Make sure the operation is called on the QuantityType
270
+ if quantity_a.is_a?(Java::JavaMath::BigDecimal) && quantity_b.is_a?(QuantityType) && operation == '*'
271
+ quantity_a, quantity_b = [quantity_b, quantity_a]
267
272
  end
268
273
  return yield quantity_a, quantity_b if block_given?
269
274
 
@@ -10,6 +10,7 @@ end
10
10
 
11
11
  Object.send(:remove_const, :QuantityType)
12
12
  java_import org.openhab.core.library.types.QuantityType
13
+ java_import org.openhab.core.types.util.UnitUtils
13
14
 
14
15
  module OpenHAB
15
16
  module DSL
@@ -17,8 +18,6 @@ module OpenHAB
17
18
  # Provides support for interacting with OpenHAB Units of Measurement
18
19
  #
19
20
  module Units
20
- java_import 'tec.uom.se.format.SimpleUnitFormat'
21
-
22
21
  #
23
22
  # Sets a thread local variable to the supplied unit such that classes operating inside the block
24
23
  # can perform automatic conversions to the supplied unit for NumberItems
@@ -28,7 +27,7 @@ module OpenHAB
28
27
  #
29
28
  #
30
29
  def unit(unit)
31
- unit = SimpleUnitFormat.instance.unitFor(unit) if unit.is_a? String
30
+ unit = UnitUtils.parse_unit(unit) if unit.is_a? String
32
31
  Thread.current.thread_variable_set(:unit, unit)
33
32
  yield
34
33
  ensure
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '3.4.1'
8
+ VERSION = '3.6.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: 3.4.1
4
+ version: 3.6.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: 2021-04-02 00:00:00.000000000 Z
11
+ date: 2021-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  requirements: []
135
135
  rubyforge_project:
136
- rubygems_version: 2.7.6.2
136
+ rubygems_version: 2.7.6.3
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: JRuby Helper Libraries for OpenHAB Scripting