Skip to content

Commit

Permalink
Use the default cron handler in openHAB 4 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Aug 30, 2023
1 parent 77c1b36 commit 27f019b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 98 deletions.
12 changes: 10 additions & 2 deletions lib/openhab/dsl/rules/triggers/cron/cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ module Triggers
#
class Cron < Trigger
# Trigger ID for Cron Triggers
CRON_TRIGGER_MODULE_ID = "jsr223.jruby.CronTrigger"
CRON_TRIGGER_MODULE_ID = if Gem::Version.new(OpenHAB::Core::VERSION) >= Gem::Version.new("4.0.0")
"timer.GenericCronTrigger"
else
# @deprecated OH3.4 We need to use a custom CronTrigger handler
"jsr223.jruby.CronTrigger"
end

#
# Returns a default map for cron expressions that fires every second
Expand Down Expand Up @@ -186,7 +191,10 @@ def self.at_condition(expression_map, at_time)
#
#
def trigger(config:, attach:)
CronHandler::CronTriggerHandlerFactory.instance # ensure it's registered
# @deprecated OH3.4 needs a custom CronTriggerHandlerFactory
if Gem::Version.new(OpenHAB::Core::VERSION) < Gem::Version.new("4.0.0")
CronHandler::CronTriggerHandlerFactory.instance # ensure it's registered
end
append_trigger(type: CRON_TRIGGER_MODULE_ID, config: config, attach: attach)
end
end
Expand Down
197 changes: 101 additions & 96 deletions lib/openhab/dsl/rules/triggers/cron/cron_handler.rb
Original file line number Diff line number Diff line change
@@ -1,115 +1,120 @@
# frozen_string_literal: true

require "singleton"

require_relative "cron"

module OpenHAB
module DSL
module Rules
module Triggers
# @!visibility private
#
# Cron trigger handler that provides trigger ID
#
module CronHandler
# Cron Trigger Handler that provides trigger IDs
# Unfortunatly because the CronTriggerHandler in openHAB core is marked internal
# the entire thing must be recreated here
class CronTriggerHandler < org.openhab.core.automation.handler.BaseTriggerModuleHandler
include org.openhab.core.scheduler.SchedulerRunnable
include org.openhab.core.automation.handler.TimeBasedTriggerHandler

# Provides access to protected fields
field_accessor :callback

# Creates a new CronTriggerHandler
# @param [org.openhab.core.automation.Trigger] trigger openHAB trigger associated with handler
#
def initialize(trigger)
@trigger = trigger
@scheduler = OSGi.service("org.openhab.core.scheduler.CronScheduler")
@schedule = nil
@expression = trigger.configuration.get("cronExpression")
super(trigger)
end
# @deprecated OH3.4 this module is not needed in OH4+
if Gem::Version.new(OpenHAB::Core::VERSION) < Gem::Version.new("4.0.0")

#
# Set the callback to execute when cron trigger fires
# @param [Object] callback to run
#
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
require "singleton"

#
# Get the temporal adjuster
# @return [CronAdjuster]
#
def getTemporalAdjuster # rubocop:disable Naming/MethodName
org.openhab.core.scheduler.CronAdjuster.new(@expression)
end
require_relative "cron"

#
# Execute the callback
#
def run
callback&.triggered(@trigger, { "module" => @trigger.id })
end
module OpenHAB
module DSL
module Rules
module Triggers
# @!visibility private
#
# Cron trigger handler that provides trigger ID
#
module CronHandler
# Cron Trigger Handler that provides trigger IDs
# Unfortunatly because the CronTriggerHandler in openHAB core is marked internal
# the entire thing must be recreated here
class CronTriggerHandler < org.openhab.core.automation.handler.BaseTriggerModuleHandler
include org.openhab.core.scheduler.SchedulerRunnable
include org.openhab.core.automation.handler.TimeBasedTriggerHandler

#
# Displose of the handler
# cancel the cron scheduled task
#
def dispose
synchronized do
super
return unless @schedule
# Provides access to protected fields
field_accessor :callback

@schedule.cancel(true)
# Creates a new CronTriggerHandler
# @param [org.openhab.core.automation.Trigger] trigger openHAB trigger associated with handler
#
def initialize(trigger)
@trigger = trigger
@scheduler = OSGi.service("org.openhab.core.scheduler.CronScheduler")
@schedule = nil
@expression = trigger.configuration.get("cronExpression")
super(trigger)
end

#
# Set the callback to execute when cron trigger fires
# @param [Object] callback to run
#
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

#
# Get the temporal adjuster
# @return [CronAdjuster]
#
def getTemporalAdjuster # rubocop:disable Naming/MethodName
org.openhab.core.scheduler.CronAdjuster.new(@expression)
end
logger.trace("cancelled job for trigger '#{@trigger.id}'.")
end
end

# Implements the ScriptedTriggerHandlerFactory interface to create a new Cron Trigger Handler
class CronTriggerHandlerFactory
include Singleton
include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory

def initialize
Core.automation_manager.add_trigger_handler(
Cron::CRON_TRIGGER_MODULE_ID,
self
)

Core.automation_manager.add_trigger_type(org.openhab.core.automation.type.TriggerType.new(
Cron::CRON_TRIGGER_MODULE_ID,
nil,
"A specific instant occurs",
"Triggers when the specified instant occurs",
nil,
org.openhab.core.automation.Visibility::VISIBLE,
nil
))
logger.trace("Added script cron trigger handler")
#
# Execute the callback
#
def run
callback&.triggered(@trigger, { "module" => @trigger.id })
end

#
# Dispose of the handler
# cancel the cron scheduled task
#
def dispose
synchronized do
super
return unless @schedule

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

# Invoked by openHAB core to get a trigger handler for the supplied trigger
# @param [org.openhab.core.automation.Trigger] trigger
#
# @return [WatchTriggerHandler] trigger handler for supplied trigger
def get(trigger)
CronTriggerHandler.new(trigger)
# Implements the ScriptedTriggerHandlerFactory interface to create a new Cron Trigger Handler
class CronTriggerHandlerFactory
include Singleton
include org.openhab.core.automation.module.script.rulesupport.shared.factories.ScriptedTriggerHandlerFactory # rubocop:disable Layout/LineLength

def initialize
Core.automation_manager.add_trigger_handler(
Cron::CRON_TRIGGER_MODULE_ID,
self
)

Core.automation_manager.add_trigger_type(org.openhab.core.automation.type.TriggerType.new(
Cron::CRON_TRIGGER_MODULE_ID,
nil,
"A specific instant occurs",
"Triggers when the specified instant occurs",
nil,
org.openhab.core.automation.Visibility::VISIBLE,
nil
))
logger.trace("Added script cron trigger handler")
end

# Invoked by openHAB core to get a trigger handler for the supplied trigger
# @param [org.openhab.core.automation.Trigger] trigger
#
# @return [WatchTriggerHandler] trigger handler for supplied trigger
def get(trigger)
CronTriggerHandler.new(trigger)
end
end
end
end
end
end
end

end

0 comments on commit 27f019b

Please sign in to comment.