openhab-jrubyscripting 5.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/lib/openhab/core/actions.rb +163 -0
  3. data/lib/openhab/core/entity_lookup.rb +144 -0
  4. data/lib/openhab/core/events/abstract_event.rb +17 -0
  5. data/lib/openhab/core/events/item_channel_link.rb +36 -0
  6. data/lib/openhab/core/events/item_command_event.rb +78 -0
  7. data/lib/openhab/core/events/item_event.rb +22 -0
  8. data/lib/openhab/core/events/item_state_changed_event.rb +52 -0
  9. data/lib/openhab/core/events/item_state_event.rb +51 -0
  10. data/lib/openhab/core/events/thing.rb +29 -0
  11. data/lib/openhab/core/events/thing_status_info_event.rb +53 -0
  12. data/lib/openhab/core/events.rb +10 -0
  13. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  14. data/lib/openhab/core/items/color_item.rb +52 -0
  15. data/lib/openhab/core/items/contact_item.rb +52 -0
  16. data/lib/openhab/core/items/date_time_item.rb +58 -0
  17. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  18. data/lib/openhab/core/items/generic_item.rb +344 -0
  19. data/lib/openhab/core/items/group_item.rb +174 -0
  20. data/lib/openhab/core/items/image_item.rb +109 -0
  21. data/lib/openhab/core/items/location_item.rb +34 -0
  22. data/lib/openhab/core/items/metadata/hash.rb +390 -0
  23. data/lib/openhab/core/items/metadata/namespace_hash.rb +469 -0
  24. data/lib/openhab/core/items/metadata.rb +11 -0
  25. data/lib/openhab/core/items/number_item.rb +62 -0
  26. data/lib/openhab/core/items/numeric_item.rb +22 -0
  27. data/lib/openhab/core/items/persistence.rb +327 -0
  28. data/lib/openhab/core/items/player_item.rb +66 -0
  29. data/lib/openhab/core/items/proxy.rb +59 -0
  30. data/lib/openhab/core/items/registry.rb +66 -0
  31. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  32. data/lib/openhab/core/items/semantics/enumerable.rb +152 -0
  33. data/lib/openhab/core/items/semantics.rb +476 -0
  34. data/lib/openhab/core/items/state_storage.rb +53 -0
  35. data/lib/openhab/core/items/string_item.rb +28 -0
  36. data/lib/openhab/core/items/switch_item.rb +78 -0
  37. data/lib/openhab/core/items.rb +114 -0
  38. data/lib/openhab/core/lazy_array.rb +52 -0
  39. data/lib/openhab/core/profile_factory.rb +118 -0
  40. data/lib/openhab/core/script_handling.rb +55 -0
  41. data/lib/openhab/core/things/channel.rb +48 -0
  42. data/lib/openhab/core/things/channel_uid.rb +51 -0
  43. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  44. data/lib/openhab/core/things/profile_callback.rb +52 -0
  45. data/lib/openhab/core/things/proxy.rb +69 -0
  46. data/lib/openhab/core/things/registry.rb +46 -0
  47. data/lib/openhab/core/things/thing.rb +194 -0
  48. data/lib/openhab/core/things.rb +22 -0
  49. data/lib/openhab/core/timer.rb +128 -0
  50. data/lib/openhab/core/types/comparable_type.rb +23 -0
  51. data/lib/openhab/core/types/date_time_type.rb +259 -0
  52. data/lib/openhab/core/types/decimal_type.rb +192 -0
  53. data/lib/openhab/core/types/hsb_type.rb +183 -0
  54. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  55. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  56. data/lib/openhab/core/types/numeric_type.rb +52 -0
  57. data/lib/openhab/core/types/on_off_type.rb +46 -0
  58. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  59. data/lib/openhab/core/types/percent_type.rb +95 -0
  60. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  61. data/lib/openhab/core/types/point_type.rb +117 -0
  62. data/lib/openhab/core/types/quantity_type.rb +327 -0
  63. data/lib/openhab/core/types/raw_type.rb +26 -0
  64. data/lib/openhab/core/types/refresh_type.rb +27 -0
  65. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  66. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  67. data/lib/openhab/core/types/string_type.rb +76 -0
  68. data/lib/openhab/core/types/type.rb +117 -0
  69. data/lib/openhab/core/types/un_def_type.rb +38 -0
  70. data/lib/openhab/core/types/up_down_type.rb +50 -0
  71. data/lib/openhab/core/types.rb +69 -0
  72. data/lib/openhab/core/uid.rb +36 -0
  73. data/lib/openhab/core.rb +85 -0
  74. data/lib/openhab/core_ext/java/duration.rb +115 -0
  75. data/lib/openhab/core_ext/java/local_date.rb +93 -0
  76. data/lib/openhab/core_ext/java/local_time.rb +106 -0
  77. data/lib/openhab/core_ext/java/month.rb +59 -0
  78. data/lib/openhab/core_ext/java/month_day.rb +105 -0
  79. data/lib/openhab/core_ext/java/period.rb +103 -0
  80. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  81. data/lib/openhab/core_ext/java/time.rb +58 -0
  82. data/lib/openhab/core_ext/java/unit.rb +15 -0
  83. data/lib/openhab/core_ext/java/zoned_date_time.rb +116 -0
  84. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  85. data/lib/openhab/core_ext/ruby/class.rb +15 -0
  86. data/lib/openhab/core_ext/ruby/date.rb +89 -0
  87. data/lib/openhab/core_ext/ruby/numeric.rb +190 -0
  88. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  89. data/lib/openhab/core_ext/ruby/time.rb +104 -0
  90. data/lib/openhab/core_ext.rb +18 -0
  91. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  92. data/lib/openhab/dsl/events.rb +9 -0
  93. data/lib/openhab/dsl/gems.rb +3 -0
  94. data/lib/openhab/dsl/items/builder.rb +618 -0
  95. data/lib/openhab/dsl/items/ensure.rb +93 -0
  96. data/lib/openhab/dsl/items/timed_command.rb +236 -0
  97. data/lib/openhab/dsl/rules/automation_rule.rb +308 -0
  98. data/lib/openhab/dsl/rules/builder.rb +1373 -0
  99. data/lib/openhab/dsl/rules/guard.rb +115 -0
  100. data/lib/openhab/dsl/rules/name_inference.rb +160 -0
  101. data/lib/openhab/dsl/rules/property.rb +76 -0
  102. data/lib/openhab/dsl/rules/rule_triggers.rb +96 -0
  103. data/lib/openhab/dsl/rules/terse.rb +63 -0
  104. data/lib/openhab/dsl/rules/triggers/changed.rb +169 -0
  105. data/lib/openhab/dsl/rules/triggers/channel.rb +57 -0
  106. data/lib/openhab/dsl/rules/triggers/command.rb +107 -0
  107. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +161 -0
  108. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +164 -0
  109. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +195 -0
  110. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +127 -0
  111. data/lib/openhab/dsl/rules/triggers/trigger.rb +56 -0
  112. data/lib/openhab/dsl/rules/triggers/updated.rb +130 -0
  113. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +55 -0
  114. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +155 -0
  115. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  116. data/lib/openhab/dsl/rules.rb +29 -0
  117. data/lib/openhab/dsl/script_handling.rb +55 -0
  118. data/lib/openhab/dsl/things/builder.rb +263 -0
  119. data/lib/openhab/dsl/thread_local.rb +48 -0
  120. data/lib/openhab/dsl/timer_manager.rb +191 -0
  121. data/lib/openhab/dsl/version.rb +9 -0
  122. data/lib/openhab/dsl.rb +686 -0
  123. data/lib/openhab/log.rb +348 -0
  124. data/lib/openhab/osgi.rb +70 -0
  125. data/lib/openhab/rspec/configuration.rb +56 -0
  126. data/lib/openhab/rspec/example_group.rb +90 -0
  127. data/lib/openhab/rspec/helpers.rb +439 -0
  128. data/lib/openhab/rspec/hooks.rb +93 -0
  129. data/lib/openhab/rspec/jruby.rb +46 -0
  130. data/lib/openhab/rspec/karaf.rb +811 -0
  131. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  132. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  133. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  134. data/lib/openhab/rspec/mocks/metadata_provider.rb +75 -0
  135. data/lib/openhab/rspec/mocks/persistence_service.rb +140 -0
  136. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  137. data/lib/openhab/rspec/mocks/synchronous_executor.rb +56 -0
  138. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  139. data/lib/openhab/rspec/mocks/timer.rb +95 -0
  140. data/lib/openhab/rspec/openhab/core/actions.rb +26 -0
  141. data/lib/openhab/rspec/openhab/core/items/proxy.rb +27 -0
  142. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  143. data/lib/openhab/rspec/shell.rb +31 -0
  144. data/lib/openhab/rspec/suspend_rules.rb +60 -0
  145. data/lib/openhab/rspec.rb +17 -0
  146. data/lib/openhab/yard/cli/stats.rb +23 -0
  147. data/lib/openhab/yard/code_objects/group_object.rb +17 -0
  148. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  149. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  150. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  151. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  152. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  153. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  154. data/lib/openhab/yard/handlers/jruby/base.rb +49 -0
  155. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  156. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  157. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +27 -0
  158. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  159. data/lib/openhab/yard/html_helper.rb +44 -0
  160. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  161. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  162. data/lib/openhab/yard/tags/library.rb +3 -0
  163. data/lib/openhab/yard.rb +32 -0
  164. metadata +504 -0
