openhab-jrubyscripting 5.0.0.rc9 → 5.0.0.rc11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +23 -120
  10. data/lib/openhab/core/{events → dto}/item_channel_link.rb +1 -4
  11. data/lib/openhab/core/{events → dto}/thing.rb +10 -12
  12. data/lib/openhab/core/dto.rb +11 -0
  13. data/lib/openhab/core/entity_lookup.rb +1 -1
  14. data/lib/openhab/core/events/abstract_event.rb +1 -0
  15. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  16. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  17. data/lib/openhab/core/events/item_command_event.rb +1 -1
  18. data/lib/openhab/core/events/item_state_changed_event.rb +6 -6
  19. data/lib/openhab/core/events/item_state_event.rb +6 -6
  20. data/lib/openhab/core/events/thing_status_info_event.rb +8 -6
  21. data/lib/openhab/core/items/generic_item.rb +2 -1
  22. data/lib/openhab/core/items/persistence.rb +52 -18
  23. data/lib/openhab/core/items/player_item.rb +1 -1
  24. data/lib/openhab/core/items/proxy.rb +20 -14
  25. data/lib/openhab/core/items/registry.rb +2 -0
  26. data/lib/openhab/core/items.rb +3 -3
  27. data/lib/openhab/core/profile_factory.rb +3 -1
  28. data/lib/openhab/core/proxy.rb +125 -0
  29. data/lib/openhab/core/things/links/provider.rb +1 -1
  30. data/lib/openhab/core/things/proxy.rb +8 -0
  31. data/lib/openhab/core/types/date_time_type.rb +2 -1
  32. data/lib/openhab/core/types/decimal_type.rb +1 -1
  33. data/lib/openhab/core/types/un_def_type.rb +2 -2
  34. data/lib/openhab/core/value_cache.rb +1 -1
  35. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  36. data/lib/openhab/core_ext/java/class.rb +1 -1
  37. data/lib/openhab/core_ext/java/duration.rb +25 -1
  38. data/lib/openhab/core_ext/java/local_date.rb +2 -0
  39. data/lib/openhab/core_ext/java/month_day.rb +2 -0
  40. data/lib/openhab/core_ext/java/zoned_date_time.rb +85 -0
  41. data/lib/openhab/core_ext/ruby/date.rb +2 -0
  42. data/lib/openhab/core_ext/ruby/date_time.rb +1 -0
  43. data/lib/openhab/core_ext/ruby/time.rb +1 -0
  44. data/lib/openhab/dsl/debouncer.rb +259 -0
  45. data/lib/openhab/dsl/items/builder.rb +4 -2
  46. data/lib/openhab/dsl/items/timed_command.rb +31 -13
  47. data/lib/openhab/dsl/rules/automation_rule.rb +28 -21
  48. data/lib/openhab/dsl/rules/builder.rb +357 -37
  49. data/lib/openhab/dsl/rules/guard.rb +12 -54
  50. data/lib/openhab/dsl/rules/name_inference.rb +11 -0
  51. data/lib/openhab/dsl/rules/property.rb +3 -4
  52. data/lib/openhab/dsl/rules/terse.rb +4 -1
  53. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +5 -6
  54. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +1 -0
  55. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +19 -31
  56. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +1 -0
  57. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +22 -30
  58. data/lib/openhab/dsl/things/builder.rb +1 -1
  59. data/lib/openhab/dsl/thread_local.rb +1 -0
  60. data/lib/openhab/dsl/version.rb +1 -1
  61. data/lib/openhab/dsl.rb +224 -3
  62. data/lib/openhab/rspec/hooks.rb +5 -2
  63. data/lib/openhab/rspec/karaf.rb +7 -0
  64. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  65. data/lib/openhab/rspec/mocks/space.rb +23 -0
  66. data/lib/openhab/rspec/openhab/core/actions.rb +16 -4
  67. data/lib/openhab/rspec/openhab/core/items/proxy.rb +1 -13
  68. data/lib/openhab/rspec/suspend_rules.rb +1 -14
  69. data/lib/openhab/yard/base_helper.rb +19 -0
  70. data/lib/openhab/yard/code_objects/group_object.rb +9 -3
  71. data/lib/openhab/yard/coderay.rb +17 -0
  72. data/lib/openhab/yard/handlers/jruby/base.rb +10 -1
  73. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +3 -0
  74. data/lib/openhab/yard/html_helper.rb +49 -15
  75. data/lib/openhab/yard/markdown_helper.rb +135 -0
  76. data/lib/openhab/yard.rb +6 -0
  77. metadata +36 -4
