openhab-scripting 4.47.0 → 5.0.1

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 (282) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +54 -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/item_state_updated_event.rb +22 -0
  23. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  24. data/lib/openhab/core/events.rb +10 -0
  25. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  26. data/lib/openhab/core/items/color_item.rb +52 -0
  27. data/lib/openhab/core/items/contact_item.rb +52 -0
  28. data/lib/openhab/core/items/date_time_item.rb +59 -0
  29. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  30. data/lib/openhab/core/items/generic_item.rb +292 -0
  31. data/lib/openhab/core/items/group_item.rb +176 -0
  32. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  33. data/lib/openhab/core/items/item.rb +273 -0
  34. data/lib/openhab/core/items/location_item.rb +34 -0
  35. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  36. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  37. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  38. data/lib/openhab/core/items/metadata.rb +11 -0
  39. data/lib/openhab/core/items/number_item.rb +62 -0
  40. data/lib/openhab/core/items/numeric_item.rb +22 -0
  41. data/lib/openhab/core/items/persistence.rb +416 -0
  42. data/lib/openhab/core/items/player_item.rb +66 -0
  43. data/lib/openhab/core/items/provider.rb +44 -0
  44. data/lib/openhab/core/items/proxy.rb +136 -0
  45. data/lib/openhab/core/items/registry.rb +86 -0
  46. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  47. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  48. data/lib/openhab/core/items/semantics.rb +473 -0
  49. data/lib/openhab/core/items/state_storage.rb +53 -0
  50. data/lib/openhab/core/items/string_item.rb +28 -0
  51. data/lib/openhab/core/items/switch_item.rb +78 -0
  52. data/lib/openhab/core/items.rb +108 -0
  53. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  54. data/lib/openhab/core/profile_factory.rb +132 -0
  55. data/lib/openhab/core/provider.rb +230 -0
  56. data/lib/openhab/core/proxy.rb +130 -0
  57. data/lib/openhab/core/registry.rb +40 -0
  58. data/lib/openhab/core/rules/module.rb +26 -0
  59. data/lib/openhab/core/rules/provider.rb +25 -0
  60. data/lib/openhab/core/rules/registry.rb +94 -0
  61. data/lib/openhab/core/rules/rule.rb +174 -0
  62. data/lib/openhab/core/rules.rb +25 -0
  63. data/lib/openhab/core/script_handling.rb +78 -20
  64. data/lib/openhab/core/things/channel.rb +48 -0
  65. data/lib/openhab/core/things/channel_uid.rb +51 -0
  66. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  67. data/lib/openhab/core/things/links/provider.rb +78 -0
  68. data/lib/openhab/core/things/profile_callback.rb +52 -0
  69. data/lib/openhab/core/things/provider.rb +29 -0
  70. data/lib/openhab/core/things/proxy.rb +87 -0
  71. data/lib/openhab/core/things/registry.rb +73 -0
  72. data/lib/openhab/core/things/thing.rb +194 -0
  73. data/lib/openhab/core/things.rb +22 -0
  74. data/lib/openhab/core/timer.rb +148 -0
  75. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  76. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  77. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  78. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  79. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  80. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  81. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  82. data/lib/openhab/core/types/on_off_type.rb +46 -0
  83. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  84. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  85. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  86. data/lib/openhab/core/types/point_type.rb +117 -0
  87. data/lib/openhab/core/types/quantity_type.rb +325 -0
  88. data/lib/openhab/core/types/raw_type.rb +26 -0
  89. data/lib/openhab/core/types/refresh_type.rb +27 -0
  90. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  91. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  92. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  93. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  94. data/lib/openhab/core/types/un_def_type.rb +38 -0
  95. data/lib/openhab/core/types/up_down_type.rb +50 -0
  96. data/lib/openhab/core/types.rb +82 -0
  97. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  98. data/lib/openhab/core/value_cache.rb +188 -0
  99. data/lib/openhab/core.rb +98 -0
  100. data/lib/openhab/core_ext/between.rb +32 -0
  101. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  102. data/lib/openhab/core_ext/java/class.rb +34 -0
  103. data/lib/openhab/core_ext/java/duration.rb +142 -0
  104. data/lib/openhab/core_ext/java/list.rb +436 -0
  105. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  106. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  107. data/lib/openhab/core_ext/java/map.rb +66 -0
  108. data/lib/openhab/core_ext/java/month.rb +71 -0
  109. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  110. data/lib/openhab/core_ext/java/period.rb +103 -0
  111. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  112. data/lib/openhab/core_ext/java/time.rb +62 -0
  113. data/lib/openhab/core_ext/java/unit.rb +15 -0
  114. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  115. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  116. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  117. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  118. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  119. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  120. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  121. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  122. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  123. data/lib/openhab/core_ext.rb +18 -0
  124. data/lib/openhab/dsl/debouncer.rb +259 -0
  125. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  126. data/lib/openhab/dsl/events.rb +9 -0
  127. data/lib/openhab/dsl/gems.rb +1 -1
  128. data/lib/openhab/dsl/items/builder.rb +578 -0
  129. data/lib/openhab/dsl/items/ensure.rb +73 -82
  130. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  131. data/lib/openhab/dsl/rules/automation_rule.rb +129 -116
  132. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  133. data/lib/openhab/dsl/rules/guard.rb +51 -114
  134. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  135. data/lib/openhab/dsl/rules/property.rb +48 -75
  136. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  137. data/lib/openhab/dsl/rules/terse.rb +58 -14
  138. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  139. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  140. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  141. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  142. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  143. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  144. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  145. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  146. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  147. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  148. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  149. data/lib/openhab/dsl/rules.rb +8 -0
  150. data/lib/openhab/dsl/things/builder.rb +299 -0
  151. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  152. data/lib/openhab/dsl/timer_manager.rb +204 -0
  153. data/lib/openhab/dsl/version.rb +9 -0
  154. data/lib/openhab/dsl.rb +979 -0
  155. data/lib/openhab/log.rb +355 -0
  156. data/lib/openhab/osgi.rb +68 -0
  157. data/lib/openhab/rspec/configuration.rb +56 -0
  158. data/lib/openhab/rspec/example_group.rb +132 -0
  159. data/lib/openhab/rspec/helpers.rb +458 -0
  160. data/lib/openhab/rspec/hooks.rb +113 -0
  161. data/lib/openhab/rspec/jruby.rb +46 -0
  162. data/lib/openhab/rspec/karaf.rb +851 -0
  163. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  164. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  165. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  166. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  167. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  168. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  169. data/lib/openhab/rspec/mocks/space.rb +23 -0
  170. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  171. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  172. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  173. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  174. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  175. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  176. data/lib/openhab/rspec/shell.rb +31 -0
  177. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  178. data/lib/openhab/rspec.rb +26 -0
  179. data/lib/openhab/yard/base_helper.rb +19 -0
  180. data/lib/openhab/yard/cli/stats.rb +23 -0
  181. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  182. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  183. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  184. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  186. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  187. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  188. data/lib/openhab/yard/coderay.rb +17 -0
  189. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  190. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  192. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  193. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  194. data/lib/openhab/yard/html_helper.rb +78 -0
  195. data/lib/openhab/yard/markdown_helper.rb +148 -0
  196. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  197. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  198. data/lib/openhab/yard/tags/library.rb +3 -0
  199. data/lib/openhab/yard.rb +38 -0
  200. metadata +476 -106
  201. data/lib/openhab/core/item_proxy.rb +0 -29
  202. data/lib/openhab/core/load_path.rb +0 -19
  203. data/lib/openhab/core/openhab_setup.rb +0 -29
  204. data/lib/openhab/core/osgi.rb +0 -58
  205. data/lib/openhab/core/services.rb +0 -24
  206. data/lib/openhab/dsl/actions.rb +0 -114
  207. data/lib/openhab/dsl/between.rb +0 -25
  208. data/lib/openhab/dsl/channel.rb +0 -43
  209. data/lib/openhab/dsl/dsl.rb +0 -59
  210. data/lib/openhab/dsl/group.rb +0 -54
  211. data/lib/openhab/dsl/imports.rb +0 -21
  212. data/lib/openhab/dsl/items/color_item.rb +0 -76
  213. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  214. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  215. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  216. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  217. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  218. data/lib/openhab/dsl/items/group_item.rb +0 -127
  219. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  220. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  221. data/lib/openhab/dsl/items/items.rb +0 -109
  222. data/lib/openhab/dsl/items/location_item.rb +0 -59
  223. data/lib/openhab/dsl/items/metadata.rb +0 -326
  224. data/lib/openhab/dsl/items/number_item.rb +0 -17
  225. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  226. data/lib/openhab/dsl/items/persistence.rb +0 -307
  227. data/lib/openhab/dsl/items/player_item.rb +0 -58
  228. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  229. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  230. data/lib/openhab/dsl/items/semantics.rb +0 -227
  231. data/lib/openhab/dsl/items/string_item.rb +0 -51
  232. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  233. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  234. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  235. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  236. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  237. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  238. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  239. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  240. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  241. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  242. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  243. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  244. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  245. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  246. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  247. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  248. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  249. data/lib/openhab/dsl/openhab.rb +0 -30
  250. data/lib/openhab/dsl/persistence.rb +0 -27
  251. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  252. data/lib/openhab/dsl/rules/rule.rb +0 -160
  253. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  254. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  255. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  256. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  257. data/lib/openhab/dsl/states.rb +0 -89
  258. data/lib/openhab/dsl/things.rb +0 -147
  259. data/lib/openhab/dsl/time/month_day.rb +0 -180
  260. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  261. data/lib/openhab/dsl/timers/manager.rb +0 -119
  262. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  263. data/lib/openhab/dsl/timers/timer.rb +0 -132
  264. data/lib/openhab/dsl/timers.rb +0 -77
  265. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  266. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  267. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  268. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  269. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  270. data/lib/openhab/dsl/types/point_type.rb +0 -180
  271. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  272. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  273. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  274. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  275. data/lib/openhab/dsl/types/types.rb +0 -83
  276. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  277. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  278. data/lib/openhab/dsl/units.rb +0 -45
  279. data/lib/openhab/log/configuration.rb +0 -21
  280. data/lib/openhab/log/logger.rb +0 -282
  281. data/lib/openhab/version.rb +0 -9
  282. data/lib/openhab.rb +0 -38
