openhab-scripting 3.1.0 → 3.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2c9b98ae8645ba9006e801fb670b8858442117e731f9eddd37186824f655704
4
- data.tar.gz: b594df3783cbce82b7d1a80187f4455e2afa5ffad3230b1c64f7c7be8a7b6a66
3
+ metadata.gz: 01e9ddbd43a59fec9db3fa391d42cacb0cd1a03c7d116e995815df940847b28b
4
+ data.tar.gz: ef98bb992444788122499056470c2fde3fbfbdb6659a72228df0f2c46bd8cecb
5
5
  SHA512:
6
- metadata.gz: 8d9dacf04c0bdeec592a0bd4742009754eb32433523b6f88a9abe34f1294d17fd41a93b940b5701f6b2dc28d1a7d29fef04d5eb07d98e9b0eeb9473aa25e1967
7
- data.tar.gz: cb5585d43b36d101db8b3d81686eb503e6393e0336d0ff626a3ad7d33b6c0720ce13993886453176df2f245c3feea5c10dd42b19908d31ae4734919c72c4f09f
6
+ metadata.gz: 4499127b7c667c3290eb3d106624649ff04eff597eb95668c868bcaf5236f99a9149adf4b74e4750e2d092bc5cd7c3cc8b56b6470968f34c6324985b8bec7472
7
+ data.tar.gz: 93a18bda44e472326eb5952d089f5a8eb1f221bc566c861c9abee4cce08c21b5a8d29aa5168b05c5c54c3e61e613e1834ff01c39f18f57dcc2efc8d4c9eacbb5
@@ -5,6 +5,7 @@ require 'java'
5
5
  require 'time'
6
6
  require 'openhab/dsl/types/datetime'
7
7
  require 'openhab/dsl/items/item_delegate'
8
+ require 'openhab/dsl/items/item_command'
8
9
 
9
10
  module OpenHAB
10
11
  module DSL
@@ -43,7 +43,7 @@ module OpenHAB
43
43
  # @return [Java::OrgOpenhabCoreLibraryTypes::PercentType] the position of the rollershutter
44
44
  #
45
45
  def position
46
- state.as(PercentType)
46
+ state&.as(PercentType)
47
47
  end
48
48
 
49
49
  #
@@ -54,6 +54,8 @@ module OpenHAB
54
54
  # @return [Integer] -1, 0 or 1 depending on the result of the comparison
55
55
  #
56
56
  def <=>(other)
57
+ return nil unless state?
58
+
57
59
  case other
58
60
  when PercentType, Java::OrgOpenhabCoreLibraryTypes::DecimalType then position.compare_to(other)
59
61
  when Numeric then position.int_value <=> other
@@ -73,8 +75,8 @@ module OpenHAB
73
75
  raise ArgumentError, "Cannot coerce to #{other.class}" unless other.is_a? Numeric
74
76
 
75
77
  case other
76
- when Integer then [other, position.int_value]
77
- when Float then [other, position.float_value]
78
+ when Integer then [other, position&.int_value]
79
+ when Float then [other, position&.float_value]
78
80
  end
79
81
  end
80
82
 
@@ -84,7 +86,7 @@ module OpenHAB
84
86
  %i[+ - * / %].each do |operator|
85
87
  define_method(operator) do |other|
86
88
  right, left = coerce(other)
87
- left.send(operator, right)
89
+ left&.send(operator, right)
88
90
  end
89
91
  end
90
92
  end
@@ -36,6 +36,8 @@ module OpenHAB
36
36
  # result from super if not supplied an OpenClosedType
37
37
  #
38
38
  def ==(other)
39
+ other = other.get_state_as(OpenClosedType) if other.respond_to?(:get_state_as)
40
+
39
41
  if other.is_a? OpenClosedType
40
42
  state? && state == other
41
43
  else
@@ -9,6 +9,18 @@ module OpenHAB
9
9
  #
10
10
  module Persistence
11
11
  java_import Java::OrgOpenhabCoreTypesUtil::UnitUtils
12
+
13
+ # A wrapper for OpenHAB's HistoricItem that returns the state directly
14
+ class HistoricState < SimpleDelegator
15
+ attr_reader :timestamp, :state
16
+
17
+ def initialize(state, timestamp)
18
+ @state = state
19
+ @timestamp = timestamp
20
+ super(@state)
21
+ end
22
+ end
23
+
12
24
  # All persistence methods that could return a Quantity
