Simplify DateTimeType handling for InfluxDB

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-11-09 23:06:35 +01:00 committed by lsiepel
parent bc540c6653
commit e794c6a0f6
2 changed files with 3 additions and 9 deletions

View File

@ -14,8 +14,6 @@ package org.openhab.persistence.influxdb.internal;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.Instant; import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.TimeZone;
import javax.measure.Unit; import javax.measure.Unit;
@ -85,7 +83,7 @@ public class InfluxDBStateConvertUtils {
} else if (state instanceof OpenClosedType) { } else if (state instanceof OpenClosedType) {
value = state == OpenClosedType.OPEN ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF; value = state == OpenClosedType.OPEN ? DIGITAL_VALUE_ON : DIGITAL_VALUE_OFF;
} else if (state instanceof DateTimeType type) { } else if (state instanceof DateTimeType type) {
value = type.getZonedDateTime().toInstant().toEpochMilli(); value = type.getInstant().toEpochMilli();
} else { } else {
value = state.toFullString(); value = state.toFullString();
} }
@ -140,9 +138,7 @@ public class InfluxDBStateConvertUtils {
} else if (item instanceof RollershutterItem) { } else if (item instanceof RollershutterItem) {
return new PercentType(valueStr); return new PercentType(valueStr);
} else if (item instanceof DateTimeItem) { } else if (item instanceof DateTimeItem) {
Instant i = Instant.ofEpochMilli(new BigDecimal(valueStr).longValue()); return new DateTimeType(Instant.ofEpochMilli(new BigDecimal(valueStr).longValue()));
ZonedDateTime z = ZonedDateTime.ofInstant(i, TimeZone.getDefault().toZoneId());
return new DateTimeType(z);
} else if (item instanceof PlayerItem) { } else if (item instanceof PlayerItem) {
try { try {
return PlayPauseType.valueOf(valueStr); return PlayPauseType.valueOf(valueStr);

View File

@ -21,7 +21,6 @@ import static org.mockito.Mockito.when;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import javax.measure.Unit; import javax.measure.Unit;
@ -130,8 +129,7 @@ public class InfluxDBStateConvertUtilsTest {
long val = System.currentTimeMillis(); long val = System.currentTimeMillis();
DateTimeItem item = new DateTimeItem("name"); DateTimeItem item = new DateTimeItem("name");
DateTimeType expected = new DateTimeType( DateTimeType expected = new DateTimeType(Instant.ofEpochMilli(val));
ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected)); assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
} }