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

Added transformation service support #20

Merged
merged 4 commits into from
Feb 2, 2022
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,23 @@ Use case: Override logging for all rules defined in one file
}
```

## Example 23

Use case: Apply transformation using openHAB transformation service

```java
public class TransformationRule extends JRule {

@JRuleName("MyTransformation")
@JRuleWhen(item = _MyStringValue.ITEM, trigger = _MyStringValue.TRIGGER_RECEIVED_COMMAND)
public void applyTransformation(JRuleEvent event) {
String transformedValue = transform("MAP(my.map):%s", event.getValue());
logInfo("Transformed {} to {}", event.getValue(), transformedValue);
_MyTransformationReceiver.sendCommand(transformedValue);
}
}
```

# Changelog
## BETA5
- Addes support for adding rules in jar-files, as an alternative.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.voice.VoiceManager;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class JRuleFactory {
@Activate
public JRuleFactory(Map<String, Object> properties, final @Reference JRuleEventSubscriber eventSubscriber,
final @Reference ItemRegistry itemRegistry, final @Reference EventPublisher eventPublisher,
final @Reference VoiceManager voiceManager) {
final @Reference VoiceManager voiceManager, final ComponentContext componentContext) {
this.itemRegistry = itemRegistry;
this.eventSubscriber = eventSubscriber;
this.eventPublisher = eventPublisher;
Expand All @@ -72,7 +73,8 @@ public JRuleFactory(Map<String, Object> properties, final @Reference JRuleEventS
config.initConfig();
jRuleEngine = JRuleEngine.get();
jRuleEngine.setConfig(config);
jRuleHandler = new JRuleHandler(config, itemRegistry, eventPublisher, eventSubscriber, voiceManager);
jRuleHandler = new JRuleHandler(properties, itemRegistry, eventPublisher, eventSubscriber, voiceManager,
componentContext.getBundleContext());
createDelayedInitialization(getInitDelaySeconds(properties));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.openhab.core.items.events.ItemRemovedEvent;
import org.openhab.core.items.events.ItemUpdatedEvent;
import org.openhab.core.voice.VoiceManager;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -72,6 +73,9 @@ public class JRuleHandler implements PropertyChangeListener {
@NonNullByDefault({})
private VoiceManager voiceManager;

@NonNullByDefault({})
private BundleContext bundleContext;

private final Logger logger = LoggerFactory.getLogger(JRuleHandler.class);

@Nullable
Expand All @@ -92,17 +96,21 @@ public class JRuleHandler implements PropertyChangeListener {
private volatile boolean recompileJar = true;

public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, EventPublisher eventPublisher,
JRuleEventSubscriber eventSubscriber, VoiceManager voiceManager) {
JRuleEventSubscriber eventSubscriber, VoiceManager voiceManager, BundleContext bundleContext) {
this.itemRegistry = itemRegistry;
this.eventSubscriber = eventSubscriber;
this.voiceManager = voiceManager;
this.config = config;
this.bundleContext = bundleContext;

JRuleEventHandler jRuleEventHandler = JRuleEventHandler.get();
jRuleEventHandler.setEventPublisher(eventPublisher);
jRuleEventHandler.setItemRegistry(itemRegistry);
eventSubscriber.addPropertyChangeListener(this);
JRuleVoiceHandler jRuleVoiceHandler = JRuleVoiceHandler.get();
jRuleVoiceHandler.setVoiceManager(voiceManager);
JRuleTransformationHandler jRuleTransformationHandler = JRuleTransformationHandler.get();
jRuleTransformationHandler.setBundleContext(bundleContext);
logDebug("JRuleHandler()");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright (c) 2010-2021 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.automation.jrule.internal.handler;

import org.openhab.core.transform.TransformationException;
import org.openhab.core.transform.TransformationHelper;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link JRuleTransformationHandler} is responsible for handling transformation requests
*
* @author Arne Seime - Initial contribution
*/
public class JRuleTransformationHandler {

private static volatile JRuleTransformationHandler instance;

private BundleContext bundleContext;

private final Logger logger = LoggerFactory.getLogger(JRuleTransformationHandler.class);

private JRuleTransformationHandler() {
}

public static JRuleTransformationHandler get() {
if (instance == null) {
synchronized (JRuleTransformationHandler.class) {
if (instance == null) {
instance = new JRuleTransformationHandler();
}
}
}
return instance;
}

public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
}

public String transform(String stateDescPattern, String state) throws TransformationException {
return TransformationHelper.transform(bundleContext, stateDescPattern, state);
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/openhab/automation/jrule/rules/JRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.openhab.automation.jrule.internal.engine.JRuleEngine;
import org.openhab.automation.jrule.internal.handler.JRuleActionHandler;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.internal.handler.JRuleTransformationHandler;
import org.openhab.automation.jrule.internal.handler.JRuleVoiceHandler;
import org.openhab.automation.jrule.items.JRulePercentType;
import org.openhab.automation.jrule.rules.value.JRuleOnOffValue;
import org.openhab.core.transform.TransformationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -165,6 +167,10 @@ protected void say(String text) {
JRuleVoiceHandler.get().say(text);
}

protected String transform(String stateDescPattern, String state) throws TransformationException {
return JRuleTransformationHandler.get().transform(stateDescPattern, state);
}

protected void executeCommandLine(String... commandLine) {
JRuleActionHandler.get().executeCommandLine(commandLine);
}
Expand Down