openhab-jrubyscripting 5.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/lib/openhab/core/actions.rb +163 -0
  3. data/lib/openhab/core/entity_lookup.rb +144 -0
  4. data/lib/openhab/core/events/abstract_event.rb +17 -0
  5. data/lib/openhab/core/events/item_channel_link.rb +36 -0
  6. data/lib/openhab/core/events/item_command_event.rb +78 -0
  7. data/lib/openhab/core/events/item_event.rb +22 -0
  8. data/lib/openhab/core/events/item_state_changed_event.rb +52 -0
  9. data/lib/openhab/core/events/item_state_event.rb +51 -0
  10. data/lib/openhab/core/events/thing.rb +29 -0
  11. data/lib/openhab/core/events/thing_status_info_event.rb +53 -0
  12. data/lib/openhab/core/events.rb +10 -0
  13. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  14. data/lib/openhab/core/items/color_item.rb +52 -0
  15. data/lib/openhab/core/items/contact_item.rb +52 -0
  16. data/lib/openhab/core/items/date_time_item.rb +58 -0
  17. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  18. data/lib/openhab/core/items/generic_item.rb +344 -0
  19. data/lib/openhab/core/items/group_item.rb +174 -0
  20. data/lib/openhab/core/items/image_item.rb +109 -0
  21. data/lib/openhab/core/items/location_item.rb +34 -0
  22. data/lib/openhab/core/items/metadata/hash.rb +390 -0
  23. data/lib/openhab/core/items/metadata/namespace_hash.rb +469 -0
  24. data/lib/openhab/core/items/metadata.rb +11 -0
  25. data/lib/openhab/core/items/number_item.rb +62 -0
  26. data/lib/openhab/core/items/numeric_item.rb +22 -0
  27. data/lib/openhab/core/items/persistence.rb +327 -0
  28. data/lib/openhab/core/items/player_item.rb +66 -0
  29. data/lib/openhab/core/items/proxy.rb +59 -0
  30. data/lib/openhab/core/items/registry.rb +66 -0
  31. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  32. data/lib/openhab/core/items/semantics/enumerable.rb +152 -0
  33. data/lib/openhab/core/items/semantics.rb +476 -0
  34. data/lib/openhab/core/items/state_storage.rb +53 -0
  35. data/lib/openhab/core/items/string_item.rb +28 -0
  36. data/lib/openhab/core/items/switch_item.rb +78 -0
  37. data/lib/openhab/core/items.rb +114 -0
  38. data/lib/openhab/core/lazy_array.rb +52 -0
  39. data/lib/openhab/core/profile_factory.rb +118 -0
  40. data/lib/openhab/core/script_handling.rb +55 -0
  41. data/lib/openhab/core/things/channel.rb +48 -0
  42. data/lib/openhab/core/things/channel_uid.rb +51 -0
  43. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  44. data/lib/openhab/core/things/profile_callback.rb +52 -0
  45. data/lib/openhab/core/things/proxy.rb +69 -0
  46. data/lib/openhab/core/things/registry.rb +46 -0
  47. data/lib/openhab/core/things/thing.rb +194 -0
  48. data/lib/openhab/core/things.rb +22 -0
  49. data/lib/openhab/core/timer.rb +128 -0
  50. data/lib/openhab/core/types/comparable_type.rb +23 -0
  51. data/lib/openhab/core/types/date_time_type.rb +259 -0
  52. data/lib/openhab/core/types/decimal_type.rb +192 -0
  53. data/lib/openhab/core/types/hsb_type.rb +183 -0
  54. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  55. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  56. data/lib/openhab/core/types/numeric_type.rb +52 -0
  57. data/lib/openhab/core/types/on_off_type.rb +46 -0
  58. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  59. data/lib/openhab/core/types/percent_type.rb +95 -0
  60. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  61. data/lib/openhab/core/types/point_type.rb +117 -0
  62. data/lib/openhab/core/types/quantity_type.rb +327 -0
  63. data/lib/openhab/core/types/raw_type.rb +26 -0
  64. data/lib/openhab/core/types/refresh_type.rb +27 -0
  65. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  66. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  67. data/lib/openhab/core/types/string_type.rb +76 -0
  68. data/lib/openhab/core/types/type.rb +117 -0
  69. data/lib/openhab/core/types/un_def_type.rb +38 -0
  70. data/lib/openhab/core/types/up_down_type.rb +50 -0
  71. data/lib/openhab/core/types.rb +69 -0
  72. data/lib/openhab/core/uid.rb +36 -0
  73. data/lib/openhab/core.rb +85 -0
  74. data/lib/openhab/core_ext/java/duration.rb +115 -0
  75. data/lib/openhab/core_ext/java/local_date.rb +93 -0
  76. data/lib/openhab/core_ext/java/local_time.rb +106 -0
  77. data/lib/openhab/core_ext/java/month.rb +59 -0
  78. data/lib/openhab/core_ext/java/month_day.rb +105 -0
  79. data/lib/openhab/core_ext/java/period.rb +103 -0
  80. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  81. data/lib/openhab/core_ext/java/time.rb +58 -0
  82. data/lib/openhab/core_ext/java/unit.rb +15 -0
  83. data/lib/openhab/core_ext/java/zoned_date_time.rb +116 -0
  84. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  85. data/lib/openhab/core_ext/ruby/class.rb +15 -0
  86. data/lib/openhab/core_ext/ruby/date.rb +89 -0
  87. data/lib/openhab/core_ext/ruby/numeric.rb +190 -0
  88. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  89. data/lib/openhab/core_ext/ruby/time.rb +104 -0
  90. data/lib/openhab/core_ext.rb +18 -0
  91. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  92. data/lib/openhab/dsl/events.rb +9 -0
  93. data/lib/openhab/dsl/gems.rb +3 -0
  94. data/lib/openhab/dsl/items/builder.rb +618 -0
  95. data/lib/openhab/dsl/items/ensure.rb +93 -0
  96. data/lib/openhab/dsl/items/timed_command.rb +236 -0
  97. data/lib/openhab/dsl/rules/automation_rule.rb +308 -0
  98. data/lib/openhab/dsl/rules/builder.rb +1373 -0
  99. data/lib/openhab/dsl/rules/guard.rb +115 -0
  100. data/lib/openhab/dsl/rules/name_inference.rb +160 -0
  101. data/lib/openhab/dsl/rules/property.rb +76 -0
  102. data/lib/openhab/dsl/rules/rule_triggers.rb +96 -0
  103. data/lib/openhab/dsl/rules/terse.rb +63 -0
  104. data/lib/openhab/dsl/rules/triggers/changed.rb +169 -0
  105. data/lib/openhab/dsl/rules/triggers/channel.rb +57 -0
  106. data/lib/openhab/dsl/rules/triggers/command.rb +107 -0
  107. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +161 -0
  108. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +164 -0
  109. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +195 -0
  110. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +127 -0
  111. data/lib/openhab/dsl/rules/triggers/trigger.rb +56 -0
  112. data/lib/openhab/dsl/rules/triggers/updated.rb +130 -0
  113. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +55 -0
  114. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +155 -0
  115. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  116. data/lib/openhab/dsl/rules.rb +29 -0
  117. data/lib/openhab/dsl/script_handling.rb +55 -0
  118. data/lib/openhab/dsl/things/builder.rb +263 -0
  119. data/lib/openhab/dsl/thread_local.rb +48 -0
  120. data/lib/openhab/dsl/timer_manager.rb +191 -0
  121. data/lib/openhab/dsl/version.rb +9 -0
  122. data/lib/openhab/dsl.rb +686 -0
  123. data/lib/openhab/log.rb +348 -0
  124. data/lib/openhab/osgi.rb +70 -0
  125. data/lib/openhab/rspec/configuration.rb +56 -0
  126. data/lib/openhab/rspec/example_group.rb +90 -0
  127. data/lib/openhab/rspec/helpers.rb +439 -0
  128. data/lib/openhab/rspec/hooks.rb +93 -0
  129. data/lib/openhab/rspec/jruby.rb +46 -0
  130. data/lib/openhab/rspec/karaf.rb +811 -0
  131. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  132. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  133. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  134. data/lib/openhab/rspec/mocks/metadata_provider.rb +75 -0
  135. data/lib/openhab/rspec/mocks/persistence_service.rb +140 -0
  136. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  137. data/lib/openhab/rspec/mocks/synchronous_executor.rb +56 -0
  138. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  139. data/lib/openhab/rspec/mocks/timer.rb +95 -0
  140. data/lib/openhab/rspec/openhab/core/actions.rb +26 -0
  141. data/lib/openhab/rspec/openhab/core/items/proxy.rb +27 -0
  142. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  143. data/lib/openhab/rspec/shell.rb +31 -0
  144. data/lib/openhab/rspec/suspend_rules.rb +60 -0
  145. data/lib/openhab/rspec.rb +17 -0
  146. data/lib/openhab/yard/cli/stats.rb +23 -0
  147. data/lib/openhab/yard/code_objects/group_object.rb +17 -0
  148. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  149. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  150. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  151. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  152. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  153. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  154. data/lib/openhab/yard/handlers/jruby/base.rb +49 -0
  155. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  156. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  157. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +27 -0
  158. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  159. data/lib/openhab/yard/html_helper.rb +44 -0
  160. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  161. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  162. data/lib/openhab/yard/tags/library.rb +3 -0
  163. data/lib/openhab/yard.rb +32 -0
  164. metadata +504 -0
