use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class AutoUpdateBinding method receiveCommand.
/**
* Handle the received command event.
*
* <p>
* If the command could be converted to a {@link State} the auto-update configurations are inspected.
* If there is at least one configuration that enable the auto-update, auto-update is applied.
* If there is no configuration provided at all the autoupdate defaults to {@code true} and an update is posted for
* the corresponding {@link State}.
*
* @param commandEvent the command event
*/
@Override
protected void receiveCommand(ItemCommandEvent commandEvent) {
final Command command = commandEvent.getItemCommand();
if (command instanceof State) {
final State state = (State) command;
final String itemName = commandEvent.getItemName();
Boolean autoUpdate = autoUpdate(itemName);
// we didn't find any autoupdate configuration, so apply the default now
if (autoUpdate == null) {
autoUpdate = Boolean.TRUE;
}
if (autoUpdate) {
postUpdate(itemName, state);
} else {
logger.trace("Won't update item '{}' as it is not configured to update its state automatically.", itemName);
}
}
}
use of org.eclipse.smarthome.core.types.Command 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);
}
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class CommunicationManager method handleEvent.
private <T extends Type> void handleEvent(String itemName, T type, String source, Function<@Nullable String, @Nullable List<Class<? extends T>>> acceptedTypesFunction, ProfileAction<T> action) {
final Item item = getItem(itemName);
if (item == null) {
logger.debug("Received an event for item {} which does not exist", itemName);
return;
}
itemChannelLinkRegistry.stream().filter(link -> {
// all links for the item
return link.getItemName().equals(itemName);
}).filter(link -> {
// make sure the command event is not sent back to its source
return !link.getLinkedUID().toString().equals(source);
}).forEach(link -> {
ChannelUID channelUID = link.getLinkedUID();
Thing thing = getThing(channelUID.getThingUID());
if (thing != null) {
Channel channel = thing.getChannel(channelUID.getId());
if (channel != null) {
@Nullable T convertedType = toAcceptedType(type, channel, acceptedTypesFunction);
if (convertedType != null) {
if (thing.getHandler() != null) {
Profile profile = getProfile(link, item, thing);
action.handle(profile, thing, convertedType);
}
} else {
logger.debug("Received event '{}' ({}) could not be converted to any type accepted by item '{}' ({})", type, type.getClass().getSimpleName(), itemName, item.getType());
}
} else {
logger.debug("Received event '{}' for non-existing channel '{}', not forwarding it to the handler", type, channelUID);
}
} else {
logger.debug("Received event '{}' for non-existing thing '{}', not forwarding it to the handler", type, channelUID.getThingUID());
}
});
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class CommunicationManager method receiveCommand.
private void receiveCommand(ItemCommandEvent commandEvent) {
final String itemName = commandEvent.getItemName();
final Command command = commandEvent.getItemCommand();
handleEvent(itemName, command, commandEvent.getSource(), s -> acceptedCommandTypeMap.get(s), (profile, thing, convertedCommand) -> {
if (profile instanceof StateProfile) {
//
safeCaller.create(((StateProfile) profile), StateProfile.class).withAsync().withIdentifier(//
thing).withTimeout(//
THINGHANDLER_EVENT_TIMEOUT).build().onCommandFromItem(convertedCommand);
}
});
}
use of org.eclipse.smarthome.core.types.Command in project smarthome by eclipse.
the class SystemFollowProfile method onStateUpdateFromItem.
@Override
public void onStateUpdateFromItem(State state) {
if (!(state instanceof Command)) {
logger.debug("The given state {} could not be transformed to a command", state);
return;
}
Command command = (Command) state;
callback.handleCommand(command);
}
Aggregations