Search in sources :

Example 1 with GenericItem

use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.

the class PersistenceManagerImpl method removed.

@Override
public void removed(Item item) {
    if (item instanceof GenericItem) {
        GenericItem genericItem = (GenericItem) item;
        genericItem.removeStateChangeListener(this);
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem)

Example 2 with GenericItem

use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.

the class PersistenceManagerImpl method initialize.

/**
 * Handles the "restoreOnStartup" strategy for the item.
 * If the item state is still undefined when entering this method, all persistence configurations are checked,
 * if they have the "restoreOnStartup" strategy configured for the item. If so, the item state will be set
 * to its last persisted value.
 *
 * @param item the item to restore the state for
 */
private void initialize(Item item) {
    // get the last persisted state from the persistence service if no state is yet set
    if (item.getState().equals(UnDefType.NULL) && item instanceof GenericItem) {
        for (Entry<String, PersistenceServiceConfiguration> entry : persistenceServiceConfigs.entrySet()) {
            final String serviceName = entry.getKey();
            final PersistenceServiceConfiguration config = entry.getValue();
            for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                if (hasStrategy(serviceName, itemConfig, SimpleStrategy.Globals.RESTORE)) {
                    if (appliesToItem(itemConfig, item)) {
                        PersistenceService service = persistenceServices.get(serviceName);
                        if (service instanceof QueryablePersistenceService) {
                            QueryablePersistenceService queryService = (QueryablePersistenceService) service;
                            FilterCriteria filter = new FilterCriteria().setItemName(item.getName()).setPageSize(1);
                            Iterable<HistoricItem> result = queryService.query(filter);
                            Iterator<HistoricItem> it = result.iterator();
                            if (it.hasNext()) {
                                HistoricItem historicItem = it.next();
                                GenericItem genericItem = (GenericItem) item;
                                genericItem.removeStateChangeListener(this);
                                genericItem.setState(historicItem.getState());
                                genericItem.addStateChangeListener(this);
                                logger.debug("Restored item state from '{}' for item '{}' -> '{}'", new Object[] { DateFormat.getDateTimeInstance().format(historicItem.getTimestamp()), item.getName(), historicItem.getState().toString() });
                                return;
                            }
                        } else if (service != null) {
                            logger.warn("Failed to restore item states as persistence service '{}' can not be queried.", serviceName);
                        }
                    }
                }
            }
        }
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) GenericItem(org.eclipse.smarthome.core.items.GenericItem) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 3 with GenericItem

use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.

the class PersistenceManagerImpl method added.

@Override
public void added(Item item) {
    initialize(item);
    if (item instanceof GenericItem) {
        GenericItem genericItem = (GenericItem) item;
        genericItem.addStateChangeListener(this);
    }
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem)

Example 4 with GenericItem

use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.

the class ItemResource method addMember.

@PUT
@RolesAllowed({ Role.ADMIN })
@Path("/{itemName: [a-zA-Z_0-9]*}/members/{memberItemName: [a-zA-Z_0-9]*}")
@ApiOperation(value = "Adds a new member to a group item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item or member item not found or item is not of type group item."), @ApiResponse(code = 405, message = "Member item is not editable.") })
public Response addMember(@PathParam("itemName") @ApiParam(value = "item name", required = true) String itemName, @PathParam("memberItemName") @ApiParam(value = "member item name", required = true) String memberItemName) {
    try {
        Item item = itemRegistry.getItem(itemName);
        if (!(item instanceof GroupItem)) {
            return Response.status(Status.NOT_FOUND).build();
        }
        GroupItem groupItem = (GroupItem) item;
        Item memberItem = itemRegistry.getItem(memberItemName);
        if (!(memberItem instanceof GenericItem)) {
            return Response.status(Status.NOT_FOUND).build();
        }
        if (managedItemProvider.get(memberItemName) == null) {
            return Response.status(Status.METHOD_NOT_ALLOWED).build();
        }
        GenericItem genericMemberItem = (GenericItem) memberItem;
        genericMemberItem.addGroupName(groupItem.getName());
        managedItemProvider.update(genericMemberItem);
        return Response.ok(null, MediaType.TEXT_PLAIN).build();
    } catch (ItemNotFoundException e) {
        return Response.status(Status.NOT_FOUND).build();
    }
}
Also used : ActiveItem(org.eclipse.smarthome.core.items.ActiveItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) GenericItem(org.eclipse.smarthome.core.items.GenericItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with GenericItem

use of org.eclipse.smarthome.core.items.GenericItem in project smarthome by eclipse.

the class SitemapResource method getAllItems.

/**
 * Collects all items that are represented by a given list of widgets
 *
 * @param widgets
 *            the widget list to get the items for added to all bundles containing REST resources
 * @return all items that are represented by the list of widgets
 */
private Set<GenericItem> getAllItems(EList<Widget> widgets) {
    Set<GenericItem> items = new HashSet<GenericItem>();
    if (itemUIRegistry != null) {
        for (Widget widget : widgets) {
            String itemName = widget.getItem();
            if (itemName != null) {
                try {
                    Item item = itemUIRegistry.getItem(itemName);
                    if (item instanceof GenericItem) {
                        items.add((GenericItem) item);
                    }
                } catch (ItemNotFoundException e) {
                // ignore
                }
            }
            // Consider all items inside the frame
            if (widget instanceof Frame) {
                items.addAll(getAllItems(((Frame) widget).getChildren()));
            }
            // Consider items involved in any visibility, labelcolor and valuecolor condition
            items.addAll(getItemsInVisibilityCond(widget.getVisibility()));
            items.addAll(getItemsInColorCond(widget.getLabelColor()));
            items.addAll(getItemsInColorCond(widget.getValueColor()));
        }
    }
    return items;
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) Frame(org.eclipse.smarthome.model.sitemap.Frame) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) HashSet(java.util.HashSet) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

GenericItem (org.eclipse.smarthome.core.items.GenericItem)27 GroupItem (org.eclipse.smarthome.core.items.GroupItem)10 Item (org.eclipse.smarthome.core.items.Item)10 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)6 Test (org.junit.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Path (javax.ws.rs.Path)2 ActiveItem (org.eclipse.smarthome.core.items.ActiveItem)2 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)2 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)2 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)2 ModelNormalItem (org.eclipse.smarthome.model.items.ModelNormalItem)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1