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
@@ -1,34 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openhab/log/logger'
4
-
5
3
  module OpenHAB
6
4
  module DSL
7
5
  module Rules
8
6
  module Triggers
9
- #
10
- # Module for conditions for triggers
11
- #
7
+ # @!visibility private
12
8
  module Conditions
13
- include OpenHAB::Log
14
- #
15
- # this is a no-op condition which simply executes the provided block
16
- #
17
-
18
9
  #
19
10
  # Struct capturing data necessary for a conditional trigger
20
11
  #
21
- # TriggerDelay = Struct.new(:to, :from, :duration, :timer, :tracking_to, keyword_init: true) do
22
- # def timer_active?
23
- # timer&.is_active
24
- # end
25
- # end
26
12
  class Duration
13
+ attr_accessor :rule
14
+
27
15
  #
28
16
  # Create a new duration condition
29
17
  # @param [Object] to optional condition on to state
30
18
  # @param [Object] from optional condition on from state
31
- # @param [Duration] duration to state must stay at specific value before triggering
19
+ # @param [java.time.temporal.TemporalAmount] duration to state must stay at specific value before triggering
32
20
  #
33
21
  def initialize(to:, from:, duration:)
34
22
  to = Conditions::Proc.from_value(to)
@@ -44,24 +32,34 @@ module OpenHAB
44
32
  # @param [Hash] inputs inputs from trigger
45
33
  #
46
34
  def process(mod:, inputs:, &block)
47
- process_trigger_delay(mod, inputs, &block)
35
+ if @timer&.active?
36
+ process_active_timer(inputs, mod, &block)
37
+ elsif check_trigger_guards(inputs)
38
+ logger.trace("Trigger Guards Matched for #{self}, delaying rule execution")
39
+ # Add timer and attach timer to delay object, and also state being tracked to so
40
+ # timer can be cancelled if state changes
41
+ # Also another timer should not be created if changed to same value again but instead rescheduled
42
+ create_trigger_delay_timer(inputs, mod, &block)
43
+ else
44
+ logger.trace("Trigger Guards did not match for #{self}, ignoring trigger.")
45
+ end
48
46
  end
49
47
 
50
- private
51
-
48
+ # Cleanup any resources from the condition
52
49
  #
53
- # Checks if there is an active timer
54
- # @return [true, false] true if the timer exists and is active, false otherwise
55
- def timer_active?
56
- @timer&.is_active
50
+ # Cancels the timer, if it's active
51
+ def cleanup
52
+ @timer&.cancel
57
53
  end
58
54
 
55
+ private
56
+
59
57
  #
60
58
  # Check if trigger guards prevent rule execution
61
59
  #
62
- # @param [Map] inputs OpenHAB map object describing rule trigger
60
+ # @param [java.util.Map] inputs map object describing rule trigger
63
61
  #
64
- # @return [Boolean] True if the rule should execute, false if trigger guard prevents execution
62
+ # @return [true,false] True if the rule should execute, false if trigger guard prevents execution
65
63
  #
66
64
  def check_trigger_guards(inputs)
67
65
  new_state, old_state = retrieve_states(inputs)
@@ -72,49 +70,17 @@ module OpenHAB
72
70
  # Rerieve the newState and oldState, alternatively newStatus and oldStatus
73
71
  # from the input map
74
72
  #
75
- # @param [Map] inputs OpenHAB map object describing rule trigger
73
+ # @param [java.util.Map] inputs map object describing rule trigger
76
74
  #
77
75
  # @return [Array] An array of the values for [newState, oldState] or [newStatus, oldStatus]
78
76
  #
79
77
  def retrieve_states(inputs)
80
- new_state = inputs['newState'] || thing_status_to_sym(inputs['newStatus'])
81
- old_state = inputs['oldState'] || thing_status_to_sym(inputs['oldStatus'])
78
+ new_state = inputs["newState"] || inputs["newStatus"]&.to_s&.downcase&.to_sym
79
+ old_state = inputs["oldState"] || inputs["oldStatus"]&.to_s&.downcase&.to_sym
82
80
 
