use of org.eclipse.smarthome.core.items.GroupFunction in project smarthome by eclipse.
the class ItemDTOMapper method map.
/**
* Maps item DTO into item object.
*
* @param itemDTO the DTO
* @param itemFactories the item factories in order to create the items
* @return the item object
*/
@Nullable
public static ActiveItem map(ItemDTO itemDTO, Set<ItemFactory> itemFactories) {
if (itemDTO == null) {
throw new IllegalArgumentException("The argument 'itemDTO' must no be null.");
}
if (itemFactories == null) {
throw new IllegalArgumentException("The argument 'itemFactories' must no be null.");
}
GenericItem newItem = null;
if (itemDTO.type != null) {
if (itemDTO instanceof GroupItemDTO && itemDTO.type.equals(GroupItem.TYPE)) {
GroupItemDTO groupItemDTO = (GroupItemDTO) itemDTO;
GenericItem baseItem = null;
if (!StringUtils.isEmpty(groupItemDTO.groupType)) {
baseItem = createItem(groupItemDTO.groupType, itemDTO.name, itemFactories);
}
GroupFunction function = new GroupFunction.Equality();
if (groupItemDTO.function != null) {
function = mapFunction(baseItem, groupItemDTO.function);
}
newItem = new GroupItem(itemDTO.name, baseItem, function);
} else {
String itemType = itemDTO.type;
newItem = createItem(itemType, itemDTO.name, itemFactories);
}
if (newItem != null) {
if (itemDTO.label != null) {
newItem.setLabel(itemDTO.label);
}
if (itemDTO.category != null) {
newItem.setCategory(itemDTO.category);
}
if (itemDTO.groupNames != null) {
newItem.addGroupNames(itemDTO.groupNames);
}
if (itemDTO.tags != null) {
newItem.addTags(itemDTO.tags);
}
}
}
return newItem;
}
use of org.eclipse.smarthome.core.items.GroupFunction in project smarthome by eclipse.
the class GenericItemProvider method applyGroupFunction.
private GroupItem applyGroupFunction(GenericItem baseItem, ModelGroupItem modelGroupItem, ModelGroupFunction function) {
GroupFunctionDTO dto = new GroupFunctionDTO();
dto.name = function.getName();
dto.params = modelGroupItem.getArgs().toArray(new String[modelGroupItem.getArgs().size()]);
GroupFunction groupFunction = ItemDTOMapper.mapFunction(baseItem, dto);
return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction);
}
use of org.eclipse.smarthome.core.items.GroupFunction in project smarthome by eclipse.
the class GenericItemProvider method hasGroupItemChanged.
private boolean hasGroupItemChanged(Item item1, Item item2) {
GroupItem gItem1 = null;
GroupItem gItem2 = null;
if (item1 instanceof GroupItem) {
gItem1 = (GroupItem) item1;
}
if (item2 instanceof GroupItem) {
gItem2 = (GroupItem) item2;
}
if (gItem1 == null && gItem2 == null) {
return false;
}
if ((gItem1 != null && gItem2 == null) || (gItem1 == null && gItem2 != null)) {
return true;
}
boolean sameBaseItemClass = Objects.equals(gItem1.getBaseItem(), gItem2.getBaseItem());
boolean sameFunction = false;
GroupFunction gf1 = gItem1.getFunction();
GroupFunction gf2 = gItem2.getFunction();
if (gf1 != null && gf2 != null) {
if (Objects.equals(gf1.getClass(), gf2.getClass())) {
sameFunction = Arrays.equals(gf1.getParameters(), gf2.getParameters());
}
} else if (gf1 == null && gf2 == null) {
sameFunction = true;
}
return !(sameBaseItemClass && sameFunction);
}
use of org.eclipse.smarthome.core.items.GroupFunction in project smarthome by eclipse.
the class ItemDTOMapperTest method testMapFunctionWithNumberItemAndCountFunction.
@Test
public void testMapFunctionWithNumberItemAndCountFunction() {
// testing Group:Number:Count(".*hello.*")
NumberItem item1 = new NumberItem("item1");
GroupFunctionDTO gFuncDTO = new GroupFunctionDTO();
gFuncDTO.name = "COUNT";
gFuncDTO.params = new String[] { ".*hello.*" };
GroupFunction gFunc = ItemDTOMapper.mapFunction(item1, gFuncDTO);
assertThat(gFunc, instanceOf(ArithmeticGroupFunction.Count.class));
assertThat(gFunc.getParameters().length, is(1));
assertThat(gFunc.getParameters()[0], instanceOf(StringType.class));
}
Aggregations