@@ -0,0 +1,106 @@
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
+ # @example
12
+ # break_time = LocalTime::NOON
13
+ #
14
+ # if LocalTime.now > LocalTime.of(17, 30) # comparing two LocalTime objects
15
+ # # do something
16
+ # elsif LocalTime.now < LocalTime.parse('8:30') # comparison against a string
17
+ # # do something
18
+ # end
19
+ # four_pm = LocalTime.parse('16:00')
20
+ #
21
+ # @example
22
+ # # Trigger security light between sunset and sunrise when motion is detected
23
+ # rule 'Outside Light Motion' do
24
+ # updated Motion_Sensor, to: OPEN
25
+ # run do
26
+ # astro = things['astro:sun:home']
27
+ # sunrise = astro.get_event_time('SUN_RISE', nil, nil).to_local_time
28
+ # sunset = astro.get_event_time('SUN_SET', nil, nil).to_local_time
29
+ # next if (sunrise..sunset).cover?(Time.now)
30
+ #
31
+ # Security_Light.on for: 10.minutes
32
+ # end
33
+ # end
34
+ #
35
+ class LocalTime
36
+ include Time
37
+
38
+ # @!visibility private
39
+ class << self
40
+ #
41
+ # Parses strings in the form "h[:mm[:ss]] [am/pm]"
42
+ #
43
+ # @param [String] string
44
+ # @return [LocalTime]
45
+ #
46
+ def parse(string)
47
+ format = /(am|pm)$/i.match?(string) ? "h[:mm[:ss][.S]][ ]a" : "H[:mm[:ss][.S]]"
48
+ java_send(:parse, [java.lang.CharSequence, java.time.format.DateTimeFormatter],
49
+ string, java.time.format.DateTimeFormatterBuilder.new
50
+ .parse_case_insensitive
51
+ .parse_lenient
52
+ .append_pattern(format)
53
+ .to_formatter(java.util.Locale::ENGLISH))
54
+ end
55
+ end
56
+
57
+ # @param [TemporalAmount, Numeric] other
58
+ # If other is a Numeric, it's interpreted as seconds.
59
+ # @return [LocalTime]
60
+ def -(other)
61
+ return minus(other.seconds) if other.is_a?(Numeric)
62
+ return self if other.is_a?(Period)
63
+
64
+ minus(other)
65
+ end
66
+
67
+ # @param [TemporalAmount, Numeric] other
68
+ # If other is a Numeric, it's interpreted as seconds.
69
+ # @return [LocalTime]
70
+ def +(other)
71
+ return plus(other.seconds) if other.is_a?(Numeric)
72
+ return self if other.is_a?(Period)
73
+
74
+ plus(other)
75
+ end
76
+
77
+ #
78
+ # Returns the next second
79
+ #
80
+ # Will loop back to the beginning of the day if necessary.
81
+ #
82
+ # @return [LocalTime]
83
+ #
84
+ def succ
85
+ plus_seconds(1)
86
+ end
87
+
88
+ # @return [self]
89
+ def to_local_time
90
+ self
91
+ end
92
+
93
+ # @param [ZonedDateTime, nil] context
94
+ # A {ZonedDateTime} used to fill in missing fields
95
+ # during conversion. {ZonedDateTime.now} is assumed if not given.
96
+ # @return [ZonedDateTime]
97
+ def to_zoned_date_time(context = nil)
98
+ context ||= ZonedDateTime.now
99
+ context.with(self)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ LocalTime = OpenHAB::CoreExt::Java::LocalTime unless Object.const_defined?(:LocalTime)
@@ -0,0 +1,59 @@
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.Month
9
+
10
+ # Extensions to Month
11
+ class Month
12
+ include Time
13
+
14
+ #
15
+ # Returns the next month
16
+ #
17
+ # Will loop back to January if necessary.
18
+ #
19
+ # @return [Month]
20
+ #
21
+ def succ
22
+ plus(1)
23
+ end
24
+
25
+ # @return [LocalDate]
26
+ def to_local_date(context = nil)
27
+ context ||= java.time.Year.now
28
+ year = java.time.Year.from(context)
29
+ year.at_month_day(to_month_day)
30
+ end
31
+
32
+ # @return [Date]
33
+ def to_date(context = nil)
34
+ to_local_date(context).to_date
35
+ end
36
+
37
+ # @return [self]
38
+ def to_month
39
+ self
40
+ end
41
+
42
+ # @return [MonthDay]
43
+ def to_month_day
44
+ MonthDay.of(self, 1)
45
+ end
46
+
47
+ # @param [ZonedDateTime, nil] context
48
+ # A {ZonedDateTime} used to fill in missing fields
49
+ # during conversion. {ZonedDateTime.now} is assumed if not given.
50
+ # @return [ZonedDateTime]
51
+ def to_zoned_date_time(context = nil)
52
+ to_local_date(context).to_zoned_date_time
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ Month = OpenHAB::CoreExt::Java::Month unless Object.const_defined?(:Month)
@@ -0,0 +1,105 @@
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 MonthDay
11
+ class MonthDay
12
+ class << self
13
+ #
14
+ # Parses strings in the form "M-d"
15
+ #
16
+ # @param [String] string
17
+ # @return [MonthDay]
18
+ #
19
+ def parse(string)
20
+ logger.trace("#{self.class}.parse #{string} (#{string.class})")
21
+ java_send(:parse, [java.lang.CharSequence, java.time.format.DateTimeFormatter],
22
+ string.to_s,
23
+ java.time.format.DateTimeFormatter.ofPattern("[--]M-d"))
24
+ end
25
+ end
26
+
27
+ # @return [String]
28
+ def to_s
29
+ # Remove -- from MonthDay string representation
30
+ to_string.delete_prefix("--")
31
+ end
32
+ alias_method :inspect, :to_s
33
+
34
+ # wait until we redefine #to_s
35
+ include Time
36
+
37
+ # @return [MonthDay]
38
+ def +(other)
39
+ (LocalDate.of(1900, month, day_of_month) + other).to_month_day
40
+ end
41
+
42
+ # @return [MonthDay, Period]
43
+ def -(other)
44
+ d = (LocalDate.of(1900, month, day_of_month) - other)
45
+ return d if d.is_a?(java.time.Period)
46
+
47
+ d.to_month_day
48
+ end
49
+
50
+ #
51
+ # Returns the next day
52
+ #
53
+ # Will go to the next month, or loop back to January if necessary.
54
+ #
55
+ # @return [MonthDay]
56
+ #
57
+ def succ
58
+ if day_of_month == month.max_length
59
+ return MonthDay.of(1, 1) if month_value == 12
60
+
61
+ return MonthDay.of(month_value + 1, 1)
62
+ end
63
+
64
+ MonthDay.of(month_value, day_of_month + 1)
65
+ end
66
+
67
+ # @param [TemporalAmount, nil] context
68
+ # A {TemporalAmount} used to fill in missing
69
+ # fields during conversion. {LocalDate.now} is assumed if not given.
70
+ # @return [LocalDate]
71
+ def to_local_date(context = nil)
72
+ context ||= java.time.Year.now
73
+ year = java.time.Year.from(context)
74
+ year.at_month_day(self)
75
+ end
76
+
77
+ alias_method :to_month, :month
78
+
79
+ # @param [Date, nil] context
80
+ # A {Date} used to fill in missing fields
81
+ # during conversion. `Date.today` is assumed if not given.
82
+ # @return [Date]
83
+ def to_date(context = nil)
84
+ context ||= Date.today
85
+ Date.new(context.year, month_value, day_of_month)
86
+ end
87
+
88
+ # @return [self]
89
+ def to_month_day
90
+ self
91
+ end
92
+
93
+ # @param [ZonedDateTime, nil] context
94
+ # A {ZonedDateTime} used to fill in missing fields
95
+ # during conversion. {ZonedDateTime.now} is assumed if not given.
96
+ # @return [ZonedDateTime]
97
+ def to_zoned_date_time(context = nil)
98
+ to_local_date(context).to_zoned_date_time(context)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ 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 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 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,58 @@
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
+ [other.send(self.class.coercion_method), self] if other.respond_to?(self.class.coercion_method)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ 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
@@ -0,0 +1,116 @@
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 ZonedDateTime
11
+ class ZonedDateTime
12
+ include Time
13
+
14
+ class << self # rubocop:disable Lint/EmptyClass
15
+ # @!attribute [r] now
16
+ # @return [ZonedDateTime]
17
+ end
18
+
19
+ alias_method :to_local_time, :toLocalTime
20
+ alias_method :to_month, :month
21
+
22
+ # @param [TemporalAmount, #to_zoned_date_time, Numeric] other
23
+ # If other is a Numeric, it's interpreted as seconds.
24
+ # @return [Duration] If other responds to #to_zoned_date_time
25
+ # @return [ZonedDateTime] If other is a TemporalAmount
26
+ def -(other)
27
+ if other.respond_to?(:to_zoned_date_time)
28
+ nanos = other.to_zoned_date_time.until(self, java.time.temporal.ChronoUnit::NANOS)
29
+ (nanos.to_f / 1_000_000_000).seconds
30
+ elsif other.is_a?(Numeric)
31
+ minus(other.seconds)
32
+ else
33
+ minus(other)
34
+ end
35
+ end
36
+
37
+ # @param [TemporalAmount, Numeric] other
38
+ # If other is a Numeric, it's interpreted as seconds.
39
+ # @return [ZonedDateTime]
40
+ def +(other)
41
+ return plus(other.seconds) if other.is_a?(Numeric)
42
+
43
+ plus(other)
44
+ end
45
+
46
+ #
47
+ # The number of seconds since the Unix epoch.
48
+ #
49
+ # @return [Integer]
50
+ def to_i
51
+ to_instant.epoch_second
52
+ end
53
+
54
+ #
55
+ # The number of seconds since the Unix epoch.
56
+ #
57
+ # @return [Float]
58
+ def to_f
59
+ to_instant.to_epoch_milli / 1000.0
60
+ end
61
+
62
+ # @return [Date]
63
+ def to_date
64
+ Date.new(year, month_value, day_of_month)
65
+ end
66
+
67
+ # @return [LocalDate]
68
+ def to_local_date(_context = nil)
69
+ toLocalDate
70
+ end
71
+
72
+ # @return [MonthDay]
73
+ def to_month_day
74
+ MonthDay.of(month, day_of_month)
75
+ end
76
+
77
+ # This comes from JRuby
78
+
79
+ # @!method to_time
80
+ # @return [Time]
81
+
82
+ # @param [ZonedDateTime, nil] context
83
+ # A {ZonedDateTime} used to fill in missing fields
84
+ # during conversion. Not used in this class.
85
+ # @return [self]
86
+ def to_zoned_date_time(context = nil) # rubocop:disable Lint/UnusedMethodArgument
87
+ self
88
+ end
89
+
90
+ # @return [Integer, nil]
91
+ def <=>(other)
92
+ # compare instants, otherwise it will differ by timezone, which we don't want
93
+ # (use eql? if you care about that)
94
+ if other.respond_to?(:to_zoned_date_time)
95
+ return to_instant.compare_to(other.to_zoned_date_time(self).to_instant)
96
+ end
97
+ return nil unless (lhs, rhs = other.coerce(self))
98
+
99
+ lhs <=> rhs
100
+ end
101
+
102
+ #
103
+ # Converts `other` to {ZonedDateTime}, if possible
104
+ #
105
+ # @param [#to_zoned_date_time] other
106
+ # @return [Array, nil]
107
+ #
108
+ def coerce(other)
109
+ [other.to_zoned_date_time(self), self] if other.respond_to?(:to_zoned_date_time)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ # @!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,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Extensions to Class
4
+ class Class
5
+ #
6
+ # Returns the name of the class, 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