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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ # @interface
7
+ java_import org.openhab.core.automation.Module
8
+
9
+ # @!visibility private
10
+ module Module
11
+ # @return [String]
12
+ def inspect
13
+ r = "#<OpenHAB::Core::Rules::#{self.class.simple_name} #{id} (#{type_uid})"
14
+ r += " #{label.inspect}" if label
15
+ r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
16
+ "#{r}>"
17
+ end
18
+
19
+ # @return [String]
20
+ def to_s
21
+ id
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ #
7
+ # Provides rules created in Ruby to openHAB
8
+ #
9
+ class Provider < Core::Provider
10
+ include org.openhab.core.automation.RuleProvider
11
+
12
+ class << self
13
+ #
14
+ # The Rule registry
15
+ #
16
+ # @return [org.openhab.core.automation.RuleRegistry]
17
+ #
18
+ def registry
19
+ $rules
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Rules
8
+ #
9
+ # Provides access to all openHAB {Rule rules}, and acts like an array.
10
+ #
11
+ class Registry
12
+ include LazyArray
13
+ include Singleton
14
+
15
+ #
16
+ # Gets a specific Rule
17
+ #
18
+ # @param [String] uid Rule UID
19
+ # @return [Rule, nil]
20
+ #
21
+ def [](uid)
22
+ $rules.get(uid)
23
+ end
24
+ alias_method :include?, :[]
25
+ alias_method :key?, :[]
26
+ # @deprecated
27
+ alias_method :has_key?, :[]
28
+
29
+ #
30
+ # Explicit conversion to array
31
+ #
32
+ # @return [Array<Rule>]
33
+ #
34
+ def to_a
35
+ $rules.all.to_a
36
+ end
37
+
38
+ #
39
+ # Enter the Rule Builder DSL.
40
+ # @param (see Core::Provider.current)
41
+ # @yield Block executed in the context of a {DSL::Rules::Builder}.
42
+ # @return [Object] The result of the block.
43
+ #
44
+ def build(preferred_provider = nil, &block)
45
+ DSL::Rules::Builder.new(preferred_provider).instance_eval_with_dummy_items(&block)
46
+ end
47
+
48
+ #
49
+ # Remove a Rule.
50
+ #
51
+ # The rule must be a managed thing (typically created by Ruby or in the UI).
52
+ #
53
+ # @param [String, Rule] rule_uid
54
+ # @return [Rule, nil] The removed rule, if found.
55
+ #
56
+ # @example
57
+ # my_rule = rule do
58
+ # every :day
59
+ # run { nil }
60
+ # end
61
+ #
62
+ # rules.remove(my_rule)
63
+ #
64
+ def remove(rule_uid)
65
+ rule_uid = rule_uid.uid if rule_uid.is_a?(Rule)
66
+ provider = Provider.registry.provider_for(rule_uid)
67
+ unless provider.is_a?(org.openhab.core.common.registry.ManagedProvider)
68
+ raise "Cannot remove rule #{rule_uid} from non-managed provider #{provider.inspect}"
69
+ end
70
+
71
+ provider.remove(rule_uid)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Rules
6
+ # @interface
7
+ java_import org.openhab.core.automation.Rule
8
+
9
+ #
10
+ # A {Rule} is a chunk of code that can execute when certain conditions are
11
+ # met, enabling the core dynamic functionality of openHAB.
12
+ #
13
+ module Rule
14
+ #
15
+ # @!method visible?
16
+ # Check if visibility == `VISIBLE`
17
+ # @return [true,false]
18
+ #
19
+
20
+ #
21
+ # @!method hidden?
22
+ # Check if visibility == `HIDDEN`
23
+ # @return [true,false]
24
+ #
25
+
26
+ #
27
+ # @!method expert?
28
+ # Check if visibility == `EXPERT`
29
+ # @return [true,false]
30
+ #
31
+
32
+ #
33
+ # @!method initializing?
34
+ # Check if rule status == `INITIALIZING`
35
+ # @return [true,false]
36
+ #
37
+ #
38
+ # @!method idle?
39
+ # Check if rule status == `IDLE`
40
+ # @return [true,false]
41
+ #
42
+ #
43
+ # @!method running?
44
+ # Check if rule status == `RUNNING`
45
+ # @return [true,false]
46
+ #
47
+
48
+ Visibility.constants.each do |visibility|
49
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
50
+ def #{visibility.to_s.downcase}? # def visibile?
51
+ visibility == Visibility::#{visibility} # visibility == Visibility::VISIBLE
52
+ end # end
53
+ RUBY
54
+ end
55
+
56
+ RuleStatus.constants.each do |status|
57
+ next if status == :UNINITIALIZED
58
+
59
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
60
+ def #{status.to_s.downcase}? # def initializing?
61
+ status == RuleStatus::#{status} # status == RuleStatus::INITIALIZING
62
+ end # end
63
+ RUBY
64
+ end
65
+
66
+ #
67
+ # Check if rule status == `UNINITIALIZED`
68
+ #
69
+ # @return [true,false]
70
+ #
71
+ def uninitialized?
72
+ s = status
73
+ s.nil? || s == RuleStatus::UNINITIALIZED
74
+ end
75
+
76
+ #
77
+ # Enable the Rule
78
+ #
79
+ # @param [true, false] enabled
80
+ # @return [void]
81
+ #
82
+ def enable(enabled: true)
83
+ Rules.manager.set_enabled(uid, enabled)
84
+ end
85
+
86
+ #
87
+ # Disable the Rule
88
+ #
89
+ # @return [void]
90
+ #
91
+ def disable
92
+ enable(enabled: false)
93
+ end
94
+
95
+ #
96
+ # Check if the rule's status detail == `DISABLED`
97
+ #
98
+ # @return [true, false]
99
+ #
100
+ def disabled?
101
+ info = status_info
102
+ info.nil? || info.status_detail == RuleStatusDetail::DISABLED
103
+ end
104
+
105
+ #
106
+ # @!attribute [r] status
107
+ # @return [RuleStatus nil]
108
+ #
109
+ def status
110
+ Rules.manager.get_status(uid)
111
+ end
112
+
113
+ #
114
+ # @!attribute [r] status_info
115
+ # @return [RuleStatusInfo, nil]
116
+ #
117
+ def status_info
118
+ Rules.manager.get_status_info(uid)
119
+ end
120
+
121
+ # @return [String]
122
+ def inspect
123
+ r = "#<OpenHAB::Core::Rules::Rule #{uid}"
124
+ r += " #{name.inspect}" if name
125
+ r += " #{visibility}" unless visible?
126
+ r += " #{status || "<detached>"}"
127
+ r += " (#{status_info.status_detail})" if status_info && status_info.status_detail != RuleStatusDetail::NONE
128
+ r += " tags=#{tags.to_a.inspect}" unless tags.empty?
129
+ r += " configuration=#{configuration.properties.to_h}" if configuration && !configuration.properties.empty?
130
+ "#{r}>"
131
+ end
132
+
133
+ # @return [String]
134
+ def to_s
135
+ uid
136
+ end
137
+
138
+ #
139
+ # Manually trigger the rule
140
+ #
141
+ # @param [Object, nil] event The event to pass to the rule's execution blocks.
142
+ # @return [void]
143
+ #
144
+ def trigger(event = nil)
145
+ Rules.manager.run_now(uid, false, { "event" => event })
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ #
6
+ # Contains the core {Rule} as well as related infrastructure.
7
+ #
8
+ module Rules
9
+ java_import org.openhab.core.automation.RuleStatus,
10
+ org.openhab.core.automation.RuleStatusInfo,
11
+ org.openhab.core.automation.RuleStatusDetail,
12
+ org.openhab.core.automation.Visibility
13
+
14
+ class << self
15
+ #
16
+ # @!attribute [r] manager
17
+ # @return [org.openhab.core.automation.RuleManager] The openHAB rule manager/engine
18
+ #
19
+ def manager
20
+ @manager ||= OSGi.service("org.openhab.core.automation.RuleManager")
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openhab/log/logger'
4
-
5
- # OpenHAB main module
6
3
  module OpenHAB
