Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[automation] Add provider script extension #4513

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* 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.automation.module.script.rulesupport.internal;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.module.script.ScriptExtensionProvider;
import org.openhab.core.automation.module.script.rulesupport.shared.ScriptedItemProvider;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;

/**
* The {@link ProviderScriptExtension} provides types to support providing openHAB entities like items through scripts.
* It handles the registration and un-registration of the {@link org.openhab.core.common.registry.Provider} services.
*
* @author Florian Hotze - Initial contribution
*/
@Component(immediate = true)
@NonNullByDefault
public class ProviderScriptExtension implements ScriptExtensionProvider {
static final String PRESET_NAME = "provider";
static final String ITEM_PROVIDER_NAME = "itemProvider";

private final Map<String, ItemProviderContainer> itemProviders = new ConcurrentHashMap<>();
private final BundleContext bundleContext;

@Activate
public ProviderScriptExtension(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}

@Override
public Collection<String> getDefaultPresets() {
return Set.of();
}

@Override
public Collection<String> getPresets() {
return Set.of(PRESET_NAME);
}

@Override
public Collection<String> getTypes() {
return Set.of(ITEM_PROVIDER_NAME);
}

@Override
public @Nullable Object get(String scriptIdentifier, String type) throws IllegalArgumentException {
if (ITEM_PROVIDER_NAME.equals(type)) {
return itemProviders.computeIfAbsent(scriptIdentifier, key -> {
ScriptedItemProvider itemProvider = new ScriptedItemProvider();
ServiceRegistration<ScriptedItemProvider> registration = bundleContext
.registerService(ScriptedItemProvider.class, itemProvider, null);
return new ItemProviderContainer(itemProvider, registration);
}).itemProvider();
}

return null;
}

@Override
public Map<String, Object> importPreset(String scriptIdentifier, String preset) {
if (PRESET_NAME.equals(preset)) {
return Map.of(ITEM_PROVIDER_NAME, Objects.requireNonNull(get(scriptIdentifier, ITEM_PROVIDER_NAME)));
}

return Map.of();
}

@Override
public void unload(String scriptIdentifier) {
ItemProviderContainer itemProviderContainer = itemProviders.remove(scriptIdentifier);
if (itemProviderContainer != null) {
itemProviderContainer.registration().unregister();
}
}

private record ItemProviderContainer(ScriptedItemProvider itemProvider,
ServiceRegistration<ScriptedItemProvider> registration) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.automation.module.script.rulesupport.shared;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.registry.AbstractProvider;
import org.openhab.core.common.registry.ManagedProvider;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemProvider;

/**
* This {@link ItemProvider} is used for a single ScriptEngine instance.
* It allows a script to provide items during its lifetime.
* This ensures that Items are not kept on reboot, but have to be provided by the script again.
*
* @author Florian Hotze - Initial contribution
*/
@NonNullByDefault
public class ScriptedItemProvider extends AbstractProvider<Item>
implements ItemProvider, ManagedProvider<Item, String> {
private final Map<String, Item> items = new HashMap<>();

@Override
public Collection<Item> getAll() {
return items.values();
}

@Override
public @Nullable Item get(String itemName) {
return items.get(itemName);
}

@Override
public void add(Item item) {
items.put(item.getName(), item);

notifyListenersAboutAddedElement(item);
}

@Override
public @Nullable Item update(Item item) {
Item oldItem = items.get(item.getName());
if (oldItem != null) {
items.put(item.getName(), item);
notifyListenersAboutUpdatedElement(oldItem, item);
}
return oldItem;
}

@Override
public @Nullable Item remove(String itemName) {
Item item = items.remove(itemName);
if (item != null) {
notifyListenersAboutRemovedElement(item);
}
return item;
}
}