83
81
  [new_state, old_state]
84
82
  end
85
83
 
86
- #
87
- # Converts a ThingStatus object to a ruby Symbol
88
- #
89
- # @param [org.openhab.core.thing.ThingStatus] status A ThingStatus instance
90
- #
91
- # @return [Symbol] A corresponding symbol, in lower case
92
- #
93
- def thing_status_to_sym(status)
94
- status&.to_s&.downcase&.to_sym
95
- end
96
-
97
- #
98
- # Process any matching trigger delays
99
- #
100
- # @param [Map] mod OpenHAB map object describing rule trigger
101
- # @param [Map] inputs OpenHAB map object describing rule trigger
102
- #
103
- #
104
- def process_trigger_delay(mod, inputs, &block)
105
- if timer_active?
106
- process_active_timer(inputs, mod, &block)
107
- elsif check_trigger_guards(inputs)
108
- logger.trace("Trigger Guards Matched for #{self}, delaying rule execution")
109
- # Add timer and attach timer to delay object, and also state being tracked to so
110
- # timer can be cancelled if state changes
111
- # Also another timer should not be created if changed to same value again but instead rescheduled
112
- create_trigger_delay_timer(inputs, mod, &block)
113
- else
114
- logger.trace("Trigger Guards did not match for #{self}, ignoring trigger.")
115
- end
116
- end
117
-
118
84
  #
119
85
  # Creates a timer for trigger delays
120
86
  #
@@ -124,12 +90,13 @@ module OpenHAB
124
90
  #
125
91
  def create_trigger_delay_timer(inputs, _mod)
126
92
  logger.trace("Creating timer for trigger delay #{self}")
127
- @timer = after(@duration) do
93
+ @timer = DSL.after(@duration) do
128
94
  logger.trace("Delay Complete for #{self}, executing rule")
129
95
  @timer = nil
130
96
  yield
131
97
  end
132
- @tracking_to, = retrieve_states(inputs)
98
+ rule.on_removal(self)
99
+ _, @tracking_from = retrieve_states(inputs)
133
100
  end
134
101
 
135
102
  #
@@ -138,17 +105,15 @@ module OpenHAB
138
105
  # @param [Hash] inputs rule trigger inputs
139
106
  # @param [Hash] mod rule trigger mods
140
107
  #
141
- #
142
108
  def process_active_timer(inputs, mod, &block)
143
- state, = retrieve_states(inputs)
144
- if state == @tracking_to
145
- logger.trace("Item changed to #{state} for #{self}, rescheduling timer.")
146
- @timer.reschedule(ZonedDateTime.now.plus(@duration))
109
+ new_state, old_state = retrieve_states(inputs)
110
+ if new_state != @tracking_from && @conditions.check_to(inputs: {})
111
+ logger.trace("Item changed from #{old_state} to #{new_state} for #{self}, keep waiting.")
147
112
  else
148
- logger.trace("Item changed to #{state} for #{self}, canceling timer.")
113
+ logger.trace("Item changed from #{old_state} to #{new_state} for #{self}, canceling timer.")
149
114
  @timer.cancel
150
115
  # Reprocess trigger delay after canceling to track new state (if guards matched, etc)
151
- process_trigger_delay(mod, inputs, &block)
116
+ process(mod: mod, inputs: inputs, &block)
152
117
  end
153
118
  end
154
119
  end
@@ -1,25 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'openhab/log/logger'
4
-
5
3
  module OpenHAB
6
4
  module DSL
7
5
  module Rules
8
6
  module Triggers
9
- #
10
- # Module for conditions for triggers
11
- #
7
+ # @!visibility private
12
8
  module Conditions
13
- include OpenHAB::Log
14
-
15
9
  #
16
10
  # This creates trigger conditions that work on procs
17
11
  # @param [Proc] from Proc
18
12
  # @param [Proc] to Proc
19
13
  #