7
4
  module Core
8
5
  #
@@ -11,24 +8,67 @@ module OpenHAB
11
8
  module ScriptHandling
12
9
  module_function
13
10
 
14
- # Add a callback to be run when the script has been fully loaded
11
+ #
12
+ # Add a block of code to be executed once the rule script has finished loading.
13
+ #
14
+ # This can occur on openHAB start up, when the script is first created, or updated.
15
+ #
16
+ # Multiple hooks can be added by calling {#script_loaded} multiple times.
17
+ # They can be used to perform final initializations.
18
+ #
19
+ # @return [void]
20
+ #
21
+ # @example
22
+ # script_loaded do
23
+ # logger.info 'Hi, this script has just finished loading'
24
+ # end
25
+ #
26
+ # @example
27
+ # script_loaded do
28
+ # logger.info 'I will be called after the script finished loading too'
29
+ # end
30
+ #
15
31
  def script_loaded(&block)
16
32
  ScriptHandlingCallbacks.script_loaded_hooks << block
17
33
  end
18
34
 
19
- # Add a callback to be run when the script is unloaded
20
- def script_unloaded(&block)
21
- ScriptHandlingCallbacks.script_unloaded_hooks << block
35
+ #
36
+ # Add a block of code to be executed when the script is unloaded.
37
+ #
38
+ # This can occur when openHAB shuts down, or when the script is being reloaded.
39
+ #
40
+ # Multiple hooks can be added by calling {#script_unloaded} multiple times.
41
+ # They can be used to perform final cleanup.
42
+ #
43
+ # @param [Integer, nil] priority The order at which the the given hook will be executed.
44
+ # The higher the number, the lower the priority. Higher priority hooks will be executed
45
+ # first, before the lower priority hooks. When nil, the default priority of zero will
46
+ # be used.
47
+ # @return [void]
48
+ #
49
+ # @example
50
+ # script_unloaded do
51
+ # logger.info 'Hi, this script has been unloaded'
52
+ # end
53
+ #
54
+ def script_unloaded(priority: nil, &block)
55
+ ScriptHandlingCallbacks.script_unloaded_hooks[priority || 0] << block
22
56
  end
