/
ScriptExecution.java
100 lines (91 loc) · 4.25 KB
/
ScriptExecution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright (c) 2010-2024 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 java.time.ZonedDateTime;
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.model.core.ModelRepository;
import org.openhab.core.model.script.ScriptServiceUtil;
import org.openhab.core.model.script.engine.Script;
import org.openhab.core.model.script.engine.ScriptEngine;
import org.openhab.core.model.script.engine.ScriptExecutionException;
import org.openhab.core.model.script.engine.action.ActionDoc;
import org.openhab.core.model.script.internal.engine.action.ScriptExecutionActionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The {@link ScriptExecution} is a wrapper for the ScriptExecution actions
*
* @author Jan N. Klug - Initial contribution
*/
@NonNullByDefault
public class ScriptExecution {
private static final Logger logger = LoggerFactory.getLogger(ScriptExecution.class);
/**
* Calls a script which must be located in the configurations/scripts folder.
*
* @param scriptName the name of the script (if the name does not end with
* the .script file extension it is added)
*
* @return the return value of the script
* @throws ScriptExecutionException if an error occurs during the execution
*/
@ActionDoc(text = "call a script file")
public static Object callScript(String scriptName) throws ScriptExecutionException {
ModelRepository repo = ScriptServiceUtil.getModelRepository();
if (repo != null) {
String scriptNameWithExt = scriptName;
if (!scriptName.endsWith(Script.SCRIPT_FILEEXT)) {
scriptNameWithExt = scriptName + "." + Script.SCRIPT_FILEEXT;
}
XExpression expr = (XExpression) repo.getModel(scriptNameWithExt);
if (expr != null) {
ScriptEngine scriptEngine = ScriptServiceUtil.getScriptEngine();
if (scriptEngine != null) {
Script script = scriptEngine.newScriptFromXExpression(expr);
return script.execute();
} else {
throw new ScriptExecutionException("Script engine is not available.");
}
} else {
throw new ScriptExecutionException("Script '" + scriptName + "' cannot be found.");
}
} else {
throw new ScriptExecutionException("Model repository is not available.");
}
}
@ActionDoc(text = "create a timer")
public static Timer createTimer(ZonedDateTime zonedDateTime, Procedures.Procedure0 closure) {
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 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 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 new Timer(ScriptExecutionActionService.getScriptExecution().createTimerWithArgument(identifier,
zonedDateTime, arg1, closure::apply));
}
}