openhab-jrubyscripting 5.0.0.rc4 → 5.0.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e06b71ae524165363db99771e7ef601f542ed14c979572d152b079d70ad82a2
4
- data.tar.gz: 0d6002391ed26a68bbb907d94787f510df30ef6ef9a3a7cd445ac9c47122851b
3
+ metadata.gz: d5963df56b31bb66f4d4cf5afa5927719d61740df847c5d5ff0965a188a73c62
4
+ data.tar.gz: 0011f05f77383e9618844437bc63b55d02e62ed9a402d775a76dbc75d20a8334
5
5
  SHA512:
6
- metadata.gz: 05a3016219eaf41fc4af71f1476029e2b8d0b320afc7bed99e9777b7726afc6897328d9aa92f643132e66d647aae8742eca36f26c703ab3763e1738c55d249e1
7
- data.tar.gz: d5a7d65543b3628e5c3abfa61cb2aa6d6f10e2ab2bb3334d9f38e1ee8952da655da6360c156d926bd53b25c013c86c7672dc1b492c85aaa056e898579a7be7dd
6
+ metadata.gz: ffdb129b03aac2d44365aa42a7f02889f5d8a20f662391b874cb795547b646d6d4f02d014284a73143862ca25f931f42429be012a25b645d02b1e713a02c073b
7
+ data.tar.gz: ca9804074c3092c6bcff87ed17bffe4543ef95b3ef0c230d4767811a266d368210658f36968d4d42b6ed2f320a8935ab28363a318a6bd0d2a7d393afab67a29a
@@ -66,10 +66,22 @@ module OpenHAB
66
66
  #
67
67
  module Actions
68
68
  OSGi.services("org.openhab.core.model.script.engine.action.ActionService")&.each do |service|
69
- klass = (java_import service.action_class.ruby_class).first
70
- logger.trace("Loaded ACTION: #{service.action_class}")
71
- Object.const_set(service.action_class.simple_name, klass)
69
+ action_class = service.action_class
70
+ module_name = action_class.simple_name
71
+ action = if action_class.interface?
72
+ impl = OSGi.service(action_class)
73
+ unless impl
74
+ logger.error("Unable to find an implementation object for action service #{action_class}.")
75
+ next
76
+ end
77
+ const_set(module_name, impl)
78
+ else
79
+ (java_import action_class.ruby_class).first
80
+ end
81
+ logger.trace("Loaded ACTION: #{action_class}")
82
+ Object.const_set(module_name, action)
72
83
  end
84
+
73
85
  # Import common actions
74
86
  %w[Exec HTTP Ping].each do |action|
75
87
  klass = (java_import "org.openhab.core.model.script.actions.#{action}").first
@@ -288,7 +288,7 @@ module OpenHAB
288
288
 
289
289
  # @!visibility private
290
290
  def hash
291
- "metadata_namespace_hash".hash + @item_name.hash
291
+ ["metadata_namespace_hash", @item_name.hash]
292
292
  end
293
293
 
294
294
  # @!visibility private
@@ -40,16 +40,26 @@ module OpenHAB
40
40
  module Persistence
41
41
  GenericItem.prepend(self)
42
42
 
43
- # A state class with an added timestamp attribute. This is used to hold OpenHAB's HistoricItem.
43
+ #
44
+ # A state class with an added timestamp attribute.
45
+ #
46
+ # This wraps {org.openhab.core.persistence.HistoricItem HistoricItem}
47
+ # to allow implicitly treating the object as its state, and wrapping of
48
+ # that state into a {QuantityType} as necessary.
49
+ #
44
50
  class HistoricState < SimpleDelegator
45
- attr_reader :timestamp
46
-
47
51
  alias_method :state, :__getobj__
48
52
 
49
- def initialize(state, timestamp)
50
- @timestamp = timestamp
53
+ def initialize(state, historic_item)
54
+ @historic_item = historic_item
51
55
  super(state)
52
56
  end
57
+
58
+ # @!attribute [r] timestamp
59
+ # @return [ZonedDateTime]
60
+ def timestamp
61
+ @historic_item.timestamp
62
+ end
53
63
  end
54
64
 
55
65
  # All persistence methods that could return a QuantityType
@@ -245,7 +255,7 @@ module OpenHAB
245
255
  def previous_state(service = nil, skip_equal: false)
246
256
  service ||= persistence_service
247
257
  result = Actions::PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
248
- HistoricState.new(quantify(result.state), result.timestamp)
258
+ HistoricState.new(quantify(result.state), result)
249
259
  end
250
260
 