20
14
  class Proc
21
- include OpenHAB::Log
22
-
23
15
  #
24
16
  # Converts supplied ranges to procs that check range
25
17
  # @param [Array] ranges objects to convert to range proc if they are ranges
@@ -63,8 +55,8 @@ module OpenHAB
63
55
  def self.from_value(value)
64
56
  logger.trace("Creating proc for Value(#{value})")
65
57
  return value if value.nil?
66
- return value if value.is_a? ::Proc
67
- return range_proc(value) if value.is_a? Range
58
+ return value if value.is_a?(::Proc)
59
+ return range_proc(value) if value.is_a?(Range)
68
60
 
69
61
  equality_proc(value)
70
62
  end
@@ -98,7 +90,7 @@ module OpenHAB
98
90
  # @return [true/false] depending on if from is set and matches supplied conditions
99
91
  #
100
92
  def check_command(inputs: nil)
101
- command = input_state(inputs, 'command')
93
+ command = input_state(inputs, "command")
102
94
  logger.trace "Checking command(#{@command}) against command(#{command})"
103
95
  check_proc(proc: @command, value: command)
104
96
  end
@@ -110,7 +102,7 @@ module OpenHAB
110
102
  # @return [true/false] depending on if from is set and matches supplied conditions
111
103
  #
112
104
  def check_from(inputs: nil, state: nil)
113
- state ||= input_state(inputs, 'oldState')
105
+ state ||= input_state(inputs, "oldState")
114
106
  logger.trace "Checking from(#{@from}) against state(#{state})"
115
107
  check_proc(proc: @from, value: state)
116
108
  end
@@ -122,7 +114,7 @@ module OpenHAB
122
114
  # @return [true/false] depending on if from is set and matches supplied conditions
123
115
  #
124
116
  def check_to(inputs: nil, state: nil)
125
- state ||= input_state(inputs, 'newState', 'state')
117
+ state ||= input_state(inputs, "newState", "state")
126
118
  logger.trace "Checking to(#{@to}) against state(#{state})"
127
119
  check_proc(proc: @to, value: state)
128
120
  end
@@ -1,64 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require 'openhab/dsl/rules/triggers/trigger'
3
+ require "openhab/dsl/rules/triggers/trigger"
5
4
 
6
5
  module OpenHAB
7
6
  module DSL
8
7
  module Rules
9
- #
10
- # Cron type rules
11
- #
12
8
  module Triggers
13
- #
14
- # Create a rule that executes at the specified interval
15
- #
16
- # @param [Object] value String, Symbol, Duration, or MonthDay to execute this rule
17
- # @param [Object] at TimeOfDay or String representing TimeOfDay in which to execute rule
18
- # @param [Object] attach object to be attached to the trigger
19
- #
20
- #
21
- def every(value, at: nil, attach: nil)
22
- return every(MonthDay.parse(value), at: at, attach: attach) if value.is_a? String
23
-
24
- @ruby_triggers << [:every, value, { at: at }]
25
-
26
- cron_expression = case value
27
- when Symbol then Cron.from_symbol(value, at)
28
- when java.time.Duration then Cron.from_duration(value, at)
29
- when java.time.MonthDay then Cron.from_monthday(value, at)
30
- else raise ArgumentError, 'Unknown interval'
31
- end
32
- cron(cron_expression, attach: attach)
33
- end
34
-
35
- #
36
- # Create a OpenHAB Cron trigger
37
- #
38
- # @param [String] expression OpenHAB style cron expression
39
- # @param [Object] attach object to be attached to the trigger
40
- # @param [Hash] fields cron expression elements (second, minute, hour, dom, month, dow, year)
41
- #
42
- def cron(expression = nil, attach: nil, **fields)
43
- if fields.any?
44
- raise ArgumentError, 'Cron elements cannot be used with a cron expression' if expression
45
-
46
- cron_expression = Cron.from_fields(fields)
47
- return cron(cron_expression, attach: attach)
48
- end
49
-
50
- raise ArgumentError, 'Missing cron expression or elements' unless expression
51
-
52
- cron = Cron.new(rule_triggers: @rule_triggers)
53
- cron.trigger(config: { 'cronExpression' => expression }, attach: attach)
54
- end
55
-
9
+ # @!visibility private
56
10
  #
