use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ScriptEngineOSGiTest method testGreaterEqualsWithItemState.
@SuppressWarnings("null")
@Test
public void testGreaterEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
((NumberItem) numberItem).setState(new QuantityType<>("20 °C"));
assertTrue(runScript("NumberA.state >= 20 [°C]"));
}
use of org.eclipse.smarthome.core.items.Item in project smarthome by eclipse.
the class ScriptEngineOSGiTest method testLessEqualsWithItemState.
@SuppressWarnings("null")
@Test
public void testLessEqualsWithItemState() throws ScriptExecutionException, ScriptParsingException {
Item numberItem = itemRegistry.get(NUMBER_ITEM_TEMPERATURE);
((NumberItem) numberItem).setState(new QuantityType<>("19 °F"));
assertTrue(runScript("NumberA.state <= 20 [°F]"));
}
use of org.eclipse.smarthome.core.items.Item 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.items.Item in project smarthome by eclipse.
the class BusEvent method postUpdate.
/**
* Posts a status update for a specified item to the event bus.
*
* @param itemName the name of the item to send the status update for
* @param stateAsString the new state of the item
*/
public static Object postUpdate(String itemName, String stateString) {
ItemRegistry registry = ScriptServiceUtil.getItemRegistry();
EventPublisher publisher = ScriptServiceUtil.getEventPublisher();
if (publisher != null && registry != null) {
try {
Item item = registry.getItem(itemName);
State state = TypeParser.parseState(item.getAcceptedDataTypes(), stateString);
if (state != null) {
publisher.post(ItemEventFactory.createStateEvent(itemName, state));
} else {
LoggerFactory.getLogger(BusEvent.class).warn("Cannot convert '{}' to a state type which item '{}' accepts: {}.", stateString, itemName, getAcceptedDataTypeNames(item));
}
} catch (ItemNotFoundException e) {
LoggerFactory.getLogger(BusEvent.class).warn("Item '{}' does not exist.", itemName);
}
}
return null;
}
use of org.eclipse.smarthome.core.items.Item 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;
}
Aggregations