Class: OpenHAB::DSL::Rules::Triggers::CronHandler::CronTriggerHandler

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

Overview

Cron Trigger Handler that provides trigger IDs Unfortunatly because the CronTriggerHandler in openHAB core is marked internal the entire thing must be recreated here

Instance Method Summary collapse

Constructor Details

#initialize(trigger) ⇒ CronTriggerHandler

Creates a new CronTriggerHandler

Parameters:

  • trigger (org.openhab.core.automation.Trigger)

    openHAB trigger associated with handler



32
33
34
35
36
37
38
# File 'lib/openhab/dsl/rules/triggers/cron/cron_handler.rb', line 32

def initialize(trigger)
  @trigger = trigger
  @scheduler = OSGi.service("org.openhab.core.scheduler.CronScheduler")
  @schedule = nil
  @expression = trigger.configuration.get("cronExpression")
  super(trigger)
end

Instance Method Details

#disposeObject

Dispose of the handler cancel the cron scheduled task



71
72
73
74
75
76
77
78
79
80
# File 'lib/openhab/dsl/rules/triggers/cron/cron_handler.rb', line 71

def dispose
  synchronized do
    super
    return unless @schedule

    @schedule.cancel(true)
    @schedule = nil
  end
  logger.trace { "cancelled job for trigger '#{@trigger.id}'." }
end

#getTemporalAdjusterCronAdjuster

Get the temporal adjuster

Returns:

  • (CronAdjuster)


56
57
58
# File 'lib/openhab/dsl/rules/triggers/cron/cron_handler.rb', line 56

def getTemporalAdjuster # rubocop:disable Naming/MethodName
  org.openhab.core.scheduler.CronAdjuster.new(@expression)
end

#runObject

Execute the callback



63
64
65
# File 'lib/openhab/dsl/rules/triggers/cron/cron_handler.rb', line 63

def run
  callback&.triggered(@trigger, { "module" => @trigger.id })
end

#setCallback(callback) ⇒ Object

Set the callback to execute when cron trigger fires

Parameters:

  • callback (Object)

    to run



44
45
46
47
48
49
50
# File 'lib/openhab/dsl/rules/triggers/cron/cron_handler.rb', line 44

def setCallback(callback) # rubocop:disable Naming/MethodName
  synchronized do
    super(callback)
    @schedule = @scheduler.schedule(self, @expression)
    logger.trace { "Scheduled cron job '#{@expression}' for trigger '#{@trigger.id}'." }
  end
end