251
261
  PERSISTENCE_METHODS.each do |method|
@@ -308,7 +318,7 @@ module OpenHAB
308
318
  def wrap_result(result, method)
309
319
  if result.is_a?(org.openhab.core.persistence.HistoricItem)
310
320
  return HistoricState.new(quantify(result.state),
311
- result.timestamp)
321
+ result)
312
322
  end
313
323
  return quantify(result) if QUANTITY_METHODS.include?(method)
314
324
 
@@ -100,7 +100,7 @@ module OpenHAB
100
100
  constants.map { |c| const_get(c) }
101
101
  .grep(Module)
102
102
  .select { |k| k <= GenericItem && k != GroupItem && k != StringItem }
103
- .sort { |a, b| a < b ? 1 : -1 }
103
+ .sort { |a, b| (a < b) ? 1 : -1 }
104
104
  .each do |klass|
105
105
  klass.field_reader :ACCEPTED_COMMAND_TYPES, :ACCEPTED_DATA_TYPES unless klass == GenericItem
106
106
 
@@ -19,7 +19,7 @@ module OpenHAB
19
19
  def def_state_parsing_method(method, param_name)
20
20
  class_eval <<~RUBY, __FILE__, __LINE__ + 1
21
21
  def #{method}(type) # def handle_command(type)
22
- type = link.item.format_#{param_name == :state ? :update : param_name}(type) # type = link.item.format_command(type)
22
+ type = link.item.format_#{(param_name == :state) ? :update : param_name}(type) # type = link.item.format_command(type)
23
23
  super(type) # super(type)
24
24
  end # end
25
25
  RUBY
@@ -8,8 +8,6 @@ module OpenHAB
8
8
  # Timer allows you to administer the block of code that
9
9
  # has been scheduled to run later with {OpenHAB::DSL.after after}.
10
10
  #
11
- # @!attribute [r] execution_time
12
- # @return [ZonedDateTime] the scheduled execution time, or null if the timer was cancelled
13
11
  class Timer
14
12
  extend Forwardable
15
13
 
@@ -30,7 +28,7 @@ module OpenHAB
30
28
  # @return [true,false]
31
29
 
32
30
  def_delegator :@timer, :has_terminated, :terminated?
33
- def_delegators :@timer, :execution_time, :active?, :cancelled?, :running?
31
+ def_delegators :@timer, :active?, :cancelled?, :running?
34
32
 
35
33
  # @return [Object, nil]
36
34
  attr_accessor :id
@@ -51,13 +49,19 @@ module OpenHAB
51
49
  @id = id
52
50
  @thread_locals = thread_locals
53
51
  @block = block
54
- @timer = org.openhab.core.model.script.actions.ScriptExecution.create_timer(
55
- # create it far enough in the future so it won't execute until we finish setting it up
56
- 1.minute.from_now,
57
- # when running in rspec, it may have troubles finding this class
58
- # for auto-conversion of block to interface, so use .impl
59
- org.eclipse.xtext.xbase.lib.Procedures::Procedure0.impl { execute }
60
- )
52
+ @timer = if defined?(ScriptExecution)
53
+ ScriptExecution.create_timer(1.minute.from_now) { execute }
54
+ else # DEPRECATED: openHAB 3.4.0
55
+ org.openhab.core.model.script.actions.ScriptExecution.create_timer(
56
+ # create it far enough in the future so it won't execute until we finish setting it up
57
+ 1.minute.from_now,
58
+ # when running in rspec, it may have troubles finding this class
59
+ # for auto-conversion of block to interface, so use .impl
60
+ org.eclipse.xtext.xbase.lib.Procedures::Procedure0.impl { execute }
61
+ )
62
+ end
63
+ # DEPRECATED: openHAB 3.4.0.M6
64
+ @timer.class.field_reader :future unless @timer.respond_to?(:future)
61
65
  reschedule(@time)
62
66
  end
63
67
 
@@ -74,6 +78,13 @@ module OpenHAB
74
78
  end
75
79
  alias_method :to_s, :inspect
76
80
 
81
+ # @!attribute [r] execution_time
82
+ # @return [ZonedDateTime, nil] the scheduled execution time, or `nil` if the timer was cancelled
83
+ def execution_time
84
+ # DEPRECATED: openHAB 3.4.0.M6 (just remove the entire method)
85
+ @timer.future.scheduled_time
86
+ end
87
+
77
88
  #
78
89
  # Reschedule timer
79
90
  #
@@ -21,7 +21,7 @@ module OpenHAB
21
21
  if value.is_a?(DecimalType) || value.is_a?(StringType)
