From f0c00959aa0f00138512913125e878fc072c841d Mon Sep 17 00:00:00 2001 From: Mark Herwege Date: Fri, 14 Jul 2023 19:01:22 +0100 Subject: [PATCH] [rest] add unit to item response (#3654) Signed-off-by: Mark Herwege --- .../core/io/rest/core/item/EnrichedGroupItemDTO.java | 4 ++-- .../openhab/core/io/rest/core/item/EnrichedItemDTO.java | 5 ++++- .../core/io/rest/core/item/EnrichedItemDTOMapper.java | 9 +++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedGroupItemDTO.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedGroupItemDTO.java index f35fc14648b..4604bf4aa74 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedGroupItemDTO.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedGroupItemDTO.java @@ -25,8 +25,8 @@ public class EnrichedGroupItemDTO extends EnrichedItemDTO { public EnrichedGroupItemDTO(ItemDTO itemDTO, EnrichedItemDTO[] members, String link, String state, - String transformedState, StateDescription stateDescription) { - super(itemDTO, link, state, transformedState, stateDescription, null); + String transformedState, StateDescription stateDescription, String unitSymbol) { + super(itemDTO, link, state, transformedState, stateDescription, null, unitSymbol); this.members = members; this.groupType = ((GroupItemDTO) itemDTO).groupType; this.function = ((GroupItemDTO) itemDTO).function; diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTO.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTO.java index 5a9da86eafd..62f6aaf906f 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTO.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTO.java @@ -24,6 +24,7 @@ * * @author Dennis Nobel - Initial contribution * @author Kai Kreuzer - Added metadata + * @author Mark Herwege - Added default unit symbol */ public class EnrichedItemDTO extends ItemDTO { @@ -31,12 +32,13 @@ public class EnrichedItemDTO extends ItemDTO { public String state; public String transformedState; public StateDescription stateDescription; + public String unitSymbol; public CommandDescription commandDescription; public Map metadata; public Boolean editable; public EnrichedItemDTO(ItemDTO itemDTO, String link, String state, String transformedState, - StateDescription stateDescription, CommandDescription commandDescription) { + StateDescription stateDescription, CommandDescription commandDescription, String unitSymbol) { this.type = itemDTO.type; this.name = itemDTO.name; this.label = itemDTO.label; @@ -48,5 +50,6 @@ public EnrichedItemDTO(ItemDTO itemDTO, String link, String state, String transf this.transformedState = transformedState; this.stateDescription = stateDescription; this.commandDescription = commandDescription; + this.unitSymbol = unitSymbol; } } diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java index 2036e5b853f..94a4b85c122 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/item/EnrichedItemDTOMapper.java @@ -27,6 +27,7 @@ import org.openhab.core.items.Item; import org.openhab.core.items.dto.ItemDTO; import org.openhab.core.items.dto.ItemDTOMapper; +import org.openhab.core.library.items.NumberItem; import org.openhab.core.transform.TransformationException; import org.openhab.core.transform.TransformationHelper; import org.openhab.core.types.StateDescription; @@ -93,6 +94,10 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown EnrichedItemDTO enrichedItemDTO; + String unitSymbol = null; + if (item instanceof NumberItem numberItem) { + unitSymbol = numberItem.getUnitSymbol(); + } if (item instanceof GroupItem groupItem) { EnrichedItemDTO[] memberDTOs; if (drillDown) { @@ -111,10 +116,10 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown memberDTOs = new EnrichedItemDTO[0]; } enrichedItemDTO = new EnrichedGroupItemDTO(itemDTO, memberDTOs, link, state, transformedState, - stateDescription); + stateDescription, unitSymbol); } else { enrichedItemDTO = new EnrichedItemDTO(itemDTO, link, state, transformedState, stateDescription, - item.getCommandDescription(locale)); + item.getCommandDescription(locale), unitSymbol); } return enrichedItemDTO;