From 977b29a7b09b8d0950e202b53c43f88fb6baf789 Mon Sep 17 00:00:00 2001 From: Theo Weiss Date: Sat, 28 Feb 2015 20:26:47 +0100 Subject: [PATCH] Influxdb bugfix for issue #2208 (restoring switch or contact state) --- .../internal/InfluxDBPersistenceService.java | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/bundles/persistence/org.openhab.persistence.influxdb/java/org/openhab/persistence/influxdb/internal/InfluxDBPersistenceService.java b/bundles/persistence/org.openhab.persistence.influxdb/java/org/openhab/persistence/influxdb/internal/InfluxDBPersistenceService.java index 9b86925ca1f..2e434be14e0 100644 --- a/bundles/persistence/org.openhab.persistence.influxdb/java/org/openhab/persistence/influxdb/internal/InfluxDBPersistenceService.java +++ b/bundles/persistence/org.openhab.persistence.influxdb/java/org/openhab/persistence/influxdb/internal/InfluxDBPersistenceService.java @@ -473,9 +473,13 @@ private State objectToState(Object value, String itemName) { try { Item item = itemRegistry.getItem(itemName); if (item instanceof SwitchItem && !(item instanceof DimmerItem)) { - return valueStr.equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON; + return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) + ? OnOffType.OFF + : OnOffType.ON; } else if (item instanceof ContactItem) { - return valueStr.equals(DIGITAL_VALUE_OFF) ? OpenClosedType.CLOSED : OpenClosedType.OPEN; + return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) + ? OpenClosedType.CLOSED + : OpenClosedType.OPEN; } } catch (ItemNotFoundException e) { logger.warn("Could not find item '{}' in registry", itemName); @@ -485,4 +489,22 @@ private State objectToState(Object value, String itemName) { return new DecimalType(valueStr); } + /** + * Maps a string value which expresses a {@link BigDecimal.ZERO } to DIGITAL_VALUE_OFF, all others + * to DIGITAL_VALUE_ON + * + * @param value to be mapped + * @return + */ + private String string2DigitalValue(String value) { + BigDecimal num = new BigDecimal(value); + if (num.compareTo(BigDecimal.ZERO) == 0) { + logger.trace("digitalvalue {}", DIGITAL_VALUE_OFF); + return DIGITAL_VALUE_OFF; + } else { + logger.trace("digitalvalue {}", DIGITAL_VALUE_ON); + return DIGITAL_VALUE_ON; + } + } + }