Skip to content

Commit

Permalink
Expose three classes used as bindings in JSR-223 rules as interfaces (o…
Browse files Browse the repository at this point in the history
…penhab#2723)

Signed-off-by: Jan N. Klug <github@klug.nrw>
GitOrigin-RevId: 9a9217e
  • Loading branch information
J-N-K authored and splatch committed Jul 12, 2023
1 parent a6028e6 commit c7cc73e
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 141 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2010-2022 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;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.module.script.internal.ScriptExtensionManager;

/**
*
* @author Simon Merschjohann - Initial contribution
*/
@NonNullByDefault
public interface ScriptExtensionManagerWrapper {
List<String> getTypes();

List<String> getPresets();

@Nullable
Object get(String type);

String getScriptIdentifier();

List<String> getDefaultPresets();

/**
* Imports a collection of named host objects/classes into a script engine instance. Sets of objects are provided
* under their object name, and categorized by preset name. This method will import all named objects for a specific
* preset name.
*
* @param preset the name of the preset to import
* @return a map of host object names to objects
* @see ScriptExtensionManager
*/
Map<String, Object> importPreset(String preset);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright (c) 2010-2022 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.defaultscope;

import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.Item;
import org.openhab.core.types.Command;
import org.openhab.core.types.State;

/**
* The static methods of this class are made available as functions in the scripts.
* This gives direct write access to the event bus from within scripts.
* Items should not be updated directly (setting the state property), but updates should
* be sent to the bus, so that all interested bundles are notified.
*
* Note: This class is a copy from the {@link org.openhab.core.model.script.actions.BusEvent} class
*
* @author Kai Kreuzer - Initial contribution
* @author Jan N. Klug - Refactored to interface
*/
@NonNullByDefault
public interface ScriptBusEvent {
/**
* Sends a command for a specified item to the event bus.
*
* @param item the item to send the command to
* @param commandString the command to send
*/
@Nullable
Object sendCommand(@Nullable Item item, @Nullable String commandString);

/**
* Sends a number as a command for a specified item to the event bus.
*
* @param item the item to send the command to
* @param number the number to send as a command
*/
@Nullable
Object sendCommand(@Nullable Item item, @Nullable Number number);

/**
* Sends a command for a specified item to the event bus.
*
* @param itemName the name of the item to send the command to
* @param commandString the command to send
*/
@Nullable
Object sendCommand(@Nullable String itemName, @Nullable String commandString);

/**
* Sends a command for a specified item to the event bus.
*
* @param item the item to send the command to
* @param command the command to send
*/
@Nullable
Object sendCommand(@Nullable Item item, @Nullable Command command);

/**
* Posts a status update for a specified item to the event bus.
*
* @param item the item to send the status update for
* @param state the new state of the item as a number
*/
@Nullable
Object postUpdate(@Nullable Item item, @Nullable Number state);

/**
* Posts a status update for a specified item to the event bus.
*
* @param item the item to send the status update for
* @param stateAsString the new state of the item
*/
@Nullable
Object postUpdate(@Nullable Item item, @Nullable String stateAsString);

/**
* Posts a status update for a specified item to the event bus.
*
* @param itemName the name of the item to send the status update for
* @param stateAsString the new state of the item
*/
@Nullable
Object postUpdate(@Nullable String itemName, @Nullable String stateAsString);

/**
* Posts a status update for a specified item to the event bus.
* t
*
* @param item the item to send the status update for
* @param state the new state of the item
*/
@Nullable
Object postUpdate(@Nullable Item item, @Nullable State state);

/**
* Stores the current states for a list of items in a map.
* A group item is not itself put into the map, but instead all its members.
*
* @param items the items for which the state should be stored
* @return the map of items with their states
*/
Map<Item, State> storeStates(Item @Nullable... items);

/**
* Restores item states from a map.
* If the saved state can be interpreted as a command, a command is sent for the item
* (and the physical device can send a status update if occurred). If it is no valid
* command, the item state is directly updated to the saved value.
*
* @param statesMap a map with ({@link Item}, {@link State}) entries
* @return null
*/
@Nullable
Object restoreStates(@Nullable Map<Item, State> statesMap);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2010-2022 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.defaultscope;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.binding.ThingActions;

/**
* The methods of this class are made available as functions in the scripts.
*
* Note: This class is a copy from the {@link org.openhab.core.model.script.internal.engine.action.ThingActionService}
* class
*
* @author Kai Kreuzer - Initial contribution
* @author Jan N. Klug - Refactored to interface
*/
@NonNullByDefault
public interface ScriptThingActions {
/**
* Gets an actions instance of a certain scope for a given thing UID
*
* @param scope the action scope
* @param thingUid the UID of the thing
* @return actions the actions instance or null, if not available
*/
@Nullable
ThingActions get(@Nullable String scope, @Nullable String thingUid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.core.automation.module.script.ScriptEngineContainer;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptEngineManager;
import org.openhab.core.automation.module.script.ScriptExtensionManagerWrapper;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -136,8 +137,8 @@ private boolean isCustomFactory(ScriptEngineFactory engineFactory) {
if (engine != null) {
Map<String, Object> scriptExManager = new HashMap<>();
result = new ScriptEngineContainer(engine, engineFactory, engineIdentifier);
ScriptExtensionManagerWrapper wrapper = new ScriptExtensionManagerWrapper(scriptExtensionManager,
result);
ScriptExtensionManagerWrapper wrapper = new ScriptExtensionManagerWrapperImpl(
scriptExtensionManager, result);
scriptExManager.put("scriptExtension", wrapper);
scriptExManager.put("se", wrapper);
engineFactory.scopeValues(engine, scriptExManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.module.script.ScriptEngineContainer;
import org.openhab.core.automation.module.script.ScriptEngineFactory;
import org.openhab.core.automation.module.script.ScriptExtensionManagerWrapper;
import org.openhab.core.automation.module.script.ScriptExtensionProvider;

/**
*
* @author Simon Merschjohann - Initial contribution
*/
@NonNullByDefault
public class ScriptExtensionManagerWrapper {
public class ScriptExtensionManagerWrapperImpl implements ScriptExtensionManagerWrapper {

private final ScriptEngineContainer container;
private final ScriptExtensionManager manager;

public ScriptExtensionManagerWrapper(ScriptExtensionManager manager, ScriptEngineContainer container) {
public ScriptExtensionManagerWrapperImpl(ScriptExtensionManager manager, ScriptEngineContainer container) {
this.manager = manager;
this.container = container;
}
Expand All @@ -44,38 +44,32 @@ public void removeScriptExtensionProvider(ScriptExtensionProvider provider) {
manager.removeExtension(provider);
}

@Override
public List<String> getTypes() {
return manager.getTypes();
}

@Override
public String getScriptIdentifier() {
return container.getIdentifier();
}

@Override
public List<String> getPresets() {
return manager.getPresets();
}

@Override
public @Nullable Object get(String type) {
return manager.get(type, container.getIdentifier());
}

@Override
public List<String> getDefaultPresets() {
return manager.getDefaultPresets();
}

/**
* Imports a collection of named host objects/classes into a script engine instance. Sets of objects are provided
* under their object name, and categorized by preset name. This method will import all named objects for a specific
* preset name.
*
* @implNote This call both returns the imported objects, and requests that the {@link ScriptEngineFactory} import
* them. The mechanism of how they are imported by the ScriptEngineFactory, or whether they are imported
* at all (aside from eing returned by this call) is dependent of the implementation of the
* ScriptEngineFactory.
*
* @apiNote Objects may appear in multiple named presets.
* @see ScriptExtensionManager
*
* @param preset the name of the preset to import
* @return a map of host object names to objects
*/
@Override
public Map<String, Object> importPreset(String preset) {
return manager.importPreset(preset, container.getFactory(), container.getScriptEngine(),
container.getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ public class DefaultScriptScopeProvider implements ScriptExtensionProvider {

private final Map<String, Object> elements = new ConcurrentHashMap<>();

private final ScriptBusEvent busEvent;
private final ScriptThingActions thingActions;
private final ScriptBusEventImpl busEvent;
private final ScriptThingActionsImpl thingActions;

@Activate
public DefaultScriptScopeProvider(final @Reference ItemRegistry itemRegistry,
final @Reference ThingRegistry thingRegistry, final @Reference RuleRegistry ruleRegistry,
final @Reference EventPublisher eventPublisher) {
this.busEvent = new ScriptBusEvent(itemRegistry, eventPublisher);
this.thingActions = new ScriptThingActions(thingRegistry);
this.busEvent = new ScriptBusEventImpl(itemRegistry, eventPublisher);
this.thingActions = new ScriptThingActionsImpl(thingRegistry);

elements.put("State", State.class);
elements.put("Command", Command.class);
Expand Down
Loading

0 comments on commit c7cc73e

Please sign in to comment.