openhab-scripting 4.47.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) 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 +82 -0
  10. data/lib/openhab/core/dependency_tracking.rb +34 -0
  11. data/lib/openhab/core/dto/item_channel_link.rb +33 -0
  12. data/lib/openhab/core/dto/thing.rb +27 -0
  13. data/lib/openhab/core/dto.rb +11 -0
  14. data/lib/openhab/core/entity_lookup.rb +152 -70
  15. data/lib/openhab/core/events/abstract_event.rb +18 -0
  16. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  17. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  18. data/lib/openhab/core/events/item_command_event.rb +78 -0
  19. data/lib/openhab/core/events/item_event.rb +22 -0
  20. data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
  21. data/lib/openhab/core/events/item_state_event.rb +79 -0
  22. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  23. data/lib/openhab/core/events.rb +10 -0
  24. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  25. data/lib/openhab/core/items/color_item.rb +52 -0
  26. data/lib/openhab/core/items/contact_item.rb +52 -0
  27. data/lib/openhab/core/items/date_time_item.rb +59 -0
  28. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  29. data/lib/openhab/core/items/generic_item.rb +292 -0
  30. data/lib/openhab/core/items/group_item.rb +176 -0
  31. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  32. data/lib/openhab/core/items/item.rb +273 -0
  33. data/lib/openhab/core/items/location_item.rb +34 -0
  34. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  35. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  36. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  37. data/lib/openhab/core/items/metadata.rb +11 -0
  38. data/lib/openhab/core/items/number_item.rb +62 -0
  39. data/lib/openhab/core/items/numeric_item.rb +22 -0
  40. data/lib/openhab/core/items/persistence.rb +416 -0
  41. data/lib/openhab/core/items/player_item.rb +66 -0
  42. data/lib/openhab/core/items/provider.rb +44 -0
  43. data/lib/openhab/core/items/proxy.rb +136 -0
  44. data/lib/openhab/core/items/registry.rb +86 -0
  45. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  46. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  47. data/lib/openhab/core/items/semantics.rb +473 -0
  48. data/lib/openhab/core/items/state_storage.rb +53 -0
  49. data/lib/openhab/core/items/string_item.rb +28 -0
  50. data/lib/openhab/core/items/switch_item.rb +78 -0
  51. data/lib/openhab/core/items.rb +108 -0
  52. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  53. data/lib/openhab/core/profile_factory.rb +132 -0
  54. data/lib/openhab/core/provider.rb +230 -0
  55. data/lib/openhab/core/proxy.rb +130 -0
  56. data/lib/openhab/core/registry.rb +40 -0
  57. data/lib/openhab/core/rules/module.rb +26 -0
  58. data/lib/openhab/core/rules/provider.rb +25 -0
  59. data/lib/openhab/core/rules/registry.rb +76 -0
  60. data/lib/openhab/core/rules/rule.rb +150 -0
  61. data/lib/openhab/core/rules.rb +25 -0
  62. data/lib/openhab/core/script_handling.rb +78 -20
  63. data/lib/openhab/core/things/channel.rb +48 -0
  64. data/lib/openhab/core/things/channel_uid.rb +51 -0
  65. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  66. data/lib/openhab/core/things/links/provider.rb +78 -0
  67. data/lib/openhab/core/things/profile_callback.rb +52 -0
  68. data/lib/openhab/core/things/provider.rb +29 -0
  69. data/lib/openhab/core/things/proxy.rb +87 -0
  70. data/lib/openhab/core/things/registry.rb +73 -0
  71. data/lib/openhab/core/things/thing.rb +194 -0
  72. data/lib/openhab/core/things.rb +22 -0
  73. data/lib/openhab/core/timer.rb +148 -0
  74. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  75. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  76. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  77. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  78. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  79. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  80. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  81. data/lib/openhab/core/types/on_off_type.rb +46 -0
  82. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  83. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  84. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  85. data/lib/openhab/core/types/point_type.rb +117 -0
  86. data/lib/openhab/core/types/quantity_type.rb +325 -0
  87. data/lib/openhab/core/types/raw_type.rb +26 -0
  88. data/lib/openhab/core/types/refresh_type.rb +27 -0
  89. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  90. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  91. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  92. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  93. data/lib/openhab/core/types/un_def_type.rb +38 -0
  94. data/lib/openhab/core/types/up_down_type.rb +50 -0
  95. data/lib/openhab/core/types.rb +82 -0
  96. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  97. data/lib/openhab/core/value_cache.rb +188 -0
  98. data/lib/openhab/core.rb +98 -0
  99. data/lib/openhab/core_ext/between.rb +32 -0
  100. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  101. data/lib/openhab/core_ext/java/class.rb +34 -0
  102. data/lib/openhab/core_ext/java/duration.rb +142 -0
  103. data/lib/openhab/core_ext/java/list.rb +436 -0
  104. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  105. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  106. data/lib/openhab/core_ext/java/map.rb +66 -0
  107. data/lib/openhab/core_ext/java/month.rb +71 -0
  108. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  109. data/lib/openhab/core_ext/java/period.rb +103 -0
  110. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  111. data/lib/openhab/core_ext/java/time.rb +62 -0
  112. data/lib/openhab/core_ext/java/unit.rb +15 -0
  113. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  114. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  115. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  116. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  117. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  118. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  119. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  120. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  121. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  122. data/lib/openhab/core_ext.rb +18 -0
  123. data/lib/openhab/dsl/debouncer.rb +259 -0
  124. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  125. data/lib/openhab/dsl/events.rb +9 -0
  126. data/lib/openhab/dsl/gems.rb +1 -1
  127. data/lib/openhab/dsl/items/builder.rb +578 -0
  128. data/lib/openhab/dsl/items/ensure.rb +73 -82
  129. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  130. data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
  131. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  132. data/lib/openhab/dsl/rules/guard.rb +51 -114
  133. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  134. data/lib/openhab/dsl/rules/property.rb +48 -75
  135. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  136. data/lib/openhab/dsl/rules/terse.rb +58 -14
  137. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  138. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  139. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  140. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  141. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  142. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  143. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  144. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  145. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  146. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  147. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  148. data/lib/openhab/dsl/rules.rb +8 -0
  149. data/lib/openhab/dsl/things/builder.rb +299 -0
  150. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  151. data/lib/openhab/dsl/timer_manager.rb +204 -0
  152. data/lib/openhab/dsl/version.rb +9 -0
  153. data/lib/openhab/dsl.rb +979 -0
  154. data/lib/openhab/log.rb +355 -0
  155. data/lib/openhab/osgi.rb +68 -0
  156. data/lib/openhab/rspec/configuration.rb +56 -0
  157. data/lib/openhab/rspec/example_group.rb +132 -0
  158. data/lib/openhab/rspec/helpers.rb +458 -0
  159. data/lib/openhab/rspec/hooks.rb +113 -0
  160. data/lib/openhab/rspec/jruby.rb +46 -0
  161. data/lib/openhab/rspec/karaf.rb +851 -0
  162. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  163. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  164. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  165. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  166. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  167. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  168. data/lib/openhab/rspec/mocks/space.rb +23 -0
  169. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  170. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  171. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  172. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  173. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  174. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  175. data/lib/openhab/rspec/shell.rb +31 -0
  176. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  177. data/lib/openhab/rspec.rb +26 -0
  178. data/lib/openhab/yard/base_helper.rb +19 -0
  179. data/lib/openhab/yard/cli/stats.rb +23 -0
  180. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  181. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  182. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  183. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  184. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  186. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  187. data/lib/openhab/yard/coderay.rb +17 -0
  188. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  189. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  190. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  192. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  193. data/lib/openhab/yard/html_helper.rb +78 -0
  194. data/lib/openhab/yard/markdown_helper.rb +148 -0
  195. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  196. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  197. data/lib/openhab/yard/tags/library.rb +3 -0
  198. data/lib/openhab/yard.rb +38 -0
  199. metadata +475 -106
  200. data/lib/openhab/core/item_proxy.rb +0 -29
  201. data/lib/openhab/core/load_path.rb +0 -19
  202. data/lib/openhab/core/openhab_setup.rb +0 -29
  203. data/lib/openhab/core/osgi.rb +0 -58
  204. data/lib/openhab/core/services.rb +0 -24
  205. data/lib/openhab/dsl/actions.rb +0 -114
  206. data/lib/openhab/dsl/between.rb +0 -25
  207. data/lib/openhab/dsl/channel.rb +0 -43
  208. data/lib/openhab/dsl/dsl.rb +0 -59
  209. data/lib/openhab/dsl/group.rb +0 -54
  210. data/lib/openhab/dsl/imports.rb +0 -21
  211. data/lib/openhab/dsl/items/color_item.rb +0 -76
  212. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  213. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  214. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  215. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  216. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  217. data/lib/openhab/dsl/items/group_item.rb +0 -127
  218. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  219. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  220. data/lib/openhab/dsl/items/items.rb +0 -109
  221. data/lib/openhab/dsl/items/location_item.rb +0 -59
  222. data/lib/openhab/dsl/items/metadata.rb +0 -326
  223. data/lib/openhab/dsl/items/number_item.rb +0 -17
  224. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  225. data/lib/openhab/dsl/items/persistence.rb +0 -307
  226. data/lib/openhab/dsl/items/player_item.rb +0 -58
  227. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  228. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  229. data/lib/openhab/dsl/items/semantics.rb +0 -227
  230. data/lib/openhab/dsl/items/string_item.rb +0 -51
  231. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  232. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  233. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  234. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  235. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  236. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  237. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  238. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  239. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  240. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  241. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  242. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  243. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  244. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  245. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  246. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  247. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  248. data/lib/openhab/dsl/openhab.rb +0 -30
  249. data/lib/openhab/dsl/persistence.rb +0 -27
  250. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  251. data/lib/openhab/dsl/rules/rule.rb +0 -160
  252. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  253. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  254. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  255. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  256. data/lib/openhab/dsl/states.rb +0 -89
  257. data/lib/openhab/dsl/things.rb +0 -147
  258. data/lib/openhab/dsl/time/month_day.rb +0 -180
  259. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  260. data/lib/openhab/dsl/timers/manager.rb +0 -119
  261. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  262. data/lib/openhab/dsl/timers/timer.rb +0 -132
  263. data/lib/openhab/dsl/timers.rb +0 -77
  264. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  265. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  266. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  267. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  268. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  269. data/lib/openhab/dsl/types/point_type.rb +0 -180
  270. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  271. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  272. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  273. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  274. data/lib/openhab/dsl/types/types.rb +0 -83
  275. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  276. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  277. data/lib/openhab/dsl/units.rb +0 -45
  278. data/lib/openhab/log/configuration.rb +0 -21
  279. data/lib/openhab/log/logger.rb +0 -282
  280. data/lib/openhab/version.rb +0 -9
  281. data/lib/openhab.rb +0 -38
