openhab-scripting 4.46.2 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 -36
@@ -0,0 +1,176 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openhab/core/lazy_array"
4
+
5
+ require_relative "generic_item"
6
+
7
+ module OpenHAB
8
+ module Core
9
+ module Items
10
+ java_import org.openhab.core.items.GroupItem
11
+
12
+ #
13
+ # A group behaves like a regular item, but also has {#members} which are
14
+ # nested items that can be enumerated.
15
+ #
16
+ # If the group has a particular type, the methods from that type are
17
+ # directly available.
18
+ #
19
+ #
20
+ # The examples all assume the following items exist.
21
+ # ```xtend
22
+ # Group House
23
+ # // Location perspective
24
+ # Group GroundFloor (House)
25
+ # Group Livingroom (GroundFloor)
26
+ # // Functional perspective
27
+ # Group Sensors (House)
28
+ # Group Temperatures (Sensors)
29
+ #
30
+ # Number Livingroom_Temperature "Living Room temperature" (Livingroom, Temperatures)
31
+ # Number Bedroom_Temp "Bedroom temperature" (GroundFloor, Temperatures)
32
+ # Number Den_Temp "Den temperature" (GroundFloor, Temperatures)
33
+ # ```
34
+ #
35
+ # @!attribute [r] base_item
36
+ # @return [Item, nil] A typed item if the group has a particular type.
37
+ #
38
+ # @example Operate on items in a group using enumerable methods
39
+ # logger.info("Total Temperatures: #{Temperatures.members.count}")
40
+ # # Total Temperatures: 3
41
+ # logger.info("Temperatures: #{House.members.map(&:name).sort.join(', ')}")
42
+ # # Temperatures: GroundFloor, Sensors
43
+ #
44
+ # @example Access to the methods and attributes like any item
45
+ # logger.info("Group: #{Temperatures.name}" # Group: Temperatures'
46
+ #
47
+ # @example Operates on items in nested groups using enumerable methods
48
+ # logger.info("House Count: #{House.all_members.count}")
49
+ # # House Count: 7
50
+ # logger.info("Items: #{House.all_members.grep_v(GroupItem).map(&:label).sort.join(', ')}")
51
+ # # Items: Bedroom temperature, Den temperature, Living Room temperature
52
+ #
53
+ # @example Iterate through the direct members of the group
54
+ # Temperatures.members.each do |item|
55
+ # logger.info("#{item.label} is: #{item.state}")
56
+ # end
57
+ # # Living Room temperature is 22
58
+ # # Bedroom temperature is 21
59
+ # # Den temperature is 19
60
+ #
61
+ # @example
62
+ # rule 'Turn off any switch that changes' do
63
+ # changed Switches.members
64
+ # triggered(&:off)
65
+ # end
66
+ #
67
+ # @example Built in {Enumerable} functions can be applied to groups.
68
+ # logger.info("Max is #{Temperatures.members.map(&:state).max}")
69
+ # logger.info("Min is #{Temperatures.members.map(&:state).min}")
70
+ #
71
+ class GroupItem < GenericItem
72
+ #
73
+ # Class for indicating to triggers that a group trigger should be used
74
+ #
75
+ class Members
76
+ include LazyArray
77
+
78
+ # @return [GroupItem]
79
+ attr_reader :group
80
+
81
+ # @!visibility private
82
+ def initialize(group_item)
83
+ @group = group_item
84
+ end
85
+
86
+ # Explicit conversion to Array
87
+ #
88
+ # @return [Array]
89
+ def to_a
90
+ group.get_members.map { |i| Proxy.new(i) }
91
+ end
92
+
93
+ # Name of the group
94
+ #
95
+ # @return [String]
96
+ def name
97
+ group.name
98
+ end
99
+
100
+ # @return [String]
101
+ def inspect
102
+ r = "#<OpenHAB::Core::Items::GroupItems::Members #{name}"
103
+ r += " #{map(&:name).inspect}>" unless @group.__getobj__.nil?
104
+ "#{r}>"
105
+ end
106
+ alias_method :to_s, :inspect
107
+ end
108
+
109
+ # Override because we want to send them to the base item if possible
110
+ %i[command update].each do |method|
111
+ define_method(method) do |command|
112
+ return base_item.__send__(method, command) if base_item
113
+
114
+ super(command)
115
+ end
116
+ end
117
+
118
+ #
119
+ # @!attribute [r] members
120
+ # @return [Members] Get an Array-like object representing the members of the group
121
+ #
122
+ # @see Enumerable
123
+ #
124
+ def members
125
+ Members.new(Proxy.new(self))
126
+ end
127
+
128
+ #
129
+ # @!attribute [r] all_members
130
+ # @return [Array] Get all non-group members of the group recursively.
131
+ #
132
+ # @see Enumerable
133
+ #
134
+ def all_members
135
+ getAllMembers.map { |m| Proxy.new(m) }
136
+ end
137
+
138
+ # give the base item type a chance to format commands
139
+ # @!visibility private
140
+ def format_type(command)
141
+ return super unless base_item
142
+
143
+ base_item.format_type(command)
144
+ end
145
+
146
+ private
147
+
148
+ # Add base type and function details
149
+ def type_details
150
+ r = ""
151
+ r += ":#{base_item.type}#{base_item.__send__(:type_details)}" if base_item
152
+ if function && (fn = function.class.java_class.simple_name.upcase) != "EQUALITY"
153
+ r += ":#{fn}"
154
+ r += "(#{function.parameters.map(&:inspect).join(",")})" unless function.parameters.empty?
155
+ end
156
+ r
157
+ end
158
+
159
+ # Delegate missing methods to {base_item} if possible
160
+ def method_missing(method, *args, &block)
161
+ return base_item.__send__(method, *args, &block) if base_item&.respond_to?(method) # rubocop:disable Lint/RedundantSafeNavigation nil responds to :to_a
162
+
163
+ super
164
+ end
165
+
166
+ def respond_to_missing?(method, include_private = false)
167
+ return true if base_item&.respond_to?(method) # rubocop:disable Lint/RedundantSafeNavigation
168
+
169
+ super
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ # @!parse GroupItem = OpenHAB::Core::Items::GroupItem
@@ -1,17 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'base64'
4
- require 'pathname'
5
- require 'net/http'
6
- require 'marcel'
3
+ require "base64"
4
+ require "pathname"
5
+ require "net/http"
6
+ require "marcel"
7
+
8
+ require_relative "generic_item"
7
9
 
