openhab-scripting 2.11.1 → 2.14.1
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/dsl/items/items.rb +10 -0
- data/lib/openhab/core/dsl/items/number_item.rb +3 -3
- data/lib/openhab/core/dsl/monkey_patch/items/dimmer_item.rb +42 -0
- data/lib/openhab/core/dsl/monkey_patch/ruby/number.rb +7 -35
- data/lib/openhab/core/dsl/monkey_patch/ruby/ruby.rb +1 -0
- data/lib/openhab/core/dsl/rule/cron.rb +26 -3
- data/lib/openhab/core/dsl/rule/item.rb +0 -5
- data/lib/openhab/core/dsl/rule/rule.rb +14 -1
- data/lib/openhab/core/dsl/timers.rb +2 -4
- data/lib/openhab/core/dsl/types/quantity.rb +2 -2
- data/lib/openhab/version.rb +1 -1
- metadata +2 -3
- data/lib/openhab/core/duration.rb +0 -78
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6851ec9dce8db775e48aaf7d08078281e2a2e9c1d6a2c2cd379de0d3fd2391f0
         | 
| 4 | 
            +
              data.tar.gz: 2f656d9cb621d3826c72c2aa588d1ba478108bc30cffd32bf8e0e5fae85dbe47
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 248767b8ec14f2f68412f152755beb872b15b6db26b363cc0837419e16f044b8c6b078843cd93c4efe8fe03358256df7cb8d390ebe43ddb6ad00b962ecb197ce
         | 
| 7 | 
            +
              data.tar.gz: 9bb8e09d8a34c96b220b16b209c92cf5fce0f1bd54f28086ed3f6e4ae83ab24b0288bb828d9b2111fec3fbef10025b70624b13005140690ce5a9b2556df1a132
         | 
| @@ -22,7 +22,17 @@ module OpenHAB | |
| 22 22 | 
             
                        item = $ir.getItem(name)
         | 
| 23 23 | 
             
                        # rubocop: enable Style/GlobalVars
         | 
| 24 24 | 
             
                        (item.is_a? GroupItem) ? nil : item
         | 
| 25 | 
            +
                      rescue Java::OrgOpenhabCoreItems::ItemNotFoundException
         | 
| 26 | 
            +
                        nil
         | 
| 25 27 | 
             
                      end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                      # Returns true if the given item name exists
         | 
| 30 | 
            +
                      # @param name [String] Item name to check
         | 
| 31 | 
            +
                      # @return [Boolean] true if the item exists, false otherwise
         | 
| 32 | 
            +
                      def include?(name)
         | 
| 33 | 
            +
                        !$ir.getItems(name).empty?
         | 
| 34 | 
            +
                      end
         | 
| 35 | 
            +
                      alias key? include?
         | 
| 26 36 | 
             
                    end
         | 
| 27 37 |  | 
| 28 38 | 
             
                    java_import org.openhab.core.items.GroupItem
         | 
| @@ -59,7 +59,7 @@ module OpenHAB | |
| 59 59 | 
             
                          if dimension
         | 
| 60 60 | 
             
                            [Quantity.new(other), to_qt]
         | 
| 61 61 | 
             
                          elsif @number_item.state?
         | 
| 62 | 
            -
                            [ | 
| 62 | 
            +
                            [other.to_d, @number_item.state.to_big_decimal.to_d]
         | 
| 63 63 | 
             
                          end
         | 
| 64 64 | 
             
                        else
         | 
| 65 65 | 
             
                          logger.trace("#{self} cannot be coereced to #{other.class}")
         | 
| @@ -85,7 +85,7 @@ module OpenHAB | |
| 85 85 | 
             
                            @number_item.state <=> other.state
         | 
| 86 86 | 
             
                          end
         | 
| 87 87 | 
             
                        when Numeric
         | 
| 88 | 
            -
                          @number_item.state.to_big_decimal.to_d <=>  | 
| 88 | 
            +
                          @number_item.state.to_big_decimal.to_d <=> other.to_d
         | 
| 89 89 | 
             
                        when String
         | 
| 90 90 | 
             
                          @number_item.state <=> QuantityType.new(other) if dimension
         | 
| 91 91 | 
             
                        end
         | 
| @@ -199,7 +199,7 @@ module OpenHAB | |
| 199 199 | 
             
                              to_d.public_send(operation, other.to_d)
         | 
| 200 200 | 
             
                            end
         | 
| 201 201 | 
             
                          elsif other.is_a? Numeric
         | 
| 202 | 
            -
                            to_d.public_send(operation,  | 
| 202 | 
            +
                            to_d.public_send(operation, other.to_d)
         | 
| 203 203 | 
             
                          elsif dimension && other.is_a?(String)
         | 
| 204 204 | 
             
                            to_qt.public_send(operation, Quantity.new(other))
         | 
| 205 205 | 
             
                          elsif other.respond_to? :coerce
         | 
| @@ -14,6 +14,7 @@ Dimmer = DimmerItem | |
| 14 14 | 
             
            #
         | 
| 15 15 | 
             
            # rubocop:disable Style/ClassAndModuleChildren
         | 
| 16 16 | 
             
            class Java::OrgOpenhabCoreLibraryItems::DimmerItem
         | 
| 17 | 
            +
              include Comparable
         | 
| 17 18 | 
             
              # rubocop:enable Style/ClassAndModuleChildren
         | 
| 18 19 | 
             
              java_import org.openhab.core.library.types.DecimalType
         | 
| 19 20 | 
             
              java_import org.openhab.core.library.types.IncreaseDecreaseType
         | 
| @@ -85,6 +86,38 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem | |
| 85 86 | 
             
                target
         | 
| 86 87 | 
             
              end
         | 
| 87 88 |  | 
| 89 | 
            +
              #
         | 
| 90 | 
            +
              # Compare DimmerItem to supplied object
         | 
| 91 | 
            +
              #
         | 
| 92 | 
            +
              # @param [Object] other object to compare to
         | 
| 93 | 
            +
              #
         | 
| 94 | 
            +
              # @return [Integer] -1,0,1 or nil depending on value supplied, nil comparison to supplied object is not possible.
         | 
| 95 | 
            +
              #
         | 
| 96 | 
            +
              def <=>(other)
         | 
| 97 | 
            +
                logger.trace("Comparing #{self} to #{other}")
         | 
| 98 | 
            +
                case other
         | 
| 99 | 
            +
                when DimmerItem, NumberItem
         | 
| 100 | 
            +
                  state <=> other.state
         | 
| 101 | 
            +
                when DecimalType
         | 
| 102 | 
            +
                  state <=> other
         | 
| 103 | 
            +
                else
         | 
| 104 | 
            +
                  to_i <=> other.to_i
         | 
| 105 | 
            +
                end
         | 
| 106 | 
            +
              end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              #
         | 
| 109 | 
            +
              # Compare DimmerItem to supplied object.
         | 
| 110 | 
            +
              # The == operator needs to be overridden because the parent java object
         | 
| 111 | 
            +
              # has .equals which overrides the <=> operator above
         | 
| 112 | 
            +
              #
         | 
| 113 | 
            +
              # @param [Object] other object to compare to
         | 
| 114 | 
            +
              #
         | 
| 115 | 
            +
              # @return [Integer] true if the two objects contain the same value, false otherwise
         | 
| 116 | 
            +
              #
         | 
| 117 | 
            +
              def ==(other)
         | 
| 118 | 
            +
                (self <=> other).zero?
         | 
| 119 | 
            +
              end
         | 
| 120 | 
            +
             | 
| 88 121 | 
             
              #
         | 
| 89 122 | 
             
              # Check if dimmer has a state and state is not zero
         | 
| 90 123 | 
             
              #
         | 
| @@ -105,6 +138,15 @@ class Java::OrgOpenhabCoreLibraryItems::DimmerItem | |
| 105 138 |  | 
| 106 139 | 
             
              alias to_int to_i
         | 
| 107 140 |  | 
| 141 | 
            +
              #
         | 
| 142 | 
            +
              # Return the string representation of the dimmer item
         | 
| 143 | 
            +
              #
         | 
| 144 | 
            +
              # @return [String] String version of the dimmer value
         | 
| 145 | 
            +
              #
         | 
| 146 | 
            +
              def to_s
         | 
| 147 | 
            +
                to_i.to_s
         | 
| 148 | 
            +
              end
         | 
| 149 | 
            +
             | 
| 108 150 | 
             
              #
         | 
| 109 151 | 
             
              # Check if dimmer is on
         | 
| 110 152 | 
             
              #
         | 
| @@ -1,7 +1,5 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            -
            require 'core/duration'
         | 
| 4 | 
            -
             | 
| 5 3 | 
             
            module OpenHAB
         | 
| 6 4 | 
             
              module Core
         | 
| 7 5 | 
             
                module DSL
         | 
| @@ -14,44 +12,18 @@ module OpenHAB | |
| 14 12 | 
             
                        include OpenHAB::Core
         | 
| 15 13 |  | 
| 16 14 | 
             
                        #
         | 
| 17 | 
            -
                        # Create Duration with  | 
| 18 | 
            -
                        #
         | 
| 19 | 
            -
                        # @return [OpenHAB::Core::Duration] Duration with number of seconds from self
         | 
| 20 | 
            -
                        #
         | 
| 21 | 
            -
                        def seconds
         | 
| 22 | 
            -
                          Duration.new(temporal_unit: :SECONDS, amount: self)
         | 
| 23 | 
            -
                        end
         | 
| 24 | 
            -
             | 
| 25 | 
            -
                        #
         | 
| 26 | 
            -
                        # Create Duration with unit of milliseconds
         | 
| 27 | 
            -
                        #
         | 
| 28 | 
            -
                        # @return [OpenHAB::Core::Duration] Duration with number of milliseconds from self
         | 
| 29 | 
            -
                        #
         | 
| 30 | 
            -
                        def milliseconds
         | 
| 31 | 
            -
                          Duration.new(temporal_unit: :MILLISECONDS, amount: self)
         | 
| 32 | 
            -
                        end
         | 
| 33 | 
            -
             | 
| 34 | 
            -
                        #
         | 
| 35 | 
            -
                        # Create Duration with unit of minutes
         | 
| 36 | 
            -
                        #
         | 
| 37 | 
            -
                        # @return [OpenHAB::Core::Duration] Duration with number of minutes from self
         | 
| 38 | 
            -
                        #
         | 
| 39 | 
            -
                        def minutes
         | 
| 40 | 
            -
                          Duration.new(temporal_unit: :MINUTES, amount: self)
         | 
| 41 | 
            -
                        end
         | 
| 42 | 
            -
             | 
| 43 | 
            -
                        #
         | 
| 44 | 
            -
                        # Create Duration with unit of hours
         | 
| 15 | 
            +
                        # Create Duration with the specified unit
         | 
| 45 16 | 
             
                        #
         | 
| 46 | 
            -
                        # @return [ | 
| 17 | 
            +
                        # @return [Java::JavaTime::Duration] Duration with number of units from self
         | 
| 47 18 | 
             
                        #
         | 
| 48 | 
            -
                         | 
| 49 | 
            -
                          Duration. | 
| 19 | 
            +
                        %w[millis seconds minutes hours].each do |unit|
         | 
| 20 | 
            +
                          define_method(unit) { Java::JavaTime::Duration.public_send("of_#{unit}", self) }
         | 
| 50 21 | 
             
                        end
         | 
| 51 22 |  | 
| 52 23 | 
             
                        alias second seconds
         | 
| 53 | 
            -
                        alias millisecond  | 
| 54 | 
            -
                        alias  | 
| 24 | 
            +
                        alias millisecond millis
         | 
| 25 | 
            +
                        alias milliseconds millis
         | 
| 26 | 
            +
                        alias ms millis
         | 
| 55 27 | 
             
                        alias minute minutes
         | 
| 56 28 | 
             
                        alias hour hours
         | 
| 57 29 | 
             
                      end
         | 
| @@ -1,7 +1,6 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 3 | 
             
            require 'java'
         | 
| 4 | 
            -
            require 'core/duration'
         | 
| 5 4 | 
             
            require 'core/dsl/time_of_day'
         | 
| 6 5 | 
             
            require 'core/cron'
         | 
| 7 6 |  | 
| @@ -64,8 +63,10 @@ module OpenHAB | |
| 64 63 | 
             
                          expression_map = EXPRESSION_MAP[value]
         | 
| 65 64 | 
             
                          expression_map = at_condition(expression_map, at) if at
         | 
| 66 65 | 
             
                          cron(map_to_cron(expression_map))
         | 
| 67 | 
            -
                        when Duration
         | 
| 68 | 
            -
                           | 
| 66 | 
            +
                        when Java::JavaTime::Duration
         | 
| 67 | 
            +
                          raise ArgumentError, '"at" cannot be used with duration' if at
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                          cron(map_to_cron(duration_to_map(value)))
         | 
| 69 70 | 
             
                        else
         | 
| 70 71 | 
             
                          raise ArgumentExpression, 'Unknown interval' unless expression_map
         | 
| 71 72 | 
             
                        end
         | 
| @@ -93,6 +94,28 @@ module OpenHAB | |
| 93 94 | 
             
                        %i[second minute hour dom month dow].map { |field| map.fetch(field) }.join(' ')
         | 
| 94 95 | 
             
                      end
         | 
| 95 96 |  | 
| 97 | 
            +
                      #
         | 
| 98 | 
            +
                      # Convert a Java Duration to a map for the map_to_cron method
         | 
| 99 | 
            +
                      #
         | 
| 100 | 
            +
                      # @param duration [Java::JavaTime::Duration] The duration object
         | 
| 101 | 
            +
                      #
         | 
| 102 | 
            +
                      # @return [Hash] a map suitable for map_to_cron
         | 
| 103 | 
            +
                      #
         | 
| 104 | 
            +
                      def duration_to_map(duration)
         | 
| 105 | 
            +
                        if duration.to_millis_part.zero? && duration.to_nanos_part.zero? && duration.to_days.zero?
         | 
| 106 | 
            +
                          %i[second minute hour].each do |unit|
         | 
| 107 | 
            +
                            to_unit_part = duration.public_send("to_#{unit}s_part")
         | 
| 108 | 
            +
                            next unless to_unit_part.positive?
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                            to_unit = duration.public_send("to_#{unit}s")
         | 
| 111 | 
            +
                            break unless to_unit_part == to_unit
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                            return EXPRESSION_MAP[unit].merge(unit => "*/#{to_unit}")
         | 
| 114 | 
            +
                          end
         | 
| 115 | 
            +
                        end
         | 
| 116 | 
            +
                        raise ArgumentError, "Cron Expression not supported for duration: #{duration}"
         | 
| 117 | 
            +
                      end
         | 
| 118 | 
            +
             | 
| 96 119 | 
             
                      #
         | 
| 97 120 | 
             
                      # If an at time is provided, parse that and merge the new fields into the expression.
         | 
| 98 121 | 
             
                      #
         | 
| @@ -35,9 +35,6 @@ module OpenHAB | |
| 35 35 | 
             
                      # @return [Array] Array of current TriggerDelay objects
         | 
| 36 36 | 
             
                      #
         | 
| 37 37 | 
             
                      def changed_wait(item, duration:, to: nil, from: nil)
         | 
| 38 | 
            -
                        # Convert to testing the group if group specified rather than item
         | 
| 39 | 
            -
                        item = item.group if item.is_a? Group
         | 
| 40 | 
            -
             | 
| 41 38 | 
             
                        # If GroupItems specified, use the group state trigger instead
         | 
| 42 39 | 
             
                        if item.is_a? GroupItems
         | 
| 43 40 | 
             
                          config = { 'groupName' => item.group.name }
         | 
| @@ -97,7 +94,6 @@ module OpenHAB | |
| 97 94 | 
             
                      #
         | 
| 98 95 | 
             
                      def updated(*items, to: nil)
         | 
| 99 96 | 
             
                        items.flatten.each do |item|
         | 
| 100 | 
            -
                          item = item.group if item.is_a? Group
         | 
| 101 97 | 
             
                          logger.trace("Creating updated trigger for item(#{item}) to(#{to})")
         | 
| 102 98 | 
             
                          [to].flatten.each do |to_state|
         | 
| 103 99 | 
             
                            case item
         | 
| @@ -129,7 +125,6 @@ module OpenHAB | |
| 129 125 | 
             
                      #
         | 
| 130 126 | 
             
                      def changed(*items, to: nil, from: nil, for: nil)
         | 
| 131 127 | 
             
                        items.flatten.each do |item|
         | 
| 132 | 
            -
                          item = item.group if item.is_a? Group
         | 
| 133 128 | 
             
                          logger.trace("Creating changed trigger for entity(#{item}), to(#{to}), from(#{from})")
         | 
| 134 129 | 
             
                          # for is a reserved word in ruby, so use local_variable_get :for
         | 
| 135 130 | 
             
                          if (wait_duration = binding.local_variable_get(:for))
         | 
| @@ -113,6 +113,19 @@ module OpenHAB | |
| 113 113 | 
             
                      def my(&block)
         | 
| 114 114 | 
             
                        @caller.instance_eval(&block)
         | 
| 115 115 | 
             
                      end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                      #
         | 
| 118 | 
            +
                      # Create a logger where name includes rule name if name is set
         | 
| 119 | 
            +
                      #
         | 
| 120 | 
            +
                      # @return [Logging::Logger] Logger with name that appended with rule name if rule name is set
         | 
| 121 | 
            +
                      #
         | 
| 122 | 
            +
                      def logger
         | 
| 123 | 
            +
                        if name
         | 
| 124 | 
            +
                          Logging.logger(name.chomp.gsub(/\s+/, '_'))
         | 
| 125 | 
            +
                        else
         | 
| 126 | 
            +
                          super
         | 
| 127 | 
            +
                        end
         | 
| 128 | 
            +
                      end
         | 
| 116 129 | 
             
                    end
         | 
| 117 130 |  | 
| 118 131 | 
             
                    #
         | 
| @@ -274,7 +287,7 @@ module OpenHAB | |
| 274 287 | 
             
                              process_trigger_delay(mod, inputs)
         | 
| 275 288 | 
             
                            else
         | 
| 276 289 | 
             
                              logger.trace("Item changed to #{state} for #{trigger_delay}, rescheduling timer.")
         | 
| 277 | 
            -
                              trigger_delay.timer.reschedule(ZonedDateTime.now.plus( | 
| 290 | 
            +
                              trigger_delay.timer.reschedule(ZonedDateTime.now.plus(duration))
         | 
| 278 291 | 
             
                            end
         | 
| 279 292 | 
             
                          end
         | 
| 280 293 | 
             
                        else
         | 
| @@ -4,8 +4,6 @@ require 'java' | |
| 4 4 | 
             
            require 'delegate'
         | 
| 5 5 | 
             
            require 'forwardable'
         | 
| 6 6 |  | 
| 7 | 
            -
            require 'core/duration'
         | 
| 8 | 
            -
             | 
| 9 7 | 
             
            module OpenHAB
         | 
| 10 8 | 
             
              module Core
         | 
| 11 9 | 
             
                module DSL
         | 
| @@ -49,7 +47,7 @@ module OpenHAB | |
| 49 47 |  | 
| 50 48 | 
             
                        semaphore.synchronize do
         | 
| 51 49 | 
             
                          @timer = ScriptExecution.createTimer(
         | 
| 52 | 
            -
                            ZonedDateTime.now.plus( | 
| 50 | 
            +
                            ZonedDateTime.now.plus(@duration), @block
         | 
| 53 51 | 
             
                          )
         | 
| 54 52 | 
             
                          super(@timer)
         | 
| 55 53 | 
             
                        end
         | 
| @@ -64,7 +62,7 @@ module OpenHAB | |
| 64 62 | 
             
                      #
         | 
| 65 63 | 
             
                      def reschedule(duration = nil)
         | 
| 66 64 | 
             
                        duration ||= @duration
         | 
| 67 | 
            -
                        @timer.reschedule(ZonedDateTime.now.plus( | 
| 65 | 
            +
                        @timer.reschedule(ZonedDateTime.now.plus(duration))
         | 
| 68 66 | 
             
                      end
         | 
| 69 67 | 
             
                    end
         | 
| 70 68 |  | 
| @@ -46,7 +46,7 @@ module OpenHAB | |
| 46 46 | 
             
                                    when QuantityType
         | 
| 47 47 | 
             
                                      quantity
         | 
| 48 48 | 
             
                                    when Numeric
         | 
| 49 | 
            -
                                      QuantityType.new( | 
| 49 | 
            +
                                      QuantityType.new(quantity.to_d.to_java, AbstractUnit::ONE)
         | 
| 50 50 | 
             
                                    else
         | 
| 51 51 | 
             
                                      raise "Unexpected type #{quantity.class} provided to Quantity initializer"
         | 
| 52 52 | 
             
                                    end
         | 
| @@ -152,7 +152,7 @@ module OpenHAB | |
| 152 152 | 
             
                                   logger.trace("Number Item coerced result a(#{a.class})='#{a}' b(#{b.class})='#{b}'")
         | 
| 153 153 | 
             
                                   [a.quantity, b.quantity]
         | 
| 154 154 | 
             
                                 when Numeric
         | 
| 155 | 
            -
                                   [quantity, QuantityType.new( | 
| 155 | 
            +
                                   [quantity, QuantityType.new(other.to_d.to_java, AbstractUnit::ONE)]
         | 
| 156 156 | 
             
                                 else
         | 
| 157 157 | 
             
                                   raise TypeError,
         | 
| 158 158 | 
             
                                         "Operation '#{operation}' cannot be performed between #{self} and #{other.class}"
         | 
    
        data/lib/openhab/version.rb
    CHANGED
    
    
    
        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: 2. | 
| 4 | 
            +
              version: 2.14.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-02- | 
| 11 | 
            +
            date: 2021-02-05 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         | 
| @@ -76,7 +76,6 @@ files: | |
| 76 76 | 
             
            - lib/openhab/core/dsl/timers.rb
         | 
| 77 77 | 
             
            - lib/openhab/core/dsl/types/quantity.rb
         | 
| 78 78 | 
             
            - lib/openhab/core/dsl/units.rb
         | 
| 79 | 
            -
            - lib/openhab/core/duration.rb
         | 
| 80 79 | 
             
            - lib/openhab/core/log.rb
         | 
| 81 80 | 
             
            - lib/openhab/core/patch_load_path.rb
         | 
| 82 81 | 
             
            - lib/openhab/core/startup_delay.rb
         | 
| @@ -1,78 +0,0 @@ | |
| 1 | 
            -
            # frozen_string_literal: true
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            require 'openhab/core/cron'
         | 
| 4 | 
            -
             | 
| 5 | 
            -
            module OpenHAB
         | 
| 6 | 
            -
              module Core
         | 
| 7 | 
            -
                #
         | 
| 8 | 
            -
                # This class represents a duration of time
         | 
| 9 | 
            -
                #
         | 
| 10 | 
            -
                class Duration
         | 
| 11 | 
            -
                  include OpenHAB::Core::Cron
         | 
| 12 | 
            -
             | 
| 13 | 
            -
                  # @return [Array] of supported temperal units (milliseconds, seconds, minutes and hours)
         | 
| 14 | 
            -
                  TEMPORAL_UNITS = %i[MILLISECONDS SECONDS MINUTES HOURS].freeze
         | 
| 15 | 
            -
             | 
| 16 | 
            -
                  #
         | 
| 17 | 
            -
                  # Create a new Duration object
         | 
| 18 | 
            -
                  #
         | 
| 19 | 
            -
                  # @param [Symbol] temporal_unit Unit for duration
         | 
| 20 | 
            -
                  # @param [Integer] amount of that unit
         | 
| 21 | 
            -
                  #
         | 
| 22 | 
            -
                  def initialize(temporal_unit:, amount:)
         | 
| 23 | 
            -
                    unless TEMPORAL_UNITS.include? temporal_unit
         | 
| 24 | 
            -
                      raise ArgumentError,
         | 
| 25 | 
            -
                            "Unexpected Temporal Unit: #{temporal_unit}"
         | 
| 26 | 
            -
                    end
         | 
| 27 | 
            -
             | 
| 28 | 
            -
                    @temporal_unit = temporal_unit
         | 
| 29 | 
            -
                    @amount = amount
         | 
| 30 | 
            -
                  end
         | 
| 31 | 
            -
             | 
| 32 | 
            -
                  #
         | 
| 33 | 
            -
                  # Return a map
         | 
| 34 | 
            -
                  #
         | 
| 35 | 
            -
                  # @return [Map] Map with fields representing this duration @see OpenHAB::Core::Cron
         | 
| 36 | 
            -
                  #
         | 
| 37 | 
            -
                  def cron_map
         | 
| 38 | 
            -
                    case @temporal_unit
         | 
| 39 | 
            -
                    when :SECONDS
         | 
| 40 | 
            -
                      cron_expression_map.merge(second: "*/#{@amount}")
         | 
| 41 | 
            -
                    when :MINUTES
         | 
| 42 | 
            -
                      cron_expression_map.merge(minute: "*/#{@amount}")
         | 
| 43 | 
            -
                    when :HOURS
         | 
| 44 | 
            -
                      cron_expression_map.merge(hour: "*/#{@amount}")
         | 
| 45 | 
            -
                    else
         | 
| 46 | 
            -
                      raise ArgumentError, "Cron Expression not supported for temporal unit: #{temporal_unit}"
         | 
| 47 | 
            -
                    end
         | 
| 48 | 
            -
                  end
         | 
| 49 | 
            -
             | 
| 50 | 
            -
                  #
         | 
| 51 | 
            -
                  # Convert the duration to milliseconds
         | 
| 52 | 
            -
                  #
         | 
| 53 | 
            -
                  # @return [Integer] Duration in milliseconds
         | 
| 54 | 
            -
                  #
         | 
| 55 | 
            -
                  def to_ms
         | 
| 56 | 
            -
                    case @temporal_unit
         | 
| 57 | 
            -
                    when :MILLISECONDS
         | 
| 58 | 
            -
                      @amount
         | 
| 59 | 
            -
                    when :SECONDS
         | 
| 60 | 
            -
                      @amount * 1000
         | 
| 61 | 
            -
                    when :MINUTES
         | 
| 62 | 
            -
                      @amount * 1000 * 60
         | 
| 63 | 
            -
                    when :HOURS
         | 
| 64 | 
            -
                      @amount * 1000 * 60 * 60
         | 
| 65 | 
            -
                    end
         | 
| 66 | 
            -
                  end
         | 
| 67 | 
            -
             | 
| 68 | 
            -
                  #
         | 
| 69 | 
            -
                  # Return a string representation of the duration
         | 
| 70 | 
            -
                  #
         | 
| 71 | 
            -
                  # @return [String] Duration in string
         | 
| 72 | 
            -
                  #
         | 
| 73 | 
            -
                  def to_s
         | 
| 74 | 
            -
                    "#{@amount} #{(@amount == 1) ? @temporal_unit.to_s.downcase.delete_suffix('s') : @temporal_unit.to_s.downcase}"
         | 
| 75 | 
            -
                  end
         | 
| 76 | 
            -
                end
         | 
| 77 | 
            -
              end
         | 
| 78 | 
            -
            end
         |