23
57
  end
24
58
 
25
59
  #
26
60
  # Manages script loading and unloading
27
61
  #
62
+ # @!visibility private
28
63
  module ScriptHandlingCallbacks
29
- include OpenHAB::Log
30
-
31
64
  class << self
65
+ #
66
+ # Has the script completed loading?
67
+ #
68
+ # @!visibility private
69
+ # @return [true, false]
70
+ attr_accessor :script_loaded
71
+
32
72
  #
33
73
  # Return script_loaded_hooks
34
74
  #
@@ -42,35 +82,53 @@ module OpenHAB
42
82
  #
43
83
  # @!visibility private
44
84
  def script_unloaded_hooks
45
- @script_unloaded_hooks ||= []
85
+ @script_unloaded_hooks ||= Hash.new { |hash, key| hash[key] = [] }
46
86
  end
47
87
  end
88
+ self.script_loaded = false
48
89
 
49
90
  #
50
- # Executed when OpenHAB unloads a script file
91
+ # Executed when openHAB unloads a script file
51
92
  #
52
- # @!visibility private
53
- def scriptUnloaded # rubocop:disable Naming/MethodName method name dictated by OpenHAB
54
- logger.trace('Script unloaded')
55
- ScriptHandlingCallbacks.script_unloaded_hooks.each do |hook|
93
+ def scriptUnloaded # rubocop:disable Naming/MethodName method name dictated by openHAB
94
+ logger.trace("Script unloaded")
95
+ ScriptHandlingCallbacks.script_unloaded_hooks.sort_by(&:first).flat_map(&:last).each do |hook|
56
96
  hook.call
