openhab-jrubyscripting 5.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
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,327 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ #
9
+ # Items extensions to support
10
+ # {https://www.openhab.org/docs/configuration/persistence.html OpenHAB's Persistence} feature.
11
+ #
12
+ # @see OpenHAB::DSL.persistence Persistence Block
13
+ #
14
+ # @example The following examples are based on these items
15
+ # Number UV_Index
16
+ # Number:Power Power_Usage "Power Usage [%.2f W]"
17
+ #
18
+ # @example Getting persistence data from the system default persistence service
19
+ # UV_Index.average_since(1.hour.ago) # returns a DecimalType
20
+ # Power_Usage.average_since(12.hours.ago) # returns a QuantityType that corresponds to the item's type
21
+ #
22
+ # @example Querying a non-default persistence service
23
+ # UV_Index.average_since(1.hour.ago, :influxdb)
24
+ # Power_Usage.average_since(12.hours.ago, :rrd4j)
25
+ #
26
+ # @example Comparison using Quantity
27
+ # # Because Power_Usage has a unit, the return value
28
+ # # from average_since is a QuantityType which can be
29
+ # # compared against a string with quantity
30
+ # if Power_Usage.average_since(15.minutes.ago) > 5 | "kW"
31
+ # logger.info("The power usage exceeded its 15 min average)
32
+ # end
33
+ #
34
+ # @example HistoricState
35
+ # max = Power_Usage.maximum_since(LocalTime::MIDNIGHT)
36
+ # logger.info("Max power usage today: #{max}, at: #{max.timestamp})
37
+ #
38
+ module Persistence
39
+ GenericItem.prepend(self)
40
+
41
+ # A state class with an added timestamp attribute. This is used to hold OpenHAB's HistoricItem.
42
+ class HistoricState < SimpleDelegator
43
+ attr_reader :timestamp
44
+
45
+ alias_method :state, :__getobj__
46
+
47
+ def initialize(state, timestamp)
48
+ @timestamp = timestamp
49
+ super(state)
50
+ end
51
+ end
52
+
53
+ # All persistence methods that could return a QuantityType
54
+ QUANTITY_METHODS = %i[average_since
55
+ delta_since
56
+ deviation_since
57
+ sum_since
58
+ variance_since].freeze
59
+
60
+ # All persistence methods that require a timestamp
61
+ PERSISTENCE_METHODS = (QUANTITY_METHODS +
62
+ %i[changed_since?
63
+ evolution_rate
64
+ historic_state
65
+ maximum_since
66
+ minimum_since
67
+ updated_since?]).freeze
68
+ private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
69
+
70
+ # @!method persist(service = nil)
71
+ # Persist the state of the item
72
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
73
+ # @return [void]
74
+
75
+ # @!method last_update(service = nil)
76
+ # Return the time the item was last updated.
77
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
78
+ # @return [ZonedDateTime] The timestamp of the last update
79
+
80
+ # @!method average_since(timestamp, service = nil)
81
+ # Return the average value of the item's state since the given time
82
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
83
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
84
+ # @return [DecimalType, QuantityType, nil] The average value since `timestamp`,
85
+ # or nil if no previous state could be found.
86
+
87
+ # @!method average_between(start, finish, service = nil)
88
+ # Return the average value of the item's state between two points in time
89
+ # @param [#to_zoned_date_time] start The point in time from which to search
90
+ # @param [#to_zoned_date_time] finish The point in time to which to search
91
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
92
+ # @return [DecimalType, QuantityType, nil] The average value between `start` and `finish`,
93
+ # or nil if no previous state could be found.
94
+
95
+ # @!method delta_since(timestamp, service = nil)
96
+ # Return the difference value of the item's state since the given time
97
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
98
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
99
+ # @return [DecimalType, QuantityType, nil] The difference value since `timestamp`,
100
+ # or nil if no previous state could be found.
101
+
102
+ # @!method delta_between(start, finish, service = nil)
103
+ # Return the difference value of the item's state between two points in time
104
+ # @param [#to_zoned_date_time] start The point in time from which to search
105
+ # @param [#to_zoned_date_time] finish The point in time to which to search
106
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
107
+ # @return [DecimalType, QuantityType, nil] The difference value between `start` and `finish`,
108
+ # or nil if no previous state could be found.
109
+
110
+ # @!method deviation_since(timestamp, service = nil)
111
+ # Return the standard deviation of the item's state since the given time
112
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
113
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
114
+ # @return [DecimalType, QuantityType, nil] The standard deviation since `timestamp`,
115
+ # or nil if no previous state could be found.
116
+
117
+ # @!method deviation_between(start, finish, service = nil)
118
+ # Return the standard deviation of the item's state between two points in time
119
+ # @param [#to_zoned_date_time] start The point in time from which to search
120
+ # @param [#to_zoned_date_time] finish The point in time to which to search
121
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
122
+ # @return [DecimalType, QuantityType, nil] The standard deviation between `start` and `finish`,
123
+ # or nil if no previous state could be found.
124
+
125
+ # @!method sum_since(timestamp, service = nil)
126
+ # Return the sum of the item's state since the given time
127
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
128
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
129
+ # @return [DecimalType, QuantityType, nil] The sum since `timestamp`,
130
+ # or nil if no previous state could be found.
131
+
132
+ # @!method sum_between(start, finish, service = nil)
133
+ # Return the sum of the item's state between two points in time
134
+ # @param [#to_zoned_date_time] start The point in time from which to search
135
+ # @param [#to_zoned_date_time] finish The point in time to which to search
136
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
137
+ # @return [DecimalType, QuantityType, nil] The sum between `start` and `finish`,
138
+ # or nil if no previous state could be found.
139
+
140
+ # @!method variance_since(timestamp, service = nil)
141
+ # Return the variance of the item's state since the given time
142
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
143
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
144
+ # @return [DecimalType, QuantityType, nil] The variance since `timestamp`,
145
+ # or nil if no previous state could be found.
146
+
147
+ # @!method variance_between(start, finish, service = nil)
148
+ # Return the variance of the item's state between two points in time
149
+ # @param [#to_zoned_date_time] start The point in time from which to search
150
+ # @param [#to_zoned_date_time] finish The point in time to which to search
151
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
152
+ # @return [DecimalType, QuantityType, nil] The variance between `start` and `finish`,
153
+ # or nil if no previous state could be found.
154
+
155
+ # @!method changed_since?(timestamp, service = nil)
156
+ # Whether the item's state has changed since the given time
157
+ # @param [#to_zoned_date_time] 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 [true,false] True if the item's state has changed since the given `timestamp`, False otherwise.
160
+
161
+ # @!method changed_between?(start, finish, service = nil)
162
+ # Whether the item's state changed between two points in time
163
+ # @param [#to_zoned_date_time] start The point in time from which to search
164
+ # @param [#to_zoned_date_time] finish The point in time to which to search
165
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
166
+ # @return [true,false] True if the item's state changed between `start` and `finish`, False otherwise.
167
+
168
+ # @!method evolution_rate(timestamp, service = nil)
169
+ # Return the evolution rate of the item's state since the given time
170
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
171
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
172
+ # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
173
+ # or nil if no previous state could be found.
174
+
175
+ # @!method historic_state(timestamp, service = nil)
176
+ # Return the the item's state at the given time
177
+ # @param [#to_zoned_date_time] timestamp The point in time at which to search
178
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
179
+ # @return [HistoricState, nil] The item's state at `timestamp`,
180
+ # or nil if no previous state could be found.
181
+
182
+ # @!method maximum_since(timestamp, service = nil)
183
+ # Return the maximum value of the item's state since the given time
184
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
185
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
186
+ # @return [HistoricState, nil] The maximum value since `timestamp`,
187
+ # or nil if no previous state could be found.
188
+
189
+ # @!method maximum_between(start, finish, service = nil)
190
+ # Return the maximum value of the item's state between two points in time
191
+ # @param [#to_zoned_date_time] start The point in time from which to search
192
+ # @param [#to_zoned_date_time] finish The point in time to which to search
193
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
194
+ # @return [HistoricState, nil] The maximum value between `start` and `finish`,
195
+ # or nil if no previous state could be found.
196
+
197
+ # @!method minimum_since(timestamp, service = nil)
198
+ # Return the minimum value of the item's state since the given time
199
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
200
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
201
+ # @return [HistoricState, nil] The minimum value since `timestamp`,
202
+ # or nil if no previous state could be found.
203
+
204
+ # @!method minimum_between(start, finish, service = nil)
205
+ # Return the minimum value of the item's state between two points in time
206
+ # @param [#to_zoned_date_time] start The point in time from which to search
207
+ # @param [#to_zoned_date_time] finish The point in time to which to search
208
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
209
+ # @return [HistoricState, nil] The minimum value between `start` and `finish`,
210
+ # or nil if no previous state could be found.
211
+
212
+ # @!method updated_since?(timestamp, service = nil)
213
+ # Whether the item's state has been updated since the given time
214
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
215
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
216
+ # @return [true,false] True if the item's state has been updated since the given `timestamp`, False otherwise.
217
+
218
+ # @!method updated_between?(start, finish, service = nil)
219
+ # Whether the item's state was updated between two points in time
220
+ # @param [#to_zoned_date_time] start The point in time from which to search
221
+ # @param [#to_zoned_date_time] finish The point in time to which to search
222
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
223
+ # @return [true,false] True if the item's state was updated between `start` and `finish`, False otherwise.
224
+
225
+ %i[persist last_update].each do |method|
226
+ define_method(method) do |service = nil|
227
+ service ||= persistence_service
228
+ Actions::PersistenceExtensions.public_send(method, self, service&.to_s)
229
+ end
230
+ end
231
+
232
+ #
233
+ # Return the previous state of the item
234
+ #
235
+ # @param skip_equal [true,false] if true, skips equal state values and
236
+ # searches the first state not equal the current state
237
+ # @param service [String] the name of the PersistenceService to use
238
+ #
239
+ # @return [HistoricState, nil] the previous state or nil if no previous state could be found,
240
+ # or if the default persistence service is not configured or
241
+ # does not refer to a valid service
242
+ #
243
+ def previous_state(service = nil, skip_equal: false)
244
+ service ||= persistence_service
245
+ result = Actions::PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
246
+ HistoricState.new(quantify(result.state), result.timestamp)
247
+ end
248
+
249
+ PERSISTENCE_METHODS.each do |method|
250
+ define_method(method) do |timestamp, service = nil|
251
+ service ||= persistence_service
252
+ result = Actions::PersistenceExtensions.public_send(
253
+ method.to_s.delete_suffix("?"),
254
+ self,
255
+ timestamp.to_zoned_date_time,
256
+ service&.to_s
257
+ )
258
+ wrap_result(result, method)
259
+ end
260
+
261
+ next unless /_since\??$/.match?(method.to_s)
262
+
263
+ between_method = method.to_s.sub("_since", "_between").to_sym
264
+ define_method(between_method) do |start, finish, service = nil|
265
+ service ||= persistence_service
266
+ result = Actions::PersistenceExtensions.public_send(
267
+ between_method.to_s.delete_suffix("?"),
268
+ self,
269
+ start.to_zoned_date_time,
270
+ finish.to_zoned_date_time,
271
+ service&.to_s
272
+ )
273
+ wrap_result(result, method)
274
+ end
275
+ end
276
+
277
+ private
278
+
279
+ #
280
+ # Convert value to QuantityType if it is a DecimalType and a unit is defined
281
+ #
282
+ # @param [Object] value The value to convert
283
+ #
284
+ # @return [Object] QuantityType or the original value
285
+ #
286
+ def quantify(value)
287
+ if value.is_a?(DecimalType) && unit
288
+ logger.trace("Unitizing #{value} with unit #{unit}")
289
+ QuantityType.new(value.to_big_decimal, unit)
290
+ else
291
+ value
292
+ end
293
+ end
294
+
295
+ #
296
+ # Wrap the result into a more convenient object type depending on the method and result.
297
+ #
298
+ # @param [Object] result the raw result type to be wrapped
299
+ # @param [Symbol] method the name of the called method
300
+ #
301
+ # @return [HistoricState] a {HistoricState} object if the result was a HistoricItem
302
+ # @return [QuantityType] a `QuantityType` object if the result was an average, delta, deviation,
303
+ # sum, or variance.
304
+ # @return [Object] the original result object otherwise.
305
+ #
306
+ def wrap_result(result, method)
307
+ if result.is_a?(org.openhab.core.persistence.HistoricItem)
308
+ return HistoricState.new(quantify(result.state),
309
+ result.timestamp)
310
+ end
311
+ return quantify(result) if QUANTITY_METHODS.include?(method)
312
+
313
+ result
314
+ end
315
+
316
+ #
317
+ # Get the specified persistence service from the current thread local variable
318
+ #
319
+ # @return [Object] Persistence service name as String or Symbol, or nil if not set
320
+ #
321
+ def persistence_service
322
+ Thread.current[:openhab_persistence_service]
323
+ end
324
+ end
325
+ end
326
+ end
327
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ java_import org.openhab.core.library.items.PlayerItem
9
+
10
+ #
11
+ # A {PlayerItem} allows control of a player, e.g. an audio player.
12
+ #
13
+ # @!attribute [r] state
14
+ # @return [PlayPauseType, RewindFastforwardType, nil]
15
+ #
16
+ # @example Start playing on a player item
17
+ # Chromecast.play
18
+ # @example Check if a player is paused
19
+ # logger.warn("#{item.name} is paused) if Chromecast.paused?
20
+ #
21
+ class PlayerItem < GenericItem
22
+ # @!method play?
23
+ # Check if the item state == {PLAY}
24
+ # @return [true,false]
25
+
26
+ # @!method paused?
27
+ # Check if the item state == {PAUSE}
28
+ # @return [true,false]
29
+
30
+ # @!method rewinding?
31
+ # Check if the item state == {REWIND}
32
+ # @return [true,false]
33
+
34
+ # @!method fast_forwarding?
35
+ # Check if the item state == {FASTFORWARD}
36
+ # @return [true,false]
37
+
38
+ # @!method play
39
+ # Send the {PLAY} command to the item
40
+ # @return [PlayerItem] `self`
41
+
42
+ # @!method pause
43
+ # Send the {PAUSE} command to the item
44
+ # @return [PlayerItem] `self`
45
+
46
+ # @!method rewind
47
+ # Send the {REWIND} command to the item
48
+ # @return [PlayerItem] `self`
49
+
50
+ # @!method fast_forward
51
+ # Send the {FASTFORWARD} command to the item
52
+ # @return [PlayerItem] `self`
53
+
54
+ # @!method next
55
+ # Send the {NEXT} command to the item
56
+ # @return [PlayerItem] `self`
57
+
58
+ # @!method previous
59
+ # Send the {PREVIOUS} command to the item
60
+ # @return [PlayerItem] `self`
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ # @!parse PlayerItem = OpenHAB::Core::Items::PlayerItem
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+ require "forwardable"
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ # Class is a proxy to underlying item
10
+ # @!visibility private
11
+ class Proxy < Delegator
12
+ extend Forwardable
13
+ def_delegators :__getobj__, :class, :is_a?, :kind_of?
14
+
15
+ #
16
+ # Set the proxy item (called by super)
17
+ #
18
+ def __setobj__(item)
19
+ # Convert name to java version for faster lookups
20
+ @item_name = item.name.to_java
21
+ end
22
+
23
+ #
24
+ # Lookup item from item registry
25
+ #
26
+ def __getobj__
27
+ $ir.get(@item_name)
28
+ end
29
+
30
+ #
31
+ # Need to check if `self` _or_ the delegate is an instance of the
32
+ # given class
33
+ #
34
+ # So that {#==} can work
35
+ #
36
+ # @return [true, false]
37
+ #
38
+ # @!visibility private
39
+ def instance_of?(klass)
40
+ __getobj__.instance_of?(klass) || super
41
+ end
42
+
43
+ #
44
+ # Check if delegates are equal for comparison
45
+ #
46
+ # Otherwise items can't be used in Java maps
47
+ #
48
+ # @return [true, false]
49
+ #
50
+ # @!visibility private
51
+ def ==(other)
52
+ return __getobj__ == other.__getobj__ if other.instance_of?(Proxy)
53
+
54
+ super
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ require "openhab/core/entity_lookup"
6
+ require "openhab/core/lazy_array"
7
+ require "openhab/dsl/items/builder"
8
+
9
+ module OpenHAB
10
+ module Core
11
+ module Items
12
+ #
13
+ # Provides access to all OpenHAB {GenericItem items}, and acts like an array.
14
+ #
15
+ class Registry
16
+ include LazyArray
17
+ include Singleton
18
+
19
+ # Fetches the named item from the the ItemRegistry
20
+ # @param [String] name
21
+ # @return [GenericItem] Item from registry, nil if item missing or requested item is a Group Type
22
+ def [](name)
23
+ EntityLookup.lookup_item(name)
24
+ rescue org.openhab.core.items.ItemNotFoundException
25
+ nil
26
+ end
27
+
28
+ # Returns true if the given item name exists
29
+ # @param name [String] Item name to check
30
+ # @return [true,false] true if the item exists, false otherwise
31
+ def key?(name)
32
+ !$ir.getItems(name).empty?
33
+ end
34
+ alias_method :include?, :key?
35
+
36
+ # Explicit conversion to array
37
+ # @return [Array]
38
+ def to_a
39
+ $ir.items.map { |item| Proxy.new(item) }
40
+ end
41
+
42
+ # Enter the Item Builder DSL.
43
+ # @yield Block executed in the context of a {DSL::Items::Builder}
44
+ # @return [Object] The return value of the block.
45
+ def build(&block)
46
+ DSL::Items::BaseBuilderDSL.new.instance_eval(&block)
47
+ end
48
+
49
+ # Remove an item.
50
+ #
51
+ # The item must have either been created by this script, or be a
52
+ # managed item (typically created in the UI).
53
+ #
54
+ # @param recursive [true, false] Remove the item's members if it's a group
55
+ # @return [GenericItem, nil] The removed item, if found.
56
+ def remove(item_name, recursive: false)
57
+ item_name = item_name.name if item_name.is_a?(GenericItem)
58
+ result = DSL::Items::ItemProvider.instance.remove(item_name, recursive: recursive)
59
+ return result if result
60
+
61
+ $ir.remove(item_name, recursive)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+ require_relative "numeric_item"
5
+
6
+ module OpenHAB
7
+ module Core
8
+ module Items
9
+ java_import org.openhab.core.library.items.RollershutterItem
10
+
11
+ #
12
+ # A {RollershutterItem} allows the control of roller shutters, i.e.
13
+ # moving them up, down, stopping or setting it to close to a certain
14
+ # percentage.
15
+ #
16
+ # @!attribute [r] state
17
+ # @return [PercentType, UpDownType, nil]
18
+ #
19
+ # @example Roll up all Rollershutters in a group
20
+ # Shutters.up
21
+ #
22
+ # @example Log a warning for all rollershutters that are not up
23
+ # Shutters.reject(&:up?).each do |item|
24
+ # logger.warn("#{item.name} is not rolled up!")
25
+ # end
26
+ #
27
+ # @example Set rollershutter to a specified position
28
+ # Example_Rollershutter << 40
29
+ class RollershutterItem < GenericItem
30
+ include NumericItem
31
+
32
+ # @!method up?
33
+ # Check if the item state == {UP}
34
+ # @return [true,false]
35
+
36
+ # @!method down?
37
+ # Check if the item state == {DOWN}
38
+ # @return [true,false]
39
+
40
+ # @!method up
41
+ # Send the {UP} command to the item
42
+ # @return [RollershutterItem] `self`
43
+
44
+ # @!method down
45
+ # Send the {DOWN} command to the item
46
+ # @return [RollershutterItem] `self`
47
+
48
+ # @!method stop
49
+ # Send the {STOP} command to the item
50
+ # @return [RollershutterItem] `self`
51
+
52
+ # @!method move
53
+ # Send the {MOVE} command to the item
54
+ # @return [RollershutterItem] `self`
55
+
56
+ # raw numbers translate directly to PercentType, not a DecimalType
57
+ # @!visibility private
58
+ def format_type(command)
59
+ return Types::PercentType.new(command) if command.is_a?(Numeric)
60
+
61
+ super
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ # @!parse RollershutterItem = OpenHAB::Core::Items::RollershutterItem