57
11
  # Creates cron triggers
58
12
  #
59
13
  class Cron < Trigger
60
- # Trigger ID for Watch Triggers
61
- CRON_TRIGGER_MODULE_ID = 'jsr223.jruby.CronTrigger'
14
+ # Trigger ID for Cron Triggers
15
+ CRON_TRIGGER_MODULE_ID = "jsr223.jruby.CronTrigger"
62
16
 
63
17
  #
64
18
  # Returns a default map for cron expressions that fires every second
@@ -69,25 +23,25 @@ module OpenHAB
69
23
  #
70
24
  CRON_EXPRESSION_MAP =
71
25
  {
72
- second: '*',
73
- minute: '*',
74
- hour: '*',
75
- dom: '?',
76
- month: '*',
77
- dow: '?',
78
- year: '*'
26
+ second: "*",
27
+ minute: "*",
28
+ hour: "*",
29
+ dom: "?",
30
+ month: "*",
31
+ dow: "?",
32
+ year: "*"
79
33
  }.freeze
80
34
  private_constant :CRON_EXPRESSION_MAP
81
35
 
82
- # @return [Hash] Map of days of the week from symbols to to OpenHAB cron strings
36
+ # @return [Hash] Map of days of the week from symbols to to openHAB cron strings
83
37
  DAY_OF_WEEK_MAP = {
84
- monday: 'MON',
85
- tuesday: 'TUE',
86
- wednesday: 'WED',
87
- thursday: 'THU',
88
- friday: 'FRI',
89
- saturday: 'SAT',
90
- sunday: 'SUN'
38
+ monday: "MON",
39
+ tuesday: "TUE",
40
+ wednesday: "WED",
41
+ thursday: "THU",
42
+ friday: "FRI",
43
+ saturday: "SAT",
44
+ sunday: "SUN"
91
45
  }.freeze
92
46
  private_constant :DAY_OF_WEEK_MAP
93
47
 
@@ -99,12 +53,12 @@ module OpenHAB
99
53
  # @return [Hash] Create a set of cron expressions based on different time intervals
100
54
  EXPRESSION_MAP = {
101
55
  second: CRON_EXPRESSION_MAP,
102
- minute: CRON_EXPRESSION_MAP.merge(second: '0'),
103
- hour: CRON_EXPRESSION_MAP.merge(second: '0', minute: '0'),
104
- day: CRON_EXPRESSION_MAP.merge(second: '0', minute: '0', hour: '0'),
105
- week: CRON_EXPRESSION_MAP.merge(second: '0', minute: '0', hour: '0', dow: 'MON'),
106
- month: CRON_EXPRESSION_MAP.merge(second: '0', minute: '0', hour: '0', dom: '1'),
107
- year: CRON_EXPRESSION_MAP.merge(second: '0', minute: '0', hour: '0', dom: '1', month: '1')
56
+ minute: CRON_EXPRESSION_MAP.merge(second: "0"),
57
+ hour: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0"),
58
+ day: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0"),
59
+ week: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0", dow: "MON"),
60
+ month: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0", dom: "1"),
61
+ year: CRON_EXPRESSION_MAP.merge(second: "0", minute: "0", hour: "0", dom: "1", month: "1")
108
62
  }.merge(DAY_OF_WEEK_EXPRESSION_MAP).freeze
109
63
 
110
64
  private_constant :EXPRESSION_MAP
@@ -112,8 +66,8 @@ module OpenHAB
112
66
  #
113
67
  # Create a cron map from a duration
114
68
  #
115
- # @param [java.time.Duration] duration
116
- # @param [Object] at TimeOfDay or String representing time of day
69
+ # @param [Duration] duration
70
+ # @param [Object] at LocalTime or String representing time of day
117
71
  #
