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
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static JRuleExecutionContext getCurrentContext() {
return JRule.JRULE_EXECUTION_CONTEXT.get();
}

private synchronized List<JRuleTimer> getTimers(String timerName) {
public synchronized List<JRuleTimer> getTimers(String timerName) {
List<JRuleTimer> list = timers.stream().filter(timer -> timer.name.equals(timerName))
.collect(Collectors.toList());
logger.trace("timers for name '{}': {}", timerName, list.size());
Expand Down Expand Up @@ -281,6 +281,11 @@ public JRuleTimerHandler.JRuleTimer rescheduleTimer(Duration delay) {
return JRuleTimerHandler.this.createOrReplaceTimer(this.name, delay, this.function, context);
}

public JRuleTimerHandler.JRuleTimer invoke() {
function.accept(this);
return this;
}

public boolean isDone() {
return futures.stream().allMatch(CompletableFuture::isDone);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.items;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The {@link JRuleItemDefinition} definition. Used to annotate the generated Items classes.
*
* @author Robert Delbrück - Initial contribution
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface JRuleItemDefinition {
String name();

String type();

String[] groupNames();
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private Map<String, Object> createItemModel(Item item, Map<String, JRuleItemMeta
itemModel.put("metadata", metadata.entrySet().stream().map(e -> e.getKey() + ": " + e.getValue())
.collect(Collectors.joining(", ")));
itemModel.put("tags", StringUtils.join(item.getTags().stream().sorted().toList(), ", "));
itemModel.put("groupNames", item.getGroupNames().stream().sorted().toList());
return itemModel;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/templates/items/ItemNames.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package ${packageName};

import org.openhab.automation.jrule.items.JRuleItemDefinition;

/**
* Automatically Generated Enum for ItemNames - DO NOT EDIT!
*
Expand All @@ -20,6 +22,7 @@ public class JRuleItemNames {
* <br/>
* Metadata: ${itemName.metadata}
*/
@JRuleItemDefinition(name="${itemName.name}", type="${itemName.type}", groupNames={<#list itemName.groupNames as groupName>"${groupName}"<#sep>, </#sep></#list>})
public static final String ${itemName.name} = "${itemName.name}";
</#list>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -35,16 +34,11 @@
import org.mockito.Mockito;
import org.openhab.automation.jrule.internal.JRuleConfig;
import org.openhab.automation.jrule.internal.compiler.JRuleCompiler;
import org.openhab.automation.jrule.internal.handler.JRuleEventHandler;
import org.openhab.automation.jrule.items.JRuleItemNameClassGenerator;
import org.openhab.automation.jrule.items.JRuleItemRegistry;
import org.openhab.automation.jrule.test_utils.JRuleItemTestUtils;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.Metadata;
import org.openhab.core.items.MetadataKey;
import org.openhab.core.items.MetadataRegistry;
import org.openhab.core.items.*;
import org.openhab.core.library.items.CallItem;
import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.ContactItem;
Expand Down Expand Up @@ -123,19 +117,21 @@ private Item decorate(GenericItem item) {
}

@Test
public void testGenerateItemsFile()
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException,
MalformedURLException, ClassNotFoundException, NoSuchFieldException, ItemNotFoundException {
public void testGenerateItemsFile() throws Exception {
List<Item> items = new ArrayList<>();

items.add(createItem(StringItem.class, new StringType("abc")));
GenericItem itemString = createItem(StringItem.class, new StringType("abc"));
itemString.addGroupNames("StringItemGroup");
items.add(itemString);
items.add(createItem(ColorItem.class, new HSBType(new DecimalType(1), new PercentType(2), new PercentType(3))));
items.add(createItem(ContactItem.class, OpenClosedType.OPEN));
items.add(createItem(DateTimeItem.class, new DateTimeType(ZonedDateTime.now())));
items.add(createItem(DimmerItem.class, new PercentType(50)));
items.add(createItem(PlayerItem.class, PlayPauseType.PAUSE));
items.add(createItem(SwitchItem.class, OnOffType.OFF));
items.add(createItem(NumberItem.class, new DecimalType(340)));
GenericItem itemNumber = createItem(NumberItem.class, new DecimalType(340));
itemNumber.addGroupNames("NumberItemGroup", "DimmerItemGroup");
items.add(itemNumber);
items.add(createItem(RollershutterItem.class, new PercentType(22)));
items.add(createItem(LocationItem.class, new PointType(new DecimalType(22.22), new DecimalType(54.12))));
// items.add(createItem(CallItem.class, new StringType("+4930123456")));
Expand Down Expand Up @@ -164,39 +160,23 @@ public void testGenerateItemsFile()
boolean success = sourceFileGenerator.generateItemNamesSource(items, metadataRegistry);
assertTrue(success, "Failed to generate source file for items");

compiler.compile(List.of(new File(targetFolder, "JRuleItemNames.java")), "target/classes:target/gen");
Assertions.assertTrue(
compiler.compile(List.of(new File(targetFolder, "JRuleItemNames.java")), "target/classes:target/gen"));

// ItemRegistry itemRegistry = Mockito.mock(ItemRegistry.class);
// Mockito.when(itemRegistry.getItem(Mockito.anyString())).thenAnswer(invocationOnMock -> {
// Object itemName = invocationOnMock.getArgument(0);
// return items.stream().filter(item -> item.getName().equals(itemName)).findFirst().orElseThrow();
// });
// JRuleEventHandler.get().setItemRegistry(itemRegistry);
//
// File compiledClass = new File(targetFolder, "JRuleItems.class");
// assertTrue(compiledClass.exists());
//
// URLClassLoader classLoader = new URLClassLoader(new URL[] { new File("target/gen").toURI().toURL() },
// JRuleActionClassGeneratorTest.class.getClassLoader());
// final String className = "org.openhab.automation.jrule.generated.items.JRuleItems";
// Class<?> aClass = classLoader.loadClass(className);
// Object jRuleItems = aClass.getConstructor().newInstance();
//
// for (Item item : items) {
// testAllMethodsOnGeneratedItem(aClass, jRuleItems, item.getName());
// }
}

private static void testAllMethodsOnGeneratedItem(Class<?> aClass, Object jRuleItems, String itemName)
throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Field itemField = aClass.getDeclaredField(itemName);
Object item = itemField.get(jRuleItems);
ItemRegistry itemRegistry = Mockito.mock(ItemRegistry.class);
Mockito.when(itemRegistry.getItem(Mockito.anyString())).thenAnswer(invocationOnMock -> {
Object itemName = invocationOnMock.getArgument(0);
return items.stream().filter(item -> item.getName().equals(itemName)).findFirst().orElseThrow();
});
JRuleEventHandler.get().setItemRegistry(itemRegistry);

Method getName = item.getClass().getMethod("getName");
Assertions.assertEquals(itemName, getName.invoke(item));
File compiledClass = new File(targetFolder, "JRuleItemNames.class");
assertTrue(compiledClass.exists());

Method getState = item.getClass().getMethod("getState");
Assertions.assertNotNull(getState.invoke(item));
URLClassLoader classLoader = new URLClassLoader(new URL[] { new File("target/gen").toURI().toURL() },
JRuleActionClassGeneratorTest.class.getClassLoader());
final String className = "org.openhab.automation.jrule.generated.items.JRuleItemNames";
classLoader.loadClass(className);
}

private GroupItem createGroupItem(Class<? extends GenericItem> clazz, State initialState)
Expand Down