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
|
@@ -1,32 +1,18 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require
|
|
4
|
-
require
|
|
5
|
-
|
|
3
|
+
require "forwardable"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
require_relative "type"
|
|
6
7
|
|
|
7
8
|
module OpenHAB
|
|
8
|
-
module
|
|
9
|
+
module Core
|
|
9
10
|
module Types
|
|
10
11
|
DateTimeType = org.openhab.core.library.types.DateTimeType
|
|
11
12
|
|
|
12
|
-
#
|
|
13
|
-
::DateTimeType = DateTimeType if ::DateTimeType.is_a?(java.lang.Class)
|
|
14
|
-
|
|
15
|
-
# @deprecated
|
|
16
|
-
# Backwards-compatible alias
|
|
17
|
-
DateTime = DateTimeType
|
|
18
|
-
|
|
19
|
-
# rubocop: disable Metrics/ClassLength class has a single responsibility
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
# Add methods to core OpenHAB DateTimeType to make it behave as a Ruby
|
|
23
|
-
# Time object
|
|
24
|
-
#
|
|
25
|
-
# Any method not explicitly defined is forwarded to the +ZonedDateTime+
|
|
26
|
-
# or +Time+ representation of this object as appropriate.
|
|
27
|
-
#
|
|
13
|
+
# {DateTimeType} uses a {ZonedDateTime} internally.
|
|
28
14
|
class DateTimeType
|
|
29
|
-
# @!parse include
|
|
15
|
+
# @!parse include Command, State
|
|
30
16
|
|
|
31
17
|
# remove the JRuby default == so that we can inherit the Ruby method
|
|
32
18
|
remove_method :==
|
|
@@ -47,8 +33,8 @@ module OpenHAB
|
|
|
47
33
|
|
|
48
34
|
class << self
|
|
49
35
|
#
|
|
50
|
-
# Parses a String representing a time into
|
|
51
|
-
# using
|
|
36
|
+
# Parses a String representing a time into a {DateTimeType}. First tries to parse it
|
|
37
|
+
# using Java's parser, then falls back to the Ruby `Time.parse`.
|
|
52
38
|
#
|
|
53
39
|
# @param [String] time_string
|
|
54
40
|
#
|
|
@@ -57,32 +43,25 @@ module OpenHAB
|
|
|
57
43
|
def parse(time_string)
|
|
58
44
|
time_string = "#{time_string}Z" if TIME_ONLY_REGEX.match?(time_string)
|
|
59
45
|
DateTimeType.new(time_string)
|
|
60
|
-
rescue java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException
|
|
61
|
-
# Try
|
|
46
|
+
rescue java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException => e
|
|
47
|
+
# Try Ruby's Time.parse if DateTimeType parser fails
|
|
62
48
|
begin
|
|
63
|
-
DateTimeType.new(::Time.parse(time_string))
|
|
49
|
+
DateTimeType.new(::Time.parse(time_string).to_zoned_date_time)
|
|
64
50
|
rescue ArgumentError
|
|
65
|
-
raise ArgumentError,
|
|
51
|
+
raise ArgumentError, e.message
|
|
66
52
|
end
|
|
67
53
|
end
|
|
54
|
+
end
|
|
68
55
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
# @!visibility private
|
|
76
|
-
def parse_duration(time_string)
|
|
77
|
-
# convert from common HH:MM to ISO8601 for parsing
|
|
78
|
-
if (match = time_string.match(TIME_ONLY_REGEX))
|
|
79
|
-
time_string = "PT#{match[:hours]}H#{match[:minutes]}M#{match[:seconds] || 0}S"
|
|
80
|
-
end
|
|
81
|
-
java.time.Duration.parse(time_string)
|
|
82
|
-
end
|
|
56
|
+
# @param [ZonedDateTime, nil] context
|
|
57
|
+
# A {ZonedDateTime} used to fill in missing
|
|
58
|
+
# fields during conversion. Not used in this class.
|
|
59
|
+
# @return [ZonedTimeTime]
|
|
60
|
+
def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
|
|
61
|
+
zoned_date_time
|
|
83
62
|
end
|
|
84
63
|
|
|
85
|
-
# act like a
|
|
64
|
+
# act like a Ruby Time
|
|
86
65
|
def_delegator :zoned_date_time, :month_value, :month
|
|
87
66
|
def_delegator :zoned_date_time, :day_of_month, :mday
|
|
88
67
|
def_delegator :zoned_date_time, :day_of_year, :yday
|
|
@@ -90,32 +69,29 @@ module OpenHAB
|
|
|
90
69
|
def_delegator :zoned_date_time, :second, :sec
|
|
91
70
|
def_delegator :zoned_date_time, :nano, :nsec
|
|
92
71
|
def_delegator :zoned_date_time, :to_epoch_second, :to_i
|
|
72
|
+
def_delegator :zoned_date_time, :to_time
|
|
93
73
|
|
|
94
|
-
|
|
74
|
+
# @!visibility private
|
|
75
|
+
alias_method :day, :mday
|
|
95
76
|
|
|
96
77
|
#
|
|
97
78
|
# Create a new instance of DateTimeType
|
|
98
79
|
#
|
|
99
|
-
# @param value [
|
|
80
|
+
# @param value [#to_zoned_date_time, #to_time, #to_str, #to_d, nil]
|
|
100
81
|
#
|
|
101
|
-
def initialize(value = nil)
|
|
102
|
-
if value.respond_to?(:
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
super(
|
|
82
|
+
def initialize(value = nil)
|
|
83
|
+
if value.respond_to?(:to_zoned_date_time)
|
|
84
|
+
super(value.to_zoned_date_time)
|
|
85
|
+
return
|
|
86
|
+
elsif value.respond_to?(:to_time)
|
|
87
|
+
super(value.to_time.to_zoned_date_time)
|
|
107
88
|
return
|
|
108
89
|
elsif value.respond_to?(:to_str)
|
|
109
90
|
# strings respond_do?(:to_d), but we want to avoid that conversion
|
|
110
91
|
super(value.to_str)
|
|
111
92
|
return
|
|
112
93
|
elsif value.respond_to?(:to_d)
|
|
113
|
-
|
|
114
|
-
super(ZonedDateTime.ofInstant(
|
|
115
|
-
java.time.Instant.ofEpochSecond(time.to_i,
|
|
116
|
-
((time % 1) * 1_000_000_000).to_i),
|
|
117
|
-
java.time.ZoneId.systemDefault
|
|
118
|
-
))
|
|
94
|
+
super(Time.at(value.to_d).to_zoned_date_time)
|
|
119
95
|
return
|
|
120
96
|
end
|
|
121
97
|
|
|
@@ -125,7 +101,7 @@ module OpenHAB
|
|
|
125
101
|
#
|
|
126
102
|
# Check equality without type conversion
|
|
127
103
|
#
|
|
128
|
-
# @return [
|
|
104
|
+
# @return [true,false] if the same value is represented, without type
|
|
129
105
|
# conversion
|
|
130
106
|
def eql?(other)
|
|
131
107
|
return false unless other.instance_of?(self.class)
|
|
@@ -136,31 +112,19 @@ module OpenHAB
|
|
|
136
112
|
#
|
|
137
113
|
# Comparison
|
|
138
114
|
#
|
|
139
|
-
# @param [
|
|
140
|
-
# String] other object to compare to
|
|
115
|
+
# @param [Object] other object to compare to
|
|
141
116
|
#
|
|
142
|
-
# @return [Integer, nil] -1, 0, +1 depending on whether
|
|
117
|
+
# @return [Integer, nil] -1, 0, +1 depending on whether `other` is
|
|
143
118
|
# less than, equal to, or greater than self
|
|
144
119
|
#
|
|
145
|
-
# nil is returned if the two values are incomparable
|
|
120
|
+
# `nil` is returned if the two values are incomparable.
|
|
146
121
|
#
|
|
147
|
-
def <=>(other)
|
|
122
|
+
def <=>(other)
|
|
148
123
|
logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
|
|
149
124
|
if other.is_a?(self.class)
|
|
150
|
-
zoned_date_time
|
|
151
|
-
elsif other.is_a?(Items::DateTimeItem) ||
|
|
152
|
-
(other.is_a?(Items::GroupItem) && other.base_item.is_a?(Items::DateTimeItem))
|
|
153
|
-
return nil unless other.state?
|
|
154
|
-
|
|
155
|
-
zoned_date_time.to_instant.compare_to(other.state.zoned_date_time.to_instant)
|
|
156
|
-
elsif other.is_a?(TimeOfDay::TimeOfDay) || other.is_a?(TimeOfDay::TimeOfDayRangeElement)
|
|
157
|
-
to_tod <=> other
|
|
125
|
+
zoned_date_time <=> other.zoned_date_time
|
|
158
126
|
elsif other.respond_to?(:to_time)
|
|
159
127
|
to_time <=> other.to_time
|
|
160
|
-
elsif other.respond_to?(:to_str)
|
|
161
|
-
time_string = other.to_str
|
|
162
|
-
time_string = "#{time_string}T00:00:00#{zone}" if DATE_ONLY_REGEX.match?(time_string)
|
|
163
|
-
self <=> DateTimeType.parse(time_string)
|
|
164
128
|
elsif other.respond_to?(:coerce)
|
|
165
129
|
return nil unless (lhs, rhs = other.coerce(self))
|
|
166
130
|
|
|
@@ -173,42 +137,16 @@ module OpenHAB
|
|
|
173
137
|
#
|
|
174
138
|
# Coerce object to a DateTimeType
|
|
175
139
|
#
|
|
176
|
-
# @param [
|
|
177
|
-
# DateTimeType
|
|
140
|
+
# @param [Time] other object to coerce to a DateTimeType
|
|
178
141
|
#
|
|
179
|
-
# @return [[DateTimeType, DateTimeType]]
|
|
142
|
+
# @return [[DateTimeType, DateTimeType], nil]
|
|
180
143
|
#
|
|
181
144
|
def coerce(other)
|
|
182
145
|
logger.trace("Coercing #{self} as a request from #{other.class}")
|
|
183
|
-
if other.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
[other.state, self]
|
|
187
|
-
elsif other.respond_to?(:to_time)
|
|
188
|
-
[DateTimeType.new(other), self]
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
#
|
|
193
|
-
# Convert this DateTimeType to a ruby Time object
|
|
194
|
-
#
|
|
195
|
-
# @return [Time] A Time object representing the same instant and timezone
|
|
196
|
-
#
|
|
197
|
-
def to_time
|
|
198
|
-
::Time.at(to_i, nsec, :nsec).localtime(utc_offset)
|
|
146
|
+
return [other, zoned_date_time] if other.respond_to?(:to_zoned_date_time)
|
|
147
|
+
return [DateTimeType.new(other), self] if other.respond_to?(:to_time)
|
|
199
148
|
end
|
|
200
149
|
|
|
201
|
-
#
|
|
202
|
-
# Convert the time part of this DateTimeType to a TimeOfDay object
|
|
203
|
-
#
|
|
204
|
-
# @return [TimeOfDay] A TimeOfDay object representing the time
|
|
205
|
-
#
|
|
206
|
-
def to_time_of_day
|
|
207
|
-
TimeOfDay::TimeOfDay.new(h: hour, m: minute, s: second)
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
alias to_tod to_time_of_day
|
|
211
|
-
|
|
212
150
|
#
|
|
213
151
|
# Returns the value of time as a floating point number of seconds since the Epoch
|
|
214
152
|
#
|
|
@@ -230,7 +168,7 @@ module OpenHAB
|
|
|
230
168
|
#
|
|
231
169
|
# Returns true if time represents a time in UTC (GMT)
|
|
232
170
|
#
|
|
233
|
-
# @return [
|
|
171
|
+
# @return [true,false] true if utc_offset == 0, false otherwise
|
|
234
172
|
#
|
|
235
173
|
def utc?
|
|
236
174
|
utc_offset.zero?
|
|
@@ -263,7 +201,7 @@ module OpenHAB
|
|
|
263
201
|
end
|
|
264
202
|
|
|
265
203
|
#
|
|
266
|
-
# Forward missing methods to the
|
|
204
|
+
# Forward missing methods to the `ZonedDateTime` object or a ruby `Time`
|
|
267
205
|
# object representing the same instant
|
|
268
206
|
#
|
|
269
207
|
def method_missing(method, *args, &block)
|
|
@@ -275,17 +213,14 @@ module OpenHAB
|
|
|
275
213
|
|
|
276
214
|
# Add other to self
|
|
277
215
|
#
|
|
278
|
-
# @param other [
|
|
216
|
+
# @param other [Duration, Numeric]
|
|
279
217
|
#
|
|
280
218
|
# @return [DateTimeType]
|
|
281
|
-
def +(other)
|
|
282
|
-
if other.is_a?(
|
|
219
|
+
def +(other)
|
|
220
|
+
if other.is_a?(Duration)
|
|
283
221
|
DateTimeType.new(zoned_date_time.plus(other))
|
|
284
|
-
elsif other.respond_to?(:to_str)
|
|
285
|
-
other = self.class.parse_duration(other.to_str)
|
|
286
|
-
self + other
|
|
287
222
|
elsif other.respond_to?(:to_d)
|
|
288
|
-
DateTimeType.new(zoned_date_time.
|
|
223
|
+
DateTimeType.new(zoned_date_time.plus_nanos((other.to_d * 1_000_000_000).to_i))
|
|
289
224
|
elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
|
|
290
225
|
lhs + rhs
|
|
291
226
|
else
|
|
@@ -301,24 +236,16 @@ module OpenHAB
|
|
|
301
236
|
# if other is a DateTime-like object, the result is a Duration
|
|
302
237
|
# representing how long between the two instants in time.
|
|
303
238
|
#
|
|
304
|
-
# @param other [
|
|
239
|
+
# @param other [Duration, Time, Numeric]
|
|
305
240
|
#
|
|
306
|
-
# @return [DateTimeType,
|
|
307
|
-
def -(other)
|
|
308
|
-
if other.is_a?(
|
|
241
|
+
# @return [DateTimeType, Duration]
|
|
242
|
+
def -(other)
|
|
243
|
+
if other.is_a?(Duration)
|
|
309
244
|
DateTimeType.new(zoned_date_time.minus(other))
|
|
310
245
|
elsif other.respond_to?(:to_time)
|
|
311
246
|
to_time - other.to_time
|
|
312
|
-
elsif other.respond_to?(:to_str)
|
|
313
|
-
time_string = other.to_str
|
|
314
|
-
other = if TIME_ONLY_REGEX.match?(time_string)
|
|
315
|
-
self.class.parse_duration(time_string)
|
|
316
|
-
else
|
|
317
|
-
DateTimeType.parse(time_string)
|
|
318
|
-
end
|
|
319
|
-
self - other
|
|
320
247
|
elsif other.respond_to?(:to_d)
|
|
321
|
-
DateTimeType.new(zoned_date_time.
|
|
248
|
+
DateTimeType.new(zoned_date_time.minus_nanos((other.to_d * 1_000_000_000).to_i))
|
|
322
249
|
elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(to_d))
|
|
323
250
|
lhs - rhs
|
|
324
251
|
else
|
|
@@ -326,7 +253,8 @@ module OpenHAB
|
|
|
326
253
|
end
|
|
327
254
|
end
|
|
328
255
|
end
|
|
329
|
-
# rubocop: enable Metrics/ClassLength
|
|
330
256
|
end
|
|
331
257
|
end
|
|
332
258
|
end
|
|
259
|
+
|
|
260
|
+
# @!parse DateTimeType = OpenHAB::Core::Types::DateTimeType
|
|
@@ -1,32 +1,51 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative
|
|
4
|
-
require_relative
|
|
3
|
+
require_relative "comparable_type"
|
|
4
|
+
require_relative "numeric_type"
|
|
5
|
+
|
|
6
|
+
require_relative "type"
|
|
5
7
|
|
|
6
8
|
module OpenHAB
|
|
7
|
-
module
|
|
9
|
+
module Core
|
|
8
10
|
module Types
|
|
9
11
|
DecimalType = org.openhab.core.library.types.DecimalType
|
|
10
12
|
|
|
11
13
|
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
14
|
+
# {DecimalType} uses a {java.math.BigDecimal} internally and thus can be
|
|
15
|
+
# used for integers, longs and floating point numbers alike.
|
|
16
|
+
#
|
|
17
|
+
# @example DecimalType can be used in case statements with ranges
|
|
18
|
+
# # Check if number item is less than 50
|
|
19
|
+
# case NumberOne.state
|
|
20
|
+
# when (0...50)
|
|
21
|
+
# logger.info("#{NumberOne} is less than 50")
|
|
22
|
+
# when (50..100)
|
|
23
|
+
# logger.info("#{NumberOne} is greater than 50")
|
|
24
|
+
# end
|
|
14
25
|
#
|
|
26
|
+
# @example DecimalType can be compared directly against Numeric
|
|
27
|
+
# if NumberOne.state > 10
|
|
28
|
+
# logger.info("Item #{NumberOne.name} is greater than 10")
|
|
29
|
+
# end
|
|
15
30
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
31
|
+
# @example DecimalType can be compared against the value of another DecimalType
|
|
32
|
+
# if NumberOne.state > NumberTwo.state
|
|
33
|
+
# logger.info("Item #{NumberOne} (#{NumberOne.state}) is greater than #{NumberTwo} (#{NumberTwo.state})")
|
|
34
|
+
# end
|
|
18
35
|
#
|
|
19
36
|
class DecimalType
|
|
20
|
-
# @!parse include
|
|
37
|
+
# @!parse include Command, State
|
|
21
38
|
include NumericType
|
|
22
39
|
include ComparableType
|
|
23
40
|
|
|
41
|
+
#
|
|
42
|
+
# @!method initialize(value)
|
|
24
43
|
#
|
|
25
44
|
# Create a new instance of DecimalType
|
|
26
45
|
#
|
|
27
|
-
# @param [java.math.BigDecimal,
|
|
46
|
+
# @param [java.math.BigDecimal, Numeric] value Create a DecimalType from the given value
|
|
28
47
|
#
|
|
29
|
-
def initialize(*args)
|
|
48
|
+
def initialize(*args)
|
|
30
49
|
unless args.length == 1
|
|
31
50
|
super
|
|
32
51
|
return
|
|
@@ -39,9 +58,6 @@ module OpenHAB
|
|
|
39
58
|
super(value.to_java.strip_trailing_zeros)
|
|
40
59
|
elsif value.is_a?(DecimalType)
|
|
41
60
|
super(value.to_big_decimal)
|
|
42
|
-
elsif value.is_a?(Items::NumericItem) ||
|
|
43
|
-
(value.is_a?(Items::GroupItem) && value.base_item.is_a?(Items::NumericItem))
|
|
44
|
-
super(value.state)
|
|
45
61
|
elsif value.respond_to?(:to_d)
|
|
46
62
|
super(value.to_d.to_java.strip_trailing_zeros)
|
|
47
63
|
else # rubocop:disable Lint/DuplicateBranch
|
|
@@ -54,9 +70,9 @@ module OpenHAB
|
|
|
54
70
|
#
|
|
55
71
|
# Convert DecimalType to a QuantityType
|
|
56
72
|
#
|
|
57
|
-
# @param [
|
|
73
|
+
# @param [String, javax.measure.units.Unit] other
|
|
58
74
|
#
|
|
59
|
-
# @return [QuantityType]
|
|
75
|
+
# @return [QuantityType] `self` as a {QuantityType} of the supplied Unit
|
|
60
76
|
#
|
|
61
77
|
def |(other)
|
|
62
78
|
other = org.openhab.core.types.util.UnitUtils.parse_unit(other.to_str) if other.respond_to?(:to_str)
|
|
@@ -66,25 +82,20 @@ module OpenHAB
|
|
|
66
82
|
#
|
|
67
83
|
# Comparison
|
|
68
84
|
#
|
|
69
|
-
# @param [NumericType,
|
|
85
|
+
# @param [NumericType, Numeric]
|
|
70
86
|
# other object to compare to
|
|
71
87
|
#
|
|
72
|
-
# @return [Integer, nil] -1, 0, +1 depending on whether
|
|
88
|
+
# @return [Integer, nil] -1, 0, +1 depending on whether `other` is
|
|
73
89
|
# less than, equal to, or greater than self
|
|
74
90
|
#
|
|
75
|
-
# nil is returned if the two values are incomparable
|
|
91
|
+
# `nil` is returned if the two values are incomparable.
|
|
76
92
|
#
|
|
77
|
-
def <=>(other)
|
|
93
|
+
def <=>(other)
|
|
78
94
|
logger.trace("(#{self.class}) #{self} <=> #{other} (#{other.class})")
|
|
79
|
-
if other.is_a?(QuantityType)
|
|
95
|
+
if other.is_a?(QuantityType) || other.is_a?(HSBType)
|
|
80
96
|
(other <=> self)&.-@
|
|
81
97
|
elsif other.is_a?(self.class)
|
|
82
98
|
compare_to(other)
|
|
83
|
-
elsif other.is_a?(Items::NumericItem) ||
|
|
84
|
-
(other.is_a?(Items::GroupItem) && other.base_item.is_a?(NumericItem))
|
|
85
|
-
return nil unless other.state?
|
|
86
|
-
|
|
87
|
-
self <=> other.state
|
|
88
99
|
elsif other.respond_to?(:to_d)
|
|
89
100
|
to_d <=> other.to_d
|
|
90
101
|
elsif other.respond_to?(:coerce)
|
|
@@ -97,28 +108,17 @@ module OpenHAB
|
|
|
97
108
|
#
|
|
98
109
|
# Type Coercion
|
|
99
110
|
#
|
|
100
|
-
# Coerce object to a DecimalType
|
|
111
|
+
# Coerce object to a {DecimalType DecimalType}
|
|
101
112
|
#
|
|
102
|
-
# @param [
|
|
103
|
-
# coerce to a {DecimalType}
|
|
113
|
+
# @param [Numeric] other object to coerce to a {DecimalType DecimalType}
|
|
104
114
|
#
|
|
105
|
-
#
|
|
106
|
-
# to that type to accomodate comparison with things such as {OnOffType}
|
|
115
|
+
# @return [Array<(DecimalType, DecimalType)>, nil]
|
|
107
116
|
#
|
|
108
|
-
|
|
109
|
-
#
|
|
110
|
-
def coerce(other) # rubocop:disable Metrics
|
|
117
|
+
def coerce(other)
|
|
111
118
|
logger.trace("Coercing #{self} as a request from #{other.class}")
|
|
112
|
-
|
|
113
|
-
(other.is_a?(Items::GroupItem) && other.base_item.is_a?(Items::NumericItem))
|
|
114
|
-
return unless other.state?
|
|
119
|
+
return unless other.respond_to?(:to_d)
|
|
115
120
|
|
|
116
|
-
|
|
117
|
-
elsif other.is_a?(Type)
|
|
118
|
-
[other, as(other.class)]
|
|
119
|
-
elsif other.respond_to?(:to_d)
|
|
120
|
-
[self.class.new(other.to_d), self]
|
|
121
|
-
end
|
|
121
|
+
[self.class.new(other.to_d), self]
|
|
122
122
|
end
|
|
123
123
|
|
|
124
124
|
#
|
|
@@ -132,12 +132,12 @@ module OpenHAB
|
|
|
132
132
|
end
|
|
133
133
|
|
|
134
134
|
{
|
|
135
|
-
:
|
|
136
|
-
:
|
|
137
|
-
:
|
|
138
|
-
:
|
|
139
|
-
:
|
|
140
|
-
:
|
|
135
|
+
add: :+,
|
|
136
|
+
subtract: :-,
|
|
137
|
+
multiply: :*,
|
|
138
|
+
divide: :/,
|
|
139
|
+
remainder: :%,
|
|
140
|
+
pow: :**
|
|
141
141
|
}.each do |java_op, ruby_op|
|
|
142
142
|
class_eval( # rubocop:disable Style/DocumentDynamicEvalDefinition https://github.com/rubocop/rubocop/issues/10179
|
|
143
143
|
# def +(other)
|
|
@@ -183,3 +183,5 @@ module OpenHAB
|
|
|
183
183
|
end
|
|
184
184
|
end
|
|
185
185
|
end
|
|
186
|
+
|
|
187
|
+
# @!parse DecimalType = OpenHAB::Core::Types::DecimalType
|