13
25
  QUANTITY_METHODS = %i[average_since
14
26
  delta_since
@@ -45,13 +57,18 @@ module OpenHAB
45
57
  #
46
58
  def previous_state(service = nil, skip_equal: false)
47
59
  service ||= persistence_service
48
- PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
60
+ result = PersistenceExtensions.previous_state(self, skip_equal, service&.to_s)
61
+ HistoricState.new(quantify(result.state), result.timestamp)
49
62
  end
50
63
 
51
64
  PERSISTENCE_METHODS.each do |method|
52
65
  define_method(method) do |timestamp, service = nil|
53
66
  service ||= persistence_service
54
67
  result = PersistenceExtensions.public_send(method, self, to_zdt(timestamp), service&.to_s)
68
+ if result.is_a? Java::OrgOpenhabCorePersistence::HistoricItem
69
+ return HistoricState.new(quantify(result.state), result.timestamp)
70
+ end
71
+
55
72
  QUANTITY_METHODS.include?(method) ? quantify(result) : result
56
73
  end
57
74
  end
@@ -56,6 +56,8 @@ module OpenHAB
56
56
  # otherwise result from super
57
57
  #
58
58
  def ==(other)
59
+ other = other.get_state_as(OnOffType) if other.respond_to?(:get_state_as)
60
+
59
61
  if other.is_a? OnOffType
60
62
  state? && state == other
61
63
  else
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'openhab/dsl/types/quantity'
4
+ require 'openhab/dsl/types/datetime'
5
+ require 'openhab/dsl/items/datetime_item'
4
6
 
5
7
  module OpenHAB
6
8
  module DSL
@@ -21,7 +23,8 @@ module OpenHAB
21
23
  #
22
24
  def ==(other)
23
25
  case other
24
- when OpenHAB::DSL::Types::Quantity, QuantityType, Java::OrgOpenhabCoreLibraryTypes::StringType
26
+ when OpenHAB::DSL::Types::Quantity, QuantityType, Java::OrgOpenhabCoreLibraryTypes::StringType,
27
+ OpenHAB::DSL::Types::DateTime, OpenHAB::DSL::Items::DateTimeItem
25
28
  other == self
26
29
  else
27
30
  super
@@ -44,6 +44,21 @@ module OpenHAB
44
44
  end
45
45
  # rubocop:enable Style/CaseLikeIf
46
46
  end
47
+
48
+ #
49
+ # Test for equality
50
+ #
51
+ # @param [Object] other Other object to compare against
52
+ #
53
+ # @return [Boolean] true if self and other can be considered equal, false otherwise
54
+ #
55
+ def ==(other)
56
+ if other.respond_to?(:get_state_as)
57
+ self == other.get_state_as(OnOffType)
58
+ else
59
+ super
60
+ end
61
+ end
47
62
  end
48
63
  end
49
64
  end
@@ -33,6 +33,21 @@ module OpenHAB
33
33
  super
34
34
  end
35
35
  end
36
+
37
+ #
38
+ # Test for equality
39
+ #
40
+ # @param [Object] other Other object to compare against
41
+ #
42
+ # @return [Boolean] true if self and other can be considered equal, false otherwise
43
+ #
44
+ def ==(other)
45
+ if other.respond_to?(:get_state_as)
46
+ self == other.get_state_as(OpenClosedType)
47
+ else
48
+ super
49
+ end
50
+ end
36
51
  end
37
52
  end
38
53
  end
@@ -30,6 +30,21 @@ module OpenHAB
30
30
  super
31
31
  end
32
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
33
48
  end
34
49
  end
35
50
  end
@@ -29,6 +29,7 @@ module OpenHAB
29
29
  set_name(config.name)
30
30
  set_description(config.description)
31
31
  set_triggers(config.triggers)
32
+ @run_context = config.caller
32
33
  @run_queue = config.run
33
34
  @guard = config.guard
34
35
  between = config.between&.yield_self { between(config.between) }
@@ -288,7 +289,7 @@ module OpenHAB
288
289
  #
289
290
  def process_otherwise_task(event, task)
290
291
  logger.trace { "Executing rule '#{name}' otherwise block with event(#{event})" }
291
- task.block.call(event)
292
+ @run_context.instance_exec(event, &task.block)
292
293
  end
293
294
 
294
295
  #
@@ -316,7 +317,7 @@ module OpenHAB
316
317
  return unless event&.item
317
318
 
318
319
  logger.trace { "Executing rule '#{name}' trigger block with item (#{event.item})" }
319
- task.block.call(event.item)
320
+ @run_context.instance_exec(event.item, &task.block)
320
321
  end
321
322
 
322
323
  #
@@ -328,7 +329,7 @@ module OpenHAB
328
329
  #
329
330
  def process_run_task(event, task)
330
331
  logger.trace { "Executing rule '#{name}' run block with event(#{event})" }
331
- task.block.call(event)
332
+ @run_context.instance_exec(event, &task.block)
332
333
  end
333
334
 
334
335
  #
@@ -23,10 +23,11 @@ module OpenHAB
23
23
  def rule(rule_name, &block)
24
24
  @rule_name = rule_name
25
25
  config = RuleConfig.new(rule_name, block.binding)
26
- config.instance_eval(&block)
26
+ config.instance_exec(config, &block)
27
27
  config.guard = Guard::Guard.new(only_if: config.only_if, not_if: config.not_if)
28
28
  logger.trace { config.inspect }
29
29
  process_rule_config(config)
30
+ config
30
31
  rescue StandardError => e
31
32
  re_raise_with_backtrace(e)
32
33
  end
@@ -40,6 +40,9 @@ module OpenHAB
40
40
  # @return [Array] Of trigger guards
41
41
  attr_accessor :guard
42
42
 
43
+ # @return [Object] object that invoked rule method
44
+ attr_accessor :caller
45
+
43
46
  #
44
47
  # Struct holding a run block
45
48
  #
@@ -75,13 +75,13 @@ module OpenHAB
75
75
  # @return [Integer] -1, 0 or 1 depending on the outcome
76
76
  #
77
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
+
78
82
  case other
79
- when DateTime, DateTimeType, DateTimeItem
80
- zoned_date_time.to_instant.compare_to(other.zoned_date_time.to_instant)
81
- when TimeOfDay::TimeOfDay, TimeOfDay::TimeOfDayRangeElement
82
- to_tod <=> other
83
- when String
84
- self <=> DateTime.parse(DATE_ONLY_REGEX =~ other ? "#{other}'T'00:00:00#{zone}" : other)
83
+ when TimeOfDay::TimeOfDay, TimeOfDay::TimeOfDayRangeElement then to_tod <=> other
84
+ when String then self <=> DateTime.parse(DATE_ONLY_REGEX =~ other ? "#{other}'T'00:00:00#{zone}" : other)
85
85
  else
86
86
  self <=> DateTime.from(other)
87
87
  end
@@ -22,9 +22,21 @@ module OpenHAB
22
22
  #
23
23
  # Regex for matching internal calls in a stack trace
24
24
  #
25
- INTERNAL_CALL_REGEX = %r{(openhab-scripting-.*/lib)|(org/jruby/)}.freeze
25
+ INTERNAL_CALL_REGEX = %r{(openhab-scripting-.*/lib)|org[./]jruby}.freeze
26
26
  private_constant :INTERNAL_CALL_REGEX
27
27
 
28
+ #
29
+ # Regex for matching internal calls in a java stack trace
30
+ #
31
+ EXCLUDED_JAVA_PACKAGES = /jdk\.internal\.reflect|java\.lang\.reflect|org\.openhab|java\.lang\.Thread\.run/.freeze
32
+ private_constant :EXCLUDED_JAVA_PACKAGES
33
+
34
+ #
35
+ # Regex for matching internal calls in a java stack trace
36
+ #
37
+ JAVA_INTERNAL_CALL_REGEX = Regexp.union(INTERNAL_CALL_REGEX, EXCLUDED_JAVA_PACKAGES).freeze
38
+ private_constant :JAVA_INTERNAL_CALL_REGEX
39
+
28
40
  #
29
41
  # Create a new logger
30
42
  #
@@ -60,8 +72,13 @@ module OpenHAB
60
72
  def clean_backtrace(error)
61
73
  return error if debug_enabled?
62
74
 
63
- backtrace = error.backtrace_locations.reject { |line| INTERNAL_CALL_REGEX.match? line.to_s }
64
- error.set_backtrace(backtrace.map(&:to_s))
75
+ if error.respond_to? :backtrace_locations
76
+ backtrace = error.backtrace_locations.map(&:to_s).reject { |line| INTERNAL_CALL_REGEX.match? line }
77
+ error.set_backtrace(backtrace)
78
+ elsif error.respond_to? :stack_trace
79
+ backtrace = error.stack_trace.reject { |line| JAVA_INTERNAL_CALL_REGEX.match? line.to_s }
80
+ error.set_stack_trace(backtrace)
81
+ end
65
82
  error
66
83
  end
67
84
 
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '3.1.0'
8
+ VERSION = '3.3.0'
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openhab-scripting
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian O'Connell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-08 00:00:00.000000000 Z
11
+ date: 2021-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler