Class: OpenHAB::Core::Types::TimeSeries

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/core/types/time_series.rb

Overview

TimeSeries is used to transport a set of states together with their timestamp.

The states are sorted chronologically. The entries can be accessed like an array.

Examples:

time_series = TimeSeries.new # defaults to :add policy
                        .add(Time.at(2), DecimalType.new(2))
                        .add(Time.at(1), DecimalType.new(1))
                        .add(Time.at(3), DecimalType.new(3))
logger.info "first entry: #{time_series.first.state}" # => 1
logger.info "last entry: #{time_series.last.state}" # => 3
logger.info "second entry: #{time_series[1].state}" # => 2
logger.info "sum: #{time_series.sum(&:state)}" # => 6

See Also:

Since:

  • openHAB 4.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy = :replace) ⇒ TimeSeries

Create a new instance of TimeSeries

Parameters:

  • policy (:add, :replace, org.openhab.core.types.TimeSeries.Policy) (defaults to: :replace)

    The persistence policy of this series.

Since:

  • openHAB 4.1



58
59
60
61
# File 'lib/openhab/core/types/time_series.rb', line 58

def initialize(policy = :replace)
  policy = Policy.value_of(policy.to_s.upcase) if policy.is_a?(Symbol)
  super
end

Instance Attribute Details

#beginInstant (readonly)

Returns the timestamp of the first element in this series.

Returns:



# File 'lib/openhab/core/types/time_series.rb', line 40

#endInstant (readonly)

Returns the timestamp of the last element in this series.

Returns:



# File 'lib/openhab/core/types/time_series.rb', line 44

#policyorg.openhab.core.types.TimeSeries.Policy (readonly)

Returns the persistence policy of this series.

Returns:

See Also:

  • OpenHAB::Core::Types::TimeSeries.orgorg.openhaborg.openhab.coreorg.openhab.core.typesorg.openhab.core.types.TimeSeriesorg.openhab.core.types.TimeSeries#getPolicy()


# File 'lib/openhab/core/types/time_series.rb', line 35

#sizeInteger (readonly)

Returns the number of elements in this series.

Returns:



# File 'lib/openhab/core/types/time_series.rb', line 48

Instance Method Details

#add(instant, state) ⇒ self

Note:

This method returns self so it can be chained, unlike the Java version.

Adds a new element to this series.

Elements can be added in an arbitrary order and are sorted chronologically.

Parameters:

  • instant (Instant, #to_zoned_date_time, #to_instant)

    An instant for the given state.

  • state (State, String, Numeric)

    The State at the given timestamp. If a String is given, it will be converted to StringType. If a Numeric is given, it will be converted to DecimalType.

Returns:

  • (self)

Raises:

  • (ArgumentError)

    if state is not a State, String or Numeric

Since:

  • openHAB 4.1



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openhab/core/types/time_series.rb', line 110

def add(instant, state)
  instant = instant.to_zoned_date_time if instant.respond_to?(:to_zoned_date_time)
  instant = instant.to_instant if instant.respond_to?(:to_instant)
  state = case state
          when State then state
          when String then StringType.new(state)
          when Numeric then DecimalType.new(state)
          else
            raise ArgumentError, "state must be a State, String or Numeric, but was #{state.class}"
          end
  add_instant(instant, state)
  self
end

#add?true, false

Returns true if the series’ policy is ‘ADD`.

Returns:

  • (true, false)

Since:

  • openHAB 4.1



65
66
67
# File 'lib/openhab/core/types/time_series.rb', line 65

def add?
  policy == Policy::ADD
end

#replace?true, false

Returns true if the series’ policy is ‘REPLACE`.

Returns:

  • (true, false)

Since:

  • openHAB 4.1



71
72
73
# File 'lib/openhab/core/types/time_series.rb', line 71

def replace?
  policy == Policy::REPLACE
end

#statesArray<org.openhab.core.types.TimeSeries.Entry>

Returns the content of this series.

Returns:

Since:

  • openHAB 4.1



88
89
90
# File 'lib/openhab/core/types/time_series.rb', line 88

def states
  get_states.to_array.to_a.freeze
end