Search in sources :

Example 1 with Item

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

the class ItemStateConverterImplTest method testStateConversion.

@Test
public void testStateConversion() {
    Item item = new NumberItem("number");
    State originalState = new PercentType("42");
    State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState, is(new DecimalType("0.42")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) Test(org.junit.Test)

Example 2 with Item

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

the class ItemStateConverterImplTest method testNoConversion.

@Test
public void testNoConversion() {
    Item item = new NumberItem("number");
    State originalState = new DecimalType(12.34);
    State state = itemStateConverter.convertToAcceptedState(originalState, item);
    assertTrue(originalState == state);
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 3 with Item

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

the class PersistenceManagerImpl method appliesToItem.

/**
 * Checks if a given persistence configuration entry is relevant for an item
 *
 * @param config the persistence configuration entry
 * @param item to check if the configuration applies to
 * @return true, if the configuration applies to the item
 */
private boolean appliesToItem(SimpleItemConfiguration config, Item item) {
    for (SimpleConfig itemCfg : config.getItems()) {
        if (itemCfg instanceof SimpleAllConfig) {
            return true;
        }
        if (itemCfg instanceof SimpleItemConfig) {
            SimpleItemConfig singleItemConfig = (SimpleItemConfig) itemCfg;
            if (item.getName().equals(singleItemConfig.getItem())) {
                return true;
            }
        }
        if (itemCfg instanceof SimpleGroupConfig) {
            SimpleGroupConfig groupItemCfg = (SimpleGroupConfig) itemCfg;
            String groupName = groupItemCfg.getGroup();
            try {
                Item gItem = itemRegistry.getItem(groupName);
                if (gItem instanceof GroupItem) {
                    GroupItem groupItem = (GroupItem) gItem;
                    if (groupItem.getAllMembers().contains(item)) {
                        return true;
                    }
                }
            } catch (Exception e) {
            }
        }
    }
    return false;
}
Also used : SimpleConfig(org.eclipse.smarthome.core.persistence.config.SimpleConfig) SimpleGroupConfig(org.eclipse.smarthome.core.persistence.config.SimpleGroupConfig) GroupItem(org.eclipse.smarthome.core.items.GroupItem) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) SimpleAllConfig(org.eclipse.smarthome.core.persistence.config.SimpleAllConfig) SimpleItemConfig(org.eclipse.smarthome.core.persistence.config.SimpleItemConfig) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ParseException(java.text.ParseException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 4 with Item

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

the class PersistenceManagerImpl method startEventHandling.

@Override
public void startEventHandling(final String dbId) {
    synchronized (persistenceServiceConfigs) {
        final PersistenceServiceConfiguration config = persistenceServiceConfigs.get(dbId);
        if (config == null) {
            return;
        }
        if (itemRegistry != null) {
            for (SimpleItemConfiguration itemConfig : config.getConfigs()) {
                if (hasStrategy(dbId, itemConfig, SimpleStrategy.Globals.RESTORE)) {
                    for (Item item : getAllItems(itemConfig)) {
                        initialize(item);
                    }
                }
            }
        }
        createTimers(dbId, config.getStrategies());
    }
}
Also used : GroupItem(org.eclipse.smarthome.core.items.GroupItem) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) SimpleItemConfiguration(org.eclipse.smarthome.core.persistence.SimpleItemConfiguration) PersistenceServiceConfiguration(org.eclipse.smarthome.core.persistence.PersistenceServiceConfiguration)

Example 5 with Item

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

the class ItemResource method postItemCommand.

@POST
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]*}")
@Consumes(MediaType.TEXT_PLAIN)
@ApiOperation(value = "Sends a command to an item.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 404, message = "Item not found"), @ApiResponse(code = 400, message = "Item command null") })
public Response postItemCommand(@PathParam("itemname") @ApiParam(value = "item name", required = true) String itemname, @ApiParam(value = "valid item command (e.g. ON, OFF, UP, DOWN, REFRESH)", required = true) String value) {
    Item item = getItem(itemname);
    Command command = null;
    if (item != null) {
        if ("toggle".equalsIgnoreCase(value) && (item instanceof SwitchItem || item instanceof RollershutterItem)) {
            if (OnOffType.ON.equals(item.getStateAs(OnOffType.class))) {
                command = OnOffType.OFF;
            }
            if (OnOffType.OFF.equals(item.getStateAs(OnOffType.class))) {
                command = OnOffType.ON;
            }
            if (UpDownType.UP.equals(item.getStateAs(UpDownType.class))) {
                command = UpDownType.DOWN;
            }
            if (UpDownType.DOWN.equals(item.getStateAs(UpDownType.class))) {
                command = UpDownType.UP;
            }
        } else {
            command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), value);
        }
        if (command != null) {
            logger.debug("Received HTTP POST request at '{}' with value '{}'.", uriInfo.getPath(), value);
            eventPublisher.post(ItemEventFactory.createCommandEvent(itemname, command));
            ResponseBuilder resbuilder = Response.ok();
            resbuilder.type(MediaType.TEXT_PLAIN);
            return resbuilder.build();
        } else {
            logger.warn("Received HTTP POST request at '{}' with an invalid status value '{}'.", uriInfo.getPath(), value);
            return Response.status(Status.BAD_REQUEST).build();
        }
    } else {
        logger.info("Received HTTP POST request at '{}' for the unknown item '{}'.", uriInfo.getPath(), itemname);
        throw new WebApplicationException(404);
    }
}
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) WebApplicationException(javax.ws.rs.WebApplicationException) Command(org.eclipse.smarthome.core.types.Command) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) UpDownType(org.eclipse.smarthome.core.library.types.UpDownType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Item (org.eclipse.smarthome.core.items.Item)111 GroupItem (org.eclipse.smarthome.core.items.GroupItem)42 GenericItem (org.eclipse.smarthome.core.items.GenericItem)39 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)37 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)35 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)31 State (org.eclipse.smarthome.core.types.State)26 Test (org.junit.Test)26 ItemRegistry (org.eclipse.smarthome.core.items.ItemRegistry)14 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)14 StringItem (org.eclipse.smarthome.core.library.items.StringItem)14 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)12 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)10 IntentInterpretation (org.openhab.ui.habot.nlp.IntentInterpretation)10 EventPublisher (org.eclipse.smarthome.core.events.EventPublisher)9 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 Thing (org.eclipse.smarthome.core.thing.Thing)9 Set (java.util.Set)8 Widget (org.eclipse.smarthome.model.sitemap.Widget)8