Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bundles/org.openhab.core.model.script/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Export-Package: org.openhab.core.model.script,\
org.openhab.core.model.script.validation
Import-Package: \
org.openhab.core.audio,\
org.openhab.core.automation,\
org.openhab.core.automation.module.script.action,\
org.openhab.core.automation.module.script.rulesupport.shared,\
org.openhab.core.common.registry,\
Expand Down
5 changes: 5 additions & 0 deletions bundles/org.openhab.core.model.script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>org.openhab.core.io.net</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.automation</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.automation.module.script</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.RuleManager;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.model.core.ModelRepository;
import org.openhab.core.model.script.engine.ScriptEngine;
import org.openhab.core.model.script.engine.action.ActionService;
Expand Down Expand Up @@ -50,6 +54,9 @@ public class ScriptServiceUtil {
private final ThingRegistry thingRegistry;
private final EventPublisher eventPublisher;
private final ModelRepository modelRepository;
private final MetadataRegistry metadataRegistry;
private final RuleRegistry ruleRegistry;
private volatile @Nullable RuleManager ruleManager;
private final Scheduler scheduler;

private final AtomicReference<ScriptEngine> scriptEngine = new AtomicReference<>();
Expand All @@ -59,11 +66,14 @@ public class ScriptServiceUtil {
@Activate
public ScriptServiceUtil(final @Reference ItemRegistry itemRegistry, final @Reference ThingRegistry thingRegistry,
final @Reference EventPublisher eventPublisher, final @Reference ModelRepository modelRepository,
final @Reference MetadataRegistry metadataRegistry, final @Reference RuleRegistry ruleRegistry,
final @Reference Scheduler scheduler) {
this.itemRegistry = itemRegistry;
this.thingRegistry = thingRegistry;
this.eventPublisher = eventPublisher;
this.modelRepository = modelRepository;
this.metadataRegistry = metadataRegistry;
this.ruleRegistry = ruleRegistry;
this.scheduler = scheduler;

if (instance != null) {
Expand All @@ -73,6 +83,15 @@ public ScriptServiceUtil(final @Reference ItemRegistry itemRegistry, final @Refe
logger.debug("ScriptServiceUtil started");
}

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
void setRuleManager(RuleManager ruleManager) {
this.ruleManager = ruleManager;
}

void unsetRuleManager(RuleManager ruleManager) {
this.ruleManager = null;
}

@Deactivate
public void deactivate() {
logger.debug("ScriptServiceUtil stopped");
Expand All @@ -91,6 +110,10 @@ public ItemRegistry getItemRegistryInstance() {
return itemRegistry;
}

public static ThingRegistry getThingRegistry() {
return getInstance().thingRegistry;
}

public ThingRegistry getThingRegistryInstance() {
return thingRegistry;
}
Expand All @@ -107,6 +130,30 @@ public ModelRepository getModelRepositoryInstance() {
return modelRepository;
}

public static MetadataRegistry getMetadataRegistry() {
return getInstance().metadataRegistry;
}

public MetadataRegistry getMetadataRegistryInstance() {
return metadataRegistry;
}

public static RuleRegistry getRuleRegistry() {
return getInstance().ruleRegistry;
}

public RuleRegistry getRuleRegistryInstance() {
return ruleRegistry;
}

public @Nullable static RuleManager getRuleManager() {
return getInstance().ruleManager;
}

public @Nullable RuleManager getRuleManagerInstance() {
return ruleManager;
}

public static Scheduler getScheduler() {
return getInstance().scheduler;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2010-2026 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.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.items.Metadata;

/**
* {@link ItemExtensions} provides DSL access to things like OSGi instances, system registries and the ability to run
* other
* rules.
*
* @author Ravi Nadahar - Initial contribution
*/
@NonNullByDefault
public class ItemExtensions {

@NonNullByDefault({})
public static void addMetadata(Item item, String namespace, String value) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
Items.addMetadata(item.getName(), namespace, value, (String) null);
}

@NonNullByDefault({})
public static void addMetadata(Item item, String namespace, String value, Object... configuration) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
Items.addMetadata(item.getName(), namespace, value, parseObjectArray(configuration));
}

@NonNullByDefault({})
public static void addMetadata(Item item, String namespace, String value,
@Nullable Map<@NonNull String, @NonNull Object> configuration) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
Items.addMetadata(item.getName(), namespace, value, configuration);
}

@NonNullByDefault({})
public static @Nullable Metadata getMetadata(Item item, String namespace) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
return Items.getMetadata(item.getName(), namespace);
}

@NonNullByDefault({})
public static @Nullable Metadata removeMetadata(Item item, String namespace) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
return Items.removeMetadata(item.getName(), namespace);
}

@NonNullByDefault({})
public static @Nullable Metadata updateMetadata(Item item, String namespace, String value) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
return Items.updateMetadata(item.getName(), namespace, value);
}

@NonNullByDefault({})
public static @Nullable Metadata updateMetadata(Item item, String namespace, String value,
Object... configuration) {
return Items.updateMetadata(item.getName(), namespace, value, parseObjectArray(configuration));
}

@NonNullByDefault({})
public static @Nullable Metadata updateMetadata(Item item, String namespace, String value,
@Nullable Map<@NonNull String, @NonNull Object> configuration) {
if (item == null) {
throw new IllegalArgumentException("item cannot be null");
}
return Items.updateMetadata(item.getName(), namespace, value);
}

private static Map<String, Object> parseObjectArray(Object @Nullable [] objects) throws IllegalArgumentException {
if (objects == null || objects.length == 0) {
return Map.of();
}
if ((objects.length % 2) != 0) {
throw new IllegalArgumentException("There must be an even number of objects (" + objects.length + ')');
}
Map<String, Object> result = new LinkedHashMap<>();
for (int i = 0; i < objects.length; i += 2) {
if (objects[i] instanceof String key) {
result.put(key, objects[i + 1]);
} else {
throw new IllegalArgumentException("Keys must be strings: " + objects[i]);
}
}
return result;
}
}
Loading
Loading