22
22
  value
23
23
  elsif value.is_a?(QuantityType)
24
- unit = index == 2 ? DSL.unit(SIUnits::METRE.dimension) || SIUnits::METRE : Units::DEGREE_ANGLE
24
+ unit = (index == 2) ? DSL.unit(SIUnits::METRE.dimension) || SIUnits::METRE : Units::DEGREE_ANGLE
25
25
  DecimalType.new(value.to_unit(unit).to_big_decimal)
26
26
  elsif value.respond_to?(:to_str)
27
27
  StringType.new(value.to_str)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module CoreExt
5
+ # Extensions that apply to both Date and Time classes
6
+ module Between
7
+ #
8
+ # Checks whether the the object falls between the given range.
9
+ #
10
+ # @overload between?(min, max)
11
+ # @param [Object] min The minimum value to check, inclusive
12
+ # @param [Object] max The maximum value to check, inclusive
13
+ # @return [true,false]
14
+ #
15
+ # @overload between?(range)
16
+ # @param [Range] range A range to check
17
+ # @return [true,false]
18
+ #
19
+ def between?(min, max = nil)
20
+ range = if max
21
+ Range.new(min, max, false)
22
+ else
23
+ raise ArgumentError, "Expecting a range when given a single argument" unless min.is_a?(Range)
24
+
25
+ min
26
+ end
27
+
28
+ OpenHAB::DSL.between(range).cover?(self)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -7,6 +7,7 @@ module OpenHAB
7
7
 
8
8
  # Extensions to Duration
9
9
  class Duration
10
+ include Between
10
11
  # @!parse include TemporalAmount
11
12
 
12
13
  alias_method :to_i, :seconds
@@ -10,6 +10,7 @@ module OpenHAB
10
10
  # Extensions to LocalDate
11
11
  class LocalDate
12
12
  include Time
13
+ include Between
13
14
 
14
15
  class << self # rubocop:disable Lint/EmptyClass
15
16
  # @!attribute [r] now
@@ -33,6 +33,7 @@ module OpenHAB
33
33
  # end
34
34
  #
35
35
  class LocalTime
36
+ include Between
36
37
  # @!parse include Time
37
38
 
38
39
  # @!visibility private
@@ -9,8 +9,19 @@ module OpenHAB
9
9
 
10
10
  # Extensions to Month
11
11
  class Month
12
+ include Between
12
13
  # @!parse include Time
13
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
+
14
25
  #
15
26
  # Returns the next month
16
27
  #
@@ -49,7 +60,7 @@ module OpenHAB
49
60
  # during conversion. {ZonedDateTime.now} is assumed if not given.
50
61
  # @return [ZonedDateTime]
51
62
  def to_zoned_date_time(context = nil)
52
- to_local_date(context).to_zoned_date_time
63
+ to_local_date(context).to_zoned_date_time(context)
53
64
  end
54
65
  end
55
66
  end
@@ -9,6 +9,8 @@ module OpenHAB
9
9
 
10
10
  # Extensions to MonthDay
11
11
  class MonthDay
12
+ include Between
13
+
12
14
  class << self
13
15
  #
14
16
  # Parses strings in the form "M-d"
@@ -10,6 +10,7 @@ module OpenHAB
10
10
  # Extensions to ZonedDateTime
11
11
  class ZonedDateTime
12
12
  include Time
13
+ include Between
13
14
 
14
15
  class << self # rubocop:disable Lint/EmptyClass
15
16
  # @!attribute [r] now
@@ -91,11 +92,10 @@ module OpenHAB
91
92
  # compare instants, otherwise it will differ by timezone, which we don't want
92
93
  # (use eql? if you care about that)
93
94
  if other.respond_to?(:to_zoned_date_time)
94
- return to_instant.compare_to(other.to_zoned_date_time(self).to_instant)
95
+ to_instant.compare_to(other.to_zoned_date_time(self).to_instant)
96
+ elsif other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
97
+ lhs <=> rhs
95
98
  end
96
- return nil unless (lhs, rhs = other.coerce(self))
97
-
98
- lhs <=> rhs
99
99
  end
100
100
 
101
101
  #
@@ -4,6 +4,8 @@ require "date"
4
4
 
5
5
  # Extensions to Date
6
6
  class Date
7
+ include OpenHAB::CoreExt::Between
8
+
7
9
  #
8
10
  # Extends {#+} to allow adding a {java.time.temporal.TemporalAmount TemporalAmount}
9
11
  #