118
72
  # @return [Hash] map describing cron expression
119
73
  #
@@ -126,8 +80,8 @@ module OpenHAB
126
80
  #
127
81
  # Create a cron map from a MonthDay
128
82
  #
129
- # @param [java.time.MonthDay] monthday a {MonthDay} object
130
- # @param [Object] at TimeOfDay or String representing time of day
83
+ # @param [MonthDay] monthday a {MonthDay} object
84
+ # @param [Object] at LocalTime or String representing time of day
131
85
  #
132
86
  # @return [Hash] map describing cron expression
133
87
  #
@@ -141,7 +95,7 @@ module OpenHAB
141
95
  # Create a cron map from a symbol
142
96
  #
143
97
  # @param [Symbol] symbol
144
- # @param [Object] at TimeOfDay or String representing time of day
98
+ # @param [Object] at LocalTime or String representing time of day
145
99
  #
146
100
  # @return [Hash] map describing cron expression created from symbol
147
101
  #
@@ -159,8 +113,19 @@ module OpenHAB
159
113
  # @return [Hash] map describing cron expression
160
114
  #
161
115
  def self.from_fields(fields)
162
- fields = fields.transform_values { |value| value.to_s.gsub(/\s+/, '') }
163
- expression_map = CRON_EXPRESSION_MAP.merge(fields)
116
+ extra_fields = fields.keys - CRON_EXPRESSION_MAP.keys
117
+ unless extra_fields.empty?
118
+ raise ArgumentError,
119
+ "unknown keyword#{"s" if extra_fields.size > 1}: #{extra_fields.map(&:inspect).join(", ")}"
120
+ end
121
+
122
+ fields = fields.transform_values { |value| value.to_s.delete(" ") }
123
+ # find the first expression map that has a field from fields.
124
+ # this ensure more-specific fields get set to 0, not *
125
+ base_key = EXPRESSION_MAP.keys.find { |field, _| fields.key?(field) }
126
+ base_expression = EXPRESSION_MAP[base_key]
127
+ expression_map = base_expression.merge(fields)
128
+
164
129
  map_to_cron(expression_map)
165
130
  end
166
131
 
@@ -169,16 +134,16 @@ module OpenHAB
169
134
  #
170
135
  # @param [Map] map of cron expression
171
136
  #
172
- # @return [String] OpenHAB cron string
137
+ # @return [String] openHAB cron string
173
138
  #
174
139
  def self.map_to_cron(map)
175
- %i[second minute hour dom month dow year].map { |field| map.fetch(field) }.join(' ')
140
+ %i[second minute hour dom month dow year].map { |field| map.fetch(field) }.join(" ")
176
141
  end
177
142
 
178
143
  #
179
144
  # Convert a Java Duration to a map for the map_to_cron method
180
145
  #
181
- # @param duration [java.time.Duration] The duration object
146
+ # @param duration [Duration] The duration object
182
147
  #
183
148
  # @return [Hash] a map suitable for map_to_cron
184
149
  #
@@ -207,7 +172,7 @@ module OpenHAB
207
172
  #
208
173
  def self.at_condition(expression_map, at_time)
209
174
  if at_time
210
- tod = at_time.is_a?(TimeOfDay::TimeOfDay) ? at_time : TimeOfDay::TimeOfDay.parse(at_time)
175
+ tod = at_time.is_a?(LocalTime) ? at_time : LocalTime.parse(at_time)
211
176
  expression_map = expression_map.merge(hour: tod.hour, minute: tod.minute, second: tod.second)
212
177
  end
213
178
  expression_map
@@ -221,6 +186,7 @@ module OpenHAB
221
186
  #
222
187
  #
223
188
  def trigger(config:, attach:)
189
+ CronHandler::CronTriggerHandlerFactory.instance # ensure it's registered
224
190
  append_trigger(type: CRON_TRIGGER_MODULE_ID, config: config, attach: attach)
