Skip to content

Commit

Permalink
Solve issues with handling of group items and functions. #27
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Dywicki <[email protected]>
  • Loading branch information
splatch committed Feb 26, 2021
1 parent 9c1f51c commit 9445586
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.internal.items.function.And;
import org.openhab.core.internal.items.function.Avg;
import org.openhab.core.internal.items.function.Count;
import org.openhab.core.internal.items.function.Earliest;
import org.openhab.core.internal.items.function.Latest;
import org.openhab.core.internal.items.function.Max;
import org.openhab.core.internal.items.function.Min;
import org.openhab.core.internal.items.function.NAnd;
import org.openhab.core.internal.items.function.NOr;
import org.openhab.core.internal.items.function.Or;
import org.openhab.core.internal.items.function.Sum;
import org.openhab.core.internal.items.function.dimensional.DimensionalAvg;
import org.openhab.core.internal.items.function.dimensional.DimensionalMax;
import org.openhab.core.internal.items.function.dimensional.DimensionalMin;
import org.openhab.core.internal.items.function.dimensional.DimensionalSum;
import org.openhab.core.internal.items.function.dimensional.Avg;
import org.openhab.core.internal.items.function.dimensional.Max;
import org.openhab.core.internal.items.function.dimensional.Min;
import org.openhab.core.internal.items.function.dimensional.Sum;
import org.openhab.core.items.GroupFunction;
import org.openhab.core.items.Item;
import org.openhab.core.items.dto.GroupFunctionDTO;
Expand Down Expand Up @@ -105,13 +101,13 @@ private GroupFunction createDimensionGroupFunction(GroupFunctionDTO function, @N
final String functionName = function.name;
switch (functionName.toUpperCase()) {
case "AVG":
return new DimensionalAvg(dimension);
return new Avg(dimension);
case "SUM":
return new DimensionalSum(dimension);
return new Sum(dimension);
case "MIN":
return new DimensionalMin(dimension);
return new Min(dimension);
case "MAX":
return new DimensionalMax(dimension);
return new Max(dimension);
default:
return createDefaultGroupFunction(function, baseItem);
}
Expand Down Expand Up @@ -163,13 +159,13 @@ private GroupFunction createDefaultGroupFunction(GroupFunctionDTO function, @Nul
}
break;
case "AVG":
return new Avg();
return new org.openhab.core.internal.items.function.Avg();
case "SUM":
return new Sum();
return new org.openhab.core.internal.items.function.Sum();
case "MIN":
return new Min();
return new org.openhab.core.internal.items.function.Min();
case "MAX":
return new Max();
return new org.openhab.core.internal.items.function.Max();
case "LATEST":
return new Latest();
case "EARLIEST":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.CopyOnWriteArraySet;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.GroupFunction;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemBuilder;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class ItemBuilderFactoryImpl implements ItemBuilderFactory {

@Activate
public ItemBuilderFactoryImpl(
final @Reference(target = "(component.name=org.openhab.core.library.CoreItemFactory)") ItemFactory coreItemFactory) {
final @Reference(target = "(component.name=org.openhab.core.library.CoreItemFactory)") ItemFactory coreItemFactory) {
itemFactories.add(coreItemFactory);
}

Expand All @@ -59,8 +60,8 @@ public ItemBuilder newItemBuilder(String itemType, String itemName) {
}

@Override
public GroupFunction newFunctionBuilder(Item baseItem, GroupFunctionDTO function) {
throw new AbstractMethodError("Not implemented yet");
public GroupFunction newFunctionBuilder(@Nullable Item baseItem, GroupFunctionDTO function) {
return new GroupFunctionHelper().createGroupFunction(function, baseItem);
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private GroupFunction getGroupFunction(PersistedItem persistedItem, @Nullable It
if (persistedItem.functionParams != null) {
functionDTO.params = persistedItem.functionParams.toArray(new String[persistedItem.functionParams.size()]);
}
return ItemDTOMapper.mapFunction(baseItem, functionDTO);
return ItemDTOMapper.mapFunction(baseItem, functionDTO, itemBuilderFactory);
}

private void configureItem(PersistedItem persistedItem, GenericItem item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class DimensionalAvg extends DimensionalGroupFunction {
public class Avg extends DimensionalGroupFunction {

public DimensionalAvg(Class<? extends Quantity<?>> dimension) {
public Avg(Class<? extends Quantity<?>> dimension) {
super(dimension);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class DimensionalMax extends DimensionalGroupFunction {
public class Max extends DimensionalGroupFunction {

public DimensionalMax(Class<? extends Quantity<?>> dimension) {
public Max(Class<? extends Quantity<?>> dimension) {
super(dimension);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class DimensionalMin extends DimensionalGroupFunction {
public class Min extends DimensionalGroupFunction {

public DimensionalMin(Class<? extends Quantity<?>> dimension) {
public Min(Class<? extends Quantity<?>> dimension) {
super(dimension);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class DimensionalSum extends DimensionalGroupFunction {
public class Sum extends DimensionalGroupFunction {

public DimensionalSum(Class<? extends Quantity<?>> dimension) {
public Sum(Class<? extends Quantity<?>> dimension) {
super(dimension);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
import org.junit.Test;
import org.mockito.Mock;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.internal.items.function.dimensional.DimensionalAvg;
import org.openhab.core.internal.items.function.dimensional.DimensionalMax;
import org.openhab.core.internal.items.function.dimensional.DimensionalMin;
import org.openhab.core.internal.items.function.dimensional.DimensionalSum;
import org.openhab.core.internal.items.function.dimensional.Avg;
import org.openhab.core.internal.items.function.dimensional.Max;
import org.openhab.core.internal.items.function.dimensional.Min;
import org.openhab.core.internal.items.function.dimensional.Sum;
import org.openhab.core.items.GroupFunction;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.QuantityTypeArithmeticGroupFunction;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.types.QuantityType;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void testSumFunctionQuantityType() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("122.41 °C")));

function = new DimensionalSum(Temperature.class);
function = new Sum(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("234.95 °C"), state);
Expand All @@ -86,7 +85,7 @@ public void testSumFunctionQuantityTypeDifferentUnits() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("395.56 K")));

function = new DimensionalSum(Temperature.class);
function = new Sum(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("234.95 °C"), state);
Expand All @@ -99,7 +98,7 @@ public void testSumFunctionQuantityTypeIncompatibleUnits() {
items.add(createNumberItem("TestItem2", Temperature.class, UnDefType.NULL));
items.add(createNumberItem("TestItem3", Pressure.class, new QuantityType<>("192.2 hPa")));

function = new DimensionalSum(Temperature.class);
function = new Sum(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("23.54 °C"), state);
Expand All @@ -113,7 +112,7 @@ public void testAvgFunctionQuantityType() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("300 °C")));

function = new DimensionalAvg(Temperature.class);
function = new Avg(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("200 °C"), state);
Expand All @@ -127,7 +126,7 @@ public void testAvgFunctionQuantityTypeDifferentUnits() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("294.15 K")));

function = new DimensionalAvg(Temperature.class);
function = new Avg(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("55.33 °C"), state);
Expand All @@ -139,7 +138,7 @@ public void testAvgFunctionQuantityTypeIncompatibleUnits() {
items.add(createNumberItem("TestItem2", Temperature.class, UnDefType.NULL));
items.add(createNumberItem("TestItem3", Pressure.class, new QuantityType<>("192.2 hPa")));

function = new DimensionalAvg(Temperature.class);
function = new Avg(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("23.54 °C"), state);
Expand All @@ -153,7 +152,7 @@ public void testMaxFunctionQuantityType() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("300 °C")));

function = new DimensionalMax(Temperature.class);
function = new Max(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("300 °C"), state);
Expand All @@ -167,7 +166,7 @@ public void testMaxFunctionQuantityTypeDifferentUnits() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("294.15 K")));

function = new DimensionalMax(Temperature.class);
function = new Max(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("100 °C"), state);
Expand All @@ -179,7 +178,7 @@ public void testMaxFunctionQuantityTypeIncompatibleUnits() {
items.add(createNumberItem("TestItem2", Temperature.class, UnDefType.NULL));
items.add(createNumberItem("TestItem3", Pressure.class, new QuantityType<>("192.2 hPa")));

function = new DimensionalMax(Temperature.class);
function = new Max(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("23.54 °C"), state);
Expand All @@ -193,7 +192,7 @@ public void testMinFunctionQuantityType() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("300 °C")));

function = new DimensionalMin(Temperature.class);
function = new Min(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("100 °C"), state);
Expand All @@ -208,7 +207,7 @@ public void testMaxFunctionQuantityTypeOnDimensionless() {
items.add(createNumberItem("TestItem5", Dimensionless.class, new QuantityType<>("0 %")));
items.add(createNumberItem("TestItem6", Dimensionless.class, new QuantityType<>("0 %")));

function = new DimensionalMax(Dimensionless.class);
function = new Max(Dimensionless.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("48 %"), state);
Expand All @@ -222,7 +221,7 @@ public void testMinFunctionQuantityTypeDifferentUnits() {
items.add(createNumberItem("TestItem4", Temperature.class, UnDefType.UNDEF));
items.add(createNumberItem("TestItem5", Temperature.class, new QuantityType<>("294.15 K")));

function = new DimensionalMin(Temperature.class);
function = new Min(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("294.15 K"), state);
Expand All @@ -234,7 +233,7 @@ public void testMinFunctionQuantityTypeIncompatibleUnits() {
items.add(createNumberItem("TestItem2", Temperature.class, UnDefType.NULL));
items.add(createNumberItem("TestItem3", Pressure.class, new QuantityType<>("192.2 hPa")));

function = new DimensionalMin(Temperature.class);
function = new Min(Temperature.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("23.54 °C"), state);
Expand All @@ -245,7 +244,7 @@ public void testSumFunctionQuantityTypeWithGroups() {
items.add(createNumberItem("TestItem1", Power.class, new QuantityType<>("5 W")));
items.add(createGroupItem("TestGroup1", Power.class, new QuantityType<>("5 W")));

function = new DimensionalSum(Power.class);
function = new Sum(Power.class);
State state = function.calculate(items);

assertEquals(new QuantityType<>("10 W"), state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.core.items;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.items.dto.GroupFunctionDTO;

/**
Expand Down Expand Up @@ -41,5 +42,5 @@ public interface ItemBuilderFactory {
ItemBuilder newItemBuilder(String itemType, String itemName);

// FIXME verify if that's valid approach
GroupFunction newFunctionBuilder(Item baseItem, GroupFunctionDTO function);
GroupFunction newFunctionBuilder(@Nullable Item baseItem, GroupFunctionDTO function);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public class ItemDTOMapper {
return null;
}

public static GroupFunction mapFunction(@Nullable Item baseItem, GroupFunctionDTO function) {
throw new AbstractMethodError("This method has been moved!");
public static GroupFunction mapFunction(@Nullable Item baseItem, GroupFunctionDTO function, ItemBuilderFactory itemBuilderFactory) {
return itemBuilderFactory.newFunctionBuilder(baseItem, function);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupFunction;
import org.openhab.core.items.ItemBuilderFactory;
import org.openhab.core.items.TestNumberItem;
import org.openhab.core.library.types.StringType;

/**
* @author Stefan Triller - Initial contribution
*/
@RunWith(MockitoJUnitRunner.class)
public class ItemDTOMapperTest {

@Mock
private ItemBuilderFactory factory;

@Test
@Ignore
public void testMapFunctionWithNumberItemAndCountFunction() {
// testing Group:Number:Count(".*hello.*")
GenericItem item1 = new TestNumberItem("item1");
Expand All @@ -41,7 +48,7 @@ public void testMapFunctionWithNumberItemAndCountFunction() {
gFuncDTO.params = new String[] { ".*hello.*" };

// FIXME This gonna fail
GroupFunction gFunc = ItemDTOMapper.mapFunction(item1, gFuncDTO);
GroupFunction gFunc = ItemDTOMapper.mapFunction(item1, gFuncDTO, factory);

assertThat(gFunc, instanceOf(GroupFunction.class));
assertThat(gFunc.getParameters().length, is(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemFactory;
import org.openhab.core.items.ItemBuilderFactory;
import org.openhab.core.items.ItemProvider;
import org.openhab.core.items.ItemUtil;
import org.openhab.core.items.dto.GroupFunctionDTO;
Expand Down Expand Up @@ -85,6 +86,7 @@ public class GenericItemProvider extends AbstractProvider<Item>
private final GenericMetadataProvider genericMetaDataProvider;

private final StateDescriptionFragmentBuilderFactory stateDescriptionFragmentBuilderFactory;
private final ItemBuilderFactory itemBuilderFactory;

private final Map<String, Collection<Item>> itemsMap = new ConcurrentHashMap<>();

Expand All @@ -98,10 +100,12 @@ public class GenericItemProvider extends AbstractProvider<Item>
public GenericItemProvider(final @Reference ModelRepository modelRepository,
final @Reference GenericMetadataProvider genericMetadataProvider,
final @Reference StateDescriptionFragmentBuilderFactory stateDescriptionFragmentBuilderFactory,
final @Reference ItemBuilderFactory itemBuilderFactory,
Map<String, Object> properties) {
this.modelRepository = modelRepository;
this.genericMetaDataProvider = genericMetadataProvider;
this.stateDescriptionFragmentBuilderFactory = stateDescriptionFragmentBuilderFactory;
this.itemBuilderFactory = itemBuilderFactory;

Object serviceRanking = properties.get(Constants.SERVICE_RANKING);
if (serviceRanking instanceof Integer) {
Expand Down Expand Up @@ -295,7 +299,7 @@ private GroupItem applyGroupFunction(Item baseItem, ModelGroupItem modelGroupIte
dto.name = function.getName();
dto.params = modelGroupItem.getArgs().toArray(new String[modelGroupItem.getArgs().size()]);

GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto);
GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto, itemBuilderFactory);

return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction);
}
Expand Down

0 comments on commit 9445586

Please sign in to comment.