|
| 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 | +} |
0 commit comments