@@ -10,7 +10,7 @@ module OpenHAB
10
10
  # Adds methods to core openHAB ItemCommandEvent to make it more natural in Ruby
11
11
  class ItemCommandEvent < ItemEvent
12
12
  # @!attribute [r] command
13
- # @return [Type] The command sent to the item.
13
+ # @return [Command] The command sent to the item.
14
14
  alias_method :command, :item_command
15
15
 
16
16
  # @!method refresh?
@@ -14,26 +14,26 @@ module OpenHAB
14
14
  include ItemState
15
15
 
16
16
  #
17
- # Check if state was == UNDEF
17
+ # Check if state was == {UNDEF}
18
18
  #
19
- # @return [true,false] True if the state is UNDEF, false otherwise
19
+ # @return [true,false] True if the state is {UNDEF}, false otherwise
20
20
  #
21
21
  def was_undef?
22
22
  old_item_state == UNDEF
23
23
  end
24
24
 
25
25
  #
26
- # Check if state was == NULL
26
+ # Check if state was == {NULL}
27
27
  #
28
- # @return [true,false] True if the state is NULL, false otherwise
28
+ # @return [true,false] True if the state is {NULL}, false otherwise
29
29
  def was_null?
30
30
  old_item_state == NULL
31
31
  end
32
32
 
33
33
  #
34
- # Check if state was defined (not UNDEF or NULL)
34
+ # Check if state was defined (not {UNDEF} or {NULL})
35
35
  #
36
- # @return [true,false] True if state is not UNDEF or NULL
36
+ # @return [true,false] True if state is not {UNDEF} or {NULL}
37
37
  #
38
38
  def was?
39
39
  !old_item_state.is_a?(UnDefType)
@@ -8,26 +8,26 @@ module OpenHAB
8
8
  # Helpers common to {ItemStateEvent} and {ItemStateChangedEvent}.
9
9
  module ItemState
10
10
  #
11
- # Check if the state == UNDEF
11
+ # Check if the state == {UNDEF}
12
12
  #
13
- # @return [true,false] True if the state is UNDEF, false otherwise
13
+ # @return [true,false] True if the state is {UNDEF}, false otherwise
14
14
  #
15
15
  def undef?
16
16
  item_state == UNDEF
17
17
  end
18
18
 
19
19
  #
20
- # Check if the state == NULL
20
+ # Check if the state == {NULL}
21
21
  #
22
- # @return [true,false] True if the state is NULL, false otherwise
22
+ # @return [true,false] True if the state is {NULL}, false otherwise
23
23
  def null?
24
24
  item_state == NULL
25
25
  end
26
26
 
27
27
  #
28
- # Check if the state is defined (not UNDEF or NULL)
28
+ # Check if the state is defined (not {UNDEF} or {NULL})
29
29
  #
30
- # @return [true,false] True if state is not UNDEF or NULL
30
+ # @return [true,false] True if state is not {UNDEF} or {NULL}
31
31
  #
32
32
  def state?
33
33
  !item_state.is_a?(UnDefType)
@@ -6,16 +6,18 @@ module OpenHAB
6
6
  java_import org.openhab.core.thing.events.ThingStatusInfoChangedEvent,
7
7
  org.openhab.core.thing.events.ThingStatusInfoEvent
8
8
 
9
- # The {AbstractEvent} sent when a {Things::Thing}'s status has changed.
9
+ #
10
+ # The {AbstractEvent} sent when a {Things::Thing Thing's} status has changed.
11
+ #
10
12
  class ThingStatusInfoChangedEvent < AbstractEvent
