Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[persistence] improve building the ItemHistoryDTO #4194

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ private Response getItemHistoryDTO(@Nullable String serviceId, String itemName,
}

Iterable<HistoricItem> result;
State state;

long quantity = 0L;

Expand Down Expand Up @@ -446,23 +445,24 @@ private Response getItemHistoryDTO(@Nullable String serviceId, String itemName,
Iterator<HistoricItem> it = result.iterator();

// Iterate through the data
HistoricItem lastItem = null;
State lastState = null;
while (it.hasNext()) {
HistoricItem historicItem = it.next();
state = historicItem.getState();
State state = historicItem.getState();
long timestamp = historicItem.getTimestamp().toInstant().toEpochMilli();

// For 'binary' states, we need to replicate the data
// to avoid diagonal lines
if (state instanceof OnOffType || state instanceof OpenClosedType) {
if (lastItem != null) {
dto.addData(historicItem.getTimestamp().toInstant().toEpochMilli(), lastItem.getState());
if (lastState != null && !lastState.equals(state)) {
dto.addData(timestamp, lastState);
quantity++;
}
}

dto.addData(historicItem.getTimestamp().toInstant().toEpochMilli(), state);
dto.addData(timestamp, state);
quantity++;
lastItem = historicItem;
lastState = state;
}

if (boundary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;

import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.types.State;

Expand All @@ -41,20 +42,23 @@ public ItemHistoryDTO() {
* @param time the time of the record
* @param state the state at this time
*/
public void addData(Long time, State state) {
public void addData(long time, State state) {
HistoryDataBean newVal = new HistoryDataBean();
newVal.time = time;
if (state instanceof QuantityType quantityState) {
// we strip the unit from the state, since historic item states are expected to be all in the default unit
newVal.state = quantityState.toBigDecimal().toString();
} else if (state instanceof DecimalType decimalType) {
// use BigDecimal.toString() to hit the internal cache
newVal.state = decimalType.toBigDecimal().toString();
} else {
newVal.state = state.toString();
}
data.add(newVal);
}

public static class HistoryDataBean {
public Long time;
public long time;
public String state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,12 @@ && getUnit().inverse().isCompatible(targetUnit)) {
}

public BigDecimal toBigDecimal() {
return new BigDecimal(quantity.getValue().toString());
Number value = quantity.getValue();
if (value instanceof BigDecimal decimal) {
return decimal;
} else {
return new BigDecimal(value.toString());
}
}

@Override
Expand Down