From f9f43e4340767db6f4905679c5d53376fc9bfb4b Mon Sep 17 00:00:00 2001 From: seaside1 Date: Wed, 4 Oct 2023 21:21:47 +0200 Subject: [PATCH 1/4] Initial commit for dynamic rules for PR --- .../internal/compiler/JRuleCompiler.java | 25 +++++-- .../jrule/internal/engine/JRuleEngine.java | 21 +++++- .../jrule/internal/handler/JRuleHandler.java | 11 +-- .../internal/handler/JRuleItemHandler.java | 70 +++++++++++++++++++ .../internal/handler/JRuleThingHandler.java | 7 ++ .../openhab/automation/jrule/rules/JRule.java | 10 ++- 6 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java diff --git a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java index 487528a2..4d3911a8 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java @@ -58,6 +58,8 @@ */ public class JRuleCompiler { + private static final String ORG_OPENHAB_CORE = "org.openhab.core-"; + private static final String ORG_OPENHAB_CORE_THING = "org.openhab.core.thing-"; private static final String JAVA_CLASS_PATH_PROPERTY = "java.class.path"; private static final String CLASSPATH_OPTION = "-classpath"; public static final String JAR_JRULE_NAME = "jrule.jar"; @@ -149,7 +151,8 @@ public void loadClass(ClassLoader classLoader, String className, boolean createI final Object obj = loadedClass.getDeclaredConstructor().newInstance(); logDebug("Created instance: {} obj: {}", className, obj); } catch (Exception x) { - logError("Could not create create instance using default constructor: {}: {}", className, x); + logError("Could not create create instance using default constructor: {}", className); + logger.error("Failed to load class", x); } } } @@ -241,11 +244,14 @@ public boolean compile(List javaSourceFiles, String classPath) { final StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); final List optionList = new ArrayList<>(); optionList.add(CLASSPATH_OPTION); - String openhabCoreJar = getOpenhabCoreJar().map(s -> s + File.pathSeparator).orElse(""); + final String openhabCoreJar = getOpenhabCoreJar().map(s -> s + File.pathSeparator).orElse(""); logDebug("Openhab-Core Jar: {}", openhabCoreJar); - String cp = openhabCoreJar + System.getProperty(JAVA_CLASS_PATH_PROPERTY) + File.pathSeparator + classPath; + final String openhabCoreThingJar = getOpenhabCoreThingJar().map(s -> s + File.pathSeparator).orElse(""); + logDebug("Openhab-Core-Thing Jar: {}", openhabCoreThingJar); + String cp = openhabCoreJar + System.getProperty(JAVA_CLASS_PATH_PROPERTY) + File.pathSeparator + + openhabCoreThingJar + System.getProperty(JAVA_CLASS_PATH_PROPERTY) + File.pathSeparator + classPath; optionList.add(cp); - logDebug("Compiling classes using classpath: {}", cp); + logDebug("1337Compiling classes using classpath: {}", cp); javaSourceFiles.stream().filter(javaSourceFile -> javaSourceFile.exists() && javaSourceFile.canRead()) .forEach(javaSourceFile -> logDebug("Compiling java Source file: {}", javaSourceFile)); @@ -273,6 +279,14 @@ public boolean compile(List javaSourceFiles, String classPath) { } private Optional getOpenhabCoreJar() { + return getOpenHABJar(ORG_OPENHAB_CORE); + } + + private Optional getOpenhabCoreThingJar() { + return getOpenHABJar(ORG_OPENHAB_CORE_THING); + } + + private Optional getOpenHABJar(String jarPrefix) { if (!System.getProperties().containsKey(PROPERTY_KARAF_HOME_URI) && !System.getProperties().containsKey(PROPERTY_KARAF_DEFAULT_REPOSITORY)) { logWarn("required system properties does not exist [{}]", @@ -284,9 +298,10 @@ private Optional getOpenhabCoreJar() { + System.getProperty(PROPERTY_KARAF_DEFAULT_REPOSITORY); logDebug("Openhab Jars path: {}", openhabJars); Optional coreJarPath; + try (Stream stream = Files.walk(Paths.get(openhabJars))) { coreJarPath = stream.filter(path -> path.getFileName().toString().endsWith(JRuleConstants.JAR_FILE_TYPE)) - .filter(path -> path.getFileName().toString().startsWith("org.openhab.core-")) + .filter(path -> path.getFileName().toString().startsWith(jarPrefix)) .map(path -> path.toFile().getAbsolutePath()).findFirst(); } catch (IOException e) { logError(e.getMessage()); diff --git a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java index 6fbe30d9..d90870d1 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java +++ b/src/main/java/org/openhab/automation/jrule/internal/engine/JRuleEngine.java @@ -18,6 +18,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.time.Duration; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -97,7 +98,7 @@ */ public class JRuleEngine implements PropertyChangeListener { public static final String MDC_KEY_TIMER = "timer"; - private static final String[] EMPTY_LOG_TAGS = new String[0]; + public static final String[] EMPTY_LOG_TAGS = new String[0]; private static final int AWAIT_TERMINATION_THREAD_SECONDS = 2; private List contextList = new CopyOnWriteArrayList<>(); private JRuleTimerExecutor timerExecutor = new JRuleTimerExecutor(this); @@ -135,6 +136,24 @@ public void add(JRule jRule, boolean enableRule) { .forEach(method -> this.add(method, jRule, enableRule)); } + public void addDynamicWhenReceivedCommand(Method method, JRule jRule, String ruleName, List items) { + JRuleModuleEntry ruleModuleEntry = new JRuleModuleEntry(jRule, method, ruleName); + List emptyPreconditionContextList = new ArrayList<>(0); + for (String itemName : items) { + JRuleItemReceivedCommandExecutionContext context = new JRuleItemReceivedCommandExecutionContext(jRule, + ruleName, JRuleEngine.EMPTY_LOG_TAGS, method, itemName, JRuleMemberOf.None, Optional.empty(), + emptyPreconditionContextList, Optional.empty(), null, null); + + addToContext(context, false); + ruleLoadingStatistics.addItemStateTrigger(); + ruleModuleEntry.addJRuleWhenItemReceivedCommand(context); + + logInfo("Adding Dynamic Rule Name: {} item: {}", ruleName, itemName); + } + ruleLoadingStatistics.addRuleClass(); + ruleProvider.add(ruleModuleEntry); + } + private void add(Method method, JRule jRule, boolean enableRule) { logDebug("Adding rule method: {}", method.getName()); diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java index 8e24c476..f5b22c3e 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java @@ -136,19 +136,22 @@ public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ThingRegistry actionGenerator = new JRuleActionClassGenerator(config); compiler = new JRuleCompiler(config); - JRuleEventHandler jRuleEventHandler = JRuleEventHandler.get(); + final JRuleEventHandler jRuleEventHandler = JRuleEventHandler.get(); jRuleEventHandler.setEventPublisher(eventPublisher); jRuleEventHandler.setItemRegistry(itemRegistry); eventSubscriber.addPropertyChangeListener(this); - JRuleVoiceHandler jRuleVoiceHandler = JRuleVoiceHandler.get(); + final JRuleVoiceHandler jRuleVoiceHandler = JRuleVoiceHandler.get(); jRuleVoiceHandler.setVoiceManager(voiceManager); - JRuleTransformationHandler jRuleTransformationHandler = JRuleTransformationHandler.get(); + final JRuleTransformationHandler jRuleTransformationHandler = JRuleTransformationHandler.get(); jRuleTransformationHandler.setBundleContext(bundleContext); - JRuleThingHandler thingHandler = JRuleThingHandler.get(); + final JRuleThingHandler thingHandler = JRuleThingHandler.get(); thingHandler.setThingManager(thingManager); thingHandler.setThingRegistry(thingRegistry); + final JRuleItemHandler itemHandler = JRuleItemHandler.get(); + itemHandler.setItemRegistry(itemRegistry); + logDebug("JRuleHandler()"); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java new file mode 100644 index 00000000..fe9174dc --- /dev/null +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleItemHandler.java @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2010-2023 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.items.Item; +import org.openhab.core.items.ItemRegistry; +import org.openhab.core.library.items.NumberItem; +import org.openhab.core.library.items.StringItem; +import org.openhab.core.library.types.DecimalType; +import org.openhab.core.library.types.StringType; + +/** + * The {@link JRuleItemHandler} provides access to item Registry + * + * @author Joseph (Seaside) Hagberg - Initial contribution + */ +public class JRuleItemHandler { + + private static volatile JRuleItemHandler instance = null; + + private JRuleItemHandler() { + } + + private ItemRegistry itemRegistry; + + public void setItemRegistry(ItemRegistry itemRegistry) { + this.itemRegistry = itemRegistry; + } + + public static JRuleItemHandler get() { + if (instance == null) { + synchronized (JRuleItemHandler.class) { + if (instance == null) { + instance = new JRuleItemHandler(); + } + } + } + return instance; + } + + public Item addToRegistry(Item item) { + return itemRegistry.add(item); + } + + public boolean itemRegistryContainsItem(String itemName) { + return itemRegistry.get(itemName) != null; + } + + public Item addNumberItem(String name, int value) { + final NumberItem numberItem = new NumberItem(name); + numberItem.setState(new DecimalType(value)); + return itemRegistry.add(numberItem); + } + + public Item addStringItem(String name, String value) { + final StringItem stringItem = new StringItem(name); + stringItem.setState(new StringType(value)); + return itemRegistry.add(stringItem); + } +} diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleThingHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleThingHandler.java index 83cf4872..8096544f 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleThingHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleThingHandler.java @@ -12,6 +12,8 @@ */ package org.openhab.automation.jrule.internal.handler; +import java.util.Collection; + import org.openhab.automation.jrule.things.JRuleThingStatus; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingManager; @@ -61,7 +63,12 @@ public void enable(String thingUID) { setEnabled(thingUID, true); } + public Collection getThings() { + return thingRegistry.getAll(); + } + private void setEnabled(String thingUID, boolean enabled) { + thingRegistry.getAll().forEach(t -> t.getBridgeUID()); Thing thing = thingRegistry.get(new ThingUID(thingUID)); if (thing != null) { thingManager.setEnabled(new ThingUID(thingUID), enabled); diff --git a/src/main/java/org/openhab/automation/jrule/rules/JRule.java b/src/main/java/org/openhab/automation/jrule/rules/JRule.java index 00a3fa9e..6283dfbb 100644 --- a/src/main/java/org/openhab/automation/jrule/rules/JRule.java +++ b/src/main/java/org/openhab/automation/jrule/rules/JRule.java @@ -50,8 +50,14 @@ public class JRule { public static final ThreadLocal JRULE_EXECUTION_CONTEXT = new ThreadLocal<>(); public JRule() { - // Add rules default disabled, let openHAB enable rules if not disabled in GUI - JRuleEngine.get().add(this, false); + this(true); + } + + public JRule(boolean addToEngine) { + if (addToEngine) { + // Add rules default disabled, let openHAB enable rules if not disabled in GUI + JRuleEngine.get().add(this, false); + } } protected void say(String text) { From f06501dd21247ce45780ca2ff93a37e1ce565fca Mon Sep 17 00:00:00 2001 From: seaside1 Date: Sat, 7 Oct 2023 23:29:17 +0200 Subject: [PATCH 2/4] Fixed error logging of throwable --- .../automation/jrule/internal/JRuleLog.java | 11 +++++++ .../internal/compiler/JRuleCompiler.java | 29 ++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java b/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java index e099f24e..7c0fc095 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java +++ b/src/main/java/org/openhab/automation/jrule/internal/JRuleLog.java @@ -46,6 +46,17 @@ public static void warn(Logger logger, String logPrefix, String message, Object. logger.warn(PREFIX_WARN_LOG, logPrefix, logMessage.getMessage()); } + public static void error(Logger logger, String logPrefix, Throwable t, String message, Object... parameters) { + final FormattingTuple logMessage = MessageFormatter.arrayFormat(message, parameters); + final FormattingTuple finalLogmessage = MessageFormatter.format(PREFIX_ERROR_LOG, + new String[] { logPrefix, logMessage.getMessage() }); + error(logger, t, finalLogmessage.getMessage()); + } + + private static void error(Logger logger, Throwable t, String message) { + logger.error(message, t); + } + public static void error(Logger logger, String logPrefix, String message, Object... parameters) { final FormattingTuple logMessage = MessageFormatter.arrayFormat(message, parameters); logger.error(PREFIX_ERROR_LOG, logPrefix, logMessage.getMessage()); diff --git a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java index 4d3911a8..3770fbd3 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java @@ -110,12 +110,12 @@ public void loadClassesFromJar(ClassLoader classLoader, File sourceFolder, Strin loadClass(classLoader, relativePathToFullClassname(jarEntryName), createInstance); } } catch (IllegalArgumentException | SecurityException | IOException e) { - logError("Error loading classes from jarfile {} due to {}", jarItem.getAbsolutePath(), e); + logError(e, "Error loading classes from jarfile {}", jarItem.getAbsolutePath()); } // Best effort }); } catch (Exception e) { - logError("Error loading classes from jarfile: {}", e); + logError(e, "Error loading classes from jarfile: {}"); } } @@ -151,8 +151,7 @@ public void loadClass(ClassLoader classLoader, String className, boolean createI final Object obj = loadedClass.getDeclaredConstructor().newInstance(); logDebug("Created instance: {} obj: {}", className, obj); } catch (Exception x) { - logError("Could not create create instance using default constructor: {}", className); - logger.error("Failed to load class", x); + logError(x, "Could not create create instance using default constructor: {}", className); } } } @@ -172,7 +171,7 @@ public void loadClassesFromFolder(ClassLoader classLoader, File rootFolder, Stri .map(this::relativePathToFullClassname).filter(e -> e.startsWith(onlyInRootPackage)) .collect(Collectors.toList()); } catch (IOException e) { - logError("Error loading classes in {} due to {}", rootFolder.getAbsolutePath(), e); + logError(e, "Error loading classes in {} due to {}", rootFolder.getAbsolutePath()); } // classFiles is now in the form "packageRoot.subPackage.classname", filtered by prefix in onlyInRootPackage @@ -205,14 +204,14 @@ private boolean compileGeneratedSource(File sourceFolder) { .filter(f -> f.getFileName().toString().endsWith(JRuleConstants.JAVA_FILE_TYPE)).map(Path::toFile) .collect(Collectors.toList()); } catch (IOException e) { - logError("Error listing source files in folder: {}", sourceFolder.getAbsolutePath(), e); + logError(e, "Error listing source files in folder: {}", sourceFolder.getAbsolutePath()); } try (Stream paths = Files.walk(Paths.get(sourceFolder.toURI()))) { javaClassFiles = paths.filter(Files::isRegularFile) // is a file .filter(f -> f.getFileName().toString().endsWith(JRuleConstants.CLASS_FILE_TYPE)).map(Path::toFile) .collect(Collectors.toList()); } catch (IOException e) { - logError("Error listing class files in folder: {}", sourceFolder.getAbsolutePath(), e); + logError(e, "Error listing class files in folder: {}", sourceFolder.getAbsolutePath()); } Map classFiles = new HashMap<>(); @@ -272,7 +271,7 @@ public boolean compile(List javaSourceFiles, String classPath) { } fileManager.close(); } catch (Exception x) { - logError("Compiler threw error {}", x.toString()); + logError(x, "Compiler threw error {}"); } return false; @@ -304,7 +303,7 @@ private Optional getOpenHABJar(String jarPrefix) { .filter(path -> path.getFileName().toString().startsWith(jarPrefix)) .map(path -> path.toFile().getAbsolutePath()).findFirst(); } catch (IOException e) { - logError(e.getMessage()); + logError(e, "Failed to get CoreJar"); return Optional.empty(); } return coreJarPath; @@ -338,7 +337,7 @@ public boolean compileRules() { logWarn("Found no java rules to compile and use in folder {}", jRuleConfig.getRulesDirectory()); } } catch (IOException e) { - logError("Error listing java files in folder: {}", jRuleConfig.getRulesDirectory(), e); + logError(e, "Error listing java files in folder: {}", jRuleConfig.getRulesDirectory()); } return false; @@ -349,7 +348,7 @@ public List getExtLibsAsUrls() { final File[] extLibsFiles = getExtLibsAsFiles(); return Arrays.stream(extLibsFiles).map(this::getUrl).collect(Collectors.toList()); } catch (Exception x) { - logError("Failed to get extLib urls"); + logError(x, "Failed to get extLib urls"); return new ArrayList<>(); } } @@ -359,7 +358,7 @@ public List getJarRulesAsUrls() { final File[] jarRulesFiles = getJarRulesAsFiles(); return Arrays.stream(jarRulesFiles).map(this::getUrl).collect(Collectors.toList()); } catch (Exception x) { - logError("Failed to get jar-rules urls"); + logError(x, "Failed to get jar-rules urls"); return new ArrayList<>(); } } @@ -368,7 +367,7 @@ private URL getUrl(File f) { try { return f.toURI().toURL(); } catch (MalformedURLException e) { - logError("Failed to convert to URL: {}", f.getAbsolutePath(), e); + logError(e, "Failed to convert to URL: {}", f.getAbsolutePath()); } return null; } @@ -428,6 +427,10 @@ private void logError(String message, Object... parameters) { JRuleLog.error(logger, LOG_NAME_COMPILER, message, parameters); } + private void logError(Throwable t, String message, Object... parameters) { + JRuleLog.error(logger, LOG_NAME_COMPILER, t, message); + } + private void logWarn(String message, Object... parameters) { JRuleLog.warn(logger, LOG_NAME_COMPILER, message, parameters); } From 93f043e9816368fd7a4b724d3f00788e49ac00e7 Mon Sep 17 00:00:00 2001 From: seaside1 Date: Tue, 21 Nov 2023 20:43:06 +0100 Subject: [PATCH 3/4] Removed magic number --- .../automation/jrule/internal/compiler/JRuleCompiler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java index 3770fbd3..c00e8cae 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/compiler/JRuleCompiler.java @@ -250,7 +250,7 @@ public boolean compile(List javaSourceFiles, String classPath) { String cp = openhabCoreJar + System.getProperty(JAVA_CLASS_PATH_PROPERTY) + File.pathSeparator + openhabCoreThingJar + System.getProperty(JAVA_CLASS_PATH_PROPERTY) + File.pathSeparator + classPath; optionList.add(cp); - logDebug("1337Compiling classes using classpath: {}", cp); + logDebug("Compiling classes using classpath: {}", cp); javaSourceFiles.stream().filter(javaSourceFile -> javaSourceFile.exists() && javaSourceFile.canRead()) .forEach(javaSourceFile -> logDebug("Compiling java Source file: {}", javaSourceFile)); From ed67bea96f52d67b851fe7bae6fef0f35ecd6988 Mon Sep 17 00:00:00 2001 From: seaside1 Date: Wed, 22 Nov 2023 20:09:22 +0100 Subject: [PATCH 4/4] Added networkaddresresolver and other oh managers to be available from rules --- .../jrule/internal/JRuleFactory.java | 9 +++++-- .../jrule/internal/handler/JRuleHandler.java | 8 +++++-- .../handler/JRuleTransformationHandler.java | 6 ++++- .../internal/handler/JRuleVoiceHandler.java | 24 +++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java b/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java index 72336e5e..2811732d 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java +++ b/src/main/java/org/openhab/automation/jrule/internal/JRuleFactory.java @@ -22,9 +22,11 @@ import org.openhab.automation.jrule.internal.handler.JRuleHandler; import org.openhab.automation.jrule.internal.module.JRuleRuleProvider; import org.openhab.automation.jrule.items.JRuleItemRegistry; +import org.openhab.core.audio.AudioHTTPServer; import org.openhab.core.events.EventPublisher; import org.openhab.core.items.ItemRegistry; import org.openhab.core.items.MetadataRegistry; +import org.openhab.core.net.NetworkAddressService; import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.ThingManager; @@ -62,7 +64,8 @@ public class JRuleFactory { public JRuleFactory(Map properties, final @Reference JRuleEventSubscriber eventSubscriber, final @Reference ItemRegistry itemRegistry, final @Reference ThingRegistry thingRegistry, final @Reference ThingManager thingManager, final @Reference EventPublisher eventPublisher, - final @Reference VoiceManager voiceManager, final ComponentContext componentContext, + final @Reference VoiceManager voiceManager, final @Reference AudioHTTPServer audioHTTPServer, + final @Reference NetworkAddressService networkAddressService, final ComponentContext componentContext, final @Reference CronScheduler cronScheduler, final @Reference MetadataRegistry metadataRegistry, final @Reference JRuleRuleProvider ruleProvider, @Reference final PersistenceServiceRegistry persistenceServiceRegistry) { @@ -74,10 +77,12 @@ public JRuleFactory(Map properties, final @Reference JRuleEventS jRuleEngine.setItemRegistry(itemRegistry); jRuleEngine.setCronScheduler(cronScheduler); jRuleEngine.setRuleProvider(ruleProvider); + jRuleEngine.initialize(); JRuleItemRegistry.setMetadataRegistry(metadataRegistry); jRuleHandler = new JRuleHandler(config, itemRegistry, thingRegistry, thingManager, eventPublisher, - eventSubscriber, voiceManager, cronScheduler, componentContext.getBundleContext(), metadataRegistry); + eventSubscriber, voiceManager, audioHTTPServer, networkAddressService, cronScheduler, + componentContext.getBundleContext(), metadataRegistry); delayedInit.call(this::init); } diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java index f5b22c3e..03db92fa 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleHandler.java @@ -50,6 +50,7 @@ import org.openhab.automation.jrule.items.JRuleItemRegistry; import org.openhab.automation.jrule.things.JRuleThingClassGenerator; import org.openhab.automation.jrule.things.JRuleThingRegistry; +import org.openhab.core.audio.AudioHTTPServer; import org.openhab.core.events.Event; import org.openhab.core.events.EventPublisher; import org.openhab.core.items.Item; @@ -59,6 +60,7 @@ import org.openhab.core.items.events.ItemAddedEvent; import org.openhab.core.items.events.ItemRemovedEvent; import org.openhab.core.items.events.ItemUpdatedEvent; +import org.openhab.core.net.NetworkAddressService; import org.openhab.core.scheduler.CronScheduler; import org.openhab.core.thing.Thing; import org.openhab.core.thing.ThingManager; @@ -119,8 +121,8 @@ public class JRuleHandler implements PropertyChangeListener { public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ThingRegistry thingRegistry, ThingManager thingManager, EventPublisher eventPublisher, JRuleEventSubscriber eventSubscriber, - VoiceManager voiceManager, CronScheduler cronScheduler, BundleContext bundleContext, - MetadataRegistry metadataRegistry) { + VoiceManager voiceManager, AudioHTTPServer audioHTTPServer, NetworkAddressService networkAddressService, + CronScheduler cronScheduler, BundleContext bundleContext, MetadataRegistry metadataRegistry) { this.itemRegistry = itemRegistry; this.thingRegistry = thingRegistry; this.metadataRegistry = metadataRegistry; @@ -142,6 +144,8 @@ public JRuleHandler(JRuleConfig config, ItemRegistry itemRegistry, ThingRegistry eventSubscriber.addPropertyChangeListener(this); final JRuleVoiceHandler jRuleVoiceHandler = JRuleVoiceHandler.get(); jRuleVoiceHandler.setVoiceManager(voiceManager); + jRuleVoiceHandler.setAudioHTTPServer(audioHTTPServer); + jRuleVoiceHandler.setNetworkAddressService(networkAddressService); final JRuleTransformationHandler jRuleTransformationHandler = JRuleTransformationHandler.get(); jRuleTransformationHandler.setBundleContext(bundleContext); diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleTransformationHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleTransformationHandler.java index 116092aa..e9478831 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleTransformationHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleTransformationHandler.java @@ -50,9 +50,13 @@ public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } + public BundleContext getBundleContext() { + return bundleContext; + } + /** * Transforms the given state with the transformation pattern. - * + * * @param stateDescPattern The transformation pattern * @param state State which should be converted * @return The transformation result diff --git a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleVoiceHandler.java b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleVoiceHandler.java index 7b219d7a..a2c071ca 100644 --- a/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleVoiceHandler.java +++ b/src/main/java/org/openhab/automation/jrule/internal/handler/JRuleVoiceHandler.java @@ -12,7 +12,9 @@ */ package org.openhab.automation.jrule.internal.handler; +import org.openhab.core.audio.AudioHTTPServer; import org.openhab.core.library.types.PercentType; +import org.openhab.core.net.NetworkAddressService; import org.openhab.core.voice.VoiceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -28,6 +30,12 @@ public class JRuleVoiceHandler { private static volatile JRuleVoiceHandler instance; private VoiceManager voiceManager; + private AudioHTTPServer audioHTTPServer; + private NetworkAddressService networkAddressService; + + public VoiceManager getVoiceManager() { + return voiceManager; + } private final Logger logger = LoggerFactory.getLogger(JRuleVoiceHandler.class); @@ -66,4 +74,20 @@ public void say(String text, String voiceId, String sinkId, int volumePercent) { final PercentType volume = new PercentType(volumePercent); voiceManager.say(text, voiceId, sinkId, volume); } + + public AudioHTTPServer getAudioHTTPServer() { + return audioHTTPServer; + } + + public void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) { + this.audioHTTPServer = audioHTTPServer; + } + + public NetworkAddressService getNetworkAddressService() { + return networkAddressService; + } + + public void setNetworkAddressService(NetworkAddressService networkAddressService) { + this.networkAddressService = networkAddressService; + } }