openhab-scripting 4.26.2 → 4.27.1

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: ee1cd79c7c26fcd970943290c98f1e4bc1eadba51143efb483e6fa3b91afef80
4
- data.tar.gz: 438b03e4cae4fc3ba9fbb98be9c4d51154afda055666dc7cafa5bef82be06232
3
+ metadata.gz: 7281829b6759d5f975fc84086826ed6a2712b841a41c47b1305eb37df54aa264
4
+ data.tar.gz: c2002b1ba4a7f207c89355775fef67b30d28fa7c0dc8b0131785b8c5527988a5
5
5
  SHA512:
6
- metadata.gz: 37e389acddba81561af2e95ac053a81f0f6878ed49c2602cd1b604df94f65801278f3236f49aa1b54c17a88f230dee02055bce5af405e1ac3c71d125559cf7a2
7
- data.tar.gz: fae20fd2c3635d1e0a0439ea59dc47f7100d8fd16124c3e7d5ac38baa676db62ebdf7ed0a909004db9f4d55137b98d5b6f39ca450fc0ae77a927f582a8775703
6
+ metadata.gz: 2f50fa017b7b74d18cf4419b9a28b25134c2d52b5e5871864b97dc1698b467e211dfc85a91a24f6b391584e2ca130c5596da7f894681fd0c2f85b3a446c983d3
7
+ data.tar.gz: 658dd4e19e86c5629998023b415269c2c11891796020872e590f7cb85e69491046194db90ab2914cfbc38cbed77b0dec273040d107167239a7efa03d97de0ef1
@@ -5,6 +5,7 @@ require 'java'
5
5
  require 'set'
6
6
 
7
7
  require 'openhab/log/logger'
8
+ require_relative 'item_proxy'
8
9
 
9
10
  # Automation lookup and injection of OpenHab entities
10
11
 
@@ -91,7 +92,8 @@ module OpenHAB
91
92
  def self.lookup_item(name)
92
93
  logger.trace("Looking up item(#{name})")
93
94
  name = name.to_s if name.is_a? Symbol
94
- $ir.get(name) # rubocop: disable Style/GlobalVars
95
+ item = $ir.get(name) # rubocop: disable Style/GlobalVars
96
+ ItemProxy.new(item) unless item.nil?
95
97
  end
96
98
  end
97
99
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'delegate'
4
+ require 'forwardable'
5
+
6
+ module OpenHAB
7
+ module Core
8
+ # Class is a proxy to underlying item
9
+ class ItemProxy < Delegator
10
+ extend Forwardable
11
+ def_delegators :__getobj__, :class, :is_a?, :kind_of?, :instance_of?
12
+
13
+ #
14
+ # Set the proxy item (called by super)
15
+ #
16
+ def __setobj__(item)
17
+ # Convert name to java version for faster lookups
18
+ @item_name = item.name.to_java
19
+ end
20
+
21
+ #
22
+ # Lookup item from item registry
23
+ #
24
+ def __getobj__
25
+ $ir.get(@item_name) # rubocop:disable Style/GlobalVars
26
+ end
27
+ end
28
+ end
29
+ end
@@ -53,7 +53,11 @@ module OpenHAB
53
53
  # sending the command
54
54
  def command(command)
55
55
  return super unless Thread.current[:ensure_states]
56
- return if command == state
56
+
57
+ logger.trace do
58
+ "#{name} ensure #{command}, format_type_pre: #{format_type_pre(command)}, current state: #{state}"
59
+ end
60
+ return if state == format_type_pre(command)
57
61
 
58
62
  super
59
63
  end
@@ -33,6 +33,13 @@ module OpenHAB
33
33
  def ACCEPTED_DATA_TYPES
34
34
  [org.openhab.core.types.UnDefType.java_class].freeze
35
35
  end
36
+
37
+ #
38
+ # Override to support ItemProxy
39
+ #
40
+ def ===(other)
41
+ other.instance_of?(self)
42
+ end
36
43
  end
37
44
  # rubocop:enable Naming/MethodName
38
45
 
@@ -128,7 +135,7 @@ module OpenHAB
128
135
  # @return [Array<Group>] All groups that this item is part of
129
136
  #
130
137
  def groups
131
- group_names.map { |name| Groups.groups[name] }
138
+ group_names.map { |name| Groups.groups[name] }.compact
132
139
  end
133
140
 
134
141
  # Return the item's thing if this item is linked with a thing. If an item is linked to more than one thing,
@@ -6,11 +6,9 @@ require 'openhab/dsl/types/types'
6
6
  require_relative 'item_registry'
7
7
 
8
8
  require_relative 'generic_item'
9
-
10
9
  require_relative 'switch_item'
11
10
  require_relative 'date_time_item'
12
11
  require_relative 'dimmer_item'
13
-
14
12
  require_relative 'color_item'
15
13
  require_relative 'contact_item'
16
14
  require_relative 'group_item'
@@ -48,7 +48,9 @@ module OpenHAB
48
48
  # @return [StateStorage] item states
49
49
  #
50
50
  def store_states(*items)
