openhab-scripting 4.46.2 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +82 -0
  10. data/lib/openhab/core/dependency_tracking.rb +34 -0
  11. data/lib/openhab/core/dto/item_channel_link.rb +33 -0
  12. data/lib/openhab/core/dto/thing.rb +27 -0
  13. data/lib/openhab/core/dto.rb +11 -0
  14. data/lib/openhab/core/entity_lookup.rb +152 -70
  15. data/lib/openhab/core/events/abstract_event.rb +18 -0
  16. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  17. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  18. data/lib/openhab/core/events/item_command_event.rb +78 -0
  19. data/lib/openhab/core/events/item_event.rb +22 -0
  20. data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
  21. data/lib/openhab/core/events/item_state_event.rb +79 -0
  22. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  23. data/lib/openhab/core/events.rb +10 -0
  24. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  25. data/lib/openhab/core/items/color_item.rb +52 -0
  26. data/lib/openhab/core/items/contact_item.rb +52 -0
  27. data/lib/openhab/core/items/date_time_item.rb +59 -0
  28. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  29. data/lib/openhab/core/items/generic_item.rb +292 -0
  30. data/lib/openhab/core/items/group_item.rb +176 -0
  31. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  32. data/lib/openhab/core/items/item.rb +273 -0
  33. data/lib/openhab/core/items/location_item.rb +34 -0
  34. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  35. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  36. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  37. data/lib/openhab/core/items/metadata.rb +11 -0
  38. data/lib/openhab/core/items/number_item.rb +62 -0
  39. data/lib/openhab/core/items/numeric_item.rb +22 -0
  40. data/lib/openhab/core/items/persistence.rb +416 -0
  41. data/lib/openhab/core/items/player_item.rb +66 -0
  42. data/lib/openhab/core/items/provider.rb +44 -0
  43. data/lib/openhab/core/items/proxy.rb +136 -0
  44. data/lib/openhab/core/items/registry.rb +86 -0
  45. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  46. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  47. data/lib/openhab/core/items/semantics.rb +473 -0
  48. data/lib/openhab/core/items/state_storage.rb +53 -0
  49. data/lib/openhab/core/items/string_item.rb +28 -0
  50. data/lib/openhab/core/items/switch_item.rb +78 -0
  51. data/lib/openhab/core/items.rb +108 -0
  52. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  53. data/lib/openhab/core/profile_factory.rb +132 -0
  54. data/lib/openhab/core/provider.rb +230 -0
  55. data/lib/openhab/core/proxy.rb +130 -0
  56. data/lib/openhab/core/registry.rb +40 -0
  57. data/lib/openhab/core/rules/module.rb +26 -0
  58. data/lib/openhab/core/rules/provider.rb +25 -0
  59. data/lib/openhab/core/rules/registry.rb +76 -0
  60. data/lib/openhab/core/rules/rule.rb +150 -0
  61. data/lib/openhab/core/rules.rb +25 -0
  62. data/lib/openhab/core/script_handling.rb +78 -20
  63. data/lib/openhab/core/things/channel.rb +48 -0
  64. data/lib/openhab/core/things/channel_uid.rb +51 -0
  65. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  66. data/lib/openhab/core/things/links/provider.rb +78 -0
  67. data/lib/openhab/core/things/profile_callback.rb +52 -0
  68. data/lib/openhab/core/things/provider.rb +29 -0
  69. data/lib/openhab/core/things/proxy.rb +87 -0
  70. data/lib/openhab/core/things/registry.rb +73 -0
  71. data/lib/openhab/core/things/thing.rb +194 -0
  72. data/lib/openhab/core/things.rb +22 -0
  73. data/lib/openhab/core/timer.rb +148 -0
  74. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  75. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  76. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  77. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  78. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  79. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  80. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  81. data/lib/openhab/core/types/on_off_type.rb +46 -0
  82. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  83. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  84. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  85. data/lib/openhab/core/types/point_type.rb +117 -0
  86. data/lib/openhab/core/types/quantity_type.rb +325 -0
  87. data/lib/openhab/core/types/raw_type.rb +26 -0
  88. data/lib/openhab/core/types/refresh_type.rb +27 -0
  89. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  90. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  91. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  92. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  93. data/lib/openhab/core/types/un_def_type.rb +38 -0
  94. data/lib/openhab/core/types/up_down_type.rb +50 -0
  95. data/lib/openhab/core/types.rb +82 -0
  96. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  97. data/lib/openhab/core/value_cache.rb +188 -0
  98. data/lib/openhab/core.rb +98 -0
  99. data/lib/openhab/core_ext/between.rb +32 -0
  100. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  101. data/lib/openhab/core_ext/java/class.rb +34 -0
  102. data/lib/openhab/core_ext/java/duration.rb +142 -0
  103. data/lib/openhab/core_ext/java/list.rb +436 -0
  104. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  105. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  106. data/lib/openhab/core_ext/java/map.rb +66 -0
  107. data/lib/openhab/core_ext/java/month.rb +71 -0
  108. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  109. data/lib/openhab/core_ext/java/period.rb +103 -0
  110. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  111. data/lib/openhab/core_ext/java/time.rb +62 -0
  112. data/lib/openhab/core_ext/java/unit.rb +15 -0
  113. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  114. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  115. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  116. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  117. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  118. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  119. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  120. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  121. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  122. data/lib/openhab/core_ext.rb +18 -0
  123. data/lib/openhab/dsl/debouncer.rb +259 -0
  124. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  125. data/lib/openhab/dsl/events.rb +9 -0
  126. data/lib/openhab/dsl/gems.rb +1 -1
  127. data/lib/openhab/dsl/items/builder.rb +578 -0
  128. data/lib/openhab/dsl/items/ensure.rb +73 -82
  129. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  130. data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
  131. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  132. data/lib/openhab/dsl/rules/guard.rb +51 -114
  133. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  134. data/lib/openhab/dsl/rules/property.rb +48 -75
  135. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  136. data/lib/openhab/dsl/rules/terse.rb +58 -14
  137. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  138. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  139. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  140. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  141. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  142. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  143. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  144. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  145. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  146. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  147. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  148. data/lib/openhab/dsl/rules.rb +8 -0
  149. data/lib/openhab/dsl/things/builder.rb +299 -0
  150. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  151. data/lib/openhab/dsl/timer_manager.rb +204 -0
  152. data/lib/openhab/dsl/version.rb +9 -0
  153. data/lib/openhab/dsl.rb +979 -0
  154. data/lib/openhab/log.rb +355 -0
  155. data/lib/openhab/osgi.rb +68 -0
  156. data/lib/openhab/rspec/configuration.rb +56 -0
  157. data/lib/openhab/rspec/example_group.rb +132 -0
  158. data/lib/openhab/rspec/helpers.rb +458 -0
  159. data/lib/openhab/rspec/hooks.rb +113 -0
  160. data/lib/openhab/rspec/jruby.rb +46 -0
  161. data/lib/openhab/rspec/karaf.rb +851 -0
  162. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  163. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  164. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  165. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  166. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  167. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  168. data/lib/openhab/rspec/mocks/space.rb +23 -0
  169. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  170. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  171. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  172. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  173. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  174. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  175. data/lib/openhab/rspec/shell.rb +31 -0
  176. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  177. data/lib/openhab/rspec.rb +26 -0
  178. data/lib/openhab/yard/base_helper.rb +19 -0
  179. data/lib/openhab/yard/cli/stats.rb +23 -0
  180. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  181. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  182. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  183. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  184. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  186. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  187. data/lib/openhab/yard/coderay.rb +17 -0
  188. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  189. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  190. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  192. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  193. data/lib/openhab/yard/html_helper.rb +78 -0
  194. data/lib/openhab/yard/markdown_helper.rb +148 -0
  195. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  196. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  197. data/lib/openhab/yard/tags/library.rb +3 -0
  198. data/lib/openhab/yard.rb +38 -0
  199. metadata +475 -106
  200. data/lib/openhab/core/item_proxy.rb +0 -29
  201. data/lib/openhab/core/load_path.rb +0 -19
  202. data/lib/openhab/core/openhab_setup.rb +0 -29
  203. data/lib/openhab/core/osgi.rb +0 -58
  204. data/lib/openhab/core/services.rb +0 -24
  205. data/lib/openhab/dsl/actions.rb +0 -114
  206. data/lib/openhab/dsl/between.rb +0 -25
  207. data/lib/openhab/dsl/channel.rb +0 -43
  208. data/lib/openhab/dsl/dsl.rb +0 -59
  209. data/lib/openhab/dsl/group.rb +0 -54
  210. data/lib/openhab/dsl/imports.rb +0 -21
  211. data/lib/openhab/dsl/items/color_item.rb +0 -76
  212. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  213. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  214. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  215. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  216. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  217. data/lib/openhab/dsl/items/group_item.rb +0 -127
  218. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  219. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  220. data/lib/openhab/dsl/items/items.rb +0 -109
  221. data/lib/openhab/dsl/items/location_item.rb +0 -59
  222. data/lib/openhab/dsl/items/metadata.rb +0 -326
  223. data/lib/openhab/dsl/items/number_item.rb +0 -17
  224. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  225. data/lib/openhab/dsl/items/persistence.rb +0 -307
  226. data/lib/openhab/dsl/items/player_item.rb +0 -58
  227. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  228. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  229. data/lib/openhab/dsl/items/semantics.rb +0 -227
  230. data/lib/openhab/dsl/items/string_item.rb +0 -51
  231. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  232. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  233. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  234. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  235. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  236. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  237. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  238. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  239. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  240. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  241. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  242. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  243. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  244. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  245. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  246. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  247. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  248. data/lib/openhab/dsl/openhab.rb +0 -30
  249. data/lib/openhab/dsl/persistence.rb +0 -27
  250. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  251. data/lib/openhab/dsl/rules/rule.rb +0 -160
  252. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  253. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  254. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  255. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  256. data/lib/openhab/dsl/states.rb +0 -89
  257. data/lib/openhab/dsl/things.rb +0 -147
  258. data/lib/openhab/dsl/time/month_day.rb +0 -180
  259. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  260. data/lib/openhab/dsl/timers/manager.rb +0 -119
  261. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  262. data/lib/openhab/dsl/timers/timer.rb +0 -132
  263. data/lib/openhab/dsl/timers.rb +0 -77
  264. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  265. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  266. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  267. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  268. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  269. data/lib/openhab/dsl/types/point_type.rb +0 -180
  270. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  271. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  272. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  273. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  274. data/lib/openhab/dsl/types/types.rb +0 -83
  275. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  276. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  277. data/lib/openhab/dsl/units.rb +0 -45
  278. data/lib/openhab/log/configuration.rb +0 -21
  279. data/lib/openhab/log/logger.rb +0 -282
  280. data/lib/openhab/version.rb +0 -9
  281. data/lib/openhab.rb +0 -36
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "time"
4
+
5
+ module OpenHAB
6
+ module CoreExt
7
+ module Java
8
+ ZonedDateTime = java.time.ZonedDateTime
9
+
10
+ # Extensions to {java.time.ZonedDateTime}
11
+ class ZonedDateTime
12
+ include Time
13
+ include Between
14
+
15
+ class << self # rubocop:disable Lint/EmptyClass
16
+ # @!scope class
17
+
18
+ # @!attribute [r] now
19
+ # @return [ZonedDateTime]
20
+
21
+ # @!method parse(text, formatter = nil)
22
+ # Parses a string into a ZonedDateTime object.
23
+ #
24
+ # @param [String] text The text to parse.
25
+ # @param [java.time.format.DateTimeFormatter] formatter The formatter to use.
26
+ # @return [ZonedDateTime]
27
+ end
28
+
29
+ # @!scope instance
30
+
31
+ # @return [LocalTime]
32
+ def to_local_time(_context = nil)
33
+ toLocalTime
34
+ end
35
+
36
+ # @return [Month]
37
+ alias_method :to_month, :month
38
+
39
+ # @param [TemporalAmount, #to_zoned_date_time, Numeric] other
40
+ # If other is a Numeric, it's interpreted as seconds.
41
+ # @return [Duration] If other responds to #to_zoned_date_time
42
+ # @return [ZonedDateTime] If other is a TemporalAmount
43
+ def -(other)
44
+ if other.respond_to?(:to_zoned_date_time)
45
+ java.time.Duration.between(other.to_zoned_date_time, self)
46
+ elsif other.is_a?(Numeric)
47
+ minus(other.seconds)
48
+ else
49
+ minus(other)
50
+ end
51
+ end
52
+
53
+ # @param [TemporalAmount, Numeric] other
54
+ # If other is a Numeric, it's interpreted as seconds.
55
+ # @return [ZonedDateTime]
56
+ def +(other)
57
+ return plus(other.seconds) if other.is_a?(Numeric)
58
+
59
+ plus(other)
60
+ end
61
+
62
+ #
63
+ # The number of seconds since the Unix epoch.
64
+ #
65
+ # @return [Integer]
66
+ def to_i
67
+ to_instant.epoch_second
68
+ end
69
+
70
+ #
71
+ # The number of seconds since the Unix epoch.
72
+ #
73
+ # @return [Float]
74
+ def to_f
75
+ to_instant.to_epoch_milli / 1000.0
76
+ end
77
+
78
+ # @return [Date]
79
+ def to_date
80
+ Date.new(year, month_value, day_of_month)
81
+ end
82
+
83
+ # @return [LocalDate]
84
+ def to_local_date(_context = nil)
85
+ toLocalDate
86
+ end
87
+
88
+ # @return [MonthDay]
89
+ def to_month_day
90
+ MonthDay.of(month, day_of_month)
91
+ end
92
+
93
+ # This comes from JRuby
94
+
95
+ # @!method to_time
96
+ # @return [Time]
97
+
98
+ # @param [ZonedDateTime, nil] context
99
+ # A {ZonedDateTime} used to fill in missing fields
100
+ # during conversion. Not used in this class.
101
+ # @return [self]
102
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
103
+ self
104
+ end
105
+
106
+ # @group Ephemeris Methods
107
+ # (see CoreExt::Ephemeris)
108
+
109
+ #
110
+ # Name of the holiday for this date.
111
+ #
112
+ # @param [String, nil] holiday_file Optional path to XML file to use for holiday definitions.
113
+ # @return [Symbol, nil]
114
+ #
115
+ # @example
116
+ # MonthDay.parse("12-25").holiday # => :christmas
117
+ #
118
+ def holiday(holiday_file = nil)
119
+ ::Ephemeris.get_bank_holiday_name(*[self, holiday_file || DSL.holiday_file].compact)&.downcase&.to_sym
120
+ end
121
+
122
+ #
123
+ # Determines if this date is on a holiday.
124
+ #
125
+ # @param [String, nil] holiday_file Optional path to XML file to use for holiday definitions.
126
+ # @return [true, false]
127
+ #
128
+ def holiday?(holiday_file = nil)
129
+ ::Ephemeris.bank_holiday?(*[self, holiday_file || DSL.holiday_file].compact)
130
+ end
131
+
132
+ #
133
+ # Name of the closest holiday on or after this date.
134
+ #
135
+ # @param [String, nil] holiday_file Optional path to XML file to use for holiday definitions.
136
+ # @return [Symbol]
137
+ #
138
+ def next_holiday(holiday_file = nil)
139
+ ::Ephemeris.get_next_bank_holiday(*[self, holiday_file || DSL.holiday_file].compact).downcase.to_sym
140
+ end
141
+
142
+ #
143
+ # Determines if this time is during a weekend.
144
+ #
145
+ # @return [true, false]
146
+ #
147
+ # @example
148
+ # Time.now.weekend?
149
+ #
150
+ def weekend?
151
+ ::Ephemeris.weekend?(self)
152
+ end
153
+
154
+ #
155
+ # Determines if this time is during a specific dayset
156
+ #
157
+ # @param [String, Symbol] set
158
+ # @return [true, false]
159
+ #
160
+ # @example
161
+ # Time.now.in_dayset?("school")
162
+ #
163
+ def in_dayset?(set)
164
+ ::Ephemeris.in_dayset?(set.to_s, self)
165
+ end
166
+
167
+ #
168
+ # Calculate the number of days until a specific holiday
169
+ #
170
+ # @param [String, Symbol] holiday
171
+ # @param [String, nil] holiday_file Optional path to XML file to use for holiday definitions.
172
+ # @return [Integer]
173
+ # @raise [ArgumentError] if the holiday isn't valid
174
+ #
175
+ # @example
176
+ # Time.now.days_until(:christmas) # => 2
177
+ #
178
+ def days_until(holiday, holiday_file = nil)
179
+ holiday = holiday.to_s.upcase
180
+ r = ::Ephemeris.get_days_until(*[self, holiday, holiday_file || DSL.holiday_file].compact)
181
+ raise ArgumentError, "#{holiday.inspect} isn't a recognized holiday" if r == -1
182
+
183
+ r
184
+ end
185
+
186
+ # @endgroup
187
+
188
+ # @return [Integer, nil]
189
+ def <=>(other)
190
+ # compare instants, otherwise it will differ by timezone, which we don't want
191
+ # (use eql? if you care about that)
192
+ if other.respond_to?(:to_zoned_date_time)
193
+ to_instant.compare_to(other.to_zoned_date_time(self).to_instant)
194
+ elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
195
+ lhs <=> rhs
196
+ end
197
+ end
198
+
199
+ #
200
+ # Converts `other` to {ZonedDateTime}, if possible
201
+ #
202
+ # @param [#to_zoned_date_time] other
203
+ # @return [Array, nil]
204
+ #
205
+ def coerce(other)
206
+ [other.to_zoned_date_time(self), self] if other.respond_to?(:to_zoned_date_time)
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
212
+
213
+ # @!parse ZonedDateTime = OpenHAB::CoreExt::Java::ZonedDateTime
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Array
4
+ class Array
5
+ #
6
+ # Ensure an object is an array, by wrapping
7
+ # it in an array if it's not already an array.
8
+ # @return [Array]
9
+ #
10
+ # @see https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/array/wrap.rb
11
+ #
12
+ def self.wrap(object)
13
+ if object.nil?
14
+ []
15
+ elsif object.respond_to?(:to_ary)
16
+ object.to_ary
17
+ else
18
+ [object]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ # Extensions to Date
6
+ class Date
7
+ include OpenHAB::CoreExt::Between
8
+ include OpenHAB::CoreExt::Ephemeris
9
+
10
+ #
11
+ # Extends {#+} to allow adding a {java.time.temporal.TemporalAmount TemporalAmount}
12
+ #
13
+ # @param [java.time.temporal.TemporalAmount] other
14
+ # @return [LocalDate] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
15
+ #
16
+ def plus_with_temporal(other)
17
+ return to_local_date + other if other.is_a?(java.time.temporal.TemporalAmount)
18
+
19
+ plus_without_temporal(other)
20
+ end
21
+ alias_method :plus_without_temporal, :+
22
+ alias_method :+, :plus_with_temporal
23
+
24
+ #
25
+ # Extends {#-} to allow subtracting a {java.time.temporal.TemporalAmount TemporalAmount}
26
+ #
27
+ # @param [java.time.temporal.TemporalAmount] other
28
+ # @return [LocalDate] If other is a {java.time.temporal.TemporalAmount TemporalAmount}
29
+ #
30
+ def minus_with_temporal(other)
31
+ case other
32
+ when java.time.temporal.TemporalAmount, java.time.LocalDate
33
+ to_local_date - other
34
+ else
35
+ minus_without_temporal(other)
36
+ end
37
+ end
38
+ alias_method :minus_without_temporal, :-
39
+ alias_method :-, :minus_with_temporal
40
+
41
+ # @return [LocalDate]
42
+ def to_local_date(_context = nil)
43
+ java.time.LocalDate.of(year, month, day)
44
+ end
45
+
46
+ # @return [Month]
47
+ def to_month
48
+ java.time.Month.of(month)
49
+ end
50
+
51
+ # @return [MonthDay]
52
+ def to_month_day
53
+ java.time.MonthDay.of(month, day)
54
+ end
55
+
56
+ # @param [ZonedDateTime, nil] context
57
+ # A {ZonedDateTime} used to fill in missing fields during conversion.
58
+ # {OpenHAB::CoreExt::Java::ZonedDateTime.now ZonedDateTime.now} is assumed
59
+ # if not given.
60
+ # @return [ZonedDateTime]
61
+ def to_zoned_date_time(context = nil)
62
+ to_local_date.to_zoned_date_time(context)
63
+ end
64
+
65
+ # @return [Integer, nil]
66
+ def compare_with_coercion(other)
67
+ return compare_without_coercion(other) if other.is_a?(self.class)
68
+
69
+ return self <=> other.to_date(self) if other.is_a?(java.time.MonthDay)
70
+
71
+ if other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
72
+ return lhs <=> rhs
73
+ end
74
+
75
+ compare_without_coercion(other)
76
+ end
77
+ alias_method :compare_without_coercion, :<=>
78
+ alias_method :<=>, :compare_with_coercion
79
+
80
+ #
81
+ # Convert `other` to Date, if possible.
82
+ #
83
+ # @param [#to_date] other
84
+ # @return [Array, nil]
85
+ #
86
+ def coerce(other)
87
+ return nil unless other.respond_to?(:to_date)
88
+ return [other.to_date, self] if other.method(:to_date).arity.zero?
89
+
90
+ [other.to_date(self), self]
91
+ end
92
+
93
+ remove_method :inspect
94
+ # @return [String]
95
+ alias_method :inspect, :to_s
96
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "forwardable"
5
+
6
+ # DateTime inherits from Date, but is more similar to Time semantically.
7
+ # So it avoid alias_method chain bombs, and to ensure the correct end methods
8
+ # exist here, we define the important methods from Time here as well.
9
+
10
+ # Extensions to DateTime
11
+ class DateTime < Date
12
+ extend Forwardable
13
+ include OpenHAB::CoreExt::Between
14
+ include OpenHAB::CoreExt::Ephemeris
15
+
16
+ # (see Time#plus_with_temporal)
17
+ def plus_with_temporal(other)
18
+ return to_zoned_date_time + other if other.is_a?(java.time.temporal.TemporalAmount)
19
+
20
+ plus_without_temporal(other)
21
+ end
22
+ # alias_method :plus_without_temporal, :+ # already done by Date
23
+ alias_method :+, :plus_with_temporal
24
+
25
+ # (see Time#minus_with_temporal)
26
+ def minus_with_temporal(other)
27
+ return to_zoned_date_time - other if other.is_a?(java.time.temporal.TemporalAmount)
28
+
29
+ # Exclude subtracting against the same class
30
+ if other.respond_to?(:to_zoned_date_time) && !other.is_a?(self.class)
31
+ return to_zoned_date_time - other.to_zoned_date_time
32
+ end
33
+
34
+ minus_without_temporal(other)
35
+ end
36
+ # alias_method :minus_without_temporal, :- # already done by Date
37
+ alias_method :-, :minus_with_temporal
38
+
39
+ # @!method to_local_time
40
+ # @return [LocalTime]
41
+ def_delegator :to_zoned_date_time, :to_local_time
42
+
43
+ # (see Time#to_zoned_date_time)
44
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
45
+ to_java(ZonedDateTime)
46
+ end
47
+
48
+ # (see Time#coerce)
49
+ def coerce(other)
50
+ return unless other.respond_to?(:to_zoned_date_time)
51
+
52
+ zdt = to_zoned_date_time
53
+ [other.to_zoned_date_time(zdt), zdt]
54
+ end
55
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Module
4
+ class Module
5
+ #
6
+ # Returns the name of the class or module, without any containing module or package.
7
+ #
8
+ # @return [String, nil]
9
+ #
10
+ def simple_name
11
+ return unless name
12
+
13
+ @simple_name ||= java_class&.simple_name || name.split("::").last
14
+ end
15
+ end
@@ -0,0 +1,195 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Integer
4
+ class Integer
5
+ class << self
6
+ #
7
+ # @!macro def_duration_method
8
+ # @!method $1
9
+ #
10
+ # Create {Duration} of `self` $1
11
+ #
12
+ # @return [Duration]
13
+ #
14
+ # @!visibility private
15
+ def def_duration_method(unit)
16
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
17
+ def #{unit} # def seconds
18
+ java.time.Duration.of_#{unit}(self) # java.time.Duration.of_seconds(self)
19
+ end # end
20
+ RUBY
21
+ end
22
+
23
+ #
24
+ # @!macro def_period_method
25
+ # @!method $1
26
+ #
27
+ # Create {Period} of `self` $1
28
+ #
29
+ # @return [Period]
30
+ #
31
+ # @!visibility private
32
+ def def_period_method(unit)
33
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
34
+ def #{unit} # def days
35
+ java.time.Period.of_#{unit}(self) # java.time.Period.of_days(self)
36
+ end # end
37
+ RUBY
38
+ end
39
+ end
40
+
41
+ #
42
+ # Create {Duration} of `self` milliseconds
43
+ #
44
+ # @return [Duration]
45
+ #
46
+ def milliseconds
47
+ Duration.of_millis(self)
48
+ end
49
+ alias_method :millisecond, :milliseconds
50
+ alias_method :ms, :milliseconds
51
+ def_duration_method(:seconds)
52
+ alias_method :second, :seconds
53
+ def_duration_method(:minutes)
54
+ alias_method :minute, :minutes
55
+ def_duration_method(:hours)
56
+ alias_method :hour, :hours
57
+ def_period_method(:days)
58
+ alias_method :day, :days
59
+ def_period_method(:months)
60
+ alias_method :month, :months
61
+ def_period_method(:years)
62
+ alias_method :year, :years
63
+ end
64
+
65
+ # Extensions to Float
66
+ class Float
67
+ #
68
+ # Create {Duration} of `self` milliseconds
69
+ #
70
+ # @return [Duration]
71
+ #
72
+ def milliseconds
73
+ java.time.Duration.of_nanos((self * 1_000_000).to_i)
74
+ end
75
+ alias_method :millisecond, :milliseconds
76
+ alias_method :ms, :milliseconds
77
+
78
+ #
79
+ # Create {Duration} of `self` seconds
80
+ #
81
+ # @return [Duration]
82
+ #
83
+ def seconds
84
+ (self * 1000).milliseconds
85
+ end
86
+ alias_method :second, :seconds
87
+
88
+ #
89
+ # Create {Duration} of `self` minutes
90
+ #
91
+ # @return [Duration]
92
+ #
93
+ def minutes
94
+ (self * 60).seconds
95
+ end
96
+ alias_method :minute, :minutes
97
+
98
+ #
99
+ # Create {Duration} of `self` hours
100
+ #
101
+ # @return [Duration]
102
+ #
103
+ def hours
104
+ (self * 60).minutes
105
+ end
106
+ alias_method :hour, :hours
107
+
108
+ #
109
+ # Create {Duration} of `self` days
110
+ #
111
+ # @return [Duration]
112
+ #
113
+ def days
114
+ (self * 24).hours
115
+ end
116
+ alias_method :day, :days
117
+
118
+ #
119
+ # Create {Duration} of `self` months
120
+ #
121
+ # @return [Duration]
122
+ #
123
+ def months
124
+ (self * java.time.temporal.ChronoUnit::MONTHS.duration.to_i).seconds
125
+ end
126
+ alias_method :month, :months
127
+
128
+ #
129
+ # Create {Duration} of `self` years
130
+ #
131
+ # @return [Duration]
132
+ #
133
+ def years
134
+ (self * java.time.temporal.ChronoUnit::YEARS.duration.to_i).seconds
135
+ end
136
+ alias_method :year, :years
137
+ end
138
+
139
+ module OpenHAB
140
+ module CoreExt
141
+ module Ruby
142
+ #
143
+ # Extend Numeric to create quantity object
144
+ #
145
+ module QuantityTypeConversion
146
+ #
147
+ # Convert Numeric to a QuantityType
148
+ #
149
+ # @param [String, javax.measure.Unit] unit
150
+ #
151
+ # @return [QuantityType] `self` as a {QuantityType} of the supplied Unit
152
+ #
153
+ def |(unit) # rubocop:disable Naming/BinaryOperatorParameterName
154
+ if unit.respond_to?(:to_str)
155
+ parsed_unit = org.openhab.core.types.util.UnitUtils.parse_unit(unit.to_str)
156
+ raise ArgumentError, "Unknown unit #{unit}" unless parsed_unit
157
+
158
+ unit = parsed_unit
159
+ end
160
+
161
+ return super unless unit.is_a?(javax.measure.Unit)
162
+
163
+ Core::Types::QuantityType.new(to_d.to_java, unit)
164
+ end
165
+ end
166
+
167
+ # Extensions to Numeric
168
+ module Numeric
169
+ include QuantityTypeConversion
170
+ # non-Integer/Float (i.e. BigDecimal) can still be converted to Duration, via converting to float first
171
+ extend Forwardable
172
+ def_delegators :to_f,
173
+ :milliseconds,
174
+ :millisecond,
175
+ :ms,
176
+ :seconds,
177
+ :second,
178
+ :minutes,
179
+ :minute,
180
+ :hours,
181
+ :hour,
182
+ :day,
183
+ :days,
184
+ :month,
185
+ :months,
186
+ :year,
187
+ :years
188
+ end
189
+
190
+ # Integer already has #|, so we have to prepend it here
191
+ ::Integer.prepend(QuantityTypeConversion)
192
+ ::Numeric.include(Numeric)
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module CoreExt
5
+ module Ruby
6
+ # Extensions to Range
7
+ module Range
8
+ def cover?(object)
9
+ # inverted circular range
10
+ if circular?
11
+ return object >= self.begin || object < self.end if exclude_end?
12
+
13
+ return object >= self.begin || object <= self.end
14
+ end
15
+ super
16
+ end
17
+
18
+ # Delegates to {#cover?} if {#circular?}
19
+ # @!visibility private
20
+ def ===(object)
21
+ return cover?(object) if circular?
22
+
23
+ super
24
+ end
25
+
26
+ # normal Range#each will not yield at all if begin > end
27
+ def each
28
+ return super unless circular?
29
+ return to_enum(:each) unless block_given?
30
+
31
+ val = self.begin
32
+ loop do
33
+ is_end = val == self.end
34
+ break if is_end && exclude_end?
35
+
36
+ yield val
37
+ break if is_end
38
+
39
+ val = val.succ
40
+ end
41
+ end
42
+
43
+ #
44
+ # Checks if this range is circular
45
+ #
46
+ # A circular range is one whose data type will repeat if keep you keep
47
+ # calling #succ on it, and whose beginning is after its end.
48
+ #
49
+ # Used by {#cover?} to check if the value is between `end` and `begin`,
50
+ # instead of `begin` and `end`.
51
+ #
52
+ # @return [true, false]
53
+ #
54
+ def circular?
55
+ return false if self.begin.nil? || self.end.nil?
56
+ return false if self.begin <= self.end
57
+
58
+ case self.begin || self.end
59
+ when java.time.LocalTime, java.time.MonthDay, java.time.Month
60
+ true
61
+ else
62
+ false
63
+ end
64
+ end
65
+
66
+ ::Range.prepend(self)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Symbol
4
+ class Symbol
5
+ # Ruby 3.0 already has #name
6
+ alias_method :name, :to_s unless instance_methods.include?(:name)
7
+ end