11
13
  # @!attribute [r] uid
12
- # @return [Things::ThingUID] The UID of the [Things::Thing] that triggered this event.
14
+ # @return [Things::ThingUID] The UID of the {Things::Thing Thing} that triggered this event.
13
15
  alias_method :uid, :get_thing_uid
14
16
  # @!attribute [r] was
15
- # @return [Things::ThingStatusInfo] The thing's prior status.
17
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's prior status.
16
18
  alias_method :was, :get_old_status_info
17
19
  # @!attribute [r] status
18
- # @return [Things::ThingStatusInfo] The thing's status.
20
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's status.
19
21
  alias_method :status, :status_info
20
22
 
21
23
  #
@@ -31,12 +33,12 @@ module OpenHAB
31
33
  class ThingStatusInfoEvent < AbstractEvent
32
34
  #
33
35
  # @!attribute [r] uid
34
- # @return [Things::ThingUID] The UID of the [Things::Thing] that triggered this event.
36
+ # @return [Things::ThingUID] The UID of the {Things::Thing Thing} that triggered this event.
35
37
  #
36
38
  alias_method :uid, :get_thing_uid
37
39
  #
38
40
  # @!attribute [r] status
39
- # @return [Things::ThingStatusInfo] The thing's status.
41
+ # @return [org.openhab.core.thing.ThingStatusInfo] The thing's status.
40
42
  #
41
43
  alias_method :status, :status_info
42
44
 
@@ -8,7 +8,7 @@ module OpenHAB
8
8
  #
9
9
  # The abstract base class for all items.
10
10
  #
11
- # @see https://www.openhab.org/javadoc/latest/org/openhab/core/items/genericitem
11
+ # @see org.openhab.core.items.GenericItem
12
12
  #
13
13
  class GenericItem
14
14
  # @!parse include Item
@@ -49,6 +49,7 @@ module OpenHAB
49
49
  # The item's descriptive label.
50
50
  # @return [String, nil]
51
51
 
52
+ # @!visibility private
52
53
  alias_method :hash, :hash_code
53
54
 
54
55
  # @!attribute [r] raw_state
@@ -48,6 +48,8 @@ module OpenHAB
48
48
  # that state into a {QuantityType} as necessary.
49
49
  #
50
50
  class HistoricState < SimpleDelegator
51
+ # @!attribute [r] state
52
+ # @return [Types::State]
51
53
  alias_method :state, :__getobj__
52
54
 
53
55
  def initialize(state, historic_item)
@@ -70,8 +72,11 @@ module OpenHAB
70
72
  variance_since].freeze
71
73
 
72
74
  # All persistence methods that require a timestamp
