use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class GenericItemProvider method createItemFromModelItem.
private Item createItemFromModelItem(ModelItem modelItem) {
GenericItem item = null;
if (modelItem instanceof ModelGroupItem) {
ModelGroupItem modelGroupItem = (ModelGroupItem) modelItem;
GenericItem baseItem;
try {
baseItem = createItemOfType(modelGroupItem.getType(), modelGroupItem.getName());
} catch (IllegalArgumentException e) {
logger.debug("Error creating base item for group item '{}', item will be ignored: {}", modelGroupItem.getName(), e.getMessage());
return null;
}
if (baseItem != null) {
// if the user did not specify a function the first value of the enum in xtext (EQUAL) will be used
ModelGroupFunction function = modelGroupItem.getFunction();
item = applyGroupFunction(baseItem, modelGroupItem, function);
} else {
item = new GroupItem(modelGroupItem.getName());
}
} else {
ModelNormalItem normalItem = (ModelNormalItem) modelItem;
try {
item = createItemOfType(normalItem.getType(), normalItem.getName());
} catch (IllegalArgumentException e) {
logger.debug("Error creating item '{}', item will be ignored: {}", normalItem.getName(), e.getMessage());
return null;
}
}
if (item != null) {
String label = modelItem.getLabel();
String format = StringUtils.substringBetween(label, "[", "]");
if (format != null) {
label = StringUtils.substringBefore(label, "[").trim();
stateDescriptions.put(modelItem.getName(), new StateDescription(null, null, null, format, false, null));
}
item.setLabel(label);
item.setCategory(modelItem.getIcon());
assignTags(modelItem, item);
return item;
} else {
return null;
}
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class CoreItemFactoryTest method shouldCreateItems.
@Test
public void shouldCreateItems() {
for (String itemTypeName : itemTypeNames) {
GenericItem item = coreItemFactory.createItem(itemTypeName, itemTypeName.toLowerCase());
assertThat(item.getType(), is(itemTypeName));
assertThat(item.getName(), is(itemTypeName.toLowerCase()));
}
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class CoreItemFactoryTest method shouldReturnNullForUnsupportedItemTypeName.
@Test
public void shouldReturnNullForUnsupportedItemTypeName() {
GenericItem item = coreItemFactory.createItem("NoValidItemTypeName", "IWantMyItem");
assertThat(item, is(nullValue()));
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class AutoUpdateBinding method postUpdate.
private void postUpdate(String itemName, State newState) {
if (itemRegistry != null) {
try {
GenericItem item = (GenericItem) itemRegistry.getItem(itemName);
boolean isAccepted = false;
if (item.getAcceptedDataTypes().contains(newState.getClass())) {
isAccepted = true;
} else {
// Look for class hierarchy
for (Class<? extends State> state : item.getAcceptedDataTypes()) {
try {
if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newState.getClass())) {
isAccepted = true;
break;
}
} catch (InstantiationException e) {
// Should never happen
logger.warn("InstantiationException on {}", e.getMessage(), e);
} catch (IllegalAccessException e) {
// Should never happen
logger.warn("IllegalAccessException on {}", e.getMessage(), e);
}
}
}
if (isAccepted) {
eventPublisher.post(ItemEventFactory.createStateEvent(itemName, newState, "org.eclipse.smarthome.core.autoupdate"));
} else {
logger.debug("Received update of a not accepted type ({}) for item {}", newState.getClass().getSimpleName(), itemName);
}
} catch (ItemNotFoundException e) {
logger.debug("Received update for non-existing item: {}", e.getMessage());
}
}
}
use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.
the class RuleEngineImpl method internalItemAdded.
private void internalItemAdded(Item item) {
if (item instanceof GenericItem) {
GenericItem genericItem = (GenericItem) item;
genericItem.addStateChangeListener(this);
}
}
Aggregations