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.
- checksums.yaml +4 -4
- data/lib/openhab/core/actions/audio.rb +47 -0
- data/lib/openhab/core/actions/ephemeris.rb +39 -0
- data/lib/openhab/core/actions/exec.rb +51 -0
- data/lib/openhab/core/actions/http.rb +80 -0
- data/lib/openhab/core/actions/ping.rb +30 -0
- data/lib/openhab/core/actions/transformation.rb +32 -0
- data/lib/openhab/core/actions/voice.rb +36 -0
- data/lib/openhab/core/actions.rb +82 -0
- data/lib/openhab/core/dependency_tracking.rb +34 -0
- data/lib/openhab/core/dto/item_channel_link.rb +33 -0
- data/lib/openhab/core/dto/thing.rb +27 -0
- data/lib/openhab/core/dto.rb +11 -0
- data/lib/openhab/core/entity_lookup.rb +152 -70
- data/lib/openhab/core/events/abstract_event.rb +18 -0
- data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
- data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
- data/lib/openhab/core/events/item_command_event.rb +78 -0
- data/lib/openhab/core/events/item_event.rb +22 -0
- data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
- data/lib/openhab/core/events/item_state_event.rb +79 -0
- data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
- data/lib/openhab/core/events.rb +10 -0
- data/lib/openhab/core/items/accepted_data_types.rb +29 -0
- data/lib/openhab/core/items/color_item.rb +52 -0
- data/lib/openhab/core/items/contact_item.rb +52 -0
- data/lib/openhab/core/items/date_time_item.rb +59 -0
- data/lib/openhab/core/items/dimmer_item.rb +148 -0
- data/lib/openhab/core/items/generic_item.rb +292 -0
- data/lib/openhab/core/items/group_item.rb +176 -0
- data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
- data/lib/openhab/core/items/item.rb +273 -0
- data/lib/openhab/core/items/location_item.rb +34 -0
- data/lib/openhab/core/items/metadata/hash.rb +433 -0
- data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
- data/lib/openhab/core/items/metadata/provider.rb +48 -0
- data/lib/openhab/core/items/metadata.rb +11 -0
- data/lib/openhab/core/items/number_item.rb +62 -0
- data/lib/openhab/core/items/numeric_item.rb +22 -0
- data/lib/openhab/core/items/persistence.rb +416 -0
- data/lib/openhab/core/items/player_item.rb +66 -0
- data/lib/openhab/core/items/provider.rb +44 -0
- data/lib/openhab/core/items/proxy.rb +136 -0
- data/lib/openhab/core/items/registry.rb +86 -0
- data/lib/openhab/core/items/rollershutter_item.rb +68 -0
- data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
- data/lib/openhab/core/items/semantics.rb +473 -0
- data/lib/openhab/core/items/state_storage.rb +53 -0
- data/lib/openhab/core/items/string_item.rb +28 -0
- data/lib/openhab/core/items/switch_item.rb +78 -0
- data/lib/openhab/core/items.rb +108 -0
- data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
- data/lib/openhab/core/profile_factory.rb +132 -0
- data/lib/openhab/core/provider.rb +230 -0
- data/lib/openhab/core/proxy.rb +130 -0
- data/lib/openhab/core/registry.rb +40 -0
- data/lib/openhab/core/rules/module.rb +26 -0
- data/lib/openhab/core/rules/provider.rb +25 -0
- data/lib/openhab/core/rules/registry.rb +76 -0
- data/lib/openhab/core/rules/rule.rb +150 -0
- data/lib/openhab/core/rules.rb +25 -0
- data/lib/openhab/core/script_handling.rb +78 -20
- data/lib/openhab/core/things/channel.rb +48 -0
- data/lib/openhab/core/things/channel_uid.rb +51 -0
- data/lib/openhab/core/things/item_channel_link.rb +33 -0
- data/lib/openhab/core/things/links/provider.rb +78 -0
- data/lib/openhab/core/things/profile_callback.rb +52 -0
- data/lib/openhab/core/things/provider.rb +29 -0
- data/lib/openhab/core/things/proxy.rb +87 -0
- data/lib/openhab/core/things/registry.rb +73 -0
- data/lib/openhab/core/things/thing.rb +194 -0
- data/lib/openhab/core/things.rb +22 -0
- data/lib/openhab/core/timer.rb +148 -0
- data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
- data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
- data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
- data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
- data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
- data/lib/openhab/core/types/next_previous_type.rb +34 -0
- data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
- data/lib/openhab/core/types/on_off_type.rb +46 -0
- data/lib/openhab/core/types/open_closed_type.rb +41 -0
- data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
- data/lib/openhab/core/types/play_pause_type.rb +38 -0
- data/lib/openhab/core/types/point_type.rb +117 -0
- data/lib/openhab/core/types/quantity_type.rb +325 -0
- data/lib/openhab/core/types/raw_type.rb +26 -0
- data/lib/openhab/core/types/refresh_type.rb +27 -0
- data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
- data/lib/openhab/core/types/stop_move_type.rb +34 -0
- data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
- data/lib/openhab/{dsl → core}/types/type.rb +42 -40
- data/lib/openhab/core/types/un_def_type.rb +38 -0
- data/lib/openhab/core/types/up_down_type.rb +50 -0
- data/lib/openhab/core/types.rb +82 -0
- data/lib/openhab/{dsl → core}/uid.rb +4 -23
- data/lib/openhab/core/value_cache.rb +188 -0
- data/lib/openhab/core.rb +98 -0
- data/lib/openhab/core_ext/between.rb +32 -0
- data/lib/openhab/core_ext/ephemeris.rb +53 -0
- data/lib/openhab/core_ext/java/class.rb +34 -0
- data/lib/openhab/core_ext/java/duration.rb +142 -0
- data/lib/openhab/core_ext/java/list.rb +436 -0
- data/lib/openhab/core_ext/java/local_date.rb +104 -0
- data/lib/openhab/core_ext/java/local_time.rb +118 -0
- data/lib/openhab/core_ext/java/map.rb +66 -0
- data/lib/openhab/core_ext/java/month.rb +71 -0
- data/lib/openhab/core_ext/java/month_day.rb +119 -0
- data/lib/openhab/core_ext/java/period.rb +103 -0
- data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
- data/lib/openhab/core_ext/java/time.rb +62 -0
- data/lib/openhab/core_ext/java/unit.rb +15 -0
- data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
- data/lib/openhab/core_ext/ruby/array.rb +21 -0
- data/lib/openhab/core_ext/ruby/date.rb +96 -0
- data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
- data/lib/openhab/core_ext/ruby/module.rb +15 -0
- data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
- data/lib/openhab/core_ext/ruby/range.rb +70 -0
- data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
- data/lib/openhab/core_ext/ruby/time.rb +108 -0
- data/lib/openhab/core_ext.rb +18 -0
- data/lib/openhab/dsl/debouncer.rb +259 -0
- data/lib/openhab/dsl/events/watch_event.rb +18 -0
- data/lib/openhab/dsl/events.rb +9 -0
- data/lib/openhab/dsl/gems.rb +1 -1
- data/lib/openhab/dsl/items/builder.rb +578 -0
- data/lib/openhab/dsl/items/ensure.rb +73 -82
- data/lib/openhab/dsl/items/timed_command.rb +214 -159
- data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
- data/lib/openhab/dsl/rules/builder.rb +1935 -0
- data/lib/openhab/dsl/rules/guard.rb +51 -114
- data/lib/openhab/dsl/rules/name_inference.rb +66 -25
- data/lib/openhab/dsl/rules/property.rb +48 -75
- data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
- data/lib/openhab/dsl/rules/terse.rb +58 -14
- data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
- data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
- data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
- data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
- data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
- data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
- data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
- data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
- data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
- data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
- data/lib/openhab/dsl/rules/triggers.rb +12 -0
- data/lib/openhab/dsl/rules.rb +8 -0
- data/lib/openhab/dsl/things/builder.rb +299 -0
- data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
- data/lib/openhab/dsl/timer_manager.rb +204 -0
- data/lib/openhab/dsl/version.rb +9 -0
- data/lib/openhab/dsl.rb +979 -0
- data/lib/openhab/log.rb +355 -0
- data/lib/openhab/osgi.rb +68 -0
- data/lib/openhab/rspec/configuration.rb +56 -0
- data/lib/openhab/rspec/example_group.rb +132 -0
- data/lib/openhab/rspec/helpers.rb +458 -0
- data/lib/openhab/rspec/hooks.rb +113 -0
- data/lib/openhab/rspec/jruby.rb +46 -0
- data/lib/openhab/rspec/karaf.rb +851 -0
- data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
- data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
- data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
- data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
- data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
- data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
- data/lib/openhab/rspec/mocks/space.rb +23 -0
- data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
- data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
- data/lib/openhab/rspec/mocks/timer.rb +134 -0
- data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
- data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
- data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
- data/lib/openhab/rspec/shell.rb +31 -0
- data/lib/openhab/rspec/suspend_rules.rb +50 -0
- data/lib/openhab/rspec.rb +26 -0
- data/lib/openhab/yard/base_helper.rb +19 -0
- data/lib/openhab/yard/cli/stats.rb +23 -0
- data/lib/openhab/yard/code_objects/group_object.rb +23 -0
- data/lib/openhab/yard/code_objects/java/base.rb +31 -0
- data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
- data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
- data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
- data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
- data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
- data/lib/openhab/yard/coderay.rb +17 -0
- data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
- data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
- data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
- data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
- data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
- data/lib/openhab/yard/html_helper.rb +78 -0
- data/lib/openhab/yard/markdown_helper.rb +148 -0
- data/lib/openhab/yard/tags/constant_directive.rb +20 -0
- data/lib/openhab/yard/tags/group_directive.rb +24 -0
- data/lib/openhab/yard/tags/library.rb +3 -0
- data/lib/openhab/yard.rb +38 -0
- metadata +475 -106
- data/lib/openhab/core/item_proxy.rb +0 -29
- data/lib/openhab/core/load_path.rb +0 -19
- data/lib/openhab/core/openhab_setup.rb +0 -29
- data/lib/openhab/core/osgi.rb +0 -58
- data/lib/openhab/core/services.rb +0 -24
- data/lib/openhab/dsl/actions.rb +0 -114
- data/lib/openhab/dsl/between.rb +0 -25
- data/lib/openhab/dsl/channel.rb +0 -43
- data/lib/openhab/dsl/dsl.rb +0 -59
- data/lib/openhab/dsl/group.rb +0 -54
- data/lib/openhab/dsl/imports.rb +0 -21
- data/lib/openhab/dsl/items/color_item.rb +0 -76
- data/lib/openhab/dsl/items/comparable_item.rb +0 -62
- data/lib/openhab/dsl/items/contact_item.rb +0 -41
- data/lib/openhab/dsl/items/date_time_item.rb +0 -65
- data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
- data/lib/openhab/dsl/items/generic_item.rb +0 -229
- data/lib/openhab/dsl/items/group_item.rb +0 -127
- data/lib/openhab/dsl/items/item_equality.rb +0 -59
- data/lib/openhab/dsl/items/item_registry.rb +0 -54
- data/lib/openhab/dsl/items/items.rb +0 -109
- data/lib/openhab/dsl/items/location_item.rb +0 -59
- data/lib/openhab/dsl/items/metadata.rb +0 -326
- data/lib/openhab/dsl/items/number_item.rb +0 -17
- data/lib/openhab/dsl/items/numeric_item.rb +0 -87
- data/lib/openhab/dsl/items/persistence.rb +0 -307
- data/lib/openhab/dsl/items/player_item.rb +0 -58
- data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
- data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
- data/lib/openhab/dsl/items/semantics.rb +0 -227
- data/lib/openhab/dsl/items/string_item.rb +0 -51
- data/lib/openhab/dsl/items/switch_item.rb +0 -70
- data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
- data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
- data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
- data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
- data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
- data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
- data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
- data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
- data/lib/openhab/dsl/openhab.rb +0 -30
- data/lib/openhab/dsl/persistence.rb +0 -27
- data/lib/openhab/dsl/rules/item_event.rb +0 -19
- data/lib/openhab/dsl/rules/rule.rb +0 -160
- data/lib/openhab/dsl/rules/rule_config.rb +0 -147
- data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
- data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
- data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
- data/lib/openhab/dsl/states.rb +0 -89
- data/lib/openhab/dsl/things.rb +0 -147
- data/lib/openhab/dsl/time/month_day.rb +0 -180
- data/lib/openhab/dsl/time/time_of_day.rb +0 -235
- data/lib/openhab/dsl/timers/manager.rb +0 -119
- data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
- data/lib/openhab/dsl/timers/timer.rb +0 -132
- data/lib/openhab/dsl/timers.rb +0 -77
- data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
- data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
- data/lib/openhab/dsl/types/on_off_type.rb +0 -28
- data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
- data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
- data/lib/openhab/dsl/types/point_type.rb +0 -180
- data/lib/openhab/dsl/types/quantity_type.rb +0 -265
- data/lib/openhab/dsl/types/refresh_type.rb +0 -18
- data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
- data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
- data/lib/openhab/dsl/types/types.rb +0 -83
- data/lib/openhab/dsl/types/un_def_type.rb +0 -22
- data/lib/openhab/dsl/types/up_down_type.rb +0 -32
- data/lib/openhab/dsl/units.rb +0 -45
- data/lib/openhab/log/configuration.rb +0 -21
- data/lib/openhab/log/logger.rb +0 -282
- data/lib/openhab/version.rb +0 -9
- data/lib/openhab.rb +0 -36
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "time"
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module CoreExt
|
|
7
|
+
module Java
|
|
8
|
+
java_import java.time.LocalTime
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# Extensions to {java.time.LocalTime}
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# break_time = LocalTime::NOON
|
|
15
|
+
#
|
|
16
|
+
# if LocalTime.now > LocalTime.of(17, 30) # comparing two LocalTime objects
|
|
17
|
+
# # do something
|
|
18
|
+
# elsif LocalTime.now < LocalTime.parse('8:30') # comparison against a string
|
|
19
|
+
# # do something
|
|
20
|
+
# end
|
|
21
|
+
# four_pm = LocalTime.parse('16:00')
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# # Trigger security light between sunset and sunrise when motion is detected
|
|
25
|
+
# rule 'Outside Light Motion' do
|
|
26
|
+
# updated Motion_Sensor, to: OPEN
|
|
27
|
+
# run do
|
|
28
|
+
# astro = things['astro:sun:home']
|
|
29
|
+
# sunrise = astro.get_event_time('SUN_RISE', nil, nil).to_local_time
|
|
30
|
+
# sunset = astro.get_event_time('SUN_SET', nil, nil).to_local_time
|
|
31
|
+
# next if (sunrise..sunset).cover?(Time.now)
|
|
32
|
+
#
|
|
33
|
+
# Security_Light.on for: 10.minutes
|
|
34
|
+
# end
|
|
35
|
+
# end
|
|
36
|
+
#
|
|
37
|
+
class LocalTime
|
|
38
|
+
include Between
|
|
39
|
+
# @!parse include Time
|
|
40
|
+
|
|
41
|
+
class << self
|
|
42
|
+
# @!attribute [r] now
|
|
43
|
+
# @return [LocalTime]
|
|
44
|
+
|
|
45
|
+
# @!visibility private
|
|
46
|
+
alias_method :raw_parse, :parse
|
|
47
|
+
|
|
48
|
+
#
|
|
49
|
+
# Parses strings in the form "h[:mm[:ss]] [am/pm]" when no formatter is given.
|
|
50
|
+
#
|
|
51
|
+
# @param [String] string
|
|
52
|
+
# @param [java.time.format.DateTimeFormatter] formatter The formatter to use
|
|
53
|
+
# @return [LocalTime]
|
|
54
|
+
#
|
|
55
|
+
def parse(string, formatter = nil)
|
|
56
|
+
return raw_parse(string, formatter) if formatter
|
|
57
|
+
|
|
58
|
+
format = /(am|pm)$/i.match?(string) ? "h[:mm[:ss][.S]][ ]a" : "H[:mm[:ss][.S]]"
|
|
59
|
+
java_send(:parse, [java.lang.CharSequence, java.time.format.DateTimeFormatter],
|
|
60
|
+
string, java.time.format.DateTimeFormatterBuilder.new
|
|
61
|
+
.parse_case_insensitive
|
|
62
|
+
.parse_lenient
|
|
63
|
+
.append_pattern(format)
|
|
64
|
+
.to_formatter(java.util.Locale::ENGLISH))
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @param [TemporalAmount, Numeric] other
|
|
69
|
+
# If other is a Numeric, it's interpreted as seconds.
|
|
70
|
+
# @return [LocalTime]
|
|
71
|
+
def -(other)
|
|
72
|
+
return minus(other.seconds) if other.is_a?(Numeric)
|
|
73
|
+
return self if other.is_a?(Period)
|
|
74
|
+
|
|
75
|
+
minus(other)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @param [TemporalAmount, Numeric] other
|
|
79
|
+
# If other is a Numeric, it's interpreted as seconds.
|
|
80
|
+
# @return [LocalTime]
|
|
81
|
+
def +(other)
|
|
82
|
+
return plus(other.seconds) if other.is_a?(Numeric)
|
|
83
|
+
return self if other.is_a?(Period)
|
|
84
|
+
|
|
85
|
+
plus(other)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
#
|
|
89
|
+
# Returns the next second
|
|
90
|
+
#
|
|
91
|
+
# Will loop back to the beginning of the day if necessary.
|
|
92
|
+
#
|
|
93
|
+
# @return [LocalTime]
|
|
94
|
+
#
|
|
95
|
+
def succ
|
|
96
|
+
plus_seconds(1)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @return [self]
|
|
100
|
+
def to_local_time
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# @param [ZonedDateTime, nil] context
|
|
105
|
+
# A {ZonedDateTime} used to fill in missing fields
|
|
106
|
+
# during conversion. {ZonedDateTime.now} is assumed if not given.
|
|
107
|
+
# @return [ZonedDateTime]
|
|
108
|
+
def to_zoned_date_time(context = nil)
|
|
109
|
+
context ||= ZonedDateTime.now
|
|
110
|
+
context.with(self)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
LocalTime = OpenHAB::CoreExt::Java::LocalTime unless Object.const_defined?(:LocalTime)
|
|
118
|
+
java.time.LocalTime.include(OpenHAB::CoreExt::Java::Time)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# @!visibility private
|
|
4
|
+
module Java::JavaUtil::Map # rubocop:disable Style/ClassAndModuleChildren
|
|
5
|
+
def compact
|
|
6
|
+
reject { |_k, v| v.nil? }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def compact!
|
|
10
|
+
reject! { |_k, v| v.nil? }
|
|
11
|
+
self
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def deconstruct_keys
|
|
15
|
+
self
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def except(*keys)
|
|
19
|
+
reject { |k, _v| keys.include?(k) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def slice(*keys)
|
|
23
|
+
select { |k, _v| keys.include?(k) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def transform_keys(hash2 = nil)
|
|
27
|
+
raise NotImplementedError unless hash2 || block_given?
|
|
28
|
+
|
|
29
|
+
map do |k, v| # rubocop:disable Style/MapToHash
|
|
30
|
+
if hash2&.key?(k)
|
|
31
|
+
[hash2[k], v]
|
|
32
|
+
elsif block_given?
|
|
33
|
+
[(yield k), v]
|
|
34
|
+
else
|
|
35
|
+
[k, v]
|
|
36
|
+
end
|
|
37
|
+
end.to_h
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def transform_keys!(hash2 = nil)
|
|
41
|
+
raise NotImplementedError unless hash2 || block_given?
|
|
42
|
+
|
|
43
|
+
keys.each do |k|
|
|
44
|
+
if hash2&.key?(k)
|
|
45
|
+
self[hash2[k]] = delete(k)
|
|
46
|
+
elsif block_given?
|
|
47
|
+
new_k = yield k
|
|
48
|
+
self[new_k] = delete(k) unless new_k == k
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def transform_values
|
|
55
|
+
map do |k, v| # rubocop:disable Style/MapToHash, Style/HashTransformValues
|
|
56
|
+
[k, (yield v)]
|
|
57
|
+
end.to_h
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def transform_values!
|
|
61
|
+
replace_all do |_k, v|
|
|
62
|
+
yield v
|
|
63
|
+
end
|
|
64
|
+
self
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "time"
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module CoreExt
|
|
7
|
+
module Java
|
|
8
|
+
Month = java.time.Month
|
|
9
|
+
|
|
10
|
+
# Extensions to {java.time.Month}
|
|
11
|
+
class Month
|
|
12
|
+
include Between
|
|
13
|
+
# @!parse include Time
|
|
14
|
+
|
|
15
|
+
# @return [Month]
|
|
16
|
+
def +(other)
|
|
17
|
+
plus(other)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @return [Month]
|
|
21
|
+
def -(other)
|
|
22
|
+
minus(other)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
#
|
|
26
|
+
# Returns the next month
|
|
27
|
+
#
|
|
28
|
+
# Will loop back to January if necessary.
|
|
29
|
+
#
|
|
30
|
+
# @return [Month]
|
|
31
|
+
#
|
|
32
|
+
def succ
|
|
33
|
+
plus(1)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @return [LocalDate]
|
|
37
|
+
def to_local_date(context = nil)
|
|
38
|
+
context ||= java.time.Year.now
|
|
39
|
+
year = java.time.Year.from(context)
|
|
40
|
+
year.at_month_day(to_month_day)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [Date]
|
|
44
|
+
def to_date(context = nil)
|
|
45
|
+
to_local_date(context).to_date
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @return [self]
|
|
49
|
+
def to_month
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [MonthDay]
|
|
54
|
+
def to_month_day
|
|
55
|
+
MonthDay.of(self, 1)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# @param [ZonedDateTime, nil] context
|
|
59
|
+
# A {ZonedDateTime} used to fill in missing fields
|
|
60
|
+
# during conversion. {ZonedDateTime.now} is assumed if not given.
|
|
61
|
+
# @return [ZonedDateTime]
|
|
62
|
+
def to_zoned_date_time(context = nil)
|
|
63
|
+
to_local_date(context).to_zoned_date_time(context)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
Month = OpenHAB::CoreExt::Java::Month unless Object.const_defined?(:Month)
|
|
71
|
+
java.time.Month.include(OpenHAB::CoreExt::Java::Time)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "time"
|
|
4
|
+
|
|
5
|
+
module OpenHAB
|
|
6
|
+
module CoreExt
|
|
7
|
+
module Java
|
|
8
|
+
java_import java.time.MonthDay
|
|
9
|
+
|
|
10
|
+
# Extensions to {java.time.MonthDay}
|
|
11
|
+
class MonthDay
|
|
12
|
+
include Between
|
|
13
|
+
include Ephemeris
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
#
|
|
17
|
+
# Parses strings in the form "M-d"
|
|
18
|
+
#
|
|
19
|
+
# @param [String] string
|
|
20
|
+
# @return [MonthDay]
|
|
21
|
+
#
|
|
22
|
+
def parse(string)
|
|
23
|
+
logger.trace("#{self.class}.parse #{string} (#{string.class})")
|
|
24
|
+
java_send(:parse, [java.lang.CharSequence, java.time.format.DateTimeFormatter],
|
|
25
|
+
string.to_s,
|
|
26
|
+
java.time.format.DateTimeFormatter.ofPattern("[--]M-d"))
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @return [String]
|
|
31
|
+
def to_s
|
|
32
|
+
# Remove -- from MonthDay string representation
|
|
33
|
+
to_string.delete_prefix("--")
|
|
34
|
+
end
|
|
35
|
+
alias_method :inspect, :to_s
|
|
36
|
+
|
|
37
|
+
# wait until we redefine #to_s
|
|
38
|
+
include Time
|
|
39
|
+
|
|
40
|
+
# @return [MonthDay]
|
|
41
|
+
def +(other)
|
|
42
|
+
case other
|
|
43
|
+
when java.time.temporal.TemporalAmount, Numeric
|
|
44
|
+
(LocalDate.of(1900, month, day_of_month) + other).to_month_day
|
|
45
|
+
else
|
|
46
|
+
(to_local_date(other.to_local_date) + other).to_month_day
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @return [MonthDay, Period]
|
|
51
|
+
def -(other)
|
|
52
|
+
d = case other
|
|
53
|
+
when java.time.temporal.TemporalAmount, Numeric
|
|
54
|
+
LocalDate.of(1900, month, day_of_month) - other
|
|
55
|
+
else
|
|
56
|
+
to_local_date(other.to_local_date) - other
|
|
57
|
+
end
|
|
58
|
+
return d if d.is_a?(java.time.Period)
|
|
59
|
+
|
|
60
|
+
d.to_month_day
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
#
|
|
64
|
+
# Returns the next day
|
|
65
|
+
#
|
|
66
|
+
# Will go to the next month, or loop back to January if necessary.
|
|
67
|
+
#
|
|
68
|
+
# @return [MonthDay]
|
|
69
|
+
#
|
|
70
|
+
def succ
|
|
71
|
+
if day_of_month == month.max_length
|
|
72
|
+
return MonthDay.of(1, 1) if month_value == 12
|
|
73
|
+
|
|
74
|
+
return MonthDay.of(month_value + 1, 1)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
MonthDay.of(month_value, day_of_month + 1)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# @param [TemporalAmount, nil] context
|
|
81
|
+
# A {TemporalAmount} used to fill in missing
|
|
82
|
+
# fields during conversion. {LocalDate.now} is assumed if not given.
|
|
83
|
+
# @return [LocalDate]
|
|
84
|
+
def to_local_date(context = nil)
|
|
85
|
+
context ||= java.time.Year.now
|
|
86
|
+
year = java.time.Year.from(context)
|
|
87
|
+
year.at_month_day(self)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# @return [Month]
|
|
91
|
+
alias_method :to_month, :month
|
|
92
|
+
|
|
93
|
+
# @param [Date, nil] context
|
|
94
|
+
# A {Date} used to fill in missing fields
|
|
95
|
+
# during conversion. `Date.today` is assumed if not given.
|
|
96
|
+
# @return [Date]
|
|
97
|
+
def to_date(context = nil)
|
|
98
|
+
context ||= Date.today
|
|
99
|
+
Date.new(context.year, month_value, day_of_month)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# @return [self]
|
|
103
|
+
def to_month_day
|
|
104
|
+
self
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @param [ZonedDateTime, nil] context
|
|
108
|
+
# A {ZonedDateTime} used to fill in missing fields
|
|
109
|
+
# during conversion. {ZonedDateTime.now} is assumed if not given.
|
|
110
|
+
# @return [ZonedDateTime]
|
|
111
|
+
def to_zoned_date_time(context = nil)
|
|
112
|
+
to_local_date(context).to_zoned_date_time(context)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
MonthDay = OpenHAB::CoreExt::Java::MonthDay unless Object.const_defined?(:MonthDay)
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module CoreExt
|
|
5
|
+
module Java
|
|
6
|
+
java_import java.time.Period
|
|
7
|
+
|
|
8
|
+
# Extensions to {java.time.Period}
|
|
9
|
+
class Period
|
|
10
|
+
# @!parse include TemporalAmount
|
|
11
|
+
|
|
12
|
+
#
|
|
13
|
+
# Convert to number of seconds
|
|
14
|
+
#
|
|
15
|
+
# @return [Integer]
|
|
16
|
+
#
|
|
17
|
+
def to_i
|
|
18
|
+
[java.time.temporal.ChronoUnit::YEARS,
|
|
19
|
+
java.time.temporal.ChronoUnit::MONTHS,
|
|
20
|
+
java.time.temporal.ChronoUnit::DAYS].sum do |unit|
|
|
21
|
+
get(unit) * unit.duration.to_i
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
#
|
|
26
|
+
# Convert to number of seconds
|
|
27
|
+
#
|
|
28
|
+
# @return [Float]
|
|
29
|
+
#
|
|
30
|
+
def to_f
|
|
31
|
+
to_i.to_f
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
remove_method :==
|
|
35
|
+
|
|
36
|
+
# @return [Integer, nil]
|
|
37
|
+
def <=>(other)
|
|
38
|
+
return to_i <=> other if other.is_a?(Numeric)
|
|
39
|
+
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#
|
|
44
|
+
# Convert `self` and `other` to {Duration}, if `other` is a Numeric
|
|
45
|
+
#
|
|
46
|
+
# @param [Numeric] other
|
|
47
|
+
# @return [Array, nil]
|
|
48
|
+
#
|
|
49
|
+
def coerce(other)
|
|
50
|
+
return [other.seconds, to_i.seconds] if other.is_a?(Numeric)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
plus: :+,
|
|
55
|
+
minus: :-
|
|
56
|
+
}.each do |java_op, ruby_op|
|
|
57
|
+
# def +(other)
|
|
58
|
+
# if other.is_a?(Period)
|
|
59
|
+
# plus(other)
|
|
60
|
+
# elsif other.is_a?(Numeric)
|
|
61
|
+
# self + other.seconds
|
|
62
|
+
# elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
|
|
63
|
+
# lhs + rhs
|
|
64
|
+
# else
|
|
65
|
+
# raise TypeError, "#{other.class} can't be coerced into Period"
|
|
66
|
+
# end
|
|
67
|
+
# end
|
|
68
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
|
|
69
|
+
def #{ruby_op}(other)
|
|
70
|
+
if other.is_a?(Period)
|
|
71
|
+
#{java_op}(other)
|
|
72
|
+
elsif other.is_a?(Numeric)
|
|
73
|
+
self #{ruby_op} other.seconds
|
|
74
|
+
elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
|
|
75
|
+
lhs #{ruby_op} rhs
|
|
76
|
+
else
|
|
77
|
+
raise TypeError, "\#{other.class} can't be coerced into Period"
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
RUBY
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @!visibility private
|
|
84
|
+
def *(other)
|
|
85
|
+
if other.is_a?(Integer)
|
|
86
|
+
multipliedBy(other)
|
|
87
|
+
elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
|
|
88
|
+
lhs * rhs
|
|
89
|
+
else
|
|
90
|
+
raise TypeError, "#{other.class} can't be coerced into Period"
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @!visibility private
|
|
95
|
+
def /(other)
|
|
96
|
+
to_f.seconds / other
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
Period = OpenHAB::CoreExt::Java::Period unless Object.const_defined?(:Period)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module CoreExt
|
|
5
|
+
module Java
|
|
6
|
+
java_import java.time.temporal.TemporalAmount
|
|
7
|
+
|
|
8
|
+
# Extensions to {java.time.temporal.TemporalAmount}
|
|
9
|
+
module TemporalAmount
|
|
10
|
+
# Subtract `self` to {ZonedDateTime.now}
|
|
11
|
+
# @return [ZonedDateTime]
|
|
12
|
+
def ago
|
|
13
|
+
ZonedDateTime.now - self
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Add `self` to {ZonedDateTime.now}
|
|
17
|
+
# @return [ZonedDateTime]
|
|
18
|
+
def from_now
|
|
19
|
+
ZonedDateTime.now + self
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @return [TemporalAmount]
|
|
23
|
+
def -@
|
|
24
|
+
negated
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @return [String]
|
|
28
|
+
def inspect
|
|
29
|
+
to_s
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenHAB
|
|
4
|
+
module CoreExt
|
|
5
|
+
module Java
|
|
6
|
+
# Common extensions to Java Date/Time classes
|
|
7
|
+
module Time
|
|
8
|
+
# @!parse include Comparable
|
|
9
|
+
|
|
10
|
+
# @!visibility private
|
|
11
|
+
module ClassMethods
|
|
12
|
+
# The method used to convert another object to this class
|
|
13
|
+
def coercion_method
|
|
14
|
+
@coercion_method ||= :"to_#{java_class.simple_name.gsub(/[A-Z]/, "_\\0").downcase[1..]}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Translate java.time.format.DateTimeParseException to ArgumentError
|
|
18
|
+
def parse(*)
|
|
19
|
+
super
|
|
20
|
+
rescue java.time.format.DateTimeParseException => e
|
|
21
|
+
raise ArgumentError, e.message
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @!visibility private
|
|
26
|
+
def self.included(klass)
|
|
27
|
+
klass.singleton_class.prepend(ClassMethods)
|
|
28
|
+
klass.remove_method(:==)
|
|
29
|
+
klass.alias_method(:inspect, :to_s)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
#
|
|
33
|
+
# Compare against another time object
|
|
34
|
+
#
|
|
35
|
+
# @param [Object] other The other time object to compare against.
|
|
36
|
+
#
|
|
37
|
+
# @return [Integer] -1, 0, +1 depending on whether `other` is
|
|
38
|
+
# less than, equal to, or greater than self
|
|
39
|
+
#
|
|
40
|
+
def <=>(other)
|
|
41
|
+
if other.is_a?(self.class)
|
|
42
|
+
compare_to(other)
|
|
43
|
+
elsif other.respond_to?(:coerce)
|
|
44
|
+
return nil unless (lhs, rhs = other.coerce(self))
|
|
45
|
+
|
|
46
|
+
lhs <=> rhs
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Convert `other` to this class, if possible
|
|
51
|
+
# @return [Array, nil]
|
|
52
|
+
def coerce(other)
|
|
53
|
+
coercion_method = self.class.coercion_method
|
|
54
|
+
return unless other.respond_to?(coercion_method)
|
|
55
|
+
return [other.send(coercion_method), self] if other.method(coercion_method).arity.zero?
|
|
56
|
+
|
|
57
|
+
[other.send(coercion_method, self), self]
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# @!visibility private
|
|
4
|
+
module Java::JavaxMeasure::Unit # rubocop:disable Style/ClassAndModuleChildren
|
|
5
|
+
def inspect
|
|
6
|
+
to_s
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# @!visibility private
|
|
11
|
+
module Java::JavaxMeasure::Dimension # rubocop:disable Style/ClassAndModuleChildren
|
|
12
|
+
def inspect
|
|
13
|
+
to_s
|
|
14
|
+
end
|
|
15
|
+
end
|