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
@@ -0,0 +1,475 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ module Metadata
7
+ #
8
+ # {NamespaceHash} represents the full metadata for the item.
9
+ #
10
+ # It implements the entire interface of {::Hash}.
11
+ #
12
+ # Keys are namespaces, values are always {Hash}, though assignment allows
13
+ # using a {Hash}, a String, a `::Hash`, or an array of two items:
14
+ # `[String, ::Hash]`.
15
+ #
16
+ # All keys are converted to strings.
17
+ #
18
+ class NamespaceHash
19
+ class << self
20
+ #
21
+ # @return [org.openhab.core.items.MetadataRegistry]
22
+ #
23
+ # @!visibility private
24
+ def registry
25
+ @registry ||= OSGi.service("org.openhab.core.items.MetadataRegistry")
26
+ end
27
+ end
28
+
29
+ java_import org.openhab.core.items.MetadataKey
30
+ private_constant :MetadataKey
31
+
32
+ include Enumerable
33
+ extend Forwardable
34
+
35
+ def_delegators :to_hash,
36
+ :<,
37
+ :<=,
38
+ :==,
39
+ :>,
40
+ :>=,
41
+ :filter,
42
+ :flatten,
43
+ :invert,
44
+ :key,
45
+ :merge,
46
+ :rassoc,
47
+ :reject,
48
+ :select,
49
+ :to_a,
50
+ :transform_keys,
51
+ :transform_values
52
+
53
+ def_delegators :keys,
54
+ :length,
55
+ :size
56
+
57
+ # @!visibility private
58
+ def initialize(item_name = nil, hash = nil)
59
+ @item_name = item_name || "-"
60
+ @hash = item_name.nil? ? (hash || {}) : nil
61
+ end
62
+
63
+ # Is this object attached to an actual Item?
64
+ # @return [true,false]
65
+ def attached?
66
+ @hash.nil?
67
+ end
68
+
69
+ # @!visibility private
70
+ def dup
71
+ self.class.new(nil, transform_values(&:dup))
72
+ end
73
+
74
+ # Implicit conversion to {::Hash}
75
+ # @return [::Hash]
76
+ def to_hash
77
+ each.to_h { |namespace, meta| [namespace, meta] }
78
+ end
79
+
80
+ # @return [Hash, nil]
81
+ #
82
+ # @!visibility private
83
+ def [](namespace)
84
+ fetch(namespace, nil)
85
+ end
86
+
87
+ #
88
+ # Set the metadata namespace. If the namespace does not exist, it will be created
89
+ #
90
+ # @param value [Hash, Array[String, ::Hash], ::Hash<String, Object>]
91
+ #
92
+ # @return [Hash]
93
+ #
94
+ # @!visibility private
95
+ def []=(namespace, value)
96
+ metadata = Hash.from_item(@item_name, namespace, value)
97
+ return @hash[metadata.uid.namespace] = metadata unless attached? # rubocop:disable Lint/ReturnInVoidContext
98
+
99
+ metadata.create_or_update
100
+ metadata # rubocop:disable Lint/Void
101
+ end
102
+ alias_method :store, :[]=
103
+
104
+ # @!visibility private
105
+ def assoc(key)
106
+ to_hash.assoc(key.to_s)
107
+ end
108
+
109
+ #
110
+ # Remove all the namespaces
111
+ #
112
+ # @!visibility private
113
+ def clear
114
+ if attached?
115
+ provider = Provider.current(Thread.current[:openhab_providers]&.dig(:metadata_items, @item_name))
116
+ provider.remove_item_metadata(@item_name)
117
+ Thread.current[:openhab_providers]&.[](:metadata_namespaces)&.each_value do |namespace_provider|
118
+ Provider.current(namespace_provider).remove_item_metadata(@item_name)
119
+ end
120
+ else
121
+ @hash.clear
122
+ end
123
+ end
124
+
125
+ # @!visibility private
126
+ def compact
127
+ to_hash
128
+ end
129
+
130
+ # @!visibility private
131
+ def compact!
132
+ # no action; impossible to have nil keys
133
+ self
134
+ end
135
+
136
+ # @!visibility private
137
+ def compare_by_identity
138
+ raise NotImplementedError
139
+ end
140
+
141
+ # @!visibility private
142
+ def compare_by_identity?
143
+ false
144
+ end
145
+
146
+ # @!visibility private
147
+ def deconstruct_keys
148
+ self
149
+ end
150
+
151
+ # @!visibility private
152
+ def default(*)
153
+ nil
154
+ end
155
+
156
+ # @!visibility private
157
+ def default=(*)
158
+ raise NotImplementedError
159
+ end
160
+
161
+ # @!visibility private
162
+ def default_proc
163
+ nil
164
+ end
165
+
166
+ # @!visibility private
167
+ def default_proc=(*)
168
+ raise NotImplementedError
169
+ end
170
+
171
+ # @!visibility private
172
+ def delete(namespace, &block)
173
+ return @hash.delete(namespace.to_s, &block) unless attached?
174
+
175
+ metadata = Hash.from_item(@item_name, namespace, nil)
176
+ r = metadata.remove
177
+ return yield(namespace) if block && !r
178
+
179
+ Hash.new(r) if r
180
+ end
181
+
182
+ # @!visibility private
183
+ def delete_if
184
+ raise NotImplementedError unless block_given?
185
+
186
+ each { |k, v| delete(k) if yield(k, v) }
187
+ self
188
+ end
189
+
190
+ # @!visibility private
191
+ def dig(key, *keys)
192
+ m = self[key.to_s]
193
+ return m if keys.empty?
194
+
195
+ m&.dig(*keys)
196
+ end
197
+
198
+ #
199
+ # Enumerates through all the namespaces
200
+ #
201
+ # @yieldparam [String] namespace
202
+ # @yieldparam [Hash] metadata
203
+ #
204
+ # @!visibility private
205
+ def each(&block)
206
+ return @hash.each(&block) unless attached?
207
+ return to_enum(:each) unless block
208
+
209
+ Provider.registry.all.each do |meta|
210
+ yield meta.uid.namespace, Hash.new(meta) if meta.uid.item_name == @item_name
211
+ end
212
+ self
213
+ end
214
+ alias_method :each_pair, :each
215
+
216
+ # @!visibility private
217
+ def each_key(&block)
218
+ return @hash.each_key(&block) unless attached?
219
+ return to_enum(:each_key) unless block
220
+
221
+ if Provider.registry.respond_to?(:get_all_namespaces)
222
+ keys.each(&block)
223
+ else
224
+ Provider.registry.all.each do |meta|
225
+ yield meta.uid.namespace if meta.uid.item_name == @item_name
226
+ end
227
+ end
228
+ self
229
+ end
230
+
231
+ # @!visibility private
232
+ def each_value
233
+ return @hash.each_value(&block) unless attached?
234
+ return to_enum(:each_value) unless block_given?
235
+
236
+ Provider.registry.all.each do |meta|
237
+ yield Hash.new(meta) if meta.uid.item_name == @item_name
238
+ end
239
+ self
240
+ end
241
+
242
+ # @!visibility private
243
+ def empty?
244
+ return @hash.empty? unless attached?
245
+
246
+ Provider.registry.all.each do |meta|
247
+ return false if meta.uid.item_name == @item_name
248
+ end
249
+ true
250
+ end
251
+
252
+ # @!visibility private
253
+ def except(*keys)
254
+ to_hash.except(*keys.map(&:to_s))
255
+ end
256
+
257
+ # @!visibility private
258
+ def fetch(key, *default_value, &block)
259
+ key = key.to_s
260
+ return @hash.fetch(key, *default_value, &block) unless attached?
261
+
262
+ if default_value.length > 1
263
+ raise "wrong number of arguments (given #{default_value.length + 1}, expected 1..2)",
264
+ ArgumentError
265
+ end
266
+
267
+ logger.trace("Getting metadata for item: #{@item_name}, namespace '#{key}'")
268
+ if (m = Provider.registry.get(MetadataKey.new(key, @item_name)))
269
+ Hash.new(m)
270
+ elsif block
271
+ yield key
272
+ elsif !default_value.empty?
273
+ default_value.first
274
+ else
275
+ raise "key not found #{key.inspect}", KeyError
276
+ end
277
+ end
278
+
279
+ # @!visibility private
280
+ def fetch_values(*keys)
281
+ return @hash.fetch_values(keys.map(&:to_s)) if attached?
282
+
283
+ keys.each_with_object([]) do |key, res|
284
+ key = key.to_s
285
+ if (m = Provider.registry.get(MetadataKey.new(key, @item_name)))
286
+ res << Hash.new(m)
287
+ elsif block_given?
288
+ res << yield(key)
289
+ end
290
+ end
291
+ end
292
+
293
+ # @!visibility private
294
+ def hash
295
+ ["metadata_namespace_hash", @item_name.hash]
296
+ end
297
+
298
+ # @!visibility private
299
+ def keep_if(&block)
300
+ select!(&block)
301
+ self
302
+ end
303
+
304
+ #
305
+ # @return [true,false] True if the given namespace exists, false otherwise
306
+ #
307
+ # @!visibility private
308
+ def key?(key)
309
+ key = key.to_s
310
+ return @hash.key?(key) unless attached?
311
+
312
+ !Provider.registry.get(MetadataKey.new(key, @item_name)).nil?
313
+ end
314
+ alias_method :has_key?, :key?
315
+ alias_method :member?, :key?
316
+ alias_method :include?, :key?
317
+
318
+ # @!visibility private
319
+ def keys
320
+ if Provider.registry.respond_to?(:get_all_namespaces)
321
+ return Provider.registry.get_all_namespaces(@item_name).to_a
322
+ end
323
+
324
+ each_key.to_a
325
+ end
326
+
327
+ #
328
+ # Merge the given hash with the current metadata. Existing namespace that matches the name
329
+ # of the new namespace will be overwritten. Others will be added.
330
+ #
331
+ # @!visibility private
332
+ def merge!(*others)
333
+ return self if others.empty?
334
+
335
+ others.each do |other|
336
+ case other
337
+ when ::Hash, NamespaceHash
338
+ other.each do |namespace, new_meta|
339
+ self[namespace] = new_meta
340
+ next unless block_given?
341
+
342
+ current_meta = self[namespace]
343
+ new_meta = yield key, current_meta, new_meta unless current_meta.nil?
344
+ self[namespace] = new_meta unless new_meta.equal?(current_meta)
345
+ end
346
+ else
347
+ raise ArgumentError, "merge only supports Hash, or another item's metadata"
348
+ end
349
+ end
350
+ self
351
+ end
352
+
353
+ # @!visibility private
354
+ def select!
355
+ raise NotImplementedError unless block_given?
356
+
357
+ removed = false
358
+ each do |k, v|
359
+ unless yield(k, v)
360
+ delete(k)
361
+ removed = true
362
+ end
363
+ end
364
+ return nil unless removed
365
+
366
+ self
367
+ end
368
+ alias_method :filter!, :select!
369
+
370
+ # @!visibility private
371
+ def shift
372
+ raise NotImplementedError
373
+ end
374
+
375
+ # @!visibility private
376
+ def slice(*keys)
377
+ keys.map!(&:to_s)
378
+ return @hash.slice(*keys) unless attached?
379
+
380
+ keys = keys.to_set
381
+ r = {}
382
+ Provider.registry.all.each do |meta|
383
+ if meta.uid.item_name == @item_name && keys.include?(meta.uid.namespace)
384
+ r[meta.uid.namespace] =
385
+ Hash.new(meta)
386
+ end
387
+ end
388
+ r
389
+ end
390
+
391
+ # @!visibility private
392
+ def reject!
393
+ raise NotImplementedError unless block_given?
394
+
395
+ removed = false
396
+ each do |k, v|
397
+ if yield(k, v)
398
+ delete(k)
399
+ removed = true
400
+ end
401
+ end
402
+ return nil unless removed
403
+
404
+ self
405
+ end
406
+
407
+ # @!visibility private
408
+ def replace(other)
409
+ clear
410
+ merge!(other)
411
+ end
412
+
413
+ # @!visibility private
414
+ def to_proc
415
+ ->(k) { self[k] }
416
+ end
417
+
418
+ # @!visibility private
419
+ def transform_keys!(hash2 = nil)
420
+ hash2 = hash2&.transform_keys(&:to_s)
421
+ each_key do |k|
422
+ if hash2
423
+ next unless hash2.key?(k)
424
+
425
+ self[hash2[k]] = delete(k)
426
+ else
427
+ new_k = yield k
428
+ self[new_k] = delete(k)
429
+ end
430
+ end
431
+ self
432
+ end
433
+
434
+ # @!visibility private
435
+ def transform_values!
436
+ raise NotImplementedError unless block_given?
437
+
438
+ each do |k, v|
439
+ new_v = yield(k, v)
440
+ next if new_v.equal?(v)
441
+
442
+ self[k] = new_v
443
+ end
444
+ end
445
+
446
+ # @!visibility private
447
+ def value?(value)
448
+ each_value { |v| return true if v == value }
449
+ false
450
+ end
451
+ alias_method :has_value?, :value?
452
+
453
+ # @!visibility private
454
+ def values
455
+ each_value.to_a
456
+ end
457
+
458
+ # @!visibility private
459
+ def values_at(*keys)
460
+ keys.map(&self)
461
+ end
462
+
463
+ #
464
+ # @return [String]
465
+ #
466
+ # @!visibility private
467
+ def inspect
468
+ to_hash.inspect
469
+ end
470
+ alias_method :to_s, :inspect
471
+ end
472
+ end
473
+ end
474
+ end
475
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ module Metadata
7
+ #
8
+ # Provides metadata created in Ruby to openHAB
9
+ #
10
+ class Provider < Core::Provider
11
+ include org.openhab.core.items.ManagedMetadataProvider
12
+
13
+ class << self
14
+ #
15
+ # The Metadata registry
16
+ #
17
+ # @return [org.openhab.core.items.MetadataRegistry]
18
+ #
19
+ def registry
20
+ @registry ||= OSGi.service("org.openhab.core.items.MetadataRegistry")
21
+ end
22
+ end
23
+
24
+ # see Hash#javaify
25
+ registry.managed_provider.get.class.field_reader :storage
26
+ registry.managed_provider.get.storage.class.field_reader :entityMapper
27
+
28
+ #
29
+ # Removes all metadata of a given item.
30
+ #
31
+ # @param [String] item_name
32
+ # @return [void]
33
+ #
34
+ def remove_item_metadata(item_name)
35
+ @elements.delete_if do |_k, v|
36
+ next unless v.uid.item_name == item_name
37
+
38
+ notify_listeners_about_removed_element(v)
39
+ true
40
+ end
41
+ nil
42
+ end
43
+ alias_method :removeItemMetadata, :remove_item_metadata
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ # Contains classes wrapping interactions with item metadata.
7
+ module Metadata
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,62 @@
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.NumberItem
10
+
11
+ #
12
+ # A NumberItem has a decimal value and is usually used for all kinds
13
+ # of sensors, like temperature, brightness, wind, etc.
14
+ # It can also be used as a counter or as any other thing that can be expressed
15
+ # as a number.
16
+ #
17
+ # Non-dimensioned numbers will have a state of {DecimalType}, while
18
+ # dimensioned numbers will have a state of {QuantityType}. Be sure
19
+ # to read the documentation for those two classes for how to work with
20
+ # the different states of a {NumberItem}.
21
+ #
22
+ # @!attribute [r] dimension
23
+ # @return [Class, nil] The dimension of the number item.
24
+ # @!attribute [r] unit
25
+ # @return [javax.measure.Unit, nil]
26
+ # @!attribute [r] state
27
+ # @return [DecimalType, QuantityType, nil]
28
+ #
29
+ # @example Number Items can be selected in an enumerable with grep.
30
+ # # Get all NumberItems
31
+ # items.grep(NumberItem)
32
+ # .each { |number| logger.info("#{item.name} is a Number Item") }
33
+ #
34
+ class NumberItem < GenericItem
35
+ include NumericItem
36
+
37
+ # raw numbers translate directly to {DecimalType}, not a string
38
+ # @!visibility private
39
+ def format_type(command)
40
+ if command.is_a?(Numeric)
41
+ if unit && (target_unit = DSL.unit(unit.dimension) || unit)
42
+ return Types::QuantityType.new(command, target_unit)
43
+ end
44
+
45
+ return Types::DecimalType.new(command)
46
+ end
47
+
48
+ super
49
+ end
50
+
51
+ protected
52
+
53
+ # Adds the unit dimension
54
+ def type_details
55
+ ":#{dimension}" if dimension
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ # @!parse NumberItem = OpenHAB::Core::Items::NumberItem
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ # Mixin for implementing type coercion for number-like items
9
+ module NumericItem
10
+ %i[positive? negative? zero?].each do |predicate|
11
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
12
+ def #{predicate} # def positive?
13
+ return false unless state? # return false unless state?
14
+ #
15
+ state.#{predicate} # state.positive?
16
+ end # end
17
+ RUBY
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end