57
- rescue StandardError => e
97
+ rescue => e
58
98
  logger.error("Failed to call script_unloaded hook #{hook}: #{e}")
59
99
  end
100
+
101
+ return if ScriptHandlingCallbacks.script_loaded
102
+
103
+ # Make sure we terminate the main thread if it's still set up, in case
104
+ # it's timing out and that's why we're unloading.
105
+ #
106
+ # It would seem simpler to just record Thread.current when this file
107
+ # loads, but if the user is using the autorequire feature of the
108
+ # jrubyscripting addon, this file will load before the main script.
109
+ #
110
+ # Note that Thread.list only includes threads that have had Ruby
111
+ # execute in them, so we don't need to worry about accidentally killing
112
+ # a random Java thread.
113
+ #
114
+ main_thread = Thread.list.find { |t| t != Thread.current && t.name.include?("-safeCall-") }
115
+ main_thread&.raise(Interrupt.new)
60
116
  end
61
117
 
62
118
  #
63
- # Executed when OpenHAB loads a script file
119
+ # Executed when openHAB loads a script file
64
120
  #
65
- # @!visibility private
66
- def scriptLoaded(filename) # rubocop:disable Naming/MethodName method name dictated by OpenHAB
121
+ def scriptLoaded(filename) # rubocop:disable Naming/MethodName method name dictated by openHAB
67
122
  logger.trace("Script loaded: #{filename}")
68
123
  ScriptHandlingCallbacks.script_loaded_hooks.each do |hook|
69
124
  hook.call
70
- rescue StandardError => e
125
+ rescue => e
71
126
  logger.error("Failed to call script_loaded hook #{hook}: #{e}")
72
127
  end
128
+ ScriptHandlingCallbacks.script_loaded = true
73
129
  end
74
130
  end
75
131
  end
76
132
  end