8
10
  module OpenHAB
9
- module DSL
11
+ module Core
10
12
  module Items
11
13
  java_import org.openhab.core.library.items.ImageItem
12
14
 
13
- # Adds methods to core OpenHAB ImageItem type to make it more natural in
14
- # Ruby
15
+ #
16
+ # An {ImageItem} holds the binary image data as its state.
17
+ #
18
+ # @!attribute [r] state
19
+ # @return [RawType, nil]
20
+ #
21
+ # @example Update from a base 64 encode image string
22
+ # Image.update("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=")
23
+ #
24
+ # @example Update from image bytes and mime type
25
+ # Image.update_from_bytes(File.binread(File.join(Dir.tmpdir,'1x1.png')), mime_type: 'image/png')
26
+ #
27
+ # @example Update from URL
28
+ # Image.update_from_url('https://raw.githubusercontent.com/boc-tothefuture/openhab-jruby/main/features/assets/1x1.png')
29
+ #
30
+ # @example Update from File
31
+ # Image.update_from_file('/tmp/1x1.png')
32
+ #
33
+ # @example Log image data
34
+ # logger.info("Mime type: #{Image.state.mime_type}")
35
+ # logger.info("Number of bytes: #{Image.state.bytes.length}")
36
+ #
15
37
  class ImageItem < GenericItem
16
38
  #
17
39
  # Update image from file
@@ -35,7 +57,7 @@ module OpenHAB
35
57
  def update_from_url(uri)
36
58
  logger.trace("Downloading image from #{uri}")
37
59
  response = Net::HTTP.get_response(URI(uri))
38
- mime_type = response['content-type']
60
+ mime_type = response["content-type"]
39
61
  bytes = response.body
40
62
  mime_type ||= detect_mime_from_bytes(bytes: bytes)
41
63
  update_from_bytes(bytes, mime_type: mime_type)
@@ -54,33 +76,15 @@ module OpenHAB
54
76
  update(base_64_image)
55
77
  end
56
78
 
57
- #
58
- # Get the mime type for the image item
59
- #
60
- # @return [String] mime type for image, e.g. image/png
61
- #
62
- def mime_type
63
- state&.mime_type
64
- end
65
-
66
- #
67
- # Get the bytes of the image
68
- #
69
- # @return [Array] Bytes that comprise the image
70
- #
71
- def bytes
72
- state&.get_bytes
73
- end
74
-
75
79
  private
76
80
 
77
81
  #
78
- # Encode image information in the format required by OpenHAB
82
+ # Encode image information in the format required by openHAB
79
83
  #
80
84
  # @param [String] mime_type for image
81
85
  # @param [Object] bytes image data
82
86
  #
83
- # @return [String] OpenHAB image format with image data Base64 encoded
87
+ # @return [String] openHAB image format with image data Base64 encoded
84
88
  #
85
89
  def encode_image(mime_type:, bytes:)
86
90
  "data:#{mime_type};base64,#{Base64.strict_encode64(bytes)}"
@@ -94,10 +98,12 @@ module OpenHAB
94
98
  # @return [String] mime type if it can be detected, nil otherwise
95
99
  #
96
100
  def detect_mime_from_bytes(bytes:)
97
- logger.trace('Detecting mime type from file image contents')
101
+ logger.trace("Detecting mime type from file image contents")
98
102
  Marcel::MimeType.for(bytes)
99
103
  end
100
104
  end
101
105
  end
102
106
  end
103
107
  end
108
+
109
+ # @!parse ImageItem = OpenHAB::Core::Items::ImageItem
@@ -0,0 +1,273 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Items
6
+ # @interface
7
+ java_import org.openhab.core.items.Item
8
+
9
+ #
10
+ # The core features of an openHAB item.
11
+ #
12
+ module Item
13
+ class << self
14
+ # @!visibility private
15
+ #
16
+ # Override to support {Proxy}
17
+ #
18
+ def ===(other)
19
+ other.is_a?(self)
20
+ end
21
+ end
22
+
23
+ # @!attribute [r] name
24
+ # The item's name.
25
+ # @return [String]
26
+
27
+ # @!attribute [r] accepted_command_types
28
+ # @return [Array<Class>] An array of {Command}s that can be sent as commands to this item
29
+
30
+ # @!attribute [r] accepted_data_types
31
+ # @return [Array<Class>] An array of {State}s that can be sent as commands to this item
32
+
33
+ #
34
+ # The item's {GenericItem#label label} if one is defined, otherwise its {#name}.
35
+ #
36
+ # @return [String]
37
+ #
38
+ def to_s
39
+ label || name
40
+ end
41
+
42
+ #
43
+ # @!attribute [r] groups
44
+ #
45
+ # Returns all groups that this item is part of
46
+ #
47
+ # @return [Array<GroupItem>] All groups that this item is part of
48
+ #
49
+ def groups
50
+ group_names.map { |name| EntityLookup.lookup_item(name) }.compact
51
+ end
52
+
53
+ #
54
+ # Checks if this item is a member of at least one of the given groups.
55
+ #
56
+ # @param groups [String, GroupItem] the group to check membership in
57
+ # @return [true, false]
58
+ #
59
+ # @example
60
+ # event.item.member_of?(gFullOn)
61
+ #
62
+ def member_of?(*groups)
63
+ groups.map! do |group|
64
+ group.is_a?(GroupItem) ? group.name : group
65
+ end
66
+ !(group_names & groups).empty?
67
+ end
68
+
69
+ #
70
+ # @!attribute [r] all_groups
71
+ #
72
+ # Returns all groups that this item is a part of, as well as those groups' groups, recursively
73
+ #
74
+ # @return [Array<GroupItem>]
75
+ #
76
+ def all_groups
77
+ result = []
78
+ new_groups = Set.new(groups)
79
+
80
+ until new_groups.empty?
81
+ result.concat(new_groups.to_a)
82
+ new_groups.replace(new_groups.flat_map(&:groups))
83
+ # remove any groups we already have in the result to avoid loops
84
+ new_groups.subtract(result)
85
+ end
86
+
87
+ result
88
+ end
89
+
90
+ # rubocop:disable Layout/LineLength
91
+
92
+ # @!attribute [r] metadata
93
+ # @return [Metadata::NamespaceHash]
94
+ #
95
+ # Access to the item's metadata.
96
+ #
97
+ # Both the return value of this method as well as the individual
98
+ # namespaces can be treated as Hashes.
99
+ #
100
+ # Examples assume the following items:
101
+ #
102
+ # ```xtend
103
+ # Switch Item1 { namespace1="value" [ config1="foo", config2="bar" ] }
104
+ # String StringItem1
105
+ # ```
106
+ #
107
+ # @example Check namespace's existence
108
+ # Item1.metadata["namespace"].nil?
109
+ # Item1.metadata.key?("namespace")
110
+ #
111
+ # @example Access item's metadata value
112
+ # Item1.metadata["namespace1"].value
113
+ #
114
+ # @example Access namespace1's configuration
115
+ # Item1.metadata["namespace1"]["config1"]
116
+ #
117
+ # @example Safely search for the specified value - no errors are raised, only nil returned if a key in the chain doesn't exist
118
+ # Item1.metadata.dig("namespace1", "config1") # => "foo"
119
+ # Item1.metadata.dig("namespace2", "config1") # => nil
120
+ #
121
+ # @example Set item's metadata value, preserving its config
122
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
123
+ # Item1.metadata["namespace1"].value = "new value"
124
+ # # Item1's metadata after: {"namespace1"=>["new value", {"config1"=>"foo", "config2"=>"bar"]}}
125
+ #
126
+ # @example Set item's metadata config, preserving its value
127
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
128
+ # Item1.metadata["namespace1"].replace({ "scooby"=>"doo" })
129
+ # # Item1's metadata after: {"namespace1"=>["value", {scooby="doo"}]}
130
+ #
131
+ # @example Set a namespace to a new value and config in one line
132
+ # # Item1's metadata before: {"namespace1"=>"value", {"config1"=>"foo", "config2"=>"bar"}}
133
+ # Item1.metadata["namespace1"] = "new value", { "scooby"=>"doo" }
134
+ # # Item1's metadata after: {"namespace1"=>["new value", {scooby="doo"}]}
135
+ #
136
+ # @example Set item's metadata value and clear its previous config
137
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
138
+ # Item1.metadata["namespace1"] = "new value"
139
+ # # Item1's metadata after: {"namespace1"=>"value" }
140
+ #
141
+ # @example Set item's metadata config, set its value to nil, and wiping out previous config
142
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
143
+ # Item1.metadata["namespace1"] = { "newconfig"=>"value" }
144
+ # # Item1's metadata after: {"namespace1"=>{"config1"=>"foo", "config2"=>"bar"}}
145
+ #
146
+ # @example Update namespace1's specific configuration, preserving its value and other config
147
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
148
+ # Item1.metadata["namespace1"]["config1"] = "doo"
149
+ # # Item1's metadata will be: {"namespace1"=>["value", {"config1"=>"doo", "config2"=>"bar"}]}
150
+ #
151
+ # @example Add a new configuration to namespace1
152
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
153
+ # Item1.metadata["namespace1"]["config3"] = "boo"
154
+ # # Item1's metadata after: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar", config3="boo"}]}
155
+ #
156
+ # @example Delete a config
157
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
158
+ # Item1.metadata["namespace1"].delete("config2")
159
+ # # Item1's metadata after: {"namespace1"=>["value", {"config1"=>"foo"}]}
160
+ #
161
+ # @example Add a namespace and set it to a value
162
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
163
+ # Item1.metadata["namespace2"] = "qx"
164
+ # # Item1's metadata after: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}], "namespace2"=>"qx"}
165
+ #
166
+ # @example Add a namespace and set it to a value and config
167
+ # # Item1's metadata before: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
168
+ # Item1.metadata["namespace2"] = "qx", { "config1"=>"doo" }
169
+ # # Item1's metadata after: {"namespace1"=>["value", {"config1"=>"foo", "config2"=>"bar"}], "namespace2"=>["qx", {"config1"=>"doo"}]}
170
+ #
171
+ # @example Enumerate Item1's namespaces
172
+ # Item1.metadata.each { |namespace, metadata| logger.info("Item1's namespace: #{namespace}=#{metadata}") }
173
+ #
174
+ # @example Add metadata from a hash
175
+ # Item1.metadata.merge!({"namespace1"=>{"foo", {"config1"=>"baz"} ], "namespace2"=>{"qux", {"config"=>"quu"} ]})
176
+ #
177
+ # @example Merge Item2's metadata into Item1's metadata
178
+ # Item1.metadata.merge!(Item2.metadata)
179
+ #
180
+ # @example Delete a namespace
181
+ # Item1.metadata.delete("namespace1")
182
+ #
183
+ # @example Delete all metadata of the item
184
+ # Item1.metadata.clear
185
+ #
186
+ # @example Does this item have any metadata?
187
+ # Item1.metadata.any?
188
+ #
189
+ # @example Store another item's state
190
+ # StringItem1.update "TEST"
191
+ # Item1.metadata["other_state"] = StringItem1.state
192
+ #
193
+ # @example Store event's state
194
+ # rule "save event state" do
195
+ # changed StringItem1
196
+ # run { |event| Item1.metadata["last_event"] = event.was }
197
+ # end
198
+ #
199
+ # @example If the namespace already exists: Update the value of a namespace but preserve its config; otherwise create a new namespace with the given value and nil config.
200
+ # Item1.metadata["namespace"] = "value", Item1.metadata["namespace"]
201
+ #
202
+ # @example Copy another namespace
203
+ # # Item1's metadata before: {"namespace2"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
204
+ # Item1.metadata["namespace"] = Item1.metadata["namespace2"]
205
+ # # Item1's metadata after: {"namespace2"=>["value", {"config1"=>"foo", "config2"=>"bar"}], "namespace"=>["value", {"config1"=>"foo", "config2"=>"bar"}]}
206
+ #
207
+ def metadata
208
+ @metadata ||= Metadata::NamespaceHash.new(name)
209
+ end
210
+ # rubocop:enable Layout/LineLength
211
+
212
+ #
213
+ # Checks if this item has at least one of the given tags.
214
+ #
215
+ # @param tags [String, Module] the tag(s) to check
216
+ # @return [true, false]
217
+ #
218
+ # @example
219
+ # event.item.tagged?("Setpoint")
220
+ #
221
+ def tagged?(*tags)
222
+ tags.map! do |tag|
223
+ tag.is_a?(Module) ? tag.simple_name : tag
224
+ end
225
+ !(self.tags.to_a & tags).empty?
226
+ end
227
+
228
+ # Return the item's thing if this item is linked with a thing. If an item is linked to more than one thing,
229
+ # this method only returns the first thing.
230
+ #
231
+ # @return [Thing] The thing associated with this item or nil
232
+ def thing
233
+ all_linked_things.first
234
+ end
235
+ alias_method :linked_thing, :thing
236
+
237
+ # Returns all of the item's linked things.
238
+ #
239
+ # @return [Array<Thing>] An array of things or an empty array
240
+ def things
241
+ registry = Things::Links::Provider.registry
242
+ channels = registry.get_bound_channels(name).to_a
243
+ channels.map(&:thing_uid).uniq.map { |tuid| EntityLookup.lookup_thing(tuid) }.compact
244
+ end
245
+ alias_method :all_linked_things, :things
246
+
247
+ # @return [String]
248
+ def inspect
249
+ s = "#<OpenHAB::Core::Items::#{type}Item#{type_details} #{name} #{label.inspect} state=#{raw_state.inspect}"
250
+ s += " category=#{category.inspect}" if category
251
+ s += " tags=#{tags.to_a.inspect}" unless tags.empty?
252
+ s += " groups=#{group_names}" unless group_names.empty?
253
+ meta = metadata.to_h
254
+ s += " metadata=#{meta.inspect}" unless meta.empty?
255
+ "#{s}>"
256
+ end
257
+
258
+ # @return [org.openhab.core.common.registry.Provider]
259
+ def provider
260
+ Provider.registry.provider_for(self)
261
+ end
262
+
263
+ private
264
+
265
+ # Allows sub-classes to append additional details to the type in an inspect string
266
+ # @return [String]
267
+ def type_details; end
268
+ end
269
+ end
270
+ end
271
+ end
272
+
273
+ # @!parse Item = OpenHAB::Core::Items::Item
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generic_item"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Items
8
+ java_import org.openhab.core.library.items.LocationItem
9
+
10
+ #
11
+ # A LocationItem can be used to store GPS related information, addresses
12
+ # etc.
13
+ #
14
+ # This is useful for location awareness related functions
15
+ #
16
+ # @!attribute [r] state
17
+ # @return [PointType, nil]
18
+ #
19
+ # @example Send point commands
20
+ # Location << '30,20' # latitude of 30, longitude of 20
21
+ # Location << '30,20,80' # latitude of 30, longitude of 20, altitude of 80
22
+ # Location << PointType.new('40,20')
23
+ #
24
+ # @example Determine the distance between two locations
25
+ # logger.info "Distance from Location 1 to Location 2: #{Location1.state - Location2.state}"
26
+ # logger.info "Distance from Location 1 to Location 2: #{Location1.state - PointType.new('40,20')}"
27
+ #
28
+ class LocationItem < GenericItem
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # @!parse LocationItem = OpenHAB::Core::Items::LocationItem