@@ -65,7 +67,7 @@ class Date
65
67
 
66
68
  return self <=> other.to_date(self) if other.is_a?(java.time.MonthDay)
67
69
 
68
- if other.respond_to?(:coerce) && (lhs, rhs = coerce(self))
70
+ if other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
69
71
  return lhs <=> rhs
70
72
  end
71
73
 
@@ -10,6 +10,7 @@ require "forwardable"
10
10
  # Extensions to DateTime
11
11
  class DateTime < Date
12
12
  extend Forwardable
13
+ include OpenHAB::CoreExt::Between
13
14
 
14
15
  # (see Time#plus_with_temporal)
15
16
  def plus_with_temporal(other)
@@ -5,6 +5,7 @@ require "forwardable"
5
5
  # Extensions to Time
6
6
  class Time
7
7
  extend Forwardable
8
+ include OpenHAB::CoreExt::Between
8
9
 
9
10
  #
10
11
  # @!method +(other)
@@ -211,7 +211,7 @@ module OpenHAB
211
211
  def execute(_mod = nil, inputs = nil)
212
212
  ThreadLocal.thread_local(**@thread_locals) do
213
213
  @timed_command_details.mutex.synchronize do
214
- logger.trace "Canceling implicit timer #{@timed_command_details.timer} for "\
214
+ logger.trace "Canceling implicit timer #{@timed_command_details.timer} for " \
215
215
  "#{@timed_command_details.item.name} because received event #{inputs}"
216
216
  @timed_command_details.timer.cancel
217
217
  DSL.rules.remove(@timed_command_details.rule_uid)
@@ -170,7 +170,7 @@ module OpenHAB
170
170
  now = Time.now
171
171
  return true if @between.cover? now
172
172
 
