Skip to content

Commit

Permalink
[rest] add unit to item response (#3654)
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <[email protected]>
  • Loading branch information
mherwege authored Jul 14, 2023
1 parent fb4f45e commit f0c0095
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@
*
* @author Dennis Nobel - Initial contribution
* @author Kai Kreuzer - Added metadata
* @author Mark Herwege - Added default unit symbol
*/
public class EnrichedItemDTO extends ItemDTO {

public String link;
public String state;
public String transformedState;
public StateDescription stateDescription;
public String unitSymbol;
public CommandDescription commandDescription;
public Map<String, Object> 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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit f0c0095

Please sign in to comment.