Class: OpenHAB::DSL::Rules::Triggers::Conditions::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/openhab/dsl/rules/triggers/conditions/duration.rb

Overview

Struct capturing data necessary for a conditional trigger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to:, from:, duration:) ⇒ Duration

Create a new duration condition

Parameters:

  • to (Object)

    optional condition on to state

  • from (Object)

    optional condition on from state

  • duration (java.time.temporal.TemporalAmount)

    to state must stay at specific value before triggering



21
22
23
24
25
26
27
# File 'lib/openhab/dsl/rules/triggers/conditions/duration.rb', line 21

def initialize(to:, from:, duration:)
  @conditions = Generic.new(to: to, from: from)
  @duration = duration
  @timers = {}
  logger.trace "Created Duration Condition To(#{to}) From(#{from}) " \
               "Conditions(#{@conditions}) Duration(#{@duration})"
end

Instance Attribute Details

#ruleObject

Returns the value of attribute rule.



13
14
15
# File 'lib/openhab/dsl/rules/triggers/conditions/duration.rb', line 13

def rule
  @rule
end

Instance Method Details

#cleanupObject

Cleanup any resources from the condition

Cancels the timer, if it’s active



50
51
52
# File 'lib/openhab/dsl/rules/triggers/conditions/duration.rb', line 50

def cleanup
  @timers.each_value(&:cancel)
end

#process(mod:, inputs:, &block) ⇒ Object

Process rule

Parameters:

  • inputs (Hash)

    inputs from trigger



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openhab/dsl/rules/triggers/conditions/duration.rb', line 32

def process(mod:, inputs:, &block)
  timer = @timers[inputs["triggeringItem"]&.name]
  if timer&.active?
    process_active_timer(timer, inputs, mod, &block)
  elsif @conditions.process(mod: mod, inputs: inputs)
    logger.trace("Trigger Guards Matched for #{self}, delaying rule execution")
    # Add timer and attach timer to delay object, and also state being tracked to so
    # timer can be cancelled if state changes
    # Also another timer should not be created if changed to same value again but instead rescheduled
    create_trigger_delay_timer(inputs, mod, &block)
  else
    logger.trace("Trigger Guards did not match for #{self}, ignoring trigger.")
  end
end