@@ -1,28 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require 'openhab/core/thread_local'
5
- require 'openhab/log/logger'
6
- require 'openhab/dsl/between'
7
-
8
- require_relative 'item_event'
9
-
10
3
  module OpenHAB
11
4
  module DSL
12
- #
13
- # Creates and manages OpenHAB Rules
14
- #
15
5
  module Rules
16
6
  #
17
- # JRuby extension to OpenHAB Rule
7
+ # OpenHAB rules engine object
18
8
  #
19
- # rubocop: disable Metrics/ClassLength
20
- # Disabled because this class has a single responsibility, there does not appear a logical
21
- # way of breaking it up into multiple classes
22
- class AutomationRule < Java::OrgOpenhabCoreAutomationModuleScriptRulesupportSharedSimple::SimpleRule
23
- include OpenHAB::Log
24
- include OpenHAB::Core::ThreadLocal
25
- include OpenHAB::DSL::Between
9
+ # @!visibility private
10
+ class AutomationRule < org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule
11
+ # @!visibility private
12
+ INPUT_KEY_PATTERN = /^[a-z_]+[a-zA-Z0-9_]*$/.freeze
13
+
14
+ class << self
15
+ #
16
+ # Caches dynamically generated Struct classes so that they don't have
17
+ # to be re-generated for every event, blowing the method cache.
18
+ #
19
+ # @!visibility private
20
+ # @return [java.util.Map<Array<Symbol>, Class>]
21
+ attr_reader :event_structs
22
+ end
23
+ @event_structs = java.util.concurrent.ConcurrentHashMap.new
26
24
 