@@ -1,326 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
- require 'forwardable'
5
- require 'openhab/core/osgi'
6
- require 'openhab/log/logger'
7
-
8
- module OpenHAB
9
- module DSL
10
- module Items
11
- #
12
- # Metadata extension for Items
13
- #
14
- module Metadata
15
- include OpenHAB::Log
16
-
17
- java_import org.openhab.core.items.Metadata
18
- java_import org.openhab.core.items.MetadataKey
19
-
20
- #
21
- # Provide the interface to access namespace's value and configuration
22
- #
23
- class MetadataItem < SimpleDelegator
24
- extend Forwardable
25
-
26
- def_delegator :@metadata, :value
27
- def_delegator :__getobj__, :to_h, :to_hash
28
-
29
- def initialize(metadata: nil, key: nil, value: nil, config: nil)
30
- @metadata = metadata || Metadata.new(key || MetadataKey.new('', ''), value&.to_s, config)
31
- super(MetadataItem.to_ruby(@metadata&.configuration))
32
- end
33
-
34
- #
35
- # Updates the metadata configuration associated with the key
36
- #
37
- def []=(key, value)
38
- configuration = {}.merge(@metadata&.configuration || {}).merge({ key => value })
39
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
40
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
41
- end
42
-
43
- #
44
- # Delete the configuration with the given key
45
- #
46
- # @return [org.openhab.core.items.Metadata] the old metadata
47
- #
48
- def delete(key)
49
- configuration = {}.merge(@metadata&.configuration || {})
50
- configuration.delete(key)
51
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, configuration)
52
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
53
- end
54
-
55
- #
56
- # Set the metadata value
57
- #
58
- # @return [org.openhab.core.items.Metadata] the old metadata
59
- #
60
- def value=(value)
61
- metadata = Metadata.new(@metadata&.uID, value&.to_s, @metadata&.configuration)
62
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
63
- end
64
-
65
- #
66
- # Set the entire configuration to a hash
67
- #
68
- # @return [org.openhab.core.items.Metadata] the old metadata
69
- #
70
- def config=(config)
71
- config = config.to_hash if config.respond_to?(:to_hash)
72
- raise ArgumentError, 'Configuration must be a hash' unless config.is_a? Hash
73
-
74
- metadata = Metadata.new(@metadata&.uID, @metadata&.value, config)
75
- NamespaceAccessor.registry.update(metadata) if @metadata&.uID
76
- end
77
- alias configuration= config=
78
-
79
- #
80
- # Convert the metadata to an array
81
- #
82
- # @return [Array[2]] An array of [value, configuration]
83
- #
84
- def to_a
85
- [@metadata&.value, @metadata&.configuration || {}]
86
- end
87
-
88
- #
89
- # Recursively convert the supplied Hash object into a Ruby Hash and recreate the keys and values
90
- #
91
- # @param [Hash] hash to convert
92
- #
93
- # @return [Hash] The converted hash
94
- #
95
- def self.to_ruby_hash(hash)
96
- return unless hash.respond_to? :each_with_object
97
-
98
- hash.each_with_object({}) { |(key, value), ruby_hash| ruby_hash[to_ruby(key)] = to_ruby(value) }
99
- end
100
-
101
- #
102
- # Recursively convert the supplied array to a Ruby array and recreate all String values
103
- #
104
- # @param [Object] array to convert
105
- #
106
- # @return [Array] The converted array
107
- #
108
- def self.to_ruby_array(array)
109
- return unless array.respond_to? :each_with_object
110
-
111
- array.each_with_object([]) { |value, ruby_array| ruby_array << to_ruby(value) }
112
- end
113
-
114
- # Convert the given object to Ruby equivalent
115
- def self.to_ruby(value)
116
- case value
117
- when Hash, java.util.Map then to_ruby_hash(value)
118
- when Array, java.util.List then to_ruby_array(value)
119
- when String then String.new(value)
120
- else value
121
- end
122
- end
123
- end
124
-
125
- #
126
- # Provide the interface to access item metadata
127
- #
128
- class NamespaceAccessor
129
- include Enumerable
130
-
131
- def initialize(item_name:)
132
- @item_name = item_name
133
- end
134
-
135
- #
136
- # Return the metadata namespace
137
- #
138
- # @return [OpenHAB::DSL::Items::MetadataItem], or nil if the namespace doesn't exist
139
- #
140
- def [](namespace)
141
- logger.trace("Getting metadata for item: #{@item_name}, namespace '#{namespace}'")
142
- metadata = NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name))
143
- MetadataItem.new(metadata: metadata) if metadata
144
- end
145
-
146
- #
147
- # Set the metadata namespace. If the namespace does not exist, it will be created
148
- #
149
- # @param value [Object] The assigned value can be a OpenHAB::DSL::Items::MetadataItem,
150
- # org.openhab.core.items.Metadata, Array[2] of [value, configuration],
151
- # A String to set the value and clear the configuration,
152
- # or a Hash to set the configuration and set the value to nil
153
- #
154
- # @return [OpenHAB::DSL::Items::MetadataItem]
155
- #
156
- def []=(namespace, value)
157
- meta_value, configuration = update_from_value(value)
158
-
159
- key = MetadataKey.new(namespace, @item_name)
160
- metadata = Metadata.new(key, meta_value&.to_s, configuration.to_h)
161
- # registry.get can be omitted, but registry.update will log a warning for nonexistent metadata
162
- if NamespaceAccessor.registry.get(key)
163
- NamespaceAccessor.registry.update(metadata)
164
- else
165
- NamespaceAccessor.registry.add(metadata)
166
- end
167
- end
168
-
169
- #
170
- # Implements Hash#dig-like functionaity to metadata
171
- #
172
- # @param [String] key The first key
173
- # @param [Array<String, Symbol>] keys More keys to dig deeper
174
- #
175
- # @return [OpenHAB::DSL::Items::MetadataItem], or nil if the namespace doesn't exist
176
- #
177
- def dig(key, *keys)
178
- keys.empty? ? self[key]&.value : self[key]&.dig(*keys)
179
- end
180
-
181
- #
182
- # Enumerates through all the namespaces
183
- #
184
- def each
185
- return unless block_given?
186
-
187
- NamespaceAccessor.registry.getAll.each do |meta|
188
- if meta.uID.itemName == @item_name
189
- yield meta.uID.namespace, MetadataItem.to_ruby(meta.value), MetadataItem.to_ruby(meta.configuration)
190
- end
191
- end
192
- end
193
-
194
- #
195
- # Remove all the namespaces
196
- #
197
- def clear
198
- NamespaceAccessor.registry.removeItemMetadata @item_name
199
- end
200
-
201
- #
202
- # Delete a specific namespace
203
- #
204
- # @param namespace [String] The namespace to delete
205
- #
206
- def delete(namespace)
207
- NamespaceAccessor.registry.remove(MetadataKey.new(namespace, @item_name))
208
- end
209
-
210
- alias delete_all clear
211
-
212
- #
213
- # @return [Boolean] True if the given namespace exists, false otherwise
214
- #
215
- def key?(namespace)
216
- !NamespaceAccessor.registry.get(MetadataKey.new(namespace, @item_name)).nil?
217
- end
218
-
219
- alias has_key? key?
220
- alias include? key?
221
-
222
- #
223
- # Merge the given hash with the current metadata. Existing namespace that matches the name
224
- # of the new namespace will be overwritten. Others will be added.
225
- #
226
- def merge!(*others)
227
- return self if others.empty?
228
-
229
- others.each do |other|
230
- case other
231
- when Hash then merge_hash!(other)
232
- when self.class then merge_metadata!(other)
233
- else raise ArgumentError, "merge only supports Hash, or another item's metadata"
234
- end
235
- end
236
- self
237
- end
238
-
239
- #
240
- # @return [String] the string representation of all the namespaces with their value and config
241
- #
242
- def to_s
243
- namespaces = []
244
- each { |ns, value, config| namespaces << "\"#{ns}\"=>[\"#{value}\",#{config}]" }
245
- "{#{namespaces.join(',')}}"
246
- end
247
-
248
- #
249
- # @return [org.openhab.core.items.MetadataRegistry]
250
- #
251
- def self.registry
252
- @registry ||= OpenHAB::Core::OSGI.service('org.openhab.core.items.MetadataRegistry')
253
- end
254
-
255
- private
256
-
257
- #
258
- # perform an updated based on the supplied value
259
- #
260
- # @param [MetadataItem,Metadata,Array,Hash] value to perform update from
261
- #
262
- # @return [Array<Object,Hash>] Array containing the value and configuration based on the
263
- # the supplied object
264
- #
265
- def update_from_value(value)
266
- case value
267
- when MetadataItem then [value.value, value.__getobj__]
268
- when Metadata then [value.value, value.configuration]
269
- when Array
270
- raise ArgumentError, 'Array must contain 2 elements: value, config' if value.length != 2
271
-
272
- value
273
- when Hash then [nil, value]
274
- else [value, nil]
275
- end
276
- end
277
-
278
- #
279
- # Merge the metadata from the supplied other metadata object
280
- #
281
- # @param [Hash] other metadata object to merge
282
- # @yield [key, current_metadata, new_meta] to process merge
283
- #
284
- #
285
- def merge_metadata!(other)
286
- other.each do |key, new_value, new_config|
287
- new_meta = new_value, new_config
288
- if block_given?
289
- current_meta = self[key]&.to_a
290
- new_meta = yield key, current_meta, new_meta unless current_meta.nil?
291
- end
292
- self[key] = new_meta
293
- end
294
- end
295
-
296
- #
297
- # Merge a hash into the metadata
298
- #
299
- # @param [Hash] other to merge into metadata
300
- # @yield [key, current_metadata, new_meta] to process merge
301
- #
302
- #
303
- def merge_hash!(other)
304
- other.each do |key, new_meta|
305
- if block_given?
306
- current_meta = self[key]&.to_a
307
- new_meta = yield key, current_meta, new_meta unless current_meta.nil?
308
- end
309
- self[key] = new_meta
310
- end
311
- end
312
- end
313
-
314
- #
315
- # Accessor to the item's metadata
316
- #
317
- # @return [NamespaceAccessor] an Enumerable object to access item's namespaces
318
- #
319
- def meta
320
- @meta ||= NamespaceAccessor.new(item_name: name)
321
- end
322
- alias metadata meta
323
- end
324
- end
325
- end
326
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/dsl/items/numeric_item'
4
-
5
- module OpenHAB
6
- module DSL
7
- module Items
8
- java_import org.openhab.core.library.items.NumberItem
9
-
10
- # Adds methods to core OpenHAB NumberItem type to make it more natural in
11
- # Ruby
12
- class NumberItem < GenericItem
13
- include NumericItem
14
- end
15
- end
16
- end
17
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- require_relative 'comparable_item'
6
- require_relative 'item_equality'
7
-
8
- module OpenHAB
9
- module DSL
10
- module Items
11
- # Mixin for implementing type coercieon, equality, and arithmetic for
12
- # number-like items
13
- module NumericItem
14
- include Comparable
15
- include ComparableItem
16
-
17
- # apply meta-programming methods to including class
18
- # @!visibility private
19
- def self.included(klass)
20
- klass.prepend ItemEquality # make sure this is first
21
- klass.extend Forwardable
22
- klass.delegate %i[+ - * / % to_d to_f to_i to_int] => :state
23
- # remove the JRuby default == so that we can inherit the Ruby method
24
- klass.remove_method :==
25
- end
26
-
27
- #
28
- # Convert state to a Quantity by calling state (DecimalType)#|
29
- # Raise a NoMethodError if state is nil (NULL or UNDEF) instead of delegating to it.
30
- # because nil#| would return true, causing an unexpected result
31
- #
32
- # @param [Unit, String] other the unit to convert to
33
- #
34
- # @return [QuantityType] the QuantityType in the given unit
35
- #
36
- def |(other)
37
- raise NoMethodError, 'State is nil' unless state?
38
-
39
- state.|(other)
40
- end
41
-
42
- #
43
- # Check if NumericItem is truthy? as per defined by library
44
- #
45
- # @return [Boolean] True if item is not in state +UNDEF+ or +NULL+ and value is not zero.
46
- #
47
- def truthy?
48
- state && !state.zero?
49
- end
50
-
51
- #
52
- # Type Coercion
53
- #
54
- # Coerce object to a NumericType
55
- #
56
- # @param [Types::NumericType, Numeric] other object to coerce to a
57
- # DateTimeType
58
- #
59
- # @return [[Types::NumericType, Types::NumericType]]
60
- #
61
- def coerce(other)
62
- logger.trace("Coercing #{self} as a request from #{other.class}")
63
- return [other, nil] unless state?
64
- return [other, state] if other.is_a?(Types::NumericType) || other.respond_to?(:to_d)
65
- end
66
-
67
- # raw numbers translate directly to DecimalType, not a string
68
- # @!visibility private
69
- def format_type(command)
70
- return Types::DecimalType.new(command) if command.is_a?(Numeric)
71
-
72
- super
73
- end
74
-
75
- %i[positive? negative? zero?].each do |predicate|
76
- class_eval <<~RUBY, __FILE__, __LINE__ + 1
77
- def #{predicate} # def positive?
78
- return false unless state? # return false unless state?
79
- #
80
- state.#{predicate} # state.positive?
81
- end # end
82
- RUBY
83
- end
84
- end
85
- end
86
- end
87
- end