Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bundles/org.openhab.ui.basic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<name>openHAB UI :: Bundles :: Basic UI</name>

<dependencies>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.sitemap</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.core.bundles</groupId>
<artifactId>org.openhab.core.ui</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.types.State;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.core.util.ColorUtil;
Expand All @@ -53,6 +53,7 @@
* @author Vlad Ivanov - BasicUI changes
* @author Laurent Garnier - Refactor icon management to support other iconsets
* @author Laurent Garnier - primary/secondary colors
* @author Mark Herwege - Implement sitemap registry
*/
@NonNullByDefault
public abstract class AbstractWidgetRenderer implements WidgetRenderer {
Expand Down Expand Up @@ -112,7 +113,7 @@ public ItemUIRegistry getItemUIRegistry() {
* @return HTML code
*/
protected String preprocessSnippet(String originalSnippet, Widget w) {
return preprocessSnippet(originalSnippet, w, w.getStaticIcon() != null || !w.getIconRules().isEmpty());
return preprocessSnippet(originalSnippet, w, w.isStaticIcon() || !w.getIconRules().isEmpty());
}

/**
Expand All @@ -130,7 +131,8 @@ protected String preprocessSnippet(String originalSnippet, Widget w, boolean ign
snippet = snippet.replace("%cells_tablet%", String.valueOf(8 / DEFAULT_NB_COLUMNS_TABLET));
snippet = snippet.replace("%widget_id%", itemUIRegistry.getWidgetId(w));
snippet = snippet.replace("%icon_with_state%", Boolean.valueOf(!ignoreStateForIcon).toString());
snippet = snippet.replace("%item%", w.getItem() != null ? w.getItem() : "");
String itemName = w.getItem();
snippet = snippet.replace("%item%", itemName != null ? itemName : "");
// Optimization: avoid calling 3 times itemUIRegistry.getLabel(w)
String text = itemUIRegistry.getLabel(w);
snippet = snippet.replace("%label%", getLabel(text));
Expand Down Expand Up @@ -358,9 +360,10 @@ protected String processColor(Widget w, String originalSnippet) {
String snippet = originalSnippet;

State itemState = null;
if (w.getItem() != null) {
String itemName = w.getItem();
if (itemName != null) {
try {
Item item = itemUIRegistry.getItem(w.getItem());
Item item = itemUIRegistry.getItem(itemName);
itemState = item.getState();
} catch (ItemNotFoundException e) {
logger.debug("{}", e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Map;

import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.LocaleProvider;
Expand All @@ -27,10 +26,10 @@
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.model.sitemap.sitemap.Button;
import org.openhab.core.model.sitemap.sitemap.ButtonDefinition;
import org.openhab.core.model.sitemap.sitemap.Buttongrid;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Button;
import org.openhab.core.sitemap.ButtonDefinition;
import org.openhab.core.sitemap.Buttongrid;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.types.State;
import org.openhab.core.types.util.UnitUtils;
import org.openhab.core.ui.items.ItemUIRegistry;
Expand All @@ -48,6 +47,7 @@
* can produce HTML code for Buttongrid widgets.
*
* @author Laurent Garnier - Initial contribution
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand All @@ -67,7 +67,7 @@ public boolean canRender(Widget w) {
}

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) throws RenderException {
public List<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) throws RenderException {
Buttongrid grid = (Buttongrid) w;

Map<Integer, Map<Integer, ButtonDefinition>> rowsButtons = new HashMap<>();
Expand Down Expand Up @@ -104,7 +104,7 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
}
}
// Go through buttons defined as sub-element of the Buttongrid to fill the map rowsWidgets
for (Widget widget : grid.getChildren()) {
for (Widget widget : grid.getWidgets()) {
if (widget instanceof Button button) {
int row = button.getRow();
int column = button.getColumn();
Expand Down Expand Up @@ -205,9 +205,7 @@ private void buildRow(int columns, @Nullable Map<Integer, ButtonDefinition> butt
} else if (buttonWidgets != null) {
String buttonHtml = "";
for (Button b : buttonWidgets) {
String icon = b.getStaticIcon() != null || b.getIcon() != null || !b.getIconRules().isEmpty()
? getCategory(b)
: null;
String icon = b.getIcon() != null || !b.getIconRules().isEmpty() ? getCategory(b) : null;
buttonHtml += buildButton(b, b.getLabel(), b.getCmd(), b.getReleaseCmd(), icon, b.isStateless());
}
buildCell(false, sizeDessktop, col > 8, sizeTablet, col > 4, sizePhone, buttonHtml, builder);
Expand Down Expand Up @@ -263,7 +261,10 @@ private String buildButton(@Nullable Button buttonWidget, @Nullable String lab,
if (buttonWidget != null) {
Item item = null;
try {
item = itemUIRegistry.getItem(buttonWidget.getItem());
String widgetItemName = buttonWidget.getItem();
if (widgetItemName != null) {
item = itemUIRegistry.getItem(widgetItemName);
}
} catch (ItemNotFoundException e) {
logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
Expand All @@ -25,8 +26,8 @@
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.model.sitemap.sitemap.Chart;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Chart;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.ui.basic.render.RenderException;
import org.openhab.ui.basic.render.WidgetRenderer;
Expand All @@ -44,6 +45,7 @@
* @author Kai Kreuzer - Initial contribution and API
* @author Vlad Ivanov - BasicUI changes
* @author Laurent Garnier - Delegate the definition of certain chart URL parameters to the frontend (smarthome.js)
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand All @@ -70,11 +72,10 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th

try {
String itemParam = null;
boolean forceAsItem = false;
if (chart.getForceAsItem() != null) {
forceAsItem = chart.getForceAsItem();
}
Item item = itemUIRegistry.getItem(chart.getItem());
boolean forceAsItem = chart.forceAsItem();
Item item = null;
String itemName = Objects.requireNonNull(w.getItem()); // Checked at creation there is an item
item = itemUIRegistry.getItem(itemName);
if (item instanceof GroupItem && !forceAsItem) {
itemParam = "groups=" + chart.getItem();
} else {
Expand All @@ -92,14 +93,12 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
}

// if legend parameter is given, add corresponding GET parameter
boolean legend = item instanceof GroupItem && !forceAsItem;
if (chart.getLegend() != null) {
legend = chart.getLegend();
if (chart.getLegend()) {
chartUrl += "&legend=true";
} else {
chartUrl += "&legend=false";
}
boolean legend = chart.hasLegend();
legend = legend || (item instanceof GroupItem && !forceAsItem);
if (legend) {
chartUrl += "&legend=true";
} else {
chartUrl += "&legend=false";
}

if (chart.getInterpolation() instanceof String interpolationMethod) {
Expand Down Expand Up @@ -174,7 +173,8 @@ private void buildRow(Chart w, @Nullable String lab, String cmd, String current,
String command = cmd;
String label = lab == null ? cmd : lab;

rowSnippet = rowSnippet.replace("%item%", w.getItem() != null ? w.getItem() : "");
String itemName = w.getItem();
rowSnippet = rowSnippet.replace("%item%", itemName != null ? itemName : "");
rowSnippet = rowSnippet.replace("%cmd%", escapeHtml(command));
rowSnippet = rowSnippet.replace("%label%", escapeHtml(label));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.model.sitemap.sitemap.Colorpicker;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Colorpicker;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.types.State;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.ui.basic.render.RenderException;
Expand All @@ -39,6 +39,7 @@
*
* @author Kai Kreuzer - Initial contribution and API
* @author Vlad Ivanov - BasicUI changes
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.unit.Units;
import org.openhab.core.model.sitemap.sitemap.Colortemperaturepicker;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Colortemperaturepicker;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.thing.DefaultSystemChannelTypeProvider;
import org.openhab.core.types.State;
import org.openhab.core.types.StateDescription;
Expand All @@ -51,6 +51,7 @@
* Colortemperaturepicker widgets.
*
* @author Laurent Garnier - Initial contribution
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand Down Expand Up @@ -86,59 +87,57 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th
BigDecimal maxK = MAX_TEMPERATURE_KELVIN;
String[] colorsRGB = null;
String itemType = null;
String itemName = w.getItem();
if (itemName != null) {
try {
Item item = itemUIRegistry.getItem(itemName);
itemType = item.getType();
if ((CoreItemFactory.NUMBER + ":Temperature").equals(itemType)) {
State state = itemUIRegistry.getState(w);
if (state instanceof QuantityType<?> quantity) {
if (unit == null) {
unit = quantity.getUnit();
}
quantity = quantity.toInvertibleUnit(Units.KELVIN);
if (quantity != null) {
currentK = quantity.toBigDecimal();
try {
int[] rgb = ColorUtil
.hsbToRgb(ColorUtil.xyToHsb(ColorUtil.kelvinToXY(currentK.doubleValue())));
logger.debug("Current {} K => RGB {} {} {}", currentK, rgb[0], rgb[1], rgb[2]);
currentRGB = "#%02x%02x%02x".formatted(rgb[0], rgb[1], rgb[2]);
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
logger.debug("Can't get RGB for {} Kelvin, bypassing color gradient!", currentK);
}
}
String itemName = Objects.requireNonNull(w.getItem()); // Checked at creation there is an item
try {
Item item = itemUIRegistry.getItem(itemName);
itemType = item.getType();
if ((CoreItemFactory.NUMBER + ":Temperature").equals(itemType)) {
State state = itemUIRegistry.getState(w);
if (state instanceof QuantityType<?> quantity) {
if (unit == null) {
unit = quantity.getUnit();
}
if (unit == Units.KELVIN || unit == Units.MIRED) {
minK = getMinimumInKelvin(ctp, unit, item.getStateDescription());
maxK = getMaximumInKelvin(ctp, unit, item.getStateDescription());
logger.debug("ColortemppickerRenderer current={} unit={} min={} max={}", currentK, unit, minK,
maxK);

double valueKelvin = 0d;
quantity = quantity.toInvertibleUnit(Units.KELVIN);
if (quantity != null) {
currentK = quantity.toBigDecimal();
try {
colorsRGB = new String[(int) Math.round(100 / GRADIENT_INCREMENT_PERCENT) + 1];
for (int i = 0; Math.round(i * GRADIENT_INCREMENT_PERCENT) <= 100; i++) {
valueKelvin = (maxK.doubleValue() - minK.doubleValue()) * i * GRADIENT_INCREMENT_PERCENT
/ 100.0 + minK.doubleValue();
int[] rgb = ColorUtil.hsbToRgb(ColorUtil.xyToHsb(ColorUtil.kelvinToXY(valueKelvin)));
logger.debug("Gradient {}%: {} K => RGB {} {} {}", i * GRADIENT_INCREMENT_PERCENT,
valueKelvin, rgb[0], rgb[1], rgb[2]);
colorsRGB[i] = "#%02x%02x%02x".formatted(rgb[0], rgb[1], rgb[2]);
}
int[] rgb = ColorUtil
.hsbToRgb(ColorUtil.xyToHsb(ColorUtil.kelvinToXY(currentK.doubleValue())));
logger.debug("Current {} K => RGB {} {} {}", currentK, rgb[0], rgb[1], rgb[2]);
currentRGB = "#%02x%02x%02x".formatted(rgb[0], rgb[1], rgb[2]);
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
logger.debug("Can't get RGB for {} Kelvin, bypassing color gradient!", valueKelvin);
colorsRGB = null;
logger.debug("Can't get RGB for {} Kelvin, bypassing color gradient!", currentK);
}
} else {
logger.warn("Invalid unit {} for Colortemperaturepicker element", unit);
}
}
if (unit == Units.KELVIN || unit == Units.MIRED) {
minK = getMinimumInKelvin(ctp, unit, item.getStateDescription());
maxK = getMaximumInKelvin(ctp, unit, item.getStateDescription());
logger.debug("ColortemppickerRenderer current={} unit={} min={} max={}", currentK, unit, minK,
maxK);

double valueKelvin = 0d;
try {
colorsRGB = new String[(int) Math.round(100 / GRADIENT_INCREMENT_PERCENT) + 1];
for (int i = 0; Math.round(i * GRADIENT_INCREMENT_PERCENT) <= 100; i++) {
valueKelvin = (maxK.doubleValue() - minK.doubleValue()) * i * GRADIENT_INCREMENT_PERCENT
/ 100.0 + minK.doubleValue();
int[] rgb = ColorUtil.hsbToRgb(ColorUtil.xyToHsb(ColorUtil.kelvinToXY(valueKelvin)));
logger.debug("Gradient {}%: {} K => RGB {} {} {}", i * GRADIENT_INCREMENT_PERCENT,
valueKelvin, rgb[0], rgb[1], rgb[2]);
colorsRGB[i] = "#%02x%02x%02x".formatted(rgb[0], rgb[1], rgb[2]);
}
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
logger.debug("Can't get RGB for {} Kelvin, bypassing color gradient!", valueKelvin);
colorsRGB = null;
}
} else {
logger.warn("Invalid item type {} for Colortemperaturepicker element", itemType);
logger.warn("Invalid unit {} for Colortemperaturepicker element", unit);
}
} catch (ItemNotFoundException e) {
} else {
logger.warn("Invalid item type {} for Colortemperaturepicker element", itemType);
}
} catch (ItemNotFoundException e) {
}

String snippet = getSnippet((CoreItemFactory.NUMBER + ":Temperature").equals(itemType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.model.sitemap.sitemap.Frame;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Frame;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.ui.basic.render.RenderException;
import org.openhab.ui.basic.render.WidgetRenderer;
Expand All @@ -36,6 +35,7 @@
*
* @author Kai Kreuzer - Initial contribution and API
* @author Vlad Ivanov - BasicUI changes
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand All @@ -53,7 +53,7 @@ public boolean canRender(Widget w) {
}

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) throws RenderException {
public List<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) throws RenderException {
String snippet = getSnippet("frame");
String label = StringEscapeUtils.escapeHtml4(itemUIRegistry.getLabel(w));
List<String> frameClassList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.model.sitemap.sitemap.Group;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.sitemap.Group;
import org.openhab.core.sitemap.Widget;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.openhab.ui.basic.render.RenderException;
import org.openhab.ui.basic.render.WidgetRenderer;
Expand All @@ -33,6 +33,7 @@
*
* @author Kai Kreuzer - Initial contribution and API
* @author Vlad Ivanov - BasicUI changes
* @author Mark Herwege - Implement sitemap registry
*/
@Component(service = WidgetRenderer.class)
@NonNullByDefault
Expand Down
Loading
Loading