Skip to content

Commit 6110ff7

Browse files
author
Ravi Nadahar
committed
Add access to various system registries, OSGi instances and the ability to run and enable/disable rules to DSL scripts
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
1 parent b18e2db commit 6110ff7

5 files changed

Lines changed: 230 additions & 1 deletion

File tree

bundles/org.openhab.core.model.script/bnd.bnd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Export-Package: org.openhab.core.model.script,\
1919
org.openhab.core.model.script.validation
2020
Import-Package: \
2121
org.openhab.core.audio,\
22+
org.openhab.core.automation,\
2223
org.openhab.core.automation.module.script.action,\
2324
org.openhab.core.automation.module.script.rulesupport.shared,\
2425
org.openhab.core.common.registry,\

bundles/org.openhab.core.model.script/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
<artifactId>org.openhab.core.io.net</artifactId>
5151
<version>${project.version}</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>org.openhab.core.bundles</groupId>
55+
<artifactId>org.openhab.core.automation</artifactId>
56+
<version>${project.version}</version>
57+
</dependency>
5358
<dependency>
5459
<groupId>org.openhab.core.bundles</groupId>
5560
<artifactId>org.openhab.core.automation.module.script</artifactId>

bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/ScriptServiceUtil.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import java.util.concurrent.CopyOnWriteArrayList;
1717
import java.util.concurrent.atomic.AtomicReference;
1818

