[influxdb] Fix exception handling and improve some type conversions (#15452)

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
J-N-K 2023-09-09 15:08:58 +02:00 committed by GitHub
parent f4fed3a800
commit f24a517a0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 6 deletions

View File

@ -29,8 +29,10 @@ import org.openhab.core.library.items.ColorItem;
import org.openhab.core.library.items.ContactItem;
import org.openhab.core.library.items.DateTimeItem;
import org.openhab.core.library.items.DimmerItem;
import org.openhab.core.library.items.ImageItem;
import org.openhab.core.library.items.LocationItem;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.PlayerItem;
import org.openhab.core.library.items.RollershutterItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DateTimeType;
@ -39,10 +41,14 @@ import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PercentType;
import org.openhab.core.library.types.PlayPauseType;
import org.openhab.core.library.types.PointType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.RewindFastforwardType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -81,7 +87,7 @@ public class InfluxDBStateConvertUtils {
} else if (state instanceof DateTimeType type) {
value = type.getZonedDateTime().toInstant().toEpochMilli();
} else {
value = state.toString();
value = state.toFullString();
}
return value;
}
@ -137,9 +143,21 @@ public class InfluxDBStateConvertUtils {
Instant i = Instant.ofEpochMilli(new BigDecimal(valueStr).longValue());
ZonedDateTime z = ZonedDateTime.ofInstant(i, TimeZone.getDefault().toZoneId());
return new DateTimeType(z);
} else if (item instanceof PlayerItem) {
try {
return PlayPauseType.valueOf(valueStr);
} catch (IllegalArgumentException ignored) {
}
try {
return RewindFastforwardType.valueOf(valueStr);
} catch (IllegalArgumentException ignored) {
}
} else if (item instanceof ImageItem) {
return RawType.valueOf(valueStr);
} else {
return new StringType(valueStr);
}
return UnDefType.UNDEF;
}
private static boolean toBoolean(@Nullable Object object) {

View File

@ -29,8 +29,8 @@ import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBException;
import org.influxdb.InfluxDBFactory;
import org.influxdb.InfluxDBIOException;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.Pong;
@ -59,14 +59,12 @@ import com.influxdb.exceptions.InfluxException;
public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
private final Logger logger = LoggerFactory.getLogger(InfluxDB1RepositoryImpl.class);
private final InfluxDBConfiguration configuration;
private final InfluxDBMetadataService influxDBMetadataService;
private final FilterCriteriaQueryCreator queryCreator;
private @Nullable InfluxDB client;
public InfluxDB1RepositoryImpl(InfluxDBConfiguration configuration,
InfluxDBMetadataService influxDBMetadataService) {
this.configuration = configuration;
this.influxDBMetadataService = influxDBMetadataService;
this.queryCreator = new InfluxDB1FilterCriteriaQueryCreatorImpl(configuration, influxDBMetadataService);
}
@ -131,7 +129,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
BatchPoints batchPoints = BatchPoints.database(configuration.getDatabaseName())
.retentionPolicy(configuration.getRetentionPolicy()).points(points).build();
currentClient.write(batchPoints);
} catch (InfluxException | InfluxDBIOException e) {
} catch (InfluxException | InfluxDBException e) {
logger.debug("Writing to database failed", e);
return false;
}
@ -177,7 +175,7 @@ public class InfluxDB1RepositoryImpl implements InfluxDBRepository {
} else {
throw new InfluxException("API not present");
}
} catch (InfluxException | InfluxDBIOException e) {
} catch (InfluxException | InfluxDBException e) {
logger.warn("Failed to execute query '{}': {}", filter, e.getMessage());
return List.of();
}

View File

@ -33,13 +33,18 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.openhab.core.i18n.UnitProvider;
import org.openhab.core.library.items.ContactItem;
import org.openhab.core.library.items.DateTimeItem;
import org.openhab.core.library.items.ImageItem;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.library.items.PlayerItem;
import org.openhab.core.library.items.SwitchItem;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.OpenClosedType;
import org.openhab.core.library.types.PlayPauseType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.RawType;
import org.openhab.core.library.types.RewindFastforwardType;
import org.openhab.core.library.unit.SIUnits;
/**
@ -74,6 +79,24 @@ public class InfluxDBStateConvertUtilsTest {
assertThat(InfluxDBStateConvertUtils.stateToObject(type), equalTo(nowInMillis));
}
@Test
public void convertImageState() {
RawType type = new RawType(new byte[] { 0x64, 0x66, 0x55, 0x00, 0x34 }, RawType.DEFAULT_MIME_TYPE);
assertThat(InfluxDBStateConvertUtils.stateToObject(type), is("data:application/octet-stream;base64,ZGZVADQ="));
}
@Test
public void convertPlayPauseState() {
assertThat(InfluxDBStateConvertUtils.stateToObject(PlayPauseType.PAUSE), is("PAUSE"));
assertThat(InfluxDBStateConvertUtils.stateToObject(PlayPauseType.PLAY), is("PLAY"));
}
@Test
public void convertRewindFastForwardState() {
assertThat(InfluxDBStateConvertUtils.stateToObject(RewindFastforwardType.REWIND), is("REWIND"));
assertThat(InfluxDBStateConvertUtils.stateToObject(RewindFastforwardType.FASTFORWARD), is("FASTFORWARD"));
}
@ParameterizedTest
@ValueSource(strings = { "1.12", "25" })
public void convertDecimalToState(String number) {
@ -111,4 +134,21 @@ public class InfluxDBStateConvertUtilsTest {
ZonedDateTime.ofInstant(Instant.ofEpochMilli(val), ZoneId.systemDefault()));
assertThat(InfluxDBStateConvertUtils.objectToState(val, item), equalTo(expected));
}
@Test
public void convertImageToState() {
ImageItem item = new ImageItem("name");
RawType type = new RawType(new byte[] { 0x64, 0x66, 0x55, 0x00, 0x34 }, RawType.DEFAULT_MIME_TYPE);
assertThat(InfluxDBStateConvertUtils.objectToState("data:application/octet-stream;base64,ZGZVADQ=", item),
is(type));
}
@Test
public void convertPlayerToState() {
PlayerItem item = new PlayerItem("name");
assertThat(InfluxDBStateConvertUtils.objectToState("PLAY", item), is(PlayPauseType.PLAY));
assertThat(InfluxDBStateConvertUtils.objectToState("PAUSE", item), is(PlayPauseType.PAUSE));
assertThat(InfluxDBStateConvertUtils.objectToState("REWIND", item), is(RewindFastforwardType.REWIND));
assertThat(InfluxDBStateConvertUtils.objectToState("FASTFORWARD", item), is(RewindFastforwardType.FASTFORWARD));
}
}