225
191
  end
226
192
  end
@@ -1,55 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'java'
4
- require_relative 'cron'
3
+ require "singleton"
4
+
5
+ require_relative "cron"
5
6
 
6
7
  module OpenHAB
7
8
  module DSL
8
9
  module Rules
9
- #
10
- # Cron type rules
11
- #
12
10
  module Triggers
11
+ # @!visibility private
13
12
  #
14
13
  # Cron trigger handler that provides trigger ID
15
14
  #
16
15
  module CronHandler
17
- include OpenHAB::Log
18
-
19
- #
20
- # Creates trigger types and trigger type factories for OpenHAB
21
- #
22
- def self.add_script_cron_handler
23
- java_import org.openhab.core.automation.type.TriggerType
24
- OpenHAB::Core.automation_manager.add_trigger_handler(
25
- OpenHAB::DSL::Rules::Triggers::Cron::CRON_TRIGGER_MODULE_ID,
26
- OpenHAB::DSL::Rules::Triggers::CronHandler::CronTriggerHandlerFactory.new
27
- )
28
-
29
- OpenHAB::Core.automation_manager.add_trigger_type(cron_trigger_type)
30
- OpenHAB::Log.logger(self).trace('Added script cron trigger handler')
31
- end
32
-
33
- #
34
- # Creates trigger types and trigger type factories for OpenHAB
35
- #
36
- private_class_method def self.cron_trigger_type
37
- TriggerType.new(
38
- OpenHAB::DSL::Rules::Triggers::Cron::CRON_TRIGGER_MODULE_ID,
39
- nil,
40
- 'A specific instant occurs',
41
- 'Triggers when the specified instant occurs',
42
- nil,
43
- org.openhab.core.automation.Visibility::VISIBLE,
44
- nil
45
- )
46
- end
47
-
48
16
  # Cron Trigger Handler that provides trigger IDs
49
- # Unfortunatly because the CronTriggerHandler in OpenHAB core is marked internal
17
+ # Unfortunatly because the CronTriggerHandler in openHAB core is marked internal
50
18
  # the entire thing must be recreated here
51
19
  class CronTriggerHandler < org.openhab.core.automation.handler.BaseTriggerModuleHandler
52
- include OpenHAB::Log
53
20
  include org.openhab.core.scheduler.SchedulerRunnable
54
21
  include org.openhab.core.automation.handler.TimeBasedTriggerHandler
55
22
 
@@ -57,13 +24,13 @@ module OpenHAB
57
24
  field_accessor :callback
58
25
 
59
26
  # Creates a new CronTriggerHandler
60
- # @param [Trigger] trigger OpenHAB trigger associated with handler
27
+ # @param [org.openhab.core.automation.Trigger] trigger openHAB trigger associated with handler
61
28
  #
62
29
  def initialize(trigger)
63
30
  @trigger = trigger
64
- @scheduler = OpenHAB::Core::OSGI.service('org.openhab.core.scheduler.CronScheduler')
31
+ @scheduler = OSGi.service("org.openhab.core.scheduler.CronScheduler")
65
32
  @schedule = nil
66
- @expression = trigger.configuration.get('cronExpression')
33
+ @expression = trigger.configuration.get("cronExpression")
67
34
  super(trigger)
68
35
  end
69
36
 
@@ -91,7 +58,7 @@ module OpenHAB
91
58
  # Execute the callback
92
59
  #
93
60
  def run
94
- callback&.triggered(@trigger, { 'module' => @trigger.id })
61
+ callback&.triggered(@trigger, { "module" => @trigger.id })
95
62
  end
96
63
 
97
64
  #
@@ -112,10 +79,29 @@ module OpenHAB
112
79
 
113
80
  # Implements the ScriptedTriggerHandlerFactory interface to create a new Cron Trigger Handler
114
81
  class CronTriggerHandlerFactory
82
+ include Singleton
115
83
  include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory
116
84
 