27
25
  field_writer :uid
28
26
 
@@ -32,63 +30,96 @@ module OpenHAB
32
30
  # @param [Config] config Rule configuration
33
31
  #
34
32
  # Constructor sets a number of variables, no further decomposition necessary
35
- def initialize(config:) # rubocop:disable Metrics
36
- # Metrics disabled because only setters are called or defaults set.
33
+ #
34
+ def initialize(config)
37
35
  super()
38
36
  set_name(config.name)
39
37
  set_description(config.description)
40
- set_tags(to_string_set(config.tags))
38
+ tags = DSL::Items::ItemBuilder.normalize_tags(*config.tags)
39
+ set_tags(tags.to_set)
41
40
  set_triggers(config.triggers)
42
41
  self.uid = config.uid
43
42
  @run_context = config.caller
44
43
  @run_queue = config.run
45
44
  @guard = config.guard
46
- # Convert between to correct range or nil if not set
47
- between = config.between&.then { between(config.between) }
48
- @between = between
45
+ @between = config.between && DSL.between(config.between)
49
46
  @trigger_conditions = config.trigger_conditions
47
+ @trigger_conditions.each_value do |condition|
48
+ condition.rule = self if condition.respond_to?(:rule=)
49
+ end
50
50
  @attachments = config.attachments