51
- items = items.flatten
51
+ items = items.flatten.map do |item|
52
+ item.respond_to?(:__getobj__) ? item.__getobj__ : item
53
+ end
52
54
  states = StateStorage.new(BusEvent.storeStates(*items).to_h)
53
55
  if block_given?
54
56
  yield
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'java'
4
+
3
5
  module OpenHAB
4
6
  module DSL
5
7
  # Support for time related functions
@@ -8,40 +10,21 @@ module OpenHAB
8
10
  module MonthDayRange
9
11
  include OpenHAB::Log
10
12
 
11
- java_import java.time.Year
12
-
13
- # Lambdas are used to calculate the year for the month day
14
- # which must happen during evaluation time to support that rules
15
- # creation and evaluation for execution are done in distinct phases
16
- @current_year = -> { return Year.now }
17
- @next_year = -> { return Year.now.plus_years(1) }
18
-
19
- class << self
20
- attr_reader :current_year, :next_year
21
- end
22
-
23
13
  # Creates a range that can be compared against MonthDay objects, strings
24
14
  # or anything responding to 'to_date' to see if they are within the range
25
15
  # @return Range object representing a MonthDay Range
26
- # rubocop:disable Metrics/AbcSize
27
- # Range method cannot be broken up cleaner
28
16
  def self.range(range)
29
- logger.trace "Creating MonthDay range from #{range}"
30
17
  raise ArgumentError, 'Supplied object must be a range' unless range.is_a? Range
31
18
 
32
19
  start = MonthDay.parse(range.begin)
33
20
  ending = MonthDay.parse(range.end)
34
21
 
35
- logger.trace "Month Day Range Start(#{start}) - End (#{ending})"
36
-
37
- # Wrap to next year if ending day of month is before starting day of month
38
- ending_year = ending < start ? next_year : current_year
22
+ start_range = DayOfYear.new(month_day: start, range: start..ending)
23
+ ending_range = DayOfYear.new(month_day: ending, range: start..ending)
24
+ logger.trace "Month Day Range Start(#{start}) - End (#{ending}) - Created from (#{range})"
39
25
 
40
- start_range = MonthDayRangeElement.new(month_day: start, year: current_year)
41
- ending_range = MonthDayRangeElement.new(month_day: ending, year: ending_year)
42
26
  range.exclude_end? ? (start_range...ending_range) : (start_range..ending_range)
43
27
  end
44
- # rubocop:enable Metrics/AbcSize
45
28
 
46
29
  # Checks if supplied range can be converted to a month day range
47
30
  # @param [Range] range to check begin and end values of
@@ -52,54 +35,80 @@ module OpenHAB
52
35
  MonthDay.day_of_month?(range.begin) && MonthDay.day_of_month?(range.end)
53
36
  end
54
37
 
55
- # Represents a range element for a MonthDay object
56
- # The LocalDate (MonthDay + Year) is dynamically calculated to allow for
57
- # being used as a guard during rule evaluation
58
- class MonthDayRangeElement
38
+ # Converts a MonthDay to a day of year
39
+ # which is represented as a number from 1 to 732 to support comparisions when the range overlaps a year boundary
40
+ class DayOfYear
59
41
  include Comparable
60
42
  include OpenHAB::Log
61
43
  java_import java.time.LocalDate
62
- java_import java.time.Year
44
+
45
+ attr_accessor :month_day
46
+
47
+ # Number of days in a leap year
48
+ DAYS_IN_YEAR = 366
63
49
 
64
50
  # Create a new MonthDayRange element
65
51
  # @param [MonthDay] MonthDay element
66
- # @param [Lambda] year lambda to calculate year to convert MonthDay to LocalDate
52
+ # @param [Range] Underlying MonthDay range
67
53
  #
68
- def initialize(month_day:, year:)
54
+ def initialize(month_day:, range:)
69
55
  @month_day = month_day
70
- @year = year
71
- end
72
-
73
- # Convert into a LocalDate using year lambda supplied in initializer
74
- def to_local_date
75
- @year.call.at_month_day(@month_day)
56
+ @range = range
76
57
  end
77
58
 
78
59
  # Returns the MonthDay advanced by 1 day
79
60
  # Required by Range class
80
61
  def succ
81
- next_date = to_local_date.plus_days(1)
82
- # Handle rollover to next year
83
- year = -> { Year.from(next_date) }
84
- MonthDayRangeElement.new(month_day: MonthDay.from(next_date), year: year)
62
+ next_day_of_month = @month_day.day_of_month + 1
63
+ next_month = @month_day.month_value
64
+
65
+ if next_day_of_month > @month_day.month.max_length
66
+ next_day_of_month = 1
67
+ next_month = @month_day.month.plus(1).value
68
+ end
69
+
70
+ DayOfYear.new(month_day: MonthDay.of(next_month, next_day_of_month), range: @range)
71
+ end
72
+
73
+ #
74
+ # Offset by 1 year if the range begin is greater than the range end
75
+ # and if the month day is less than the begining of the range
76
+ # @return [Number] 366 if the month_day should be offset by a year
77
+ def offset
78
+ @range.begin > @range.end && month_day < @range.begin ? DAYS_IN_YEAR : 0
79
+ end
80
+
81
+ #
82
+ # Calculate the day within the range for the underlying month day
83
+ # @return [Number] Representation of the MonthDay as a number from 1 to 732
84
+ def day_in_range
85
+ @day_in_range ||= month_day.max_day_of_year + offset
85
86
  end
86
87
 
87
88
  # Compare MonthDayRangeElement to other objects as required by Range class
88
- # rubocop:disable Metrics/AbcSize
89
- # Case statement needs to work against multiple types
90
- def <=>(other)
89
+ def <=>(other) # rubocop:disable Metrics/AbcSize
91
90
  case other
92
- when LocalDate then to_local_date.compare_to(other)
93
- when Date then self.<=>(LocalDate.of(other.year, other.month, other.day))
94
- when MonthDay then self.<=>(MonthDayRange.current_year.call.at_month_day(other))
91
+ when DayOfYear then day_in_range.<=>(other.day_in_range)
92
+ when MonthDay then self.<=>(DayOfYear.new(month_day: other, range: @range))
93
+ when LocalDate then self.<=>(MonthDay.of(other.month_value, other.day_of_month))
94
+ when Date then self.<=>(MonthDay.of(other.month, other.day))
95
95
  else
96
- return self.<=>(other.to_local_date) if other.respond_to? :to_local_date
97
- return self.<=>(other.to_date) if other.respond_to? :to_date
96
+ return self.<=>(other.to_local_date) if other.respond_to?(:to_local_date)
97
+ return self.<=>(other.to_date) if other.respond_to?(:to_date)
98
98
 
99
99
  raise "Unable to convert #{other.class} to compare to MonthDay"
100
100
  end
101
101
  end
102
- # rubocop:enable Metrics/AbcSize
102
+ end
103
+ end
104
+
105
+ java_import java.time.Month
106
+ # Extend Month with helper method
107
+ class Month
108
+ # Calcalute and memoize the maximum number of days in a year before this month
109
+ # @return [Number] maximum nummber of days in a year before this month
110
+ def max_days_before
111
+ @max_days_before ||= Month.values.select { |month| month < self }.sum(&:max_length)
103
112
  end
104
113
  end
105
114
 
@@ -108,6 +117,7 @@ module OpenHAB
108
117
  class MonthDay
109
118
  include OpenHAB::Log
110
119
  java_import java.time.format.DateTimeFormatter
120
+ java_import java.time.Month
111
121
 
112
122
  #
113
123
  # Constructor
@@ -117,11 +127,9 @@ module OpenHAB
117
127
  #
118
128
  # @return [Object] MonthDay object
119
129
  #
120
- # rubocop: disable Naming/MethodParameterName
121
- def self.new(m:, d:)
130
+ def self.new(m:, d:) # rubocop:disable Naming/MethodParameterName
122
131
  MonthDay.of(m, d)
123
132
  end
124
- # rubocop: enable Naming/MethodParameterName
125
133
 
126
134
  # Parse MonthDay string as defined with by Monthday class without leading double dash "--"
127
135
  def self.parse(string)
@@ -136,11 +144,23 @@ module OpenHAB
136
144
  /^-*[01][0-9]-[0-3]\d$/.match? obj.to_s
137
145
  end
138
146
 
147
+ # Get the maximum (supports leap years) day of the year this month day could be
148
+ def max_day_of_year
149
+ day_of_month + month.max_days_before
150
+ end
151
+
139
152
  # Remove -- from MonthDay string representation
140
153
  def to_s
141
154
  to_string.delete_prefix('--')
142
155
  end
143
156
 
157
+ # Checks if MonthDay is between the dates of the supplied range
158
+ # @param [Range] range to check against MonthDay
159
+ # @return [true,false] true if the MonthDay falls within supplied range, false otherwise
160
+ def between?(range)
161
+ MonthDayRange.range(range).cover? self
162
+ end
163
+
144
164
  # remove the inherited #== method to use our <=> below
145
165
  remove_method :==
146
166
 
@@ -151,6 +171,9 @@ module OpenHAB
151
171
  case other
152
172
  when String
153
173
  self.<=>(MonthDay.parse(other))
174
+ when OpenHAB::DSL::Between::MonthDayRange::DayOfYear
175
+ # Compare with DayOfYear and invert result
176
+ -other.<=>(self)
154
177
  else
155
178
  super
156
179
  end
@@ -5,5 +5,5 @@
5
5
  #
6
6
  module OpenHAB
7
7
  # @return [String] Version of OpenHAB helper libraries
8
- VERSION = '4.26.2'
8
+ VERSION = '4.27.1'
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: 4.26.2
4
+ version: 4.27.1
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-12-31 00:00:00.000000000 Z
11
+ date: 2022-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -47,6 +47,7 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - lib/openhab.rb
49
49
  - lib/openhab/core/entity_lookup.rb
50
+ - lib/openhab/core/item_proxy.rb
50
51
  - lib/openhab/core/load_path.rb
51
52
  - lib/openhab/core/openhab_setup.rb
52
53
  - lib/openhab/core/osgi.rb