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

Inherits:
Object
  • Object
show all
Includes:
LazyArray
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 :replace 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

Methods included from LazyArray

#each, #method_missing, #to_ary

Methods included from Enumerable

#all_groups, #all_members, #command, #command!, #decrease, #down, #equipments, #fast_forward, #groups, #increase, #locations, #member_of, #members, #move, #next, #not_member_of, #not_tagged, #off, #on, #pause, #play, #points, #previous, #refresh, #rewind, #stop, #tagged, #toggle, #up, #update, #update!

Constructor Details

#initialize(policy = :replace) ⇒ TimeSeries

Create a new instance of TimeSeries

Parameters:

Since:

  • openHAB 4.1



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class OpenHAB::Core::LazyArray

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 41

#endInstant (readonly)

Returns the timestamp of the last element in this series.

Returns:



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

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

Returns the persistence policy of this series.



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

#sizeInteger (readonly)

Returns the number of elements in this series.

Returns:



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

Instance Method Details

#<<(entry) ⇒ self

Appends an entry to self, returns self

Examples:

Append an entry

time_series << [Time.at(2), 2]

Parameters:

  • entry (Array<Instant, State>)

    a two-element array with the timestamp and state. The timestamp can be an Instant or any object that responds to #to_zoned_date_time.

Returns:

  • (self)

Raises:

  • (ArgumentError)

Since:

  • openHAB 4.1



135
136
137
138
139
140
141
142
# File 'lib/openhab/core/types/time_series.rb', line 135

def <<(entry)
  raise ArgumentError, "entry must be an Array, but was #{entry.class}" unless entry.respond_to?(:to_ary)

  entry = entry.to_ary
  raise ArgumentError, "entry must be an Array of size 2, but was #{entry.size}" unless entry.size == 2

  add(entry[0], entry[1])
end

#add(timestamp, 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:

  • timestamp (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



118
119
120
121
122
123
# File 'lib/openhab/core/types/time_series.rb', line 118

def add(timestamp, state)
  timestamp = to_instant(timestamp)
  state = format_state(state)
  add_instant(timestamp, state)
  self
end

#add?true, false

Returns true if the series' policy is ADD.

Returns:

  • (true, false)

Since:

  • openHAB 4.1



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

def add?
  policy == Policy::ADD
end

#replace?true, false

Returns true if the series' policy is REPLACE.

Returns:

  • (true, false)

Since:

  • openHAB 4.1



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

def replace?
  policy == Policy::REPLACE
end

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

Returns the content of this series.

Returns:

Since:

  • openHAB 4.1



96
97
98
# File 'lib/openhab/core/types/time_series.rb', line 96

def states
  to_a
end

#to_aArray

Explicit conversion to Array

Returns:

Since:

  • openHAB 4.1



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

def to_a
  get_states.to_array.to_a.freeze
end