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,235 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/log/logger'
4
- require 'openhab/dsl/types/date_time_type'
5
- require 'openhab/dsl/items/items'
6
- require 'time'
7
-
8
- module OpenHAB
9
- module DSL
10
- # Times without specific dates e.g. 6:00:00
11
- # @author Brian O'Connell
12
- # @since 0.0.1
13
- module Between
14
- java_import java.time.LocalTime
15
- java_import java.time.format.DateTimeFormatterBuilder
16
- java_import java.util.Locale
17
-
18
- # Class that encapsulates a Time of Day, often viewed as hour-minute-second
19
- # @author Brian O'Connell
20
- # @since 0.0.1
21
- class TimeOfDay
22
- include Comparable
23
- include OpenHAB::Log
24
-
25
- # Immutable Java object containing Time Of Day
26
- # @return [Java.Time.LocalTime] reprsenting the Time Of Day
27
- attr_reader :local_time
28
-
29
- # Constructs a TimeOfDay representing the time when called
30
- # @since 0.0.1
31
- # @return [TimeOfDay] representing time when method was invoked
32
- def self.now
33
- now = LocalTime.now()
34
- TimeOfDay.new(h: now.hour, m: now.minute, s: now.second)
35
- end
36
-
37
- # Constructs a TimeOfDay representing midnight
38
- # @since 0.0.1
39
- # @return [TimeOfDay] representing midnight
40
- def self.midnight
41
- TimeOfDay.new(h: 0, m: 0, s: 0)
42
- end
43
-
44
- # Constructs a TimeOfDay representing noon
45
- # @since 0.0.1
46
- # @return [TimeOfDay] representing noon
47
- def self.noon
48
- TimeOfDay.new(h: 12, m: 0, s: 0)
49
- end
50
-
51
- # Constructs a TimeOfDay representing the time when called
52
- # @since 0.0.1
53
- # @param [String] string representation of TimeOfDay. Valid formats include "HH:MM:SS", "HH:MM",
54
- # "H:MM", "HH", "H", "H:MM am"
55
- # @return [TimeOfDay] object created by parsing supplied string
56
- def self.parse(string)
57
- format = /(am|pm)$/i.match?(string) ? 'h[:mm[:ss]][ ]a' : 'H[:mm[:ss]]'
58
- local_time = LocalTime.parse(string, DateTimeFormatterBuilder.new
59
- .parseCaseInsensitive.appendPattern(format).toFormatter(Locale::ENGLISH))
60
- TimeOfDay.new(h: local_time.hour, m: local_time.minute, s: local_time.second)
61
- rescue java.time.format.DateTimeParseException => e
62
- raise ArgumentError, e.message
63
- end
64
-
65
- # Constructs a TimeOfDay representing the time when called
66
- # @since 0.0.1
67
- # @option opts [Number] :h Hour of the day, defaults to 0
68
- # @option opts [Number] :m Minute of the day, defaults to 0
69
- # @option opts [Number] :s Second of the day, defaults to 0
70
- # @return [TimeOfDay] representing time when method was invoked
71
- # rubocop: disable Naming/MethodParameterName
72
- # This method has a better feel with short parameter names
73
- def initialize(h: 0, m: 0, s: 0)
74
- @local_time = LocalTime.of(h, m, s)
75
- freeze
76
- end
77
- # rubocop: enable Naming/MethodParameterName
78
-
79
- # Returns true if the time falls within a range
80
- def between?(range)
81
- between(range).cover? self
82
- end
83
-
84
- # Returns the hour of the TimeOfDay
85
- # @since 0.0.1
86
- # @return [Number] Hour of the day, from 0 to 23
87
- def hour
88
- @local_time.hour
89
- end
90
-
91
- # Returns the minute of the TimeOfDay
92
- # @since 0.0.1
93
- # @return [Number] minute of the day, from 0 to 59
94
- def minute
95
- @local_time.minute
96
- end
97
-
98
- # Returns the second of the TimeOfDay
99
- # @since 0.0.1
100
- # @return [Number] second of the day, from 0 to 59
101
- def second
102
- @local_time.second
103
- end
104
-
105
- # Returns the string representation of the TimeOfDay
106
- # @since 0.0.1
107
- # @return [String] in any of the following formats depending on time representation HH:mm, HH:mm:ss,
108
- # HH:mm:ss.SSS, HH:mm:ss.SSSSSS, HH:mm:ss.SSSSSSSSS
109
- def to_s
110
- @local_time.to_s
111
- end
112
-
113
- # Compares one TimeOfDay to another
114
- # @since 0.0.1
115
- # @return [Number, nil] -1,0,1 if other TimeOfDay is less than, equal to, or greater than this TimeOfDay
116
- # or nil if an object other than TimeOfDay is provided
117
- def <=>(other)
118
- logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
119
- case other
120
- when TimeOfDay then @local_time.compare_to(other.local_time)
121
- when String then @local_time.compare_to(TimeOfDay.parse(other).local_time)
122
- when LocalTime then @local_time.compare_to(other)
123
- else
124
- -(other <=> self)
125
- end
126
- end
127
-
128
- # Creates a range that can be compared against time of day objects or strings
129
- # to see if they are within the range
130
- # @since 2.4.0
131
- # @return Range object representing a TimeOfDay Range
132
- def self.between(range)
133
- raise ArgumentError, 'Supplied object must be a range' unless range.is_a? Range
134
-
135
- start = to_time_of_day(range.begin)
136
- ending = to_time_of_day(range.end)
137
-
138
- start_sod = start.local_time.to_second_of_day
139
- ending_sod = ending.local_time.to_second_of_day
140
- ending_sod += TimeOfDayRangeElement::NUM_SECONDS_IN_DAY if ending_sod < start_sod
141
-
142
- start_range = TimeOfDayRangeElement.new(sod: start_sod, range_begin: start_sod)
143
- ending_range = TimeOfDayRangeElement.new(sod: ending_sod, range_begin: start_sod)
144
- range.exclude_end? ? (start_range...ending_range) : (start_range..ending_range)
145
- end
146
-
147
- #
148
- # Convert object to TimeOfDay object
149
- #
150
- # @param [Object] object TimeOfDay, String, or LocalTime to be converted
151
- #
152
- # @return [TimeOfDay] TimeOfDay created from supplied object
153
- #
154
- def self.to_time_of_day(object)
155
- case object
156
- when String then TimeOfDay.parse(object)
157
- when Time, OpenHAB::DSL::Types::DateTimeType, OpenHAB::DSL::Items::DateTimeItem
158
- TimeOfDay.new(h: object.hour, m: object.min, s: object.sec)
159
- when LocalTime
160
- TimeOfDay.new(h: object.hour, m: object.minute, s: object.second)
161
- else object
162
- end
163
- end
164
- end
165
-
166
- # Modules that refines the Ruby Range object cover? and include? methods to support TimeOfDay ranges
167
- class TimeOfDayRangeElement < Numeric
168
- include Comparable
169
- include OpenHAB::Log
170
-
171
- NUM_SECONDS_IN_DAY = (60 * 60 * 24)
172
-
173
- attr_reader :sod
174
-
175
- def initialize(sod:, range_begin:)
176
- @sod = sod
177
- @range_begin = range_begin
178
- super()
179
- end
180
-
181
- # Returns the current second of day advanced by 1 second
182
- def succ
183
- TimeOfDayRangeElement.new(sod: @sod + 1, range_begin: @range_begin)
184
- end
185
-
186
- # Compares one TimeOfDayRangeElement to another
187
- # @since 2.4.0
188
- # @return [Number, nil] -1,0,1 if other is less than, equal to, or greater than this TimeOfDay
189
- def <=>(other)
190
- other_second_of_day = to_second_of_day(other)
191
- logger.trace do
192
- "SOD(#{sod}) " \
193
- "other SOD(#{other_second_of_day}) " \
194
- "Other Class (#{other.class}) " \
195
- "Result (#{sod <=> other_second_of_day})"
196
- end
197
- sod <=> other_second_of_day
198
- end
199
-
200
- private
201
-
202
- #
203
- # Convert object to the seconds of a day they reprsent
204
- #
205
- # @param [Object] object TimeofDay,String,Time, or TimeOfDayRangeElement to convert
206
- #
207
- # @return [Integer] seconds of day represented by supplied object
208
- #
209
- # rubocop:disable Metrics/AbcSize
210
- # case statement needs to compare against multiple types
211
- def to_second_of_day(object)
212
- case object
213
- when TimeOfDay then adjust_second_of_day(object.local_time.to_second_of_day)
214
- when String then adjust_second_of_day(TimeOfDay.parse(object).local_time.to_second_of_day)
215
- when ::Time, OpenHAB::DSL::Types::DateTimeType, OpenHAB::DSL::Items::DateTimeItem
216
- adjust_second_of_day(TimeOfDay.new(h: object.hour, m: object.min, s: object.sec)
217
- .local_time.to_second_of_day)
218
- when TimeOfDayRangeElement then object.sod
219
- else raise ArgumentError, "Supplied argument #{object.class} cannot be converted into Time Of Day Object"
220
- end
221
- end
222
- # rubocop:enable Metrics/AbcSize
223
-
224
- def adjust_second_of_day(second_of_day)
225
- second_of_day += NUM_SECONDS_IN_DAY if second_of_day < @range_begin
226
- second_of_day
227
- end
228
- end
229
-
230
- MIDNIGHT = TimeOfDay.midnight
231
- NOON = TimeOfDay.noon
232
- ALL_DAY = TimeOfDay.between(TimeOfDay.new(h: 0, m: 0, s: 0)..TimeOfDay.new(h: 23, m: 59, s: 59))
233
- end
234
- end
235
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'set'
4
- require 'openhab/log/logger'
5
- require_relative 'reentrant_timer'
6
-
7
- module OpenHAB
8
- module DSL
9
- #
10
- # Provides supporting classes for Timers module
11
- #
12
- module Support
13
- #
14
- # Manages data structures that track timers
15
- #
16
- class TimerManager
17
- include OpenHAB::Log
18
-
19
- attr_reader :timer_ids
20
-
21
- def initialize
22
- # Track timer IDs
23
- @timer_ids = {}
24
-
25
- # Reentrant timer lookups
26
- @reentrant_timers = {}
27
-
28
- # Tracks active timers
29
- @timers = Set.new
30
- end
31
-
32
- #
33
- # Adds the current timer to the set of rule timers if being tracked
34
- #
35
- # rubocop: disable Metrics/AbcSize
36
- # It does not make sense to break this up into seperate components
37
- def add(timer)
38
- logger.trace("Adding #{timer} to timers")
39
- @timers << timer
40
-
41
- if timer.respond_to? :id
42
- logger.trace("Adding #{timer} with id #{timer.id.inspect} timer ids")
43
- @timer_ids[timer.id] = TimerSet.new unless @timer_ids[timer.id]
44
- @timer_ids[timer.id] << timer
45
- end
46
-
47
- return unless timer.respond_to? :reentrant_id
48
-
49
- logger.trace("Adding reeentrant #{timer} with reentrant id #{timer.reentrant_id} timer ids")
50
- @reentrant_timers[timer.reentrant_id] = timer
51
- end
52
- # rubocop: enable Metrics/AbcSize
53
-
54
- # Fetches the reentrant timer that matches the supplied id and block if it exists
55
- #
56
- # @param [Object] id Object to associate with timer
57
- # @param [Block] block to execute, block is passed a Timer object
58
- #
59
- # @return [RentrantTimer] Timer object if it exists, nil otherwise
60
- #
61
- def reentrant_timer(id:, &block)
62
- reentrant_key = ReentrantTimer.reentrant_id(id: id, &block)
63
- logger.trace("Checking for existing reentrant timer for #{reentrant_key}")
64
- @reentrant_timers[reentrant_key]
65
- end
66
-
67
- #
68
- # Delete the current timer to the set of rule timers if being tracked
69
- #
70
- def delete(timer)
71
- logger.trace("Removing #{timer} from timers")
72
- @timers.delete(timer)
73
- if timer.respond_to?(:id) && (timers = @timer_ids[timer.id])
74
- timers.delete(timer)
75
- @timer_ids.delete(timer.id) if timers.empty?
76
- end
77
- @reentrant_timers.delete(timer.reentrant_id) if timer.respond_to? :reentrant_id
78
- end
79
-
80
- #
81
- # Cancels all active timers
82
- #
83
- def cancel_all
84
- logger.trace("Canceling #{@timers.length} timers")
85
- @timers.each(&:cancel)
86
- @timer_ids.clear
87
- @reentrant_timers.clear
88
- @timers.clear
89
- end
90
- end
91
-
92
- #
93
- # Provide additional methods for the timers set
94
- #
95
- class TimerSet < Set
96
- #
97
- # A shorthand to cancel all the timer objects held within the set
98
- # so that timers[timer_id].cancel is equivalent to timers[timer_id].each(&:cancel)
99
- #
100
- def cancel
101
- each(&:cancel)
102
- end
103
- # @deprecated Please use {cancel} instead
104
- alias cancel_all cancel
105
-
106
- #
107
- # A shorthand to reschedule all the timer objects held within the set
108
- #
109
- # @param [Duration] duration An optional duration to reschedule
110
- #
111
- # @return [TimerSet] Set of timers
112
- #
113
- def reschedule(duration = nil)
114
- each { |timer| timer.reschedule duration }
115
- end
116
- end
117
- end
118
- end
119
- end
@@ -1,38 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'openhab/log/logger'
4
- require_relative 'timer'
5
-
6
- module OpenHAB
7
- module DSL
8
- # A reentrant timer is a timer that is automatically rescheduled
9
- # when the block it refers to is encountered again
10
- #
11
- # @author Brian O'Connell
12
- # @since 2.0.0
13
- class ReentrantTimer < Timer
14
- include OpenHAB::Log
15
-
16
- attr_reader :id, :reentrant_id
17
-
18
- #
19
- # Create a new Timer Object
20
- #
21
- # @param [Duration] duration Duration until timer should fire
22
- # @param [Block] block Block to execute when timer fires
23
- #
24
- def initialize(duration:, id:, thread_locals: {}, &block)
25
- raise 'Reentrant timers do not work in dynamically generated code' unless block.source_location
26
-
27
- @id = id
28
- @reentrant_id = self.class.reentrant_id(id: id, &block)
29
- super(duration: duration, thread_locals: thread_locals, &block)
30
- logger.trace("Created Reentrant Timer #{self} with reentrant Key #{@reentrant_id}")
31
- end
32
-
33
- def self.reentrant_id(id:, &block)
34
- [id, block.source_location].flatten
35
- end
36
- end
37
- end
38
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'java'
4
- require 'delegate'
5
- require 'forwardable'
6
- require 'openhab/log/logger'
7
- require 'openhab/core/thread_local'
8
-
9
- module OpenHAB
10
- module DSL
11
- java_import org.openhab.core.model.script.actions.ScriptExecution
12
-
13
- # Ruby wrapper for OpenHAB Timer
14
- # This class implements delegator to delegate methods to the OpenHAB timer
15
- #
16
- # @author Brian O'Connell
17
- # @since 2.0.0
18
- class Timer < SimpleDelegator
19
- include OpenHAB::Log
20
- include OpenHAB::Core::ThreadLocal
21
- extend Forwardable
22
-
23
- def_delegator :@timer, :has_terminated, :terminated?
24
-
25
- #
26
- # Create a new Timer Object
27
- #
28
- # @param [Duration] duration Duration until timer should fire
29
- # @param [Block] block Block to execute when timer fires
30
- #
31
- def initialize(duration:, thread_locals: {}, &block)
32
- @duration = duration
33
- @thread_locals = thread_locals
34
-
35
- # A semaphore is used to prevent a race condition in which calling the block from the timer thread
36
- # occurs before the @timer variable can be set resulting in @timer being nil
37
- semaphore = Mutex.new
38
-
39
- semaphore.synchronize do
40
- @timer = ScriptExecution.createTimer(OpenHAB::DSL.to_zdt(@duration), timer_block(semaphore, &block))
41
- @rule_timers = Thread.current[:rule_timers]
42
- super(@timer)
43
- Timers.timer_manager.add(self)
44
- end
45
- end
46
-
47
- #
48
- # Reschedule timer
49
- #
50
- # @param [Duration] duration
51
- #
52
- # @return [Timer] Rescheduled timer instances
53
- #
54
- def reschedule(duration = nil)
55
- duration ||= @duration
56
-
57
- Timers.timer_manager.add(self)
58
- @timer.reschedule(OpenHAB::DSL.to_zdt(duration))
59
- end
60
-
61
- #
62
- # Cancel timer
63
- #
64
- # @return [Boolean] True if cancel was successful, false otherwise
65
- #
66
- def cancel
67
- Timers.timer_manager.delete(self)
68
- @timer.cancel
69
- end
70
-
71
- private
72
-
73
- #
74
- # Constructs a block to execute timer within
75
- #
76
- # @param [Semaphore] semaphore to obtain before executing
77
- #
78
- # @return [Proc] Block for timer to execute
79
- #
80
- def timer_block(semaphore)
81
- proc {
82
- OpenHAB::DSL.import_presets
83
- semaphore.synchronize do
84
- Timers.timer_manager.delete(self)
85
- thread_local(**@thread_locals) do
86
- yield(self)
87
- end
88
- end
89
- }
90
- end
91
- end
92
-
93
- #
94
- # Convert TemporalAmount (Duration), seconds (float, integer), and Ruby Time to ZonedDateTime
95
- # Note: TemporalAmount is added to now
96
- #
97
- # @param [Object] timestamp to convert
98
- #
99
- # @return [ZonedDateTime]
100
- #
101
- def self.to_zdt(timestamp)
102
- logger.trace("Converting #{timestamp} (#{timestamp.class}) to ZonedDateTime")
103
- return unless timestamp
104
-
105
- case timestamp
106
- when java.time.temporal.TemporalAmount then ZonedDateTime.now.plus(timestamp)
107
- when ZonedDateTime then timestamp
108
- when Time then timestamp.to_java(ZonedDateTime)
109
- else
110
- to_zdt(seconds_to_duration(timestamp)) ||
111
- raise(ArgumentError, 'Timestamp must be a ZonedDateTime, a Duration, a Numeric, or a Time object')
112
- end
113
- end
114
-
115
- #
116
- # Convert numeric seconds to a Duration object
117
- #
118
- # @param [Float, Integer] secs The number of seconds in integer or float
119
- #
120
- # @return [Duration]
121
- #
122
- def self.seconds_to_duration(secs)
123
- return unless secs
124
-
125
- if secs.respond_to?(:to_f)
126
- secs.to_f.seconds
127
- elsif secs.respond_to?(:to_i)
128
- secs.to_i.seconds
129
- end
130
- end
131
- end
132
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'timers/timer'
4
- require_relative 'timers/manager'
5
- require_relative 'timers/reentrant_timer'
6
-
7
- module OpenHAB
8
- module DSL
9
- #
10
- # Provides access to and ruby wrappers around OpenHAB timers
11
- #
12
- module Timers
13
- include OpenHAB::Log
14
-
15
- # Manages timers
16
- @timer_manager = OpenHAB::DSL::Support::TimerManager.new
17
-
18
- class << self
19
- attr_reader :timer_manager
20
- end
21
-
22
- module_function
23
-
24
- #
25
- # Execute the supplied block after the specified duration
26
- #
27
- # @param [Duration] duration after which to execute the block
28
- # @param [Object] id to associate with timer
29
- # @param [Block] block to execute, block is passed a Timer object
30
- #
31
- # @return [Timer] Timer object
32
- #
33
- def after(duration, id: nil, &block)
34
- # Carry rule name to timer thread
35
- thread_locals = { RULE_NAME: Thread.current[:RULE_NAME] } if Thread.current[:RULE_NAME]
36
- thread_locals ||= {}
37
- return Timers.reentrant_timer(duration: duration, thread_locals: thread_locals, id: id, &block) if id
38
-
39
- OpenHAB::DSL::Timer.new(duration: duration, thread_locals: thread_locals, &block)
40
- end
41
- # An alias for +after+
42
- alias create_timer after
43
-
44
- #
45
- # Provdes access to the hash for mapping timer ids to the set of active timers associated with that id
46
- # @return [Hash] hash of user specified ids to sets of times
47
- def timers
48
- Timers.timer_manager.timer_ids
49
- end
50
-
51
- #
52
- # Cancels all active timers
53
- #
54
- def self.cancel_all
55
- @timer_manager.cancel_all
56
- end
57
- Core::ScriptHandling.script_unloaded { cancel_all }
58
-
59
- # Create or reschedule a reentrant time
60
- #
61
- # @param [Duration] duration after which to execute the block
62
- # @param [Object] id to associate with timer
63
- # @param [Block] block to execute, block is passed a Timer object
64
- # @return [ReentrantTimer] Timer object
65
- def self.reentrant_timer(duration:, id:, thread_locals: nil, &block)
66
- timer = @timer_manager.reentrant_timer(id: id, &block)
67
- if timer
68
- logger.trace("Reentrant timer found - #{timer}")
69
- timer.cancel
70
- else
71
- logger.trace('No reentrant timer found, creating new timer')
72
- end
73
- OpenHAB::DSL::ReentrantTimer.new(duration: duration, id: id, thread_locals: thread_locals, &block)
74
- end
75
- end
76
- end
77
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module DSL
5
- module Types
6
- IncreaseDecreaseType = org.openhab.core.library.types.IncreaseDecreaseType
7
-
8
- # Adds methods to core OpenHAB IncreaseDecreaseType to make it more
9
- # natural in Ruby
10
- class IncreaseDecreaseType # rubocop:disable Lint/EmptyClass
11
- # @!parse include Type
12
-
13
- # @!method increase?
14
- # Check if == +INCREASE+
15
- # @return [Boolean]
16
-
17
- # @!method decrease?
18
- # Check if == +DECREASE+
19
- # @return [Boolean]
20
- end
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module DSL
5
- module Types
6
- NextPreviousType = org.openhab.core.library.types.NextPreviousType
7
-
8
- # Adds methods to core OpenHAB NextPreviousType to make it more
9
- # natural in Ruby
10
- class NextPreviousType # rubocop:disable Lint/EmptyClass
11
- # @!parse include Type
12
-
13
- # @!method next?
14
- # Check if == +NEXT+
15
- # @return [Boolean]
16
-
17
- # @!method previous?
18
- # Check if == +PREVIOUS+
19
- # @return [Boolean]
20
- end
21
- end
22
- end
23
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module OpenHAB
4
- module DSL
5
- module Types
6
- OnOffType = org.openhab.core.library.types.OnOffType
7
-
8
- # Adds methods to core OpenHAB OnOffType to make it more natural in Ruby
9
- class OnOffType
10
- # @!parse include Type
11
-
12
- # @!method on?
13
- # Check if == +ON+
14
- # @return [Boolean]
15
-
16
- # @!method off?
17
- # Check if == +OFF+
18
- # @return [Boolean]
19
-
20
- # Invert the type
21
- # @return [OnOffType] +OFF+ if +ON+, +ON+ if +OFF+
22
- def !
23
- on? ? OFF : ON
24
- end
25
- end
26
- end
27
- end
28
- end