51
+ @thread_locals = ThreadLocal.persist
52
+ @cleanup_hooks = Set.new
53
+ @listener = nil
54
+ debounce_settings = config.debounce_settings || { for: nil }
55
+ @debouncer = Debouncer.new(**debounce_settings)
51
56
  end
52
57
 
53
58
  #
54
59
  # Execute the rule
55
60
  #
56
- # @param [Map] mod map provided by OpenHAB rules engine
57
- # @param [Map] inputs map provided by OpenHAB rules engine containing event and other information
58
- #
61
+ # @param [java.util.Map] mod map provided by openHAB rules engine
62
+ # @param [java.util.Map] inputs map provided by openHAB rules engine containing event and other information
59
63
  #
60
64
  def execute(mod = nil, inputs = nil)
61
- OpenHAB::DSL.import_presets
62
- thread_local(RULE_NAME: name) do
63
- logger.trace { "Execute called with mod (#{mod&.to_string}) and inputs (#{inputs.inspect})" }
64
- logger.trace { "Event details #{inputs['event'].inspect}" } if inputs&.key?('event')
65
+ execute!(mod, inputs)
66
+ end
67
+
68
+ # @!visibility private
69
+ def on_removal(listener)
70
+ @cleanup_hooks << listener
71
+ listen_for_removal unless @listener
72
+ end
73
+
74
+ private
75
+
76
+ # This method gets called in rspec's SuspendRules as well
77
+ def execute!(mod, inputs)
78
+ ThreadLocal.thread_local(**@thread_locals) do
79
+ if logger.trace?
80
+ logger.trace("Execute called with mod (#{mod&.to_string}) and inputs (#{inputs.inspect})")
81
+ logger.trace("Event details #{inputs["event"].inspect}") if inputs&.key?("event")
82
+ end
65
83
  trigger_conditions(inputs).process(mod: mod, inputs: inputs) do
66
- process_queue(create_queue(inputs), mod, inputs)
84
+ event = extract_event(inputs)
85
+ @debouncer.call { process_queue(create_queue(event), mod, event) }
67
86
  end
87
+ rescue Exception => e
88
+ raise if defined?(::RSpec) && ::RSpec.current_example.example_group.propagate_exceptions?
89
+
90
+ @run_context.send(:logger).log_exception(e)
68
91
  end
69
92
  end
70
93
 
71
- #
72
- # Cleanup any resources associated with automation rule
73
- #
74
94
  def cleanup
75
- # No cleanup is necessary right now, trigger delays are tracked and cancelled by timers library
95
+ @cleanup_hooks.each(&:cleanup)
76
96
  end
77
97
 
78
- private
98
+ def listen_for_removal
99
+ @listener ||= org.openhab.core.common.registry.RegistryChangeListener.impl do |method, element|
100
+ next unless method == :removed
101
+
102
+ logger.trace("Rule #{element.inspect} removed from registry")
103
+ next unless element.uid == uid
104
+
105
+ cleanup
106
+ $rules.remove_registry_change_listener(@listener)
107
+ end
108
+ $rules.add_registry_change_listener(@listener)
109
+ end
79
110
 
80
111
  #
81
112
  # Create the run queue based on guards
82
113
  #
83
- # @param [Map] inputs rule inputs
114
+ # @param [Map] event Event object
84
115
  # @return [Queue] <description>
85
116
  #
86
- def create_queue(inputs)
87
- case check_guards(event: extract_event(inputs))
117
+ def create_queue(event)
118
+ case check_guards(event: event)
88
119
  when true
89
- @run_queue.dup.grep_v(RuleConfig::Otherwise)
120
+ @run_queue.dup.grep_v(BuilderDSL::Otherwise)
90
121
  when false
91
- @run_queue.dup.grep(RuleConfig::Otherwise)
122
+ @run_queue.dup.grep(BuilderDSL::Otherwise)
92
123
  end
93
124
  end
94
125
 
@@ -101,12 +132,30 @@ module OpenHAB
101
132
  # @return [Object] event object
102
133
  #
103
134
  def extract_event(inputs)
104
- event = inputs&.dig('event')
105
- unless event
106
- event = Struct.new(:event, :attachment, :command).new
107
- event.command = inputs&.dig('command')
135
+ attachment = @attachments[trigger_id(inputs)]
136
+ if inputs&.key?("event")
137
+ event = inputs["event"]
138
+ unless event
139
+ if attachment
140
+ logger.warn("Unable to attach #{attachment.inspect} to event " \
141
+ "object for rule #{uid} since the event is nil.")
142
+ end
143
+ return nil
144
+ end
145
+
146
+ event.attachment = attachment
147
+ return event
148
+ end
149
+
150
+ inputs = inputs.to_h
151
+ .select { |key, _value| key != "module" && INPUT_KEY_PATTERN.match?(key) }
152
+ .transform_keys(&:to_sym)
153
+ inputs[:attachment] = attachment
154
+ keys = inputs.keys.sort
155
+ struct_class = self.class.event_structs.compute_if_absent(keys) do
156
+ Struct.new(*keys, keyword_init: true)
108
157
  end
109
- add_attachment(event, inputs)
158
+ struct_class.new(**inputs)
110
159
  end
111
160
 
112
161
  #
@@ -115,85 +164,61 @@ module OpenHAB
115
164
  # @return [Hash] Input hash potentially containing trigger id
116
165
  #
117
166
  def trigger_id(inputs)
118
- inputs&.dig('module')
167
+ inputs&.dig("module")
119
168
  end
120
169
 
121
170
  #
122
171
  # Returns trigger conditions from inputs if it exists
123
172
  #
124
- # @param [Map] inputs map from OpenHAB containing UID
173
+ # @param [java.util.Map] inputs map from openHAB containing UID
125
174
  #
126
175
  # @return [Array] Array of trigger conditions that match rule UID
127
176
  #
128
177
  def trigger_conditions(inputs)
129
- # Parse this to get the trigger UID:
130
- # ["72698819-83cb-498a-8e61-5aab8b812623.event", "oldState", "module", \
131
- # "72698819-83cb-498a-8e61-5aab8b812623.oldState", "event", "newState",\
132
- # "72698819-83cb-498a-8e61-5aab8b812623.newState"]
133
178
  @trigger_conditions[trigger_id(inputs)]
134
179
  end
135
180
 
136
- # If an attachment exists for the trigger for this event add it to the event object
137
- # @param [Object] event Event
138
- # @param [Hash] inputs Inputs into event
139
- # @return [Object] Event with attachment added
140
- #
141
- def add_attachment(event, inputs)
142
- attachment = @attachments[trigger_id(inputs)]
143
- return event unless attachment
144
-
145
- event.attachment = attachment
146
- event
147
- end
148
-
149
181
  #
150
182
  # Check if any guards prevent execution
151
183
  #
152
- # @param [Map] event OpenHAB rule trigger event
184
+ # @param [Object] event openHAB rule trigger event
153
185
  #
154
- # @return [Boolean] True if guards says rule should execute, false otherwise
186
+ # @return [true,false] True if guards says rule should execute, false otherwise
155
187
  #
156
- # rubocop:disable Metrics/MethodLength
157
188
  # Loggging inflates method length
158
189
  def check_guards(event:)
159
- if @guard.should_run? event
160
- return true if @between.nil?
190
+ return true if @guard.nil?
161
191
 
192
+ unless @between.nil?
162
193
  now = Time.now
163
- return true if @between.cover? now
164
-
165
- logger.trace("Skipped execution of rule '#{name}' because the current time #{now} " \
166
- "is not between #{@between.begin} and #{@between.end}")
167
- else
168
- logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
194
+ unless @between.cover?(now)
195
+ logger.trace("Skipped execution of rule '#{name}' because the current time #{now} " \
196
+ "is not between #{@between.begin} and #{@between.end}")
197
+ return false
198
+ end
169
199
  end
200
+
201
+ return true if @guard.should_run?(event)
202
+
203
+ logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
170
204
  false