@@ -0,0 +1,192 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "comparable_type"
4
+ require_relative "numeric_type"
5
+
6
+ require_relative "type"
7
+
8
+ module OpenHAB
9
+ module Core
10
+ module Types
11
+ DecimalType = org.openhab.core.library.types.DecimalType
12
+
13
+ #
14
+ # {DecimalType} uses a {java.lang.BigDecimal} internally and thus can be
15
+ # used for integers, longs and floating point numbers alike.
16
+ #
17
+ # @example DecimalType can be used in case statements with ranges
18
+ # # Check if number item is less than 50
19
+ # case NumberOne.state
20
+ # when (0...50)
21
+ # logger.info("#{NumberOne} is less than 50")
22
+ # when (50..100)
23
+ # logger.info("#{NumberOne} is greater than 50")
24
+ # end
25
+ #
26
+ # @example DecimalType can be compared directly against Numeric
27
+ # if NumberOne.state > 10
28
+ # logger.info("Item #{NumberOne.name} is greater than 10")
29
+ # end
30
+ #
31
+ # @example DecimalType can be compared against the value of another DecimalType
32
+ # if NumberOne.state > NumberTwo.state
33
+ # logger.info("Item #{NumberOne} (#{NumberOne.state}) is greater than #{NumberTwo} (#{NumberTwo.state})")
34
+ # end
35
+ #
36
+ class DecimalType
37
+ # @!parse include Command, State
38
+ include NumericType
39
+ include ComparableType
40
+
41
+ #
42
+ # @!method initialize(value)
43
+ #
44
+ # Create a new instance of DecimalType
45
+ #
46
+ # @param [java.math.BigDecimal, Numeric] value Create a DecimalType from the given value
47
+ #
48
+ def initialize(*args)
49
+ unless args.length == 1
50
+ super
51
+ return
52
+ end
53
+
54
+ value = args.first
55
+ if value.is_a?(java.math.BigDecimal)
56
+ super
57
+ elsif value.is_a?(BigDecimal)
58
+ super(value.to_java.strip_trailing_zeros)
59
+ elsif value.is_a?(DecimalType)
60
+ super(value.to_big_decimal)
61
+ elsif value.respond_to?(:to_d)
62
+ super(value.to_d.to_java.strip_trailing_zeros)
63
+ else # rubocop:disable Lint/DuplicateBranch
64
+ # duplicates the Java BigDecimal branch, but that needs to go first
65
+ # in order to avoid unnecessary conversions
66
+ super
67
+ end
68
+ end
69
+
70
+ #
71
+ # Convert DecimalType to a QuantityType
72
+ #
73
+ # @param [String, javax.measure.units.Unit] other
74
+ #
75
+ # @return [QuantityType] `self` as a {QuantityType} of the supplied Unit
76
+ #
77
+ def |(other)
78
+ other = org.openhab.core.types.util.UnitUtils.parse_unit(other.to_str) if other.respond_to?(:to_str)
79
+ QuantityType.new(to_big_decimal, other)
80
+ end
81
+
82
+ #
83
+ # Comparison
84
+ #
85
+ # @param [NumericType, Numeric]
86
+ # other object to compare to
87
+ #
88
+ # @return [Integer, nil] -1, 0, +1 depending on whether `other` is
89
+ # less than, equal to, or greater than self
90
+ #
91
+ # `nil` is returned if the two values are incomparable.
92
+ #
93
+ def <=>(other)
94
+ logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
95
+ if other.is_a?(QuantityType) || other.is_a?(HSBType)
96
+ (other <=> self)&.-@
97
+ elsif other.is_a?(self.class)
98
+ compare_to(other)
99
+ elsif other.respond_to?(:to_d)
100
+ to_d <=> other.to_d
101
+ elsif other.respond_to?(:coerce)
102
+ return nil unless (lhs, rhs = other.coerce(self))
103
+
104
+ lhs <=> rhs
105
+ end
106
+ end
107
+
108
+ #
109
+ # Type Coercion
110
+ #
111
+ # Coerce object to a {DecimalType DecimalType}
112
+ #
113
+ # @param [Numeric, Type] other object to coerce to a {DecimalType DecimalType}
114
+ #
115
+ # If `other` is a {Type}, `self` will instead be coerced
116
+ # to that type to accomodate comparison with things such as {OnOffType}.
117
+ #
118
+ # @return [Array<(DecimalType, DecimalType)>, nil]
119
+ #
120
+ def coerce(other)
121
+ logger.trace("Coercing #{self} as a request from #{other.class}")
122
+ if other.is_a?(Type)
123
+ [other, as(other.class)]
124
+ elsif other.respond_to?(:to_d)
125
+ [self.class.new(other.to_d), self]
126
+ end
127
+ end
128
+
129
+ #
130
+ # Unary minus
131
+ #
132
+ # Negates self
133
+ #
134
+ # @return [DecimalType]
135
+ def -@
136
+ self.class.new(to_big_decimal.negate)
137
+ end
138
+
139
+ {
140
+ add: :+,
141
+ subtract: :-,
142
+ multiply: :*,
143
+ divide: :/,
144
+ remainder: :%,
145
+ pow: :**
146
+ }.each do |java_op, ruby_op|
147
+ class_eval( # rubocop:disable Style/DocumentDynamicEvalDefinition https://github.com/rubocop/rubocop/issues/10179
148
+ # def +(other)
149
+ # if other.is_a?(DecimalType)
150
+ # self.class.new(to_big_decimal.add(other.to_big_decimal))
151
+ # elsif other.is_a?(java.math.BigDecimal)
152
+ # self.class.new(to_big_decimal.add(other))
153
+ # elsif other.respond_to?(:to_d)
154
+ # result = to_d + other
155
+ # # result could already be a QuantityType
156
+ # result = self.class.new(result) unless result.is_a?(NumericType)
157
+ # result
158
+ # elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
159
+ # lhs + rhs
160
+ # else
161
+ # raise TypeError, "#{other.class} can't be coerced into #{self.class}"
162
+ # end
163
+ # end
164
+ <<~RUBY, __FILE__, __LINE__ + 1
165
+ def #{ruby_op}(other)
166
+ if other.is_a?(DecimalType)
167
+ self.class.new(to_big_decimal.#{java_op}(other.to_big_decimal, java.math.MathContext::DECIMAL128))
168
+ elsif other.is_a?(java.math.BigDecimal)
169
+ self.class.new(to_big_decimal.#{java_op}(other, java.math.MathContext::DECIMAL128))
170
+ elsif other.respond_to?(:to_d)
171
+ result = to_d #{ruby_op} other
172
+ # result could already be a QuantityType
173
+ result = self.class.new(result) unless result.is_a?(NumericType)
174
+ result
175
+ elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
176
+ lhs #{ruby_op} rhs
177
+ else
178
+ raise TypeError, "\#{other.class} can't be coerced into \#{self.class}"
179
+ end
180
+ end
181
+ RUBY
182
+ )
183
+ end
184
+
185
+ # any method that exists on BigDecimal gets forwarded to to_d
186
+ delegate (BigDecimal.instance_methods - instance_methods) => :to_d
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ # @!parse DecimalType = OpenHAB::Core::Types::DecimalType
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "percent_type"
4
+
5
+ require_relative "type"
6
+
7
+ module OpenHAB
8
+ module Core
9
+ module Types
10
+ HSBType = org.openhab.core.library.types.HSBType
11
+
12
+ # {HSBType} is a complex type with constituents for hue, saturation and
13
+ # brightness and can be used for color items.
14
+ class HSBType < PercentType
15
+ # @!constant BLACK
16
+ # @return [HSBType]
17
+ # @!constant WHITE
18
+ # @return [HSBType]
19
+ # @!constant RED
20
+ # @return [HSBType]
21
+ # @!constant GREEN
22
+ # @return [HSBType]
23
+ # @!constant BLUE
24
+ # @return [HSBType]
25
+
26
+ # conversion to QuantityType doesn't make sense on HSBType
27
+ undef_method :|
28
+
29
+ remove_method :==
30
+
31
+ # r, g, b as an array of symbols
32
+ RGB_KEYS = %i[r g b].freeze
33
+ private_constant :RGB_KEYS
34
+
35
+ class << self
36
+ # @!method from_rgb(r, g, b)
37
+ # Create HSBType from RGB values
38
+ # @param r [Integer] Red component (0-255)
39
+ # @param g [Integer] Green component (0-255)
40
+ # @param b [Integer] Blue component (0-255)
41
+ # @return [HSBType]
42
+
43
+ # @!method from_xy(x, y)
44
+ # Create HSBType representing the provided xy color values in CIE XY color model
45
+ # @param x [Float]
46
+ # @param y [Float]
47
+ # @return [HSBType]
48
+
49
+ # Create HSBType from hue, saturation, and brightness values
50
+ # @param hue [DecimalType, QuantityType, Numeric] Hue component (0-360º)
51
+ # @param saturation [PercentType, Numeric] Saturation component (0-100%)
52
+ # @param brightness [PercentType, Numeric] Brightness component (0-100%)
53
+ # @return [HSBType]
54
+ def from_hsb(hue, saturation, brightness)
55
+ new(hue, saturation, brightness)
56
+ end
57
+
58
+ # add additional "overloads" to the constructor
59
+ # @!visibility private
60
+ def new(*args)
61
+ if args.length == 1 && args.first.respond_to?(:to_str)
62
+ value = args.first.to_str
63
+
64
+ # parse some formats OpenHAB doesn't understand
65
+ # in this case, HTML hex format for rgb
66
+ if (match = value.match(/^#(\h{2})(\h{2})(\h{2})$/))
67
+ rgb = match.to_a[1..3].map { |v| v.to_i(16) }
68
+ logger.trace("creating from rgb #{rgb.inspect}")
69
+ return from_rgb(*rgb)
70
+ end
71
+ end
72
+
73
+ # Convert strings using java class
74
+ return value_of(args.first) if args.length == 1 && args.first.is_a?(String)
75
+
76
+ # use super constructor for empty args
77
+ return super unless args.length == 3
78
+
79
+ # convert from several numeric-like types to the exact types
80
+ # OpenHAB needs
81
+ hue = args[0]
82
+ args[0] = if hue.is_a?(DecimalType)
83
+ hue
84
+ elsif hue.is_a?(QuantityType)
85
+ DecimalType.new(hue.to_unit(Units::DEGREE_ANGLE).to_big_decimal)
86
+ elsif hue.respond_to?(:to_d)
87
+ DecimalType.new(hue)
88
+ end
89
+ args[1..2] = args[1..2].map do |v|
90
+ if v.is_a?(PercentType)
91
+ v
92
+ elsif v.respond_to?(:to_d)
93
+ PercentType.new(v)
94
+ end
95
+ end
96
+
97
+ super(*args)
98
+ end
99
+ end
100
+
101
+ #
102
+ # Comparison
103
+ #
104
+ # @param [NumericType, Numeric]
105
+ # other object to compare to
106
+ #
107
+ # @return [Integer, nil] -1, 0, +1 depending on whether `other` is
108
+ # less than, equal to, or greater than self
109
+ #
110
+ # `nil` is returned if the two values are incomparable.
111
+ #
112
+ def <=>(other)
113
+ logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
114
+ if other.is_a?(HSBType)
115
+ [brightness, hue, saturation] <=> [other.brightness, other.hue, other.saturation]
116
+ else
117
+ super
118
+ end
119
+ end
120
+
121
+ # rename raw methods so we can overwrite them
122
+ # @!visibility private
123
+ alias_method :raw_hue, :hue
124
+
125
+ # @!attribute [r] hue
126
+ # @return [QuantityType] The color's hue component as a {QuantityType} of unit DEGREE_ANGLE.
127
+ def hue
128
+ QuantityType.new(raw_hue.to_big_decimal, Units::DEGREE_ANGLE)
129
+ end
130
+
131
+ # Convert to a packed 32-bit RGB value representing the color in the default sRGB color model.
132
+ #
133
+ # The alpha component is always 100%.
134
+ #
135
+ # @return [Integer]
136
+ alias_method :argb, :rgb
137
+
138
+ # Convert to a packed 24-bit RGB value representing the color in the default sRGB color model.
139
+ # @return [Integer]
140
+ def rgb
141
+ argb & 0xffffff
142
+ end
143
+
144
+ # Convert to an HTML-style string of 6 hex characters in the default sRGB color model.
145
+ # @return [String] +'#xxxxxx'+
146
+ def to_hex
147
+ Kernel.format("#%06x", rgb)
148
+ end
149
+
150
+ # include units
151
+ # @!visibility private
152
+ def to_s
153
+ "#{hue},#{saturation},#{brightness}"
154
+ end
155
+
156
+ # @!attribute [r] saturation
157
+ # @return [PercentType]
158
+
159
+ # @!attribute [r] brightness
160
+ # @return [PercentType]
161
+
162
+ # @!attribute [r] red
163
+ # @return [PercentType]
164
+
165
+ # @!attribute [r] green
166
+ # @return [PercentType]
167
+
168
+ # @!attribute [r] blue
169
+ # @return [PercentType]
170
+
171
+ # @!method to_rgb
172
+ # Convert to RGB values representing the color in the default sRGB color model
173
+ # @return [[PercentType, PercentType, PercentType]]
174
+
175
+ # @!method to_xy
176
+ # Convert to the xyY values representing this object's color in CIE XY color model
177
+ # @return [[PercentType, PercentType]]
178
+ end
179
+ end
180
+ end
181
+ end
182
+
183
+ # @!parse HSBType = OpenHAB::Core::Types::HSBType
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "type"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Types
8
+ IncreaseDecreaseType = org.openhab.core.library.types.IncreaseDecreaseType
9
+
10
+ # Represents {INCREASE} and {DECREASE} commands.
11
+ class IncreaseDecreaseType # rubocop:disable Lint/EmptyClass
12
+ # @!parse include Command
13
+
14
+ # @!constant INCREASE
15
+ # Increase Command
16
+ # @!constant DECREASE
17
+ # Decrease Command
18
+
19
+ # @!method increase?
20
+ # Check if `self == INCREASE`
21
+ # @return [true,false]
22
+
23
+ # @!method decrease?
24
+ # Check if `self == DECREASE`
25
+ # @return [true,false]
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ # @!parse
32
+ # IncreaseDecreaseType = OpenHAB::Core::Types::IncreaseDecreaseType
33
+ # INCREASE = OpenHAB::Core::Types::IncreaseDecreaseType::INCREASE
34
+ # DECREASE = OpenHAB::Core::Types::IncreaseDecreaseType::DECREASE
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "type"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Types
8
+ NextPreviousType = org.openhab.core.library.types.NextPreviousType
9
+
10
+ # Implements {NEXT} and {PREVIOUS} commands.
11
+ class NextPreviousType # rubocop:disable Lint/EmptyClass
12
+ # @!parse include Command
13
+
14
+ # @!constant NEXT
15
+ # Next Command
16
+ # @!constant PREVIOUS
17
+ # Previous Command
18
+
19
+ # @!method next?
20
+ # Check if `self == NEXT`
21
+ # @return [true,false]
22
+
23
+ # @!method previous?
24
+ # Check if `self == PREVIOUS`
25
+ # @return [true,false]
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ # @!parse
32
+ # NextPreviousType = OpenHAB::Core::Types::NextPreviousType
33
+ # NEXT = OpenHAB::Core::Types::NextPreviousType::NEXT
34
+ # PREVIOUS = OpenHAB::Core::Types::NextPreviousType::PREVIOUS
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "bigdecimal/util"
5
+ require "forwardable"
6
+
7
+ require_relative "type"
8
+
9
+ module OpenHAB
10
+ module Core
11
+ module Types
12
+ # Mixin for methods common to DecimalType and QuantityType.
13
+ module NumericType
14
+ # @!visibility private
15
+ def self.included(klass)
16
+ klass.extend Forwardable
17
+
18
+ klass.delegate %i[to_d zero?] => :to_big_decimal
19
+ klass.delegate %i[positive? negative? to_f to_i to_int hash] => :to_d
20
+
21
+ # remove the JRuby default == so that we can inherit the Ruby method
22
+ klass.remove_method :==
23
+ end
24
+
25
+ #
26
+ # Check equality without type conversion
27
+ #
28
+ # @return [true,false] if the same value is represented, without type
29
+ # conversion
30
+ def eql?(other)
31
+ return false unless other.instance_of?(self.class)
32
+
33
+ compare_to(other).zero?
34
+ end
35
+
36
+ # @return [self]
37
+ def +@
38
+ self
39
+ end
40
+
41
+ # @!method to_d
42
+ # @return [BigDecimal]
43
+
44
+ # @!method to_i
45
+ # @return [Integer]
46
+
47
+ # @!method to_f
48
+ # @return [Float]
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "type"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Types
8
+ OnOffType = org.openhab.core.library.types.OnOffType
9
+
10
+ #
11
+ # Implements {ON} and {OFF} commands and states.
12
+ #
13
+ # Also, {PercentType} can be converted to {OnOffType}
14
+ # for more semantic comparisons. `0` is {OFF}, anything
15
+ # else if {ON}.
16
+ #
17
+ class OnOffType
18
+ # @!parse include Command, State
19
+
20
+ # @!constant ON
21
+ # On Command/State
22
+ # @!constant OFF
23
+ # Off Command/State
24
+
25
+ # @!method on?
26
+ # Check if `self == ON`
27
+ # @return [true,false]
28
+
29
+ # @!method off?
30
+ # Check if `self == OFF`
31
+ # @return [true,false]
32
+
33
+ # Invert the type
34
+ # @return [OnOffType] {OFF} if {on?}, {ON} if {off?}
35
+ def !
36
+ on? ? OFF : ON
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # @!parse
44
+ # OnOffType = OpenHAB::Core::Types::OnOffType
45
+ # ON = OpenHAB::Core::Types::OnOffType::ON
46
+ # OFF = OpenHAB::Core::Types::OnOffType::OFF
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "type"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Types
8
+ OpenClosedType = org.openhab.core.library.types.OpenClosedType
9
+
10
+ # Implements {OPEN} and {CLOSED} states.
11
+ class OpenClosedType
12
+ # @!parse include State
13
+
14
+ # @!constant OPEN
15
+ # Open State
16
+ # @!constant CLOSED
17
+ # Closed State
18
+
19
+ # @!method open?
20
+ # Check if `self == OPEN`
21
+ # @return [true,false]
22
+
23
+ # @!method closed?
24
+ # Check if `self == CLOSED`
25
+ # @return [true,false]
26
+
27
+ # Invert the type
28
+ # @return [OpenClosedType] {OPEN} if {closed?}, {CLOSED} if {open?}
29
+ def !
30
+ return CLOSED if open?
31
+ return OPEN if closed?
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ # @!parse
39
+ # OpenClosedType = OpenHAB::Core::Types::OpenClosedType
40
+ # OPEN = OpenHAB::Core::Types::OpenClosedType::OPEN
41
+ # CLOSED = OpenHAB::Core::Types::OpenClosedType::CLOSED
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "decimal_type"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Types
8
+ PercentType = org.openhab.core.library.types.PercentType
9
+
10
+ # {PercentType} extends {DecimalType} by putting constraints for its value on top (0-100).
11
+ class PercentType < DecimalType
12
+ # remove the JRuby default == so that we can inherit the Ruby method
13
+ remove_method :==
14
+
15
+ #
16
+ # Check if {ON}
17
+ #
18
+ # Note that {ON} is defined as any value besides 0%.
19
+ #
20
+ # @return [true,false]
21
+ #
22
+ def on?
23
+ as(OnOffType).on?
24
+ end
25
+
26
+ #
27
+ # Check if {OFF}
28
+ #
29
+ # Note that {OFF} is defined as 0% exactly.
30
+ #
31
+ # @return [true,false]
32
+ #
33
+ def off?
34
+ as(OnOffType).off?
35
+ end
36
+
37
+ #
38
+ # Check if {UP}
39
+ #
40
+ # Note that {UP} is defined as 0% exactly.
41
+ #
42
+ # @return [true,false]
43
+ #
44
+ def up?
45
+ !!as(UpDownType)&.up?
46
+ end
47
+
48
+ #
49
+ # Check if {DOWN}
50
+ #
51
+ # Note that {DOWN} is defined as 100% exactly.
52
+ #
53
+ # @return [true,false]
54
+ #
55
+ def down?
56
+ !!as(UpDownType)&.down?
57
+ end
58
+
59
+ # include the %
60
+ # @!visibility private
61
+ def to_s
62
+ "#{to_string}%"
63
+ end
64
+
65
+ #
66
+ # Scale the value to a particular range
67
+ #
68
+ # @param range [Range] the range as a numeric
69
+ # @return [Numeric] the value as a percentage of the range
70
+ #
71
+ def scale(range)
72
+ unless range.is_a?(Range) && range.min.is_a?(Numeric) && range.max.is_a?(Numeric)
73
+ raise ArgumentError, "range must be a Range of Numerics"
74
+ end
75
+
76
+ result = (to_d * (range.max - range.min) / 100) + range.min
77
+ case range.max
78
+ when Integer then result.round
79
+ when Float then result.to_f
80
+ else result
81
+ end
82
+ end
83
+
84
+ # scale the value to fit in a single byte
85
+ #
86
+ # @return [Integer] an integer in the range 0-255
87
+ def to_byte
88
+ scale(0..255)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ # @!parse PercentType = OpenHAB::Core::Types::PercentType