117
- # Invoked by the OpenHAB core to get a trigger handler for the supllied trigger
118
- # @param [Trigger] trigger OpenHAB trigger
85
+ def initialize
86
+ Core.automation_manager.add_trigger_handler(
87
+ Cron::CRON_TRIGGER_MODULE_ID,
88
+ self
89
+ )
90
+
91
+ Core.automation_manager.add_trigger_type(org.openhab.core.automation.type.TriggerType.new(
92
+ Cron::CRON_TRIGGER_MODULE_ID,
93
+ nil,
94
+ "A specific instant occurs",
95
+ "Triggers when the specified instant occurs",
96
+ nil,
97
+ org.openhab.core.automation.Visibility::VISIBLE,
98
+ nil
99
+ ))
100
+ logger.trace("Added script cron trigger handler")
101
+ end
102
+
103
+ # Invoked by openHAB core to get a trigger handler for the supplied trigger
104
+ # @param [org.openhab.core.automation.Trigger] trigger
119
105
  #
120
106
  # @return [WatchTriggerHandler] trigger handler for supplied trigger
121
107
  def get(trigger)
@@ -127,6 +113,3 @@ module OpenHAB
127
113
  end
128
114
  end
129
115
  end
130
-
131
- # Add the cron handler to OpenHAB
132
- OpenHAB::DSL::Rules::Triggers::CronHandler.add_script_cron_handler
@@ -1,40 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'forwardable'
3
+ require "forwardable"
4
4
 
5
5
  module OpenHAB
6
6
  module DSL
7
7
  module Rules
8
- #
9
- # Module holds rule triggers
10
- #
11
8
  module Triggers
9
+ # @!visibility private
12
10
  #
13
11
  # Class for creating and managing triggers
14
12
  #
15
13
  class Trigger
16
14
  extend Forwardable
17
15
 
18
- # Provide backwards compatibility for these fields
19
- delegate :append_trigger => :@rule_triggers
20
-
21
- #
22
- # Separates groups from items, and flattens any nested arrays of items
23
- #
24
- # @param [Array] item_array Array of items passed to a trigger
25
- #
26
- # @return [Array] A new flat array with any GroupMembers object left intact
27
- #
28
- def self.flatten_items(item_array)
29
- # we want to support anything that can be flattened... i.e. responds to to_ary
30
- # we want to be more lenient than only things that are currently Array,
31
- # but Enumerable is too lenient because Array#flatten won't traverse interior
32
- # Enumerables
33
- return item_array unless item_array.find { |item| item.respond_to?(:to_ary) }
34
-
35
- groups, items = item_array.partition { |item| item.is_a?(OpenHAB::DSL::Items::GroupItem::GroupMembers) }
36
- groups + flatten_items(items.flatten(1))
37
- end
16
+ delegate append_trigger: :@rule_triggers
38
17
 
39
18
  #
40
19
  # Creates a new Trigger
@@ -54,9 +33,9 @@ module OpenHAB
54
33
  # @return [Array] Trigger and config for thing
55
34
  #
56
35
  def trigger_for_thing(thing:, type:, to: nil, from: nil)
57
- config = { 'thingUID' => thing.uid.to_s }
58
- config['status'] = trigger_state_from_symbol(to).to_s if to
59
- config['previousStatus'] = trigger_state_from_symbol(from).to_s if from
36
+ config = { "thingUID" => thing.to_s }
37
+ config["status"] = trigger_state_from_symbol(to).to_s if to
38
+ config["previousStatus"] = trigger_state_from_symbol(from).to_s if from
60
39
  [type, config]
61
40
  end
62
41
 
@@ -68,7 +47,7 @@ module OpenHAB
68
47
  # @return [String] Upcased symbol as string
69
48
  #
70
49
  def trigger_state_from_symbol(sym)
71
- sym.to_s.upcase if (sym.is_a? Symbol) || sym
50
+ sym.to_s.upcase if sym.is_a?(Symbol) || sym
72
51
  end
73
52
  end
74
53
  end