openhab-scripting 4.1.4 → 4.2.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/entity_lookup.rb +1 -57
- data/lib/openhab/dsl/dsl.rb +6 -12
- data/lib/openhab/dsl/group.rb +1 -5
- data/lib/openhab/dsl/items/comparable_item.rb +49 -0
- data/lib/openhab/dsl/items/contact_item.rb +41 -0
- data/lib/openhab/dsl/items/date_time_item.rb +64 -0
- data/lib/openhab/dsl/items/dimmer_item.rb +59 -0
- data/lib/openhab/dsl/items/generic_item.rb +197 -0
- data/lib/openhab/dsl/items/group_item.rb +56 -92
- data/lib/openhab/dsl/items/image_item.rb +5 -41
- data/lib/openhab/dsl/items/item_registry.rb +49 -0
- data/lib/openhab/dsl/items/items.rb +71 -35
- data/lib/openhab/dsl/items/metadata.rb +325 -0
- data/lib/openhab/dsl/items/number_item.rb +6 -312
- data/lib/openhab/dsl/items/numeric_item.rb +66 -0
- data/lib/openhab/dsl/items/persistence.rb +122 -0
- data/lib/openhab/dsl/items/player_item.rb +49 -40
- data/lib/openhab/dsl/items/rollershutter_item.rb +25 -77
- data/lib/openhab/dsl/items/string_item.rb +16 -58
- data/lib/openhab/dsl/items/switch_item.rb +62 -0
- data/lib/openhab/dsl/lazy_array.rb +8 -6
- data/lib/openhab/dsl/monkey_patch/events/events.rb +2 -2
- data/lib/openhab/dsl/monkey_patch/events/item_command.rb +67 -24
- data/lib/openhab/dsl/monkey_patch/events/item_event.rb +5 -5
- data/lib/openhab/dsl/monkey_patch/events/item_state.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +10 -11
- data/lib/openhab/dsl/monkey_patch/ruby/number.rb +25 -2
- data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -3
- data/lib/openhab/dsl/monkey_patch/ruby/string.rb +24 -24
- data/lib/openhab/dsl/states.rb +1 -1
- data/lib/openhab/dsl/time_of_day.rb +3 -5
- data/lib/openhab/dsl/types/comparable_type.rb +21 -0
- data/lib/openhab/dsl/types/date_time_type.rb +334 -0
- data/lib/openhab/dsl/types/decimal_type.rb +187 -0
- data/lib/openhab/dsl/types/increase_decrease_type.rb +23 -0
- data/lib/openhab/dsl/types/next_previous_type.rb +23 -0
- data/lib/openhab/dsl/types/numeric_type.rb +39 -0
- data/lib/openhab/dsl/types/on_off_type.rb +29 -0
- data/lib/openhab/dsl/types/open_closed_type.rb +29 -0
- data/lib/openhab/dsl/types/percent_type.rb +68 -0
- data/lib/openhab/dsl/types/play_pause_type.rb +27 -0
- data/lib/openhab/dsl/types/quantity_type.rb +275 -0
- data/lib/openhab/dsl/types/refresh_type.rb +18 -0
- data/lib/openhab/dsl/types/rewind_fastforward_type.rb +33 -0
- data/lib/openhab/dsl/types/stop_move_type.rb +23 -0
- data/lib/openhab/dsl/types/string_type.rb +88 -0
- data/lib/openhab/dsl/types/type.rb +72 -0
- data/lib/openhab/dsl/types/types.rb +77 -0
- data/lib/openhab/dsl/types/un_def_type.rb +22 -0
- data/lib/openhab/dsl/types/up_down_type.rb +32 -0
- data/lib/openhab/dsl/units.rb +11 -6
- data/lib/openhab/version.rb +1 -1
- data/lib/openhab.rb +0 -1
- metadata +31 -28
- data/lib/openhab/dsl/items/datetime_item.rb +0 -75
- data/lib/openhab/dsl/items/item_command.rb +0 -90
- data/lib/openhab/dsl/items/item_delegate.rb +0 -125
- data/lib/openhab/dsl/monkey_patch/items/contact_item.rb +0 -51
- data/lib/openhab/dsl/monkey_patch/items/dimmer_item.rb +0 -140
- data/lib/openhab/dsl/monkey_patch/items/items.rb +0 -142
- data/lib/openhab/dsl/monkey_patch/items/metadata.rb +0 -328
- data/lib/openhab/dsl/monkey_patch/items/persistence.rb +0 -123
- data/lib/openhab/dsl/monkey_patch/items/switch_item.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/ruby/range.rb +0 -47
- data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -32
- data/lib/openhab/dsl/monkey_patch/types/decimal_type.rb +0 -97
- data/lib/openhab/dsl/monkey_patch/types/increase_decrease_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/next_previous_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/on_off_type.rb +0 -79
- data/lib/openhab/dsl/monkey_patch/types/open_closed_type.rb +0 -71
- data/lib/openhab/dsl/monkey_patch/types/percent_type.rb +0 -77
- data/lib/openhab/dsl/monkey_patch/types/play_pause_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/quantity_type.rb +0 -69
- data/lib/openhab/dsl/monkey_patch/types/refresh_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/rewind_fastforward_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/stop_move_type.rb +0 -23
- data/lib/openhab/dsl/monkey_patch/types/types.rb +0 -15
- data/lib/openhab/dsl/monkey_patch/types/up_down_type.rb +0 -72
- data/lib/openhab/dsl/types/datetime.rb +0 -338
- data/lib/openhab/dsl/types/quantity.rb +0 -300
@@ -1,69 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module DSL
|
7
|
-
module MonkeyPatch
|
8
|
-
#
|
9
|
-
# Patches OpenHAB types
|
10
|
-
#
|
11
|
-
module Types
|
12
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::QuantityType
|
13
|
-
|
14
|
-
#
|
15
|
-
# MonkeyPatching QuantityType
|
16
|
-
#
|
17
|
-
class QuantityType
|
18
|
-
#
|
19
|
-
# Compare QuantityType to supplied object
|
20
|
-
#
|
21
|
-
# @param [Object] other object to compare to
|
22
|
-
#
|
23
|
-
# @return [Integer] -1,0,1 or nil depending on value supplied,
|
24
|
-
# nil comparison to supplied object is not possible.
|
25
|
-
#
|
26
|
-
def <=>(other)
|
27
|
-
logger.trace("#{self.class} #{self} <=> #{other} (#{other.class})")
|
28
|
-
case other
|
29
|
-
when Java::OrgOpenhabCoreTypes::UnDefType then 1
|
30
|
-
when String then self <=> Quantity.new(other)
|
31
|
-
when OpenHAB::DSL::Types::Quantity then self <=> other.quantity
|
32
|
-
else
|
33
|
-
other = other.state if other.respond_to? :state
|
34
|
-
compare_to(other)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
#
|
39
|
-
# Coerce objects into a QuantityType
|
40
|
-
#
|
41
|
-
# @param [Object] other object to coerce to a QuantityType if possible
|
42
|
-
#
|
43
|
-
# @return [Object] Numeric when applicable
|
44
|
-
#
|
45
|
-
def coerce(other)
|
46
|
-
logger.trace("Coercing #{self} as a request from #{other.class}")
|
47
|
-
case other
|
48
|
-
when String
|
49
|
-
[Quantity.new(other), self]
|
50
|
-
else
|
51
|
-
[other, self]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
#
|
56
|
-
# Compare self to other using the spaceship operator
|
57
|
-
#
|
58
|
-
# @param [Object] other object to compare to
|
59
|
-
#
|
60
|
-
# @return [Boolean] True if equals
|
61
|
-
#
|
62
|
-
def ==(other)
|
63
|
-
(self <=> other).zero?
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module DSL
|
7
|
-
module MonkeyPatch
|
8
|
-
#
|
9
|
-
# Patches OpenHAB types
|
10
|
-
#
|
11
|
-
module Types
|
12
|
-
java_import Java::OrgOpenhabCoreTypes::RefreshType
|
13
|
-
|
14
|
-
#
|
15
|
-
# Monkey patch for DSL use
|
16
|
-
#
|
17
|
-
class RefreshType
|
18
|
-
alias inspect to_s
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module DSL
|
7
|
-
module MonkeyPatch
|
8
|
-
#
|
9
|
-
# Patches OpenHAB types
|
10
|
-
#
|
11
|
-
module Types
|
12
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::RewindFastforwardType
|
13
|
-
|
14
|
-
#
|
15
|
-
# Monkey patch for DSL use
|
16
|
-
#
|
17
|
-
class RewindFastforwardType
|
18
|
-
alias inspect to_s
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
|
5
|
-
module OpenHAB
|
6
|
-
module DSL
|
7
|
-
module MonkeyPatch
|
8
|
-
#
|
9
|
-
# Patches OpenHAB types
|
10
|
-
#
|
11
|
-
module Types
|
12
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::StopMoveType
|
13
|
-
|
14
|
-
#
|
15
|
-
# Monkey patch for DSL use
|
16
|
-
#
|
17
|
-
class StopMoveType
|
18
|
-
alias inspect to_s
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Monkey patch types
|
4
|
-
require 'openhab/dsl/monkey_patch/types/open_closed_type'
|
5
|
-
require 'openhab/dsl/monkey_patch/types/on_off_type'
|
6
|
-
require 'openhab/dsl/monkey_patch/types/decimal_type'
|
7
|
-
require 'openhab/dsl/monkey_patch/types/increase_decrease_type'
|
8
|
-
require 'openhab/dsl/monkey_patch/types/next_previous_type'
|
9
|
-
require 'openhab/dsl/monkey_patch/types/percent_type'
|
10
|
-
require 'openhab/dsl/monkey_patch/types/play_pause_type'
|
11
|
-
require 'openhab/dsl/monkey_patch/types/quantity_type'
|
12
|
-
require 'openhab/dsl/monkey_patch/types/refresh_type'
|
13
|
-
require 'openhab/dsl/monkey_patch/types/rewind_fastforward_type'
|
14
|
-
require 'openhab/dsl/monkey_patch/types/stop_move_type'
|
15
|
-
require 'openhab/dsl/monkey_patch/types/up_down_type'
|
@@ -1,72 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
module OpenHAB
|
5
|
-
module DSL
|
6
|
-
module MonkeyPatch
|
7
|
-
#
|
8
|
-
# Patches OpenHAB types
|
9
|
-
#
|
10
|
-
module Types
|
11
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::UpDownType
|
12
|
-
|
13
|
-
#
|
14
|
-
# MonkeyPatching UpDownType
|
15
|
-
#
|
16
|
-
class UpDownType
|
17
|
-
#
|
18
|
-
# Check if the supplied object is case equals to self
|
19
|
-
#
|
20
|
-
# @param [Object] other object to compare
|
21
|
-
#
|
22
|
-
# @return [Boolean] True if the other object is a RollershutterItem and has the same state
|
23
|
-
#
|
24
|
-
def ===(other)
|
25
|
-
if other.respond_to?(:state)
|
26
|
-
self == other.state&.as(UpDownType)
|
27
|
-
elsif other.is_a? UpDownType
|
28
|
-
self == other
|
29
|
-
else
|
30
|
-
super
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
# Test for equality
|
36
|
-
#
|
37
|
-
# @param [Object] other Other object to compare against
|
38
|
-
#
|
39
|
-
# @return [Boolean] true if self and other can be considered equal, false otherwise
|
40
|
-
#
|
41
|
-
def ==(other)
|
42
|
-
if other.respond_to?(:get_state_as)
|
43
|
-
self == other.get_state_as(UpDownType)
|
44
|
-
else
|
45
|
-
super
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
#
|
50
|
-
# Check if the state is UP
|
51
|
-
#
|
52
|
-
# @return [Boolean] true if UP, false otherwise
|
53
|
-
#
|
54
|
-
def up?
|
55
|
-
self == UP
|
56
|
-
end
|
57
|
-
|
58
|
-
#
|
59
|
-
# Check if the state is DOWN
|
60
|
-
#
|
61
|
-
# @return [Boolean] true if DOWN, false otherwise
|
62
|
-
#
|
63
|
-
def down?
|
64
|
-
self == DOWN
|
65
|
-
end
|
66
|
-
|
67
|
-
alias inspect to_s
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,338 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'java'
|
4
|
-
require 'forwardable'
|
5
|
-
require 'time'
|
6
|
-
|
7
|
-
module OpenHAB
|
8
|
-
module DSL
|
9
|
-
module Types
|
10
|
-
#
|
11
|
-
# Ruby implementation for OpenHAB DateTimeType
|
12
|
-
#
|
13
|
-
# @author Anders Alfredsson
|
14
|
-
#
|
15
|
-
# rubocop: disable Metrics/ClassLength
|
16
|
-
# Disabled because this class has a single responsibility, there does not appear a logical
|
17
|
-
# way of breaking it up into multiple classes
|
18
|
-
class DateTime
|
19
|
-
extend Forwardable
|
20
|
-
include Comparable
|
21
|
-
include OpenHAB::Log
|
22
|
-
|
23
|
-
def_delegator :datetime, :to_s
|
24
|
-
def_delegator :zoned_date_time, :month_value, :month
|
25
|
-
def_delegator :zoned_date_time, :day_of_month, :mday
|
26
|
-
def_delegator :zoned_date_time, :day_of_year, :yday
|
27
|
-
def_delegator :zoned_date_time, :minute, :min
|
28
|
-
def_delegator :zoned_date_time, :second, :sec
|
29
|
-
def_delegator :zoned_date_time, :nano, :nsec
|
30
|
-
def_delegator :zoned_date_time, :to_epoch_second, :to_i
|
31
|
-
alias inspect to_s
|
32
|
-
alias day mday
|
33
|
-
|
34
|
-
java_import Java::OrgOpenhabCoreLibraryTypes::DateTimeType
|
35
|
-
java_import java.time.ZonedDateTime
|
36
|
-
java_import java.time.Instant
|
37
|
-
java_import java.time.ZoneId
|
38
|
-
java_import java.time.ZoneOffset
|
39
|
-
java_import java.time.Duration
|
40
|
-
|
41
|
-
#
|
42
|
-
# Regex expression to identify strings defining a time in hours, minutes and optionally seconds
|
43
|
-
#
|
44
|
-
TIME_ONLY_REGEX = /\A\d\d:\d\d(:\d\d)?\Z/.freeze
|
45
|
-
|
46
|
-
#
|
47
|
-
# Regex expression to identify strings defining a time in hours, minutes and optionally seconds
|
48
|
-
#
|
49
|
-
DATE_ONLY_REGEX = /\A\d{4}-\d\d-\d\d\Z/.freeze
|
50
|
-
|
51
|
-
attr_reader :datetime
|
52
|
-
|
53
|
-
#
|
54
|
-
# Create a new DateTime instance wrapping an OpenHAB DateTimeType
|
55
|
-
#
|
56
|
-
# @param [Java::org::openhab::core::library::types::DateTimeType] datetime The DateTimeType instance to
|
57
|
-
# delegate to, or an object that can be converted to a DateTimeType
|
58
|
-
#
|
59
|
-
def initialize(datetime)
|
60
|
-
@datetime = case datetime
|
61
|
-
when DateTimeType
|
62
|
-
datetime
|
63
|
-
when ZonedDateTime
|
64
|
-
DateTimeType.new(datetime)
|
65
|
-
else
|
66
|
-
raise "Unexpected type #{datetime.class} provided to DateTime initializer"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
#
|
71
|
-
# Compare thes DateTime object to another
|
72
|
-
#
|
73
|
-
# @param [Object] other Other object to compare against
|
74
|
-
#
|
75
|
-
# @return [Integer] -1, 0 or 1 depending on the outcome
|
76
|
-
#
|
77
|
-
def <=>(other)
|
78
|
-
if other.respond_to?(:zoned_date_time)
|
79
|
-
return zoned_date_time.to_instant.compare_to(other.zoned_date_time.to_instant)
|
80
|
-
end
|
81
|
-
|
82
|
-
case other
|
83
|
-
when TimeOfDay::TimeOfDay, TimeOfDay::TimeOfDayRangeElement then to_tod <=> other
|
84
|
-
when String then self <=> DateTime.parse(DATE_ONLY_REGEX.match?(other) ? "#{other}'T'00:00:00#{zone}" : other)
|
85
|
-
else
|
86
|
-
self <=> DateTime.from(other)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
#
|
91
|
-
# Adds another object to this DateTime
|
92
|
-
#
|
93
|
-
# @param [Object] other Object to add to this. Can be a Numeric, another DateTime/Time/DateTimeType, a
|
94
|
-
# Duration or a String that can be parsed into a DateTimeType or Time object
|
95
|
-
#
|
96
|
-
# @return [DateTime] A new DateTime object representing the result of the calculation
|
97
|
-
#
|
98
|
-
def +(other)
|
99
|
-
logger.trace("Adding #{other} (#{other.class}) to #{self}")
|
100
|
-
case other
|
101
|
-
when Numeric then DateTime.from(to_time + other)
|
102
|
-
when DateTime, Time then self + other.to_f
|
103
|
-
when DateTimeType, String then self + DateTime.from(other).to_f
|
104
|
-
when Duration then DateTime.new(zoned_date_time.plus(other))
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
#
|
109
|
-
# Subtracts another object from this DateTime
|
110
|
-
#
|
111
|
-
# @param [Object] other Object to subtract fom this. Can be a Numeric, another DateTime/Time/DateTimeType, a
|
112
|
-
# Duration or a String that can be parsed into a DateTimeType or Time object
|
113
|
-
#
|
114
|
-
# @return [DateTime, Float] A new DateTime object representing the result of the calculation, or a Float
|
115
|
-
# representing the time difference in seconds if the subtraction is between two time objects
|
116
|
-
#
|
117
|
-
def -(other)
|
118
|
-
logger.trace("Subtracting #{other} (#{other.class}) from self")
|
119
|
-
case other
|
120
|
-
when Numeric then DateTime.from(to_time - other)
|
121
|
-
when String
|
122
|
-
dt = DateTime.parse(other)
|
123
|
-
TIME_ONLY_REGEX.match?(other) ? self - dt.to_f : time_diff(dt)
|
124
|
-
when Duration then DateTime.new(zoned_date_time.minus(other))
|
125
|
-
when Time, DateTime, DateTimeType, DateTimeItem then time_diff(other)
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
#
|
130
|
-
# Convert this DateTime to a ruby Time object
|
131
|
-
#
|
132
|
-
# @return [Time] A Time object representing the same instant and timezone
|
133
|
-
#
|
134
|
-
def to_time
|
135
|
-
Time.at(to_i, nsec, :nsec).localtime(utc_offset)
|
136
|
-
end
|
137
|
-
|
138
|
-
#
|
139
|
-
# Convert the time part of this DateTime to a TimeOfDay object
|
140
|
-
#
|
141
|
-
# @return [TimeOfDay] A TimeOfDay object representing the time
|
142
|
-
#
|
143
|
-
def to_time_of_day
|
144
|
-
TimeOfDay::TimeOfDay.new(h: hour, m: minute, s: second)
|
145
|
-
end
|
146
|
-
|
147
|
-
alias to_tod to_time_of_day
|
148
|
-
|
149
|
-
#
|
150
|
-
# Returns the value of time as a floating point number of seconds since the Epoch
|
151
|
-
#
|
152
|
-
# @return [Float] Number of seconds since the Epoch, with nanosecond presicion
|
153
|
-
#
|
154
|
-
def to_f
|
155
|
-
zoned_date_time.to_epoch_second + (zoned_date_time.nano / 1_000_000_000)
|
156
|
-
end
|
157
|
-
|
158
|
-
#
|
159
|
-
# The ZonedDateTime representing the state
|
160
|
-
#
|
161
|
-
# @return [Java::java::time::ZonedDateTime] ZonedDateTime representing the state
|
162
|
-
#
|
163
|
-
def zoned_date_time
|
164
|
-
@datetime.zonedDateTime
|
165
|
-
end
|
166
|
-
|
167
|
-
alias to_zdt zoned_date_time
|
168
|
-
|
169
|
-
#
|
170
|
-
# The offset in seconds from UTC
|
171
|
-
#
|
172
|
-
# @return [Integer] The offset from UTC, in seconds
|
173
|
-
#
|
174
|
-
def utc_offset
|
175
|
-
zoned_date_time.offset.total_seconds
|
176
|
-
end
|
177
|
-
|
178
|
-
#
|
179
|
-
# Returns true if time represents a time in UTC (GMT)
|
180
|
-
#
|
181
|
-
# @return [Boolean] true if utc_offset == 0, false otherwise
|
182
|
-
#
|
183
|
-
def utc?
|
184
|
-
utc_offset.zero?
|
185
|
-
end
|
186
|
-
|
187
|
-
#
|
188
|
-
# Returns an integer representing the day of the week, 0..6, with Sunday == 0.
|
189
|
-
#
|
190
|
-
# @return [Integer] The day of week
|
191
|
-
#
|
192
|
-
def wday
|
193
|
-
zoned_date_time.day_of_week.value % 7
|
194
|
-
end
|
195
|
-
|
196
|
-
#
|
197
|
-
# The timezone
|
198
|
-
#
|
199
|
-
# @return [String] The timezone in `[+-]hh:mm(:ss)` format ('Z' for UTC) or nil if the Item has no state
|
200
|
-
#
|
201
|
-
def zone
|
202
|
-
zoned_date_time.zone.id
|
203
|
-
end
|
204
|
-
|
205
|
-
#
|
206
|
-
# Check if missing method can be delegated to other contained objects
|
207
|
-
#
|
208
|
-
# @param [String, Symbol] meth the method name to check for
|
209
|
-
#
|
210
|
-
# @return [Boolean] true if DateTimeType, ZonedDateTime or Time responds to the method, false otherwise
|
211
|
-
#
|
212
|
-
def respond_to_missing?(meth, *)
|
213
|
-
@datetime.respond_to?(meth) ||
|
214
|
-
zoned_date_time.respond_to?(meth) ||
|
215
|
-
Time.instance_methods.include?(meth.to_sym)
|
216
|
-
end
|
217
|
-
|
218
|
-
#
|
219
|
-
# Forward missing methods to the OpenHAB DateTimeType, its ZonedDateTime object or a ruby Time
|
220
|
-
# object representing the same instant
|
221
|
-
#
|
222
|
-
# @param [String] meth method name
|
223
|
-
# @param [Array] args arguments for method
|
224
|
-
# @param [Proc] block <description>
|
225
|
-
#
|
226
|
-
# @return [Object] Value from delegated method in OpenHAB NumberItem
|
227
|
-
#
|
228
|
-
def method_missing(meth, *args, &block)
|
229
|
-
if @datetime.respond_to?(meth)
|
230
|
-
@datetime.__send__(meth, *args, &block)
|
231
|
-
elsif zoned_date_time.respond_to?(meth)
|
232
|
-
zoned_date_time.__send__(meth, *args, &block)
|
233
|
-
elsif Time.instance_methods.include?(meth.to_sym)
|
234
|
-
to_time.send(meth, *args, &block)
|
235
|
-
else
|
236
|
-
raise NoMethodError, "undefined method `#{meth}' for #{self.class}"
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
#
|
241
|
-
# Converts other objects to a DateTimeType
|
242
|
-
#
|
243
|
-
# @param [String, Numeric, Time] datetime an object that can be parsed or converted into
|
244
|
-
# a DateTimeType
|
245
|
-
#
|
246
|
-
# @return [Java::org::openhab::core::library::types::DateTimeType] Object representing the same time
|
247
|
-
#
|
248
|
-
def self.from(datetime)
|
249
|
-
case datetime
|
250
|
-
when String
|
251
|
-
parse(datetime)
|
252
|
-
when Numeric
|
253
|
-
from_numeric(datetime)
|
254
|
-
when Time
|
255
|
-
from_time(datetime)
|
256
|
-
else
|
257
|
-
raise "Cannot convert #{datetime.class} to DateTime"
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
#
|
262
|
-
# Converts a Numeric into a DateTimeType
|
263
|
-
#
|
264
|
-
# @param [Numeric] numeric A Integer or Float representing the number of seconds since the epoch
|
265
|
-
#
|
266
|
-
# @return [Java::org::openhab::core::library::types::DateTimeType] Object representing the same time
|
267
|
-
#
|
268
|
-
def self.from_numeric(numeric)
|
269
|
-
case numeric
|
270
|
-
when Integer
|
271
|
-
DateTime.new(ZonedDateTime.ofInstant(Instant.ofEpochSecond(datetime), ZoneId.systemDefault))
|
272
|
-
else
|
273
|
-
DateTime.new(ZonedDateTime.ofInstant(Instant.ofEpochSecond(datetime.to_i,
|
274
|
-
((datetime % 1) * 1_000_000_000).to_i),
|
275
|
-
ZoneId.systemDefault))
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
#
|
280
|
-
# Converts a ruby Time object to an OpenHAB DateTimeType
|
281
|
-
#
|
282
|
-
# @param [Time] time The Time object to be converted
|
283
|
-
#
|
284
|
-
# @return [Java::org::openhab::core::library::types::DateTimeType] Object representing the same time
|
285
|
-
#
|
286
|
-
def self.from_time(time)
|
287
|
-
instant = Instant.ofEpochSecond(time.to_i, time.nsec)
|
288
|
-
zone_id = ZoneId.of_offset('UTC', ZoneOffset.of_total_seconds(time.utc_offset))
|
289
|
-
DateTime.new(ZonedDateTime.ofInstant(instant, zone_id))
|
290
|
-
end
|
291
|
-
|
292
|
-
#
|
293
|
-
# Parses a string representing a time into an OpenHAB DateTimeType. First tries to parse it
|
294
|
-
# using the DateTimeType's parser, then falls back to the ruby Time.parse
|
295
|
-
#
|
296
|
-
# @param [String] time_string The string to be parsed
|
297
|
-
#
|
298
|
-
# @return [Java::org::openhab::core::library::types::DateTimeType] Object representing the same time
|
299
|
-
#
|
300
|
-
def self.parse(time_string)
|
301
|
-
time_string += 'Z' if TIME_ONLY_REGEX.match?(time_string)
|
302
|
-
DateTime.new(DateTimeType.new(time_string))
|
303
|
-
rescue Java::JavaLang::StringIndexOutOfBoundsException, Java::JavaLang::IllegalArgumentException
|
304
|
-
# Try ruby's Time.parse if OpenHAB's DateTimeType parser fails
|
305
|
-
begin
|
306
|
-
time = Time.parse(time_string)
|
307
|
-
DateTime.from(time)
|
308
|
-
rescue ArgumentError
|
309
|
-
raise "Unable to parse #{time_string} into a DateTime"
|
310
|
-
end
|
311
|
-
end
|
312
|
-
|
313
|
-
private
|
314
|
-
|
315
|
-
#
|
316
|
-
# Calculates the difference in time between this instance and another time object
|
317
|
-
#
|
318
|
-
# @param [Time, DateTime, DateTimeItem, Java::org::openhab::core::library::types::DateTimeType] time_obj
|
319
|
-
# The other time object to subtract from self
|
320
|
-
#
|
321
|
-
# @return [Float] The time difference between the two objects, in seconds
|
322
|
-
#
|
323
|
-
def time_diff(time_obj)
|
324
|
-
logger.trace("Calculate time difference between #{self} and #{time_obj}")
|
325
|
-
case time_obj
|
326
|
-
when Time
|
327
|
-
to_time - time_obj
|
328
|
-
when DateTime, DateTimeItem
|
329
|
-
self - time_obj.to_time
|
330
|
-
when DateTimeType
|
331
|
-
self - DateTime.new(time_obj).to_time
|
332
|
-
end
|
333
|
-
end
|
334
|
-
end
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
338
|
-
# rubocop: enable Metrics/ClassLength
|