19+
import org.openhab.core.automation.RuleManager;
20+
import org.openhab.core.automation.RuleRegistry;
1921
import org.openhab.core.events.EventPublisher;
2022
import org.openhab.core.items.ItemRegistry;
23+
import org.openhab.core.items.MetadataRegistry;
2124
import org.openhab.core.model.core.ModelRepository;
2225
import org.openhab.core.model.script.engine.ScriptEngine;
2326
import org.openhab.core.model.script.engine.action.ActionService;
@@ -50,6 +53,9 @@ public class ScriptServiceUtil {
5053
private final ThingRegistry thingRegistry;
5154
private final EventPublisher eventPublisher;
5255
private final ModelRepository modelRepository;
56+
private final MetadataRegistry metadataRegistry;
57+
private final RuleRegistry ruleRegistry;
58+
private final RuleManager ruleManager;
5359
private final Scheduler scheduler;
5460

5561
private final AtomicReference<ScriptEngine> scriptEngine = new AtomicReference<>();
@@ -59,11 +65,15 @@ public class ScriptServiceUtil {
5965
@Activate
6066
public ScriptServiceUtil(final @Reference ItemRegistry itemRegistry, final @Reference ThingRegistry thingRegistry,
6167
final @Reference EventPublisher eventPublisher, final @Reference ModelRepository modelRepository,
62-
final @Reference Scheduler scheduler) {
68+
final @Reference MetadataRegistry metadataRegistry, final @Reference RuleRegistry ruleRegistry,
69+
final @Reference RuleManager ruleManager, final @Reference Scheduler scheduler) {
6370
this.itemRegistry = itemRegistry;
6471
this.thingRegistry = thingRegistry;
6572
this.eventPublisher = eventPublisher;
6673
this.modelRepository = modelRepository;
74+
this.metadataRegistry = metadataRegistry;
75+
this.ruleRegistry = ruleRegistry;
76+
this.ruleManager = ruleManager;
6777
this.scheduler = scheduler;
6878

6979
if (instance != null) {
@@ -91,6 +101,10 @@ public ItemRegistry getItemRegistryInstance() {
91101
return itemRegistry;
92102
}
93103

104+
public static ThingRegistry getThingRegistry() {
105+
return getInstance().thingRegistry;
106+
}
107+
94108
public ThingRegistry getThingRegistryInstance() {
95109
return thingRegistry;
96110
}
@@ -107,6 +121,30 @@ public ModelRepository getModelRepositoryInstance() {
107121
return modelRepository;
108122
}
109123

124+
public static MetadataRegistry getMetadataRegistry() {
125+
return getInstance().metadataRegistry;
126+
}
127+
128+
public MetadataRegistry getMetadataRegistryInstance() {
129+
return metadataRegistry;
130+
}
131+
132+
public static RuleRegistry getRuleRegistry() {
133+
return getInstance().ruleRegistry;
134+
}
135+
136+
public RuleRegistry getRuleRegistryInstance() {
137+
return ruleRegistry;
138+
}
139+
140+
public static RuleManager getRuleManager() {
141+
return getInstance().ruleManager;
142+
}
143+
144+
public RuleManager getRuleManagerInstance() {
145+
return ruleManager;
146+
}
147+
110148
public static Scheduler getScheduler() {
111149
return getInstance().scheduler;
112150
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 2010-2026 Contributors to the openHAB project
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
package org.openhab.core.model.script.actions;
14+
15+
import java.util.Map;
16+
17+
import org.eclipse.jdt.annotation.NonNullByDefault;
18+
import org.eclipse.jdt.annotation.Nullable;
19+
import org.openhab.core.automation.RuleManager;
20+
import org.openhab.core.automation.RuleRegistry;
21+
import org.openhab.core.items.ItemRegistry;
22+
import org.openhab.core.items.MetadataRegistry;
23+
import org.openhab.core.model.script.ScriptServiceUtil;
24+
import org.openhab.core.model.script.engine.action.ActionDoc;
25+
import org.openhab.core.thing.ThingRegistry;
26+
import org.osgi.framework.Bundle;
27+
import org.osgi.framework.BundleContext;
28+
import org.osgi.framework.FrameworkUtil;
29+
import org.osgi.framework.ServiceReference;
30+
31+
/**
32+
* {@link System} provides DSL access to things like OSGi instances, system registries and the ability to run other
33+
* rules.
34+
*
35+
* @author Ravi Nadahar - Initial contribution
36+
*/
37+
@NonNullByDefault
38+
public class System {
39+
40+
/**
41+
* Retrieve an OSGi instance of the specified {@link Class}, if it exists.
42+
*
43+
* @param <T> the class type.
44+
* @param clazz the class of the instance to get.
45+
* @return The instance or {@code null} if the instance wasn't found.
46+
*/
47+
@ActionDoc(text = "acquire an OSGi instance")
48+
public static @Nullable <T> T getInstance(Class<T> clazz) {
49+
Bundle bundle = FrameworkUtil.getBundle(clazz);
50+
if (bundle != null) {
51+
BundleContext bc = bundle.getBundleContext();
52+
if (bc != null) {
53+
ServiceReference<T> ref = bc.getServiceReference(clazz);
54+
if (ref != null) {
55+
return bc.getService(ref);
56+
}
57+
}
58+
}
59+
return null;
60+
}
61+
62+
/**
63+
* Run the rule with the specified UID.
64+
*
65+
* @param ruleUID the UID of the rule to run.
66+
* @return A copy of the rule context, including possible return values.
67+
* @throws IllegalArgumentException If a rule with the specified UID doesn't exist.
68+
*/
69+
@ActionDoc(text = "run the rule with the specified UID")
70+
public static Map<String, Object> runRule(String ruleUID) {
71+
RuleManager ruleManager = ScriptServiceUtil.getRuleManager();
72+
if (ruleManager.getStatus(ruleUID) == null) {
73+
throw new IllegalArgumentException("Rule with UID '" + ruleUID + "' doesn't exist");
74+
}
75+
return ruleManager.runNow(ruleUID);
76+
}
77+
78+
/**
79+
* Run the rule with the specified UID with the specified context.
80+
*
81+
* @param ruleUID the UID of the rule to run.
82+
* @param context the {@link Map} of {@link String} and {@link Object} pairs that constitutes the context.
83+
* @return A copy of the rule context, including possible return values.
84+
* @throws IllegalArgumentException If a rule with the specified UID doesn't exist.
85+
*/
86+
@ActionDoc(text = "run the rule with the specified UID and context")
87+
public static Map<String, Object> runRule(String ruleUID, Map<String, Object> context) {
88+
RuleManager ruleManager = ScriptServiceUtil.getRuleManager();
89+
if (ruleManager.getStatus(ruleUID) == null) {
90+
throw new IllegalArgumentException("Rule with UID '" + ruleUID + "' doesn't exist");
91+
}
92+
return ruleManager.runNow(ruleUID, false, context);
93+
}
94+
95+
/**
96+
* Run the rule with the specified UID with the specified context, while optionally taking conditions into
97+
* account.
98+
*
99+
* @param ruleUID the UID of the rule to run.
100+
* @param considerConditions {@code true} to not run the rule if its conditions don't qualify.
101+
* @param context the {@link Map} of {@link String} and {@link Object} pairs that constitutes the context.
102+
* @return A copy of the rule context, including possible return values.
103+
* @throws IllegalArgumentException If a rule with the specified UID doesn't exist.
104+
*/
105+
@ActionDoc(text = "run the rule with the specified UID, condition evaluation setting and context")
106+
public static Map<String, Object> runRule(String ruleUID, boolean considerConditions, Map<String, Object> context) {
107+
RuleManager ruleManager = ScriptServiceUtil.getRuleManager();
108+
if (ruleManager.getStatus(ruleUID) == null) {
109+
throw new IllegalArgumentException("Rule with UID '" + ruleUID + "' doesn't exist");
110+
}
111+
return ruleManager.runNow(ruleUID, considerConditions, context);
112+
}
113+
114+
/**
115+
* Check whether the specified rule is enabled.
116+
*
117+
* @param ruleUID the UID of the rule to check.
118+
* @return {@code true} if the rule is enabled, {@code false} otherwise.
119+
* @throws IllegalArgumentException If a rule with the specified UID doesn't exist.
120+
*/
121+
@ActionDoc(text = "check whether the specified rule rule is enabled")
122+
public static boolean isRuleEnabled(String ruleUID) {
123+
RuleManager ruleManager = ScriptServiceUtil.getRuleManager();
124+
Boolean result = ruleManager.isEnabled(ruleUID);
125+
if (result == null) {
126+
throw new IllegalArgumentException("Rule with UID '" + ruleUID + "' doesn't exist");
127+
}
128+
return result.booleanValue();
129+
}
130+
131+
/**
132+
* Set whether the specified rule is enabled.
133+
*
134+
* @param ruleUID the UID of the rule to enable or disable.
135+
* @param enabled {@code true} to enable the rule, {@code false} to disable the rule.
136+
* @throws IllegalArgumentException If a rule with the specified UID doesn't exist.
137+
*/
138+
@ActionDoc(text = "set whether the specified rule is enabled")
139+
public static void setRuleEnabled(String ruleUID, boolean enabled) {
140+
ScriptServiceUtil.getRuleManager().setEnabled(ruleUID, enabled);
141+
}
142+
143+
/**
144+
* @return The {@link ThingRegistry}.
145+
*/
146+
@ActionDoc(text = "get the thing registry")
147+
public static ThingRegistry getThingRegistry() {
148+
return ScriptServiceUtil.getThingRegistry();
149+
}
150+
151+
/**
152+
* @return The {@link MetadataRegistry}.
153+
*/
154+
@ActionDoc(text = "get the metadata registry")
155+
public static MetadataRegistry getMetadataRegistry() {
156+
return ScriptServiceUtil.getMetadataRegistry();
157+
}
158+
159+
/**
160+
* @return The {@link ItemRegistry}.
161+
*/
162+
@ActionDoc(text = "get the item registry")
163+
public static ItemRegistry getItemRegistry() {
164+
return ScriptServiceUtil.getItemRegistry();
165+
}
166+
167+
/**
168+
* @return The {@link RuleRegistry}.
169+
*/
170+
@ActionDoc(text = "get the rule registry")
171+
public static RuleRegistry getRuleRegistry() {
172+
return ScriptServiceUtil.getRuleRegistry();
173+
}
174+
175+
/**
176+
* @return The {@link RuleManager}.
177+
*/
178+
@ActionDoc(text = "get the rule manager")
179+
public static RuleManager getRuleManager() {
180+
return ScriptServiceUtil.getRuleManager();
181+
}
182+
}

bundles/org.openhab.core.model.script/src/org/openhab/core/model/script/scoping/ScriptImplicitlyImportedTypes.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.openhab.core.model.script.actions.Log;
3636
import org.openhab.core.model.script.actions.Ping;
3737
import org.openhab.core.model.script.actions.ScriptExecution;
38+
import org.openhab.core.model.script.actions.System;
3839
import org.openhab.core.model.script.actions.Transformation;
3940
import org.openhab.core.model.script.engine.IActionServiceProvider;
4041
import org.openhab.core.model.script.engine.IThingActionsProvider;
@@ -76,6 +77,7 @@ protected List<Class<?>> getExtensionClasses() {
7677
result.add(Ping.class);
7778
result.add(Transformation.class);
7879
result.add(ScriptExecution.class);
80+
result.add(System.class);
7981
result.add(URLEncoder.class);
8082

8183
result.addAll(getActionClasses());
@@ -92,6 +94,7 @@ protected List<Class<?>> getStaticImportClasses() {
9294
result.add(Ping.class);
9395
result.add(Transformation.class);
9496
result.add(ScriptExecution.class);
97+
result.add(System.class);
9598
result.add(URLEncoder.class);
9699
result.add(CoreUtil.class);
97100

0 commit comments

Comments
 (0)