75
+ # Note the _between methods are automatically created from the _since methods
73
76
  PERSISTENCE_METHODS = (QUANTITY_METHODS +
74
77
  %i[changed_since?
78
+ count_since
79
+ count_state_changes_since
75
80
  evolution_rate
76
81
  historic_state
77
82
  maximum_since
@@ -80,24 +85,24 @@ module OpenHAB
80
85
  private_constant :QUANTITY_METHODS, :PERSISTENCE_METHODS
81
86
 
82
87
  # @!method persist(service = nil)
83
- # Persist the state of the item
88
+ # Persists the state of the item
84
89
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
85
90
  # @return [void]
86
91
 
87
92
  # @!method last_update(service = nil)
88
- # Return the time the item was last updated.
93
+ # Returns the time the item was last updated.
89
94
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
90
95
  # @return [ZonedDateTime, nil] The timestamp of the last update
91
96
 
92
97
  # @!method average_since(timestamp, service = nil)
93
- # Return the average value of the item's state since the given time
98
+ # Returns the average value of the item's state since the given time
94
99
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
95
100
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
96
101
  # @return [DecimalType, QuantityType, nil] The average value since `timestamp`,
97
102
  # or nil if no previous state could be found.
98
103
 
99
104
  # @!method average_between(start, finish, service = nil)
100
- # Return the average value of the item's state between two points in time
105
+ # Returns the average value of the item's state between two points in time
101
106
  # @param [#to_zoned_date_time] start The point in time from which to search
102
107
  # @param [#to_zoned_date_time] finish The point in time to which to search
103
108
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -105,14 +110,14 @@ module OpenHAB
105
110
  # or nil if no previous state could be found.
106
111
 
107
112
  # @!method delta_since(timestamp, service = nil)
108
- # Return the difference value of the item's state since the given time
113
+ # Returns the difference value of the item's state since the given time
109
114
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
110
115
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
111
116
  # @return [DecimalType, QuantityType, nil] The difference value since `timestamp`,
112
117
  # or nil if no previous state could be found.
113
118
 
114
119
  # @!method delta_between(start, finish, service = nil)
115
- # Return the difference value of the item's state between two points in time
120
+ # Returns the difference value of the item's state between two points in time
116
121
  # @param [#to_zoned_date_time] start The point in time from which to search
117
122
  # @param [#to_zoned_date_time] finish The point in time to which to search
118
123
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -120,14 +125,14 @@ module OpenHAB
120
125
  # or nil if no previous state could be found.
121
126
 
122
127
  # @!method deviation_since(timestamp, service = nil)
123
- # Return the standard deviation of the item's state since the given time
128
+ # Returns the standard deviation of the item's state since the given time
124
129
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
125
130
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
126
131
  # @return [DecimalType, QuantityType, nil] The standard deviation since `timestamp`,
127
132
  # or nil if no previous state could be found.
128
133
 
129
134
  # @!method deviation_between(start, finish, service = nil)
130
- # Return the standard deviation of the item's state between two points in time
135
+ # Returns the standard deviation of the item's state between two points in time
131
136
  # @param [#to_zoned_date_time] start The point in time from which to search
132
137
  # @param [#to_zoned_date_time] finish The point in time to which to search
133
138
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -135,14 +140,14 @@ module OpenHAB
135
140
  # or nil if no previous state could be found.
136
141
 
137
142
  # @!method sum_since(timestamp, service = nil)
138
- # Return the sum of the item's state since the given time
143
+ # Returns the sum of the item's state since the given time
139
144
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
140
145
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
141
146
  # @return [DecimalType, QuantityType, nil] The sum since `timestamp`,
142
147
  # or nil if no previous state could be found.
143
148
 
144
149
  # @!method sum_between(start, finish, service = nil)
145
- # Return the sum of the item's state between two points in time
150
+ # Returns the sum of the item's state between two points in time
146
151
  # @param [#to_zoned_date_time] start The point in time from which to search
147
152
  # @param [#to_zoned_date_time] finish The point in time to which to search
148
153
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -150,14 +155,14 @@ module OpenHAB
150
155
  # or nil if no previous state could be found.
151
156
 
152
157
  # @!method variance_since(timestamp, service = nil)
153
- # Return the variance of the item's state since the given time
158
+ # Returns the variance of the item's state since the given time
154
159
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
155
160
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
156
161
  # @return [DecimalType, QuantityType, nil] The variance since `timestamp`,
157
162
  # or nil if no previous state could be found.
158
163
 
159
164
  # @!method variance_between(start, finish, service = nil)
160
- # Return the variance of the item's state between two points in time
165
+ # Returns the variance of the item's state between two points in time
161
166
  # @param [#to_zoned_date_time] start The point in time from which to search
162
167
  # @param [#to_zoned_date_time] finish The point in time to which to search
163
168
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -178,28 +183,28 @@ module OpenHAB
178
183
  # @return [true,false] True if the item's state changed between `start` and `finish`, False otherwise.
179
184
 
180
185
  # @!method evolution_rate(timestamp, service = nil)
181
- # Return the evolution rate of the item's state since the given time
186
+ # Returns the evolution rate of the item's state since the given time
182
187
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
183
188
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
184
189
  # @return [DecimalType, QuantityType, nil] The evolution rate since `timestamp`,
185
190
  # or nil if no previous state could be found.
186
191
 
187
192
  # @!method historic_state(timestamp, service = nil)
188
- # Return the the item's state at the given time
193
+ # Returns the the item's state at the given time
189
194
  # @param [#to_zoned_date_time] timestamp The point in time at which to search
190
195
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
191
196
  # @return [HistoricState, nil] The item's state at `timestamp`,
192
197
  # or nil if no previous state could be found.
193
198
 
194
199
  # @!method maximum_since(timestamp, service = nil)
195
- # Return the maximum value of the item's state since the given time
200
+ # Returns the maximum value of the item's state since the given time
196
201
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
197
202
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
198
203
  # @return [HistoricState, nil] The maximum value since `timestamp`,
199
204
  # or nil if no previous state could be found.
200
205
 
201
206
  # @!method maximum_between(start, finish, service = nil)
202
- # Return the maximum value of the item's state between two points in time
207
+ # Returns the maximum value of the item's state between two points in time
203
208
  # @param [#to_zoned_date_time] start The point in time from which to search
204
209
  # @param [#to_zoned_date_time] finish The point in time to which to search
205
210
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -207,14 +212,14 @@ module OpenHAB
207
212
  # or nil if no previous state could be found.
208
213
 
209
214
  # @!method minimum_since(timestamp, service = nil)
210
- # Return the minimum value of the item's state since the given time
215
+ # Returns the minimum value of the item's state since the given time
211
216
  # @param [#to_zoned_date_time] timestamp The point in time from which to search
212
217
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
213
218
  # @return [HistoricState, nil] The minimum value since `timestamp`,
214
219
  # or nil if no previous state could be found.
215
220
 
216
221
  # @!method minimum_between(start, finish, service = nil)
217
- # Return the minimum value of the item's state between two points in time
222
+ # Returns the minimum value of the item's state between two points in time
218
223
  # @param [#to_zoned_date_time] start The point in time from which to search
219
224
  # @param [#to_zoned_date_time] finish The point in time to which to search
220
225
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
@@ -234,6 +239,32 @@ module OpenHAB
234
239
  # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
235
240
  # @return [true,false] True if the item's state was updated between `start` and `finish`, False otherwise.
236
241
 
242
+ # @!method count_since(timestamp, service = nil)
243
+ # Returns the number of available historic data points from a point in time until now.
244
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
245
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
246
+ # @return [Integer] The number of values persisted for this item.
247
+
248
+ # @!method count_between(start, finish, service = nil)
249
+ # Returns the number of available historic data points between two points in time.
250
+ # @param [#to_zoned_date_time] start The point in time from which to search
251
+ # @param [#to_zoned_date_time] finish The point in time to which to search
252
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
253
+ # @return [Integer] The number of values persisted for this item.
254
+
255
+ # @!method count_state_changes_since(timestamp, service = nil)
256
+ # Returns the number of changes in historic data points from a point in time until now.
257
+ # @param [#to_zoned_date_time] timestamp The point in time from which to search
258
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
259
+ # @return [Integer] The number of values persisted for this item.
260
+
261
+ # @!method count_state_changes_between(start, finish, service = nil)
262
+ # Returns the number of changes in historic data points between two points in time.
263
+ # @param [#to_zoned_date_time] start The point in time from which to search
264
+ # @param [#to_zoned_date_time] finish The point in time to which to search
265
+ # @param [Symbol, String] service An optional persistence id instead of the default persistence service.
266
+ # @return [Integer] The number of values persisted for this item.
267
+
237
268
  %i[persist last_update].each do |method|
238
269
  define_method(method) do |service = nil|
239
270
  service ||= persistence_service
@@ -286,6 +317,9 @@ module OpenHAB
286
317
  end
287
318
  end
288
319
 
320
+ alias_method :state_changes_since, :count_state_changes_since
321
+ alias_method :state_changes_between, :count_state_changes_between
322
+
289
323
  private
290
324
 
291
325
  #
@@ -16,7 +16,7 @@ module OpenHAB
16
16
  # @example Start playing on a player item
17
17
  # Chromecast.play
18
18
  # @example Check if a player is paused
19
- # logger.warn("#{item.name} is paused) if Chromecast.paused?
19
+ # logger.warn("#{item.name} is paused") if Chromecast.paused?
20
20
  #
21
21
  class PlayerItem < GenericItem
22
22
  # @!method play?
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "delegate"
4
+ require_relative "../proxy"
4
5
 
5
6
  module OpenHAB
6
7
  module Core
@@ -11,6 +12,13 @@ module OpenHAB
11
12
  # Not really an Item, but pretends to be
12
13
  # @!parse include Item
13
14
 
15
+ # @!visibility private
16
+ EVENTS = [Events::ItemAddedEvent::TYPE, Events::ItemUpdatedEvent::TYPE, Events::ItemRemovedEvent::TYPE].freeze
17
+ # @!visibility private
18
+ UID_METHOD = :name
19
+
20
+ include Core::Proxy
21
+
14
22
  # @return [String]
15
23
  attr_reader :name
16
24
 
@@ -18,19 +26,15 @@ module OpenHAB
18
26
  # Set the proxy item (called by super)
19
27
  #
20
28
  def __setobj__(item)
21
- @name = item.name
22
- # Convert name to Java version for faster lookups
23
- @java_name = item.name.to_java
29
+ @item = item.is_a?(Item) ? item : nil
30
+ @name ||= item.name if item
24
31
  end
25
32
 
26
33
  #
27
- # Lookup item from item registry
34
+ # @return [Item, nil]
28
35
  #
29
36
  def __getobj__
30
- r = $ir.get(@name)
31
- return yield if r.nil? && block_given?
32
-
33
- r
37
+ @item
34
38
  end
35
39
 
36
40
  # @return [Module]
@@ -88,9 +92,9 @@ module OpenHAB
88
92
  # @return [GroupItem::Members]
89
93
  # @raise [NoMethodError] if item is not a GroupItem, or a dummy.
90
94
  def members
91
- return super unless __getobj__.nil?
95
+ return GroupItem::Members.new(self) if __getobj__.nil?
92
96
 
93
- GroupItem::Members.new(self)
97
+ __getobj__.members
94
98
  end
95
99
 
96
100
  # @return [String]
@@ -112,15 +116,17 @@ module OpenHAB
112
116
  end
113
117
 
114
118
  # needs to return `false` if we know we're not a {GroupItem}
115
- def respond_to?(method, *)
116
- return __getobj__.nil? if method.to_sym == :members
119
+ def respond_to?(method, *args)
120
+ obj = __getobj__
121
+ return obj.respond_to?(method, *args) if method.to_sym == :members && !obj.nil?
117
122
 
118
123
  super
119
124
  end
120
125
 
121
126
  # needs to return `false` if we know we're not a {GroupItem}
122
- def respond_to_missing?(method, *)
123
- return __getobj__.nil? if method.to_sym == :members
127
+ def respond_to_missing?(method, *args)
128
+ obj = __getobj__
129
+ return obj.respond_to_missing?(method, *args) if method.to_sym == :members && !obj.nil?
124
130
 
125
131
  super
126
132
  end
@@ -48,6 +48,8 @@ module OpenHAB
48
48
  # @yield Block executed in the context of a {DSL::Items::Builder}
49
49
  # @return [Object] The return value of the block.
50
50
  #
51
+ # (see Items::Builder)
52
+ #
51
53
  def build(preferred_provider = nil, &block)
52
54
  DSL::Items::BaseBuilderDSL.new(preferred_provider).instance_eval_with_dummy_items(&block)
53
55
  end
@@ -62,9 +62,9 @@ module OpenHAB
62
62
 
63
63
  logger.trace("Defining #{klass}##{command} for #{value}")
64
64
  klass.class_eval <<~RUBY, __FILE__, __LINE__ + 1
65
- def #{command}(for: nil, on_expire: nil, &block) # def on(for: nil, on_expire: nil, &block )
66
- command(#{value}, for: binding.local_variable_get(:for), on_expire: on_expire, &block) # command(ON, for: nil, expire: nil, &block)
67
- end # end
65
+ ruby2_keywords def #{command}(*args, &block) # ruby2_keywords def on(*args, &block)
66
+ command(#{value}, *args, &block) # command(ON, *args, &block)
67
+ end # end
68
68
  RUBY
69
69
 
70
70
  logger.trace("Defining Enumerable##{command} for #{value}")
@@ -69,10 +69,12 @@ module OpenHAB
69
69
 
70
70
  params[:callback] = @callback
71
71
  params[:context] = @context
72
- params[:config] = @context.configuration
72
+ params[:configuration] = @context.configuration.properties
73
73
  params[:link] = @callback.link
74
74
  params[:item] = @callback.link.item
75
75
  params[:channel_uid] = @callback.link.linked_uid
76
+ params[:state] ||= nil
77
+ params[:command] ||= nil
76
78
 
77
79
  kwargs = {}
78
80
  @block.parameters.each do |(param_type, name)|
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require "weakref"
5
+
6
+ require_relative "script_handling"
7
+
8
+ module OpenHAB
9
+ module Core
10
+ #
11
+ # Contains the infrastructure necessary to listen for events when objects are
12
+ # added/removed from their registry, and keep Proxy objects up-to-date with
13
+ # their underlying object.
14
+ #
15
+ # The including class must meet a few requirements:
16
+ # * Have an `EVENTS` constant that is an Array<String> of the events to
17
+ # listen for
18
+ # * The _last_ entry in the `EVENTS` array must be the "removed" event
19
+ # * It must have a sibling class called `Provider`, with a `.registry`
20
+ # method
21
+ # * It's parent module, downcased and with trailing "s" stripped, must be
22
+ # the method name to retrieve an object from one of the incoming events
23
+ #
24
+ # @!visibility private
25
+ #
26
+ module Proxy
27
+ #
28
+ # Registers and listens to openHAB bus events for objects getting
29
+ # added/updated/removed, and updates references from proxy objects
30
+ # to real objects.
31
+ #
32
+ # Proxies are tracked (via a WeakRef), and their underlying object is
33
+ # if it has changed.
34
+ #
35
+ class EventSubscriber
36
+ include Singleton
37
+ include org.openhab.core.events.EventSubscriber
38
+
39
+ def initialize
40
+ @proxies = java.util.concurrent.ConcurrentHashMap.new
41
+ @parent_module = Object.const_get(self.class.name.split("::")[0..-3].join("::"), false)
42
+ @object_type = @parent_module.name.split("::").last.downcase[0..-2].to_sym
43
+
44
+ @event_types = @parent_module::Proxy::EVENTS
45
+ @uid_method = @parent_module::Proxy::UID_METHOD
46
+ @registry = @parent_module::Provider.registry
47
+ @registration = OSGi.register_service(self, "event.topics": "openhab/*")
48
+ ScriptHandling.script_unloaded { @registration.unregister }
49
+ end
50
+
51
+ #
52
+ # @!attribute [r] subscribed_event_types
53
+ # @return [Set<String>]
54
+ #
55
+ def subscribed_event_types
56
+ @event_types.to_set
57
+ end
58
+
59
+ # @return [org.openhab.core.events.EventFilter, nil]
60
+ def event_filter
61
+ nil
62
+ end
63
+
64
+ #
65
+ # @param [Events::AbstractEvent] event
66
+ # @return [void]
67
+ #
68
+ def receive(event)
69
+ uid = event.__send__(@object_type).__send__(@uid_method)
70
+ object = @registry.get(uid) unless event.class.simple_name == @event_types.last
71
+
72
+ @proxies.compute_if_present(uid) do |_, proxy_ref|
73
+ proxy = resolve_ref(proxy_ref)
74
+ next nil unless proxy
75
+
76
+ proxy.__setobj__(object)
77
+ proxy_ref
78
+ end
79
+ end
80
+
81
+ #
82
+ # Get or create a Proxy for the given raw openHAB object.
83
+ #
84
+ def fetch(object)
85
+ result = nil
86
+
87
+ @proxies.compute(object.__send__(@uid_method)) do |_k, proxy_ref|
88
+ result = resolve_ref(proxy_ref)
89
+ proxy_ref = nil unless result
90
+ result ||= yield
91
+
92
+ proxy_ref || WeakRef.new(result)
93
+ end
94
+
95
+ result
96
+ end
97
+
98
+ private
99
+
100
+ def resolve_ref(proxy_ref)
101
+ proxy_ref.__getobj__ if proxy_ref&.weakref_alive?
102
+ rescue WeakRef::RefError
103
+ nil
104
+ end
105
+ end
106
+
107
+ # @!visibility private
108
+ module ClassMethods
109
+ # Intercepts calls to create new proxies, and returns the already
110
+ # existing (and tracked) proxy if it exists. Otherwise it does create
111
+ # a new instance of Proxy.
112
+ def new(object)
113
+ self::EventSubscriber.instance.fetch(object) { super }
114
+ end
115
+ end
116
+
117
+ # @!visibility private
118
+ def self.included(klass)
119
+ klass.singleton_class.prepend(ClassMethods)
120
+ # define a sub-class of EventSubscriber as a child class of the including class
121
+ klass.const_set(:EventSubscriber, Class.new(EventSubscriber))
122
+ end
123
+ end
124
+ end
125
+ end
@@ -17,7 +17,7 @@ module OpenHAB
17
17
  #
18
18
  # The ItemChannelLink registry
19
19
  #
20
- # @return [org.openhab.core.thing.link.ItemChanneLinkRegistry]
20
+ # @return [org.openhab.core.thing.link.ItemChannelLinkRegistry]
21
21
  #
22
22
  def registry
23
23
  @registry ||= OSGi.service("org.openhab.core.thing.link.ItemChannelLinkRegistry")
@@ -12,6 +12,14 @@ module OpenHAB
12
12
  extend Forwardable
13
13
  def_delegators :__getobj__, :class, :is_a?, :kind_of?
14
14
 
15
+ # @!visibility private
16
+ EVENTS = [Events::ThingAddedEvent::TYPE, Events::ThingUpdatedEvent::TYPE,
17
+ Events::ThingRemovedEvent::TYPE].freeze
18
+ # @!visibility private
19
+ UID_METHOD = :uid
20
+
21
+ include Core::Proxy
22
+
15
23
  # Returns the list of channels associated with this Thing
16
24
  #
17
25
  # @note This is defined on this class, and not on {Thing}, because
@@ -33,7 +33,7 @@ module OpenHAB
33
33
 
34
34
  class << self
35
35
  #
36
- # Parses a String representing a time into an {java.time.DateTimeType}. First tries to parse it
36
+ # Parses a String representing a time into a {DateTimeType}. First tries to parse it
37
37
  # using Java's parser, then falls back to the Ruby `Time.parse`.
38
38
  #
39
39
  # @param [String] time_string
@@ -71,6 +71,7 @@ module OpenHAB
71
71
  def_delegator :zoned_date_time, :to_epoch_second, :to_i
72
72
  def_delegator :zoned_date_time, :to_time
73
73
 
74
+ # @!visibility private
74
75
  alias_method :day, :mday
75
76
 
76
77
  #
@@ -11,7 +11,7 @@ module OpenHAB
11
11
  DecimalType = org.openhab.core.library.types.DecimalType
12
12
 
13
13
  #
14
- # {DecimalType} uses a {java.lang.BigDecimal} internally and thus can be
14
+ # {DecimalType} uses a {java.math.BigDecimal} internally and thus can be
15
15
  # used for integers, longs and floating point numbers alike.
16
16
  #
17
17
  # @example DecimalType can be used in case statements with ranges