From f6e36c18528e816d3f2221d007f5fd845a74825e Mon Sep 17 00:00:00 2001 From: J-N-K Date: Wed, 23 Nov 2022 22:35:32 +0100 Subject: [PATCH] [auromation] Fix createTimer for DSL rules (#3172) * Fix createTimer for DSL rules Signed-off-by: Jan N. Klug --- .../model/script/actions/ScriptExecution.java | 9 ++- .../core/model/script/actions/Timer.java | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/ScriptExecution.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/ScriptExecution.java index cd1136da05..6b651e834e 100644 --- a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/ScriptExecution.java +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/ScriptExecution.java @@ -16,7 +16,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.xtext.xbase.XExpression; import org.eclipse.xtext.xbase.lib.Procedures; -import org.openhab.core.automation.module.script.action.Timer; import org.openhab.core.model.core.ModelRepository; import org.openhab.core.model.script.ScriptServiceUtil; import org.openhab.core.model.script.engine.Script; @@ -75,21 +74,21 @@ public class ScriptExecution { @ActionDoc(text = "create a timer") public static Timer createTimer(ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) { - return ScriptExecutionActionService.getScriptExecution().createTimer(zonedDateTime, closure::apply); + return new Timer(ScriptExecutionActionService.getScriptExecution().createTimer(zonedDateTime, closure::apply)); } @ActionDoc(text = "create an identifiable timer ") public static Timer createTimer(@Nullable String identifier, ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) { - return ScriptExecutionActionService.getScriptExecution().createTimer(identifier, zonedDateTime, closure::apply); + return new Timer(ScriptExecutionActionService.getScriptExecution().createTimer(identifier, zonedDateTime, closure::apply)); } @ActionDoc(text = "create a timer with argument") public static Timer createTimerWithArgument(ZonedDateTime zonedDateTime, Object arg1, Procedures.Procedure1 closure) { - return ScriptExecutionActionService.getScriptExecution().createTimerWithArgument(zonedDateTime, arg1, closure::apply); + return new Timer(ScriptExecutionActionService.getScriptExecution().createTimerWithArgument(zonedDateTime, arg1, closure::apply)); } @ActionDoc(text = "create an identifiable timer with argument") public static Timer createTimerWithArgument(@Nullable String identifier, ZonedDateTime zonedDateTime, Object arg1, Procedures.Procedure1 closure) { - return ScriptExecutionActionService.getScriptExecution().createTimerWithArgument(identifier, zonedDateTime, arg1, closure::apply); + return new Timer(ScriptExecutionActionService.getScriptExecution().createTimerWithArgument(identifier, zonedDateTime, arg1, closure::apply)); } } diff --git a/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java new file mode 100644 index 0000000000..e15d7670ee --- /dev/null +++ b/bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/actions/Timer.java @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2010-2022 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.core.model.script.actions; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; + +import java.time.ZonedDateTime; + +/** + * The {@link Timer} is a wrapper for the {@link org.openhab.core.automation.module.script.action.Timer} + * interface. This is necessary because the implementation methods of an interface can't be called from + * the script engine if the implementation is not in a public package or internal to the model bundle + * + * @author Jan N. Klug - Initial contribution + */ +@NonNullByDefault +public class Timer { + + private final org.openhab.core.automation.module.script.action.Timer timer; + + public Timer(org.openhab.core.automation.module.script.action.Timer timer) { + this.timer = timer; + } + + public boolean cancel() { + return timer.cancel(); + } + + public @Nullable ZonedDateTime getExecutionTime() { + return timer.getExecutionTime(); + } + + public boolean isActive() { + return timer.isActive(); + } + + public boolean isCancelled() { + return timer.isCancelled(); + } + + public boolean isRunning() { + return timer.isRunning(); + } + + public boolean hasTerminated() { + return timer.hasTerminated(); + } + + public boolean reschedule(ZonedDateTime newTime) { + return timer.reschedule(newTime); + } +}