171
- rescue StandardError => e
172
- logger.log_exception(e, name)
173
205
  end
174
- # rubocop:enable Metrics/MethodLength
175
206
 
176
207
  #
177
208
  # Process the run queue
178
209
  #
179
210
  # @param [Array] run_queue array of procs of various types to execute
180
- # @param [Map] mod OpenHAB map object describing rule trigger
181
- # @param [Map] inputs OpenHAB map object describing rule trigge
182
- #
211
+ # @param [java.util/Map] mod openHAB map object describing rule trigger
212
+ # @param [Object] event openHAB map object describing rule trigger
183
213
  #
184
- # No logical way to break this method up
185
- def process_queue(run_queue, mod, inputs)
186
- event = extract_event(inputs)
187
-
214
+ def process_queue(run_queue, mod, event)
188
215
  while (task = run_queue.shift)
189
- if task.is_a? RuleConfig::Delay
190
- process_delay_task(inputs, mod, run_queue, task)
216
+ if task.is_a?(BuilderDSL::Delay)
217
+ process_delay_task(event, mod, run_queue, task)
191
218
  else
192
219
  process_task(event, task)
193
220
  end
194
221
  end
195
- rescue StandardError => e
196
- logger.log_exception(e, name)
197
222
  end
198
223
 
199
224
  #
@@ -203,11 +228,12 @@ module OpenHAB
203
228
  # @param [Task] task task containing otherwise block to execute
204
229
  #
205
230
  def process_task(event, task)
206
- thread_local(RULE_NAME: name) do
231
+ ThreadLocal.thread_local(**@thread_locals) do
207
232
  case task
208
- when RuleConfig::Run then process_run_task(event, task)
209
- when RuleConfig::Trigger then process_trigger_task(event, task)
210
- when RuleConfig::Otherwise then process_otherwise_task(event, task)
233
+ when BuilderDSL::Run then process_run_task(event, task)
234
+ when BuilderDSL::Script then process_script_task(task)
235
+ when BuilderDSL::Trigger then process_trigger_task(event, task)
236
+ when BuilderDSL::Otherwise then process_otherwise_task(event, task)
211
237
  end
212
238
  end
213
239
  end
@@ -227,15 +253,15 @@ module OpenHAB
227
253
  #
228
254
  # Process delay task
229
255
  #
230
- # @param [Map] inputs Rule trigger inputs
256
+ # @param [Map] event Rule trigger event
231
257
  # @param [Map] mod Rule modes
232
258
  # @param [Queue] run_queue Queue of tasks for this rule
233
259
  # @param [Delay] task to process
234
260
  #
235
261
  #
236
- def process_delay_task(inputs, mod, run_queue, task)
262
+ def process_delay_task(event, mod, run_queue, task)
237
263
  remaining_queue = run_queue.slice!(0, run_queue.length)
238
- after(task.duration) { process_queue(remaining_queue, mod, inputs) }
264
+ DSL.after(task.duration) { process_queue(remaining_queue, mod, event) }
239
265
  end
240
266
 
241
267
  #
@@ -265,25 +291,13 @@ module OpenHAB
265
291
  end
266
292
 
267
293
  #
268
- # Convert the given array to a set of strings.
269
- # Convert Semantics classes to their simple name
294
+ # Process a script task
270
295
  #
271
- # @example
272
- # to_string_set("tag1", Semantics::LivingRoom)
296
+ # @param [Script] task to execute
273
297
  #
274
- # @param [Array] *tags An array of strings or Semantics classes
275
- #
276
- # @return [Set] A set of strings
277
- #
278
- def to_string_set(*tags)
279
- tags = tags.flatten.map do |tag|
280
- if tag.respond_to?(:java_class) && tag < org.openhab.core.semantics.Tag
281
- tag.java_class.simple_name
282
- else
283
- tag.to_s
284
- end
285
- end
286
- Set.new(tags)
298
+ def process_script_task(task)
299
+ logger.trace { "Executing script '#{name}' run block" }
300
+ @run_context.instance_exec(&task.block)
287
301
  end
288
302
 
289
303
  #
@@ -317,4 +331,3 @@ module OpenHAB
317
331
  end
318
332
  end
319
333
  end
320
- # rubocop: enable Metrics/ClassLength