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")));
}
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);
}
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;
}
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());
}
}
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);
}
}
Aggregations