use of org.eclipse.smarthome.core.library.items.RollershutterItem 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.library.items.RollershutterItem in project smarthome by eclipse.
the class SwitchRenderer method renderWidget.
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
Switch s = (Switch) w;
String snippetName = null;
Item item = null;
try {
item = itemUIRegistry.getItem(w.getItem());
if (s.getMappings().size() == 0) {
if (item instanceof RollershutterItem) {
snippetName = "rollerblind";
} else if (item instanceof GroupItem && ((GroupItem) item).getBaseItem() instanceof RollershutterItem) {
snippetName = "rollerblind";
} else {
snippetName = "switch";
}
} else {
snippetName = "buttons";
}
} catch (ItemNotFoundException e) {
logger.warn("Cannot determine item type of '{}'", w.getItem(), e);
snippetName = "switch";
}
String snippet = getSnippet(snippetName);
State state = itemUIRegistry.getState(w);
snippet = preprocessSnippet(snippet, w);
snippet = StringUtils.replace(snippet, "%count%", Integer.toString(s.getMappings().size()));
if (s.getMappings().size() == 0) {
if (state.equals(OnOffType.ON)) {
snippet = snippet.replaceAll("%checked%", "checked=true");
} else {
snippet = snippet.replaceAll("%checked%", "");
}
} else {
StringBuilder buttons = new StringBuilder();
for (Mapping mapping : s.getMappings()) {
String button = getSnippet("button");
String command = mapping.getCmd();
String label = mapping.getLabel();
if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
String unit = getUnitForWidget(w);
command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
// Special treatment for °C since uom library uses a single character: ℃
// This will ensure the current state matches the cmd and the buttonClass is set accordingly.
command = StringUtils.replace(command, "°C", "℃");
}
button = StringUtils.replace(button, "%item%", w.getItem());
button = StringUtils.replace(button, "%cmd%", escapeHtml(command));
button = StringUtils.replace(button, "%label%", label != null ? escapeHtml(label) : "");
String buttonClass;
State compareMappingState = state;
if (state instanceof QuantityType) {
// convert the item state to the command value for proper
// comparison and buttonClass calculation
compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
}
if (s.getMappings().size() > 1 && compareMappingState.toString().equals(command)) {
buttonClass = "mdl-button--accent";
} else {
buttonClass = "mdl-button";
}
button = StringUtils.replace(button, "%class%", buttonClass);
buttons.append(button);
}
snippet = StringUtils.replace(snippet, "%buttons%", buttons.toString());
}
// Process the color tags
snippet = processColor(w, snippet);
sb.append(snippet);
return null;
}
use of org.eclipse.smarthome.core.library.items.RollershutterItem in project smarthome by eclipse.
the class GroupItemTest method assertThatGroupItemWithRollershutterBaseItemConversionWorks.
@Test
public void assertThatGroupItemWithRollershutterBaseItemConversionWorks() {
// initially this group has State UndefType.NULL
GroupItem groupItem = new GroupItem("root", new RollershutterItem("myRollerShutter"));
State groupStateAsOnOff = groupItem.getStateAs(OnOffType.class);
// a state conversion from NULL to OnOffType should not be possible
assertNull(groupStateAsOnOff);
// init group
groupItem.setState(new PercentType(70));
groupStateAsOnOff = groupItem.getStateAs(OnOffType.class);
// any value >0 means on, so 50% means the group state should be ON
assertTrue(OnOffType.ON.equals(groupStateAsOnOff));
}
use of org.eclipse.smarthome.core.library.items.RollershutterItem in project smarthome by eclipse.
the class ItemUIRegistryImpl method convertState.
/**
* Converts an item state to the type the widget supports (if possible)
*
* @param w Widget in sitemap that shows the state
* @param i item
* @return the converted state or the original if conversion was not possible
*/
private State convertState(Widget w, Item i) {
State returnState = null;
State itemState = i.getState();
if (itemState instanceof QuantityType) {
itemState = convertStateToWidgetUnit((QuantityType<?>) itemState, w);
}
if (w instanceof Switch && i instanceof RollershutterItem) {
// RollerShutter are represented as Switch in a Sitemap but need a PercentType state
returnState = itemState.as(PercentType.class);
} else if (w instanceof Slider) {
if (i.getAcceptedDataTypes().contains(PercentType.class)) {
returnState = itemState.as(PercentType.class);
} else {
returnState = itemState.as(DecimalType.class);
}
} else if (w instanceof Switch) {
Switch sw = (Switch) w;
if (sw.getMappings().size() == 0) {
returnState = itemState.as(OnOffType.class);
}
}
// if returnState is null, a conversion was not possible
if (returnState == null) {
// we return the original state to not break anything
returnState = itemState;
}
return returnState;
}
use of org.eclipse.smarthome.core.library.items.RollershutterItem in project smarthome by eclipse.
the class SwitchRenderer method renderWidget.
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
Switch s = (Switch) w;
String snippetName = null;
Item item = null;
try {
item = itemUIRegistry.getItem(w.getItem());
if (s.getMappings().size() == 0) {
if (item instanceof RollershutterItem) {
snippetName = "rollerblind";
} else if (item instanceof GroupItem && ((GroupItem) item).getBaseItem() instanceof RollershutterItem) {
snippetName = "rollerblind";
} else {
snippetName = "switch";
}
} else {
snippetName = "buttons";
}
} catch (ItemNotFoundException e) {
logger.warn("Cannot determine item type of '{}'", w.getItem(), e);
snippetName = "switch";
}
String snippet = getSnippet(snippetName);
snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
snippet = StringUtils.replace(snippet, "%state%", getState(w));
snippet = StringUtils.replace(snippet, "%format%", getFormat());
snippet = StringUtils.replace(snippet, "%item%", w.getItem());
snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
State state = itemUIRegistry.getState(w);
if (s.getMappings().size() == 0) {
if (state.equals(OnOffType.ON)) {
snippet = snippet.replaceAll("%checked%", "checked=true");
} else {
snippet = snippet.replaceAll("%checked%", "");
}
} else {
StringBuilder buttons = new StringBuilder();
for (Mapping mapping : s.getMappings()) {
String button = getSnippet("button");
String command = mapping.getCmd();
String label = mapping.getLabel();
if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
String unit = getUnitForWidget(w);
command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
// Special treatment for °C since uom library uses a single character: ℃
// This will ensure the current state matches the cmd and the buttonClass is set accordingly.
command = StringUtils.replace(command, "°C", "℃");
}
button = StringUtils.replace(button, "%item%", w.getItem());
button = StringUtils.replace(button, "%cmd%", StringEscapeUtils.escapeHtml(command));
button = StringUtils.replace(button, "%label%", label != null ? StringEscapeUtils.escapeHtml(label) : "");
String buttonClass;
State compareMappingState = state;
if (state instanceof QuantityType) {
// convert the item state to the command value for proper
// comparison and buttonClass calculation
compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
}
if (s.getMappings().size() > 1 && compareMappingState.toString().equals(command)) {
// button with red color
buttonClass = "Warn";
} else {
// button with blue color
buttonClass = "Action";
}
button = StringUtils.replace(button, "%type%", buttonClass);
buttons.append(button);
}
snippet = StringUtils.replace(snippet, "%buttons%", buttons.toString());
}
// Process the color tags
snippet = processColor(w, snippet);
sb.append(snippet);
return null;
}
Aggregations