173
- logger.trace("Skipped execution of rule '#{name}' because the current time #{now} "\
173
+ logger.trace("Skipped execution of rule '#{name}' because the current time #{now} " \
174
174
  "is not between #{@between.begin} and #{@between.end}")
175
175
  else
176
176
  logger.trace("Skipped execution of rule '#{name}' because of guard #{@guard}")
@@ -110,7 +110,7 @@ module OpenHAB
110
110
 
111
111
  # formulate a readable rule name from a channel link trigger
112
112
  def infer_rule_name_from_channel_link_trigger(trigger)
113
- trigger == :channel_linked ? "Channel linked to item" : "Channel unlinked from item"
113
+ (trigger == :channel_linked) ? "Channel linked to item" : "Channel unlinked from item"
114
114
  end
115
115
 
116
116
  # formulate a readable rule name from a thing added/updated/remove trigger
@@ -60,7 +60,7 @@ module OpenHAB
60
60
  #
61
61
  def wait_trigger(item:, duration:, to: nil, from: nil, attach: nil)
62
62
  item_name = item.respond_to?(:name) ? item.name : item.to_s
63
- logger.trace("Creating Changed Wait Change Trigger for Item(#{item_name}) Duration(#{duration}) "\
63
+ logger.trace("Creating Changed Wait Change Trigger for Item(#{item_name}) Duration(#{duration}) " \
64
64
  "To(#{to}) From(#{from}) Attach(#{attach})")
65
65
  conditions = Conditions::Duration.new(to: to, from: from, duration: duration)
66
66
  changed_trigger(item: item, to: nil, from: nil, attach: attach, conditions: conditions)
@@ -24,7 +24,7 @@ module OpenHAB
24
24
  @conditions = Conditions::Proc.new(to: to, from: from)
25
25
  @duration = duration
26
26
  @timer = nil
27
- logger.trace "Created Duration Condition To(#{to}) From(#{from}) "\
27
+ logger.trace "Created Duration Condition To(#{to}) From(#{from}) " \
28
28
  "Conditions(#{@conditions}) Duration(#{@duration})"
29
29
  end
30
30
 
@@ -4,6 +4,6 @@ module OpenHAB
4
4
  module DSL
5
5
  # Version of OpenHAB helper libraries
6
6
  # @return [String]
7
- VERSION = "5.0.0.rc4"
7
+ VERSION = "5.0.0.rc5"
8
8
  end
9
9
  end
data/lib/openhab/dsl.rb CHANGED
@@ -299,6 +299,8 @@ module OpenHAB
299
299
  # @example Create a time range
300
300
  # between('7am'..'12pm').cover?(LocalTime.now)
301
301
  #
302
+ # @see CoreExt::Between#between? #between?
303
+ #
302
304
  def between(range)
303
305
  raise ArgumentError, "Supplied object must be a range" unless range.is_a?(Range)
304
306
 
@@ -124,7 +124,7 @@ module OpenHAB
124
124
  #
125
125
  def time_travel_and_execute_timers(duration)
126
126
  if self.class.mock_timers?
127
- Timecop.travel(duration)
127
+ Timecop.frozen? ? Timecop.freeze(duration) : Timecop.travel(duration)
128
128
  execute_timers
129
129
  else
130
130
  sleep duration
@@ -351,8 +351,7 @@ module OpenHAB
351
351
  }.freeze
352
352
  private_constant :BLOCKED_COMPONENTS
353
353
 
354
- START_LEVEL_OVERRIDES = {
355
- }.freeze
354
+ START_LEVEL_OVERRIDES = {}.freeze
356
355
  private_constant :START_LEVEL_OVERRIDES
357
356
 
358
357
  def set_up_bundle_listener
@@ -19,6 +19,21 @@ module OpenHAB
19
19
  end
20
20
  end
21
21
 
22
+ module HistoricState
23
+ def timestamp
24
+ # PersistenceExtensions uses an anonymous class to wrap the current
25
+ # state if that happens to be an answer. Except it calls
26
+ # ZonedDateTime.now in Java land, bypassing Timecop.
27
+ # Detect that and make the call in Ruby
28
+ #
29
+ jc = @historic_item.class.java_class
30
+ return ZonedDateTime.now if jc.anonymous? && jc.enclosing_class == PersistenceExtensions.java_class
31
+
32
+ super
33
+ end
34
+ end
35
+ Core::Items::Persistence::HistoricState.prepend(HistoricState)
36
+
22
37
  attr_reader :id
23
38
 
24
39
  def initialize
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # rubocop:disable Naming have to follow java interface names
3
+ # rubocop:disable Naming/MethodName, Naming/AccessorMethodName
4
4
 
5
5
  require "singleton"
6
6
 
@@ -73,4 +73,4 @@ module OpenHAB
73
73
  end
74
74
  end
75
75
  end
76
- # rubocop:enable Naming
76
+ # rubocop:enable Naming/MethodName, Naming/AccessorMethodName
@@ -16,8 +16,8 @@ module OpenHAB
16
16
 
17
17
  # have to completely replace this method. only change is the regex splitting
18
18
  # into parts now allows `.` as part of the identifier
19
- # rubocop:disable Style
20
- def format_types(typelist, brackets = true)
19
+ # rubocop:disable Style/NestedTernaryOperator, Style/StringConcatenation, Style/TernaryParentheses
20
+ def format_types(typelist, brackets = true) # rubocop:disable Style/OptionalBooleanParameter
21
21
  return unless typelist.is_a?(Array)
22
22
 
23
23
  list = typelist.map do |type|
@@ -27,7 +27,7 @@ module OpenHAB
27
27
  end
28
28
  list.empty? ? "" : (brackets ? "(#{list.join(", ")})" : list.join(", "))
29
29
  end
30
- # rubocop:enable Style
30
+ # rubocop:enable Style/NestedTernaryOperator, Style/StringConcatenation, Style/TernaryParentheses
31
31
 
32
32
  def link_object(obj, title = nil, *)
33
33
  ::YARD::Handlers::JRuby::Base.infer_java_class(obj) if obj.is_a?(String)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-jrubyscripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc4
4
+ version: 5.0.0.rc5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-28 00:00:00.000000000 Z
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '7.1'
75
+ version: '8.0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '7.1'
82
+ version: '8.0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: cuke_linter
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -198,14 +198,14 @@ dependencies:
198
198
  requirements:
199
199
  - - "~>"
200
200
  - !ruby/object:Gem::Version
201
- version: '12.0'
201
+ version: '13.0'
202
202
  type: :development
203
203
  prerelease: false
204
204
  version_requirements: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - "~>"
207
207
  - !ruby/object:Gem::Version
208
- version: '12.0'
208
+ version: '13.0'
209
209
  - !ruby/object:Gem::Dependency
210
210
  name: rspec
211
211
  requirement: !ruby/object:Gem::Requirement
@@ -409,6 +409,7 @@ files:
409
409
  - lib/openhab/core/types/up_down_type.rb
410
410
  - lib/openhab/core/uid.rb
411
411
  - lib/openhab/core_ext.rb
412
+ - lib/openhab/core_ext/between.rb
412
413
  - lib/openhab/core_ext/java/class.rb
413
414
  - lib/openhab/core_ext/java/duration.rb
414
415
  - lib/openhab/core_ext/java/list.rb