133
+
134
+ singleton_class.include(OpenHAB::Core::ScriptHandlingCallbacks)
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Things
8
+ java_import org.openhab.core.thing.Channel
9
+
10
+ #
11
+ # {Channel} is a part of a {Thing} that represents a functionality of it.
12
+ # Therefore {Item Items} can be linked a to a channel.
13
+ #
14
+ # @!attribute [r] item
15
+ # (see ChannelUID#item)
16
+ #
17
+ # @!attribute [r] items
18
+ # (see ChannelUID#items)
19
+ #
20
+ # @!attribute [r] thing
21
+ # (see ChannelUID#thing)
22
+ #
23
+ # @!attribute [r] uid
24
+ # @return [ChannelUID]
25
+ #
26
+ class Channel
27
+ extend Forwardable
28
+
29
+ delegate %i[item items thing] => :uid
30
+
31
+ # @return [String]
32
+ def inspect
33
+ r = "#<OpenHAB::Core::Things::Channel #{uid}"
34
+ r += " #{label.inspect}" if label
35
+ r += " auto_update_policy=#{auto_update_policy}" if auto_update_policy
36
+ r += " configuration=#{configuration.properties.to_h}" unless configuration.properties.empty?
37
+ r += " properties=#{properties.to_h}" unless properties.empty?
38
+ "#{r}>"
39
+ end
40
+
41
+ # @return [String]
42
+ def to_s
43
+ uid.to_s
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module OpenHAB
6
+ module Core
7
+ module Things
8
+ java_import org.openhab.core.thing.ChannelUID
9
+
10
+ #
11
+ # {ChannelUID} represents a unique identifier for {Channel channels}.
12
+ #
13
+ class ChannelUID
14
+ #
15
+ # @attribute [r] thing
16
+ #
17
+ # Return the thing this channel is associated with.
18
+ #
19
+ # @return [Thing, nil]
20
+ #
21
+ def thing
22
+ EntityLookup.lookup_thing(thing_uid)
23
+ end
24
+
25
+ #
26
+ # @attribute [r] item
27
+ #
28
+ # Return the item if this channel is linked with an item. If a channel is linked to more than one item,
29
+ # this method only returns the first item.
30
+ #
31
+ # @return [Item, nil]
32
+ #
33
+ def item
34
+ items.first
35
+ end
36
+
37
+ #
38
+ # @attribute [r] items
39
+ #
40
+ # Returns all of the channel's linked items.
41
+ #
42
+ # @return [Array<Item>] An array of things or an empty array
43
+ #
44
+ def items
45
+ registry = OSGi.service("org.openhab.core.thing.link.ItemChannelLinkRegistry")
46
+ registry.get_linked_items(self).map { |i| Items::Proxy.new(i) }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Things
6
+ java_import org.openhab.core.thing.link.ItemChannelLink
7
+
8
+ #
9
+ # Represents the link between an {Item} and a {Thing Thing's}
10
+ # {Channel}.
11
+ #
12
+ # @!attribute [r] thing
13
+ # @return [Thing]
14
+ #
15
+ # @!attribute [r] channel_uid
16
+ # @return [ChannelUID]
17
+ #
18
+ class ItemChannelLink
19
+ extend Forwardable
20
+
21
+ def_delegator :linked_uid, :thing
22
+
23
+ # @!attribute [r] item
24
+ # @return [Item]
25
+ def item
26
+ DSL.items[item_name]
27
+ end
28
+
29
+ alias_method :channel_uid, :linked_uid
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module Core
5
+ module Things
6
+ #
7
+ # Contains the link between a {Thing Thing's} {Channel Channels} and {Item Items}.
8
+ #
9
+ module Links
10
+ #
11
+ # Provides {Items::Item items} linked to {Channel channels} in Ruby to openHAB.
12
+ #
13
+ class Provider < Core::Provider
14
+ include org.openhab.core.thing.link.ItemChannelLinkProvider
15
+
16
+ class << self
17
+ #
18
+ # The ItemChannelLink registry
19
+ #
20
+ # @return [org.openhab.core.thing.link.ItemChannelLinkRegistry]
21
+ #
22
+ def registry
23
+ @registry ||= OSGi.service("org.openhab.core.thing.link.ItemChannelLinkRegistry")
24
+ end
25
+
26
+ # @!visibility private
27
+ def link(item, channel, config = {})
28
+ config = org.openhab.core.config.core.Configuration.new(config.transform_keys(&:to_s))
29
+ channel = ChannelUID.new(channel) if channel.is_a?(String)
30
+ channel = channel.uid if channel.is_a?(Channel)
31
+ link = org.openhab.core.thing.link.ItemChannelLink.new(item.name, channel, config)
32
+
33
+ current.add(link)
34
+ end
35
+ end
36
+
37
+ #
38
+ # Removes all links to a given item.
39
+ #
40
+ # @param [String] item_name
41
+ # @return [Integer] how many links were removed
42
+ #
43
+ def remove_links_for_item(item_name)
44
+ count = 0
45
+ @elements.delete_if do |_k, v|
46
+ next unless v.item_name == item_name
47
+
48
+ count += 1
49
+ notify_listeners_about_removed_element(v)
50
+ true
51
+ end
52
+ count
53
+ end
54
+ alias_method :removeLinksForItem, :remove_links_for_item
55
+
56
+ #
57
+ # Removes all links to a given thing.
58
+ #
59
+ # @param [ThingUID] thing_uid
60
+ # @return [Integer] how many links were removed
61
+ #
62
+ def remove_links_for_thing(thing_uid)
63
+ count = 0
64
+ @elements.delete_if do |_k, v|
65
+ next unless v.linked_uid.thing_uid == thing_uid
66
+
67
+ count += 1
68
+ notify_listeners_about_removed_element(v)
69
+ true
70
+ end
71
+ count
72
+ end
73
+ alias_method :removeLinksForThing, :remove_links_for_thing
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end