use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class ScriptEngineOSGiTest method testNotEqualsWithItemState.
@SuppressWarnings("null")
@Test
public void testNotEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
((NumberItem) numberItem).setState(new QuantityType<>("20 °C"));
assertTrue(runScript("NumberA.state != 10 [°C]"));
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class SelectionRenderer method renderWidget.
@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
String snippet = getSnippet("selection");
snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
snippet = StringUtils.replace(snippet, "%state%", getState(w));
snippet = StringUtils.replace(snippet, "%format%", getFormat());
State state = itemUIRegistry.getState(w);
Selection selection = (Selection) w;
String mappedValue = "";
Item item = null;
try {
item = itemUIRegistry.getItem(w.getItem());
} catch (ItemNotFoundException e) {
logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
}
StringBuilder rowSB = new StringBuilder();
for (Mapping mapping : selection.getMappings()) {
String rowSnippet = getSnippet("selection_row");
String command = mapping.getCmd() != null ? 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", "℃");
}
rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", StringEscapeUtils.escapeHtml(command));
rowSnippet = StringUtils.replace(rowSnippet, "%label%", label != null ? StringEscapeUtils.escapeHtml(label) : "");
State compareMappingState = state;
if (state instanceof QuantityType) {
// convert the item state to the command value for proper
// comparison and "checked" attribute calculation
compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
}
if (compareMappingState.toString().equals(command)) {
rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
mappedValue = (label != null) ? label : command;
} else {
rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
}
rowSB.append(rowSnippet);
}
snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w, mappedValue));
snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
// Process the color tags
snippet = processColor(w, snippet);
sb.append(snippet);
return null;
}
use of org.eclipse.smarthome.core.library.items.NumberItem 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;
}
use of org.eclipse.smarthome.core.library.items.NumberItem in project smarthome by eclipse.
the class EnrichedItemDTOMapperWithTransformOSGiTest method shouldConsiderTraformationWhenPresent.
@Test
public void shouldConsiderTraformationWhenPresent() {
NumberItem item1 = new NumberItem("Item1");
item1.setState(new DecimalType("12.34"));
item1.setStateDescriptionService(stateDescriptionService);
EnrichedItemDTO enrichedDTO = EnrichedItemDTOMapper.map(item1, false, null, null, null);
assertThat(enrichedDTO, is(notNullValue()));
assertThat(enrichedDTO.name, is("Item1"));
assertThat(enrichedDTO.state, is("12.34"));
StateDescription sd = enrichedDTO.stateDescription;
assertThat(sd.getMinimum(), is(BigDecimal.valueOf(0)));
assertThat(sd.getMaximum(), is(BigDecimal.valueOf(100)));
assertThat(sd.getStep(), is(BigDecimal.valueOf(10)));
assertThat(sd.getPattern(), is("%d °C"));
assertThat(sd.getOptions().get(0).getValue(), is("SOUND"));
assertThat(sd.getOptions().get(0).getLabel(), is("My great sound."));
}
Aggregations