Search in sources :

Example 1 with StringListType

use of org.openhab.core.library.types.StringListType in project openhab-addons by openhab.

the class JpaHistoricItem method fromPersistedItem.

/**
 * Converts the string value of the persisted item to the state of a HistoricItem.
 *
 * @param pItem the persisted JpaPersistentItem
 * @param item the source reference Item
 * @return historic item
 */
public static HistoricItem fromPersistedItem(JpaPersistentItem pItem, Item item) {
    State state;
    if (item instanceof NumberItem) {
        state = new DecimalType(Double.valueOf(pItem.getValue()));
    } else if (item instanceof DimmerItem) {
        state = new PercentType(Integer.valueOf(pItem.getValue()));
    } else if (item instanceof SwitchItem) {
        state = OnOffType.valueOf(pItem.getValue());
    } else if (item instanceof ContactItem) {
        state = OpenClosedType.valueOf(pItem.getValue());
    } else if (item instanceof RollershutterItem) {
        state = PercentType.valueOf(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        state = new DateTimeType(ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(pItem.getValue())), ZoneId.systemDefault()));
    } else if (item instanceof LocationItem) {
        PointType pType = null;
        String[] comps = pItem.getValue().split(";");
        if (comps.length >= 2) {
            pType = new PointType(new DecimalType(comps[0]), new DecimalType(comps[1]));
            if (comps.length == 3) {
                pType.setAltitude(new DecimalType(comps[2]));
            }
        }
        state = pType;
    } else if (item instanceof StringListType) {
        state = new StringListType(pItem.getValue());
    } else {
        state = new StringType(pItem.getValue());
    }
    return new JpaHistoricItem(item.getName(), state, pItem.getTimestamp());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) SwitchItem(org.openhab.core.library.items.SwitchItem) StringListType(org.openhab.core.library.types.StringListType)

Example 2 with StringListType

use of org.openhab.core.library.types.StringListType in project openhab-addons by openhab.

the class AbstractDynamoDBItem method asHistoricItem.

@Override
@Nullable
public HistoricItem asHistoricItem(final Item item, @Nullable Unit<?> targetUnit) {
    final State deserializedState;
    if (this.getState() == null) {
        return null;
    }
    try {
        deserializedState = accept(new DynamoDBItemVisitor<@Nullable State>() {

            @Override
            @Nullable
            public State visit(DynamoDBStringItem dynamoStringItem) {
                String stringState = dynamoStringItem.getState();
                if (stringState == null) {
                    return null;
                }
                if (item instanceof ColorItem) {
                    return new HSBType(stringState);
                } else if (item instanceof LocationItem) {
                    return new PointType(stringState);
                } else if (item instanceof PlayerItem) {
                    // Backwards-compatibility with legacy schema. New schema uses DynamoDBBigDecimalItem
                    try {
                        return PlayPauseType.valueOf(stringState);
                    } catch (IllegalArgumentException e) {
                        return RewindFastforwardType.valueOf(stringState);
                    }
                } else if (item instanceof DateTimeItem) {
                    try {
                        // We convert to default/local timezone for user convenience (e.g. display)
                        return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState).withZoneSameInstant(ZoneId.systemDefault()));
                    } catch (DateTimeParseException e) {
                        logger.warn("Failed to parse {} as date. Outputting UNDEF instead", stringState);
                        return UnDefType.UNDEF;
                    }
                } else if (item instanceof CallItem) {
                    String parts = stringState;
                    String[] strings = parts.split(",");
                    String orig = strings[0];
                    String dest = strings[1];
                    return new StringListType(orig, dest);
                } else {
                    return new StringType(dynamoStringItem.getState());
                }
            }

            @Override
            @Nullable
            public State visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
                BigDecimal numberState = dynamoBigDecimalItem.getState();
                if (numberState == null) {
                    return null;
                }
                if (item instanceof NumberItem) {
                    NumberItem numberItem = ((NumberItem) item);
                    Unit<? extends Quantity<?>> unit = targetUnit == null ? numberItem.getUnit() : targetUnit;
                    if (unit != null) {
                        return new QuantityType<>(numberState, unit);
                    } else {
                        return new DecimalType(numberState);
                    }
                } else if (item instanceof DimmerItem) {
                    // % values have been stored as-is
                    return new PercentType(numberState);
                } else if (item instanceof SwitchItem) {
                    return numberState.compareTo(BigDecimal.ZERO) != 0 ? OnOffType.ON : OnOffType.OFF;
                } else if (item instanceof ContactItem) {
                    return numberState.compareTo(BigDecimal.ZERO) != 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
                } else if (item instanceof RollershutterItem) {
                    // Percents and UP/DOWN have been stored % values (not fractional)
                    return new PercentType(numberState);
                } else if (item instanceof PlayerItem) {
                    if (numberState.equals(PLAY_BIGDECIMAL)) {
                        return PlayPauseType.PLAY;
                    } else if (numberState.equals(PAUSE_BIGDECIMAL)) {
                        return PlayPauseType.PAUSE;
                    } else if (numberState.equals(FAST_FORWARD_BIGDECIMAL)) {
                        return RewindFastforwardType.FASTFORWARD;
                    } else if (numberState.equals(REWIND_BIGDECIMAL)) {
                        return RewindFastforwardType.REWIND;
                    } else {
                        throw new IllegalArgumentException("Unknown serialized value");
                    }
                } else {
                    logger.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                    return new StringType(numberState.toString());
                }
            }
        });
        if (deserializedState == null) {
            return null;
        }
        return new DynamoDBHistoricItem(getName(), deserializedState, getTime());
    } catch (Exception e) {
        logger.trace("Failed to convert state '{}' to item {} {}: {} {}. Data persisted with incompatible item.", this.state, item.getClass().getSimpleName(), item.getName(), e.getClass().getSimpleName(), e.getMessage());
        return null;
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) PlayerItem(org.openhab.core.library.items.PlayerItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) StringListType(org.openhab.core.library.types.StringListType) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) DateTimeParseException(java.time.format.DateTimeParseException) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeParseException(java.time.format.DateTimeParseException) DateTimeType(org.openhab.core.library.types.DateTimeType) QuantityType(org.openhab.core.library.types.QuantityType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.core.library.items.CallItem) Nullable(org.eclipse.jdt.annotation.Nullable) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 3 with StringListType

use of org.openhab.core.library.types.StringListType in project addons by smarthomej.

the class PhonebookProfile method onStateUpdateFromHandler.

@Override
public void onStateUpdateFromHandler(State state) {
    if (state instanceof UnDefType) {
        // we cannot adjust UNDEF or NULL values, thus we simply apply them without reporting an error or warning
        callback.sendUpdate(state);
    }
    if (state instanceof StringType) {
        Optional<String> match = resolveNumber(state.toString());
        State newState = Objects.requireNonNull(match.map(name -> (State) new StringType(name)).orElse(state));
        if (newState.equals(state)) {
            logger.debug("Number '{}' not found in phonebook '{}' from provider '{}'", state, phonebookName, thingUID);
        }
        callback.sendUpdate(newState);
    } else if (state instanceof StringListType) {
        StringListType stringList = (StringListType) state;
        try {
            String phoneNumber = stringList.getValue(phoneNumberIndex);
            Optional<String> match = resolveNumber(phoneNumber);
            final State newState;
            if (match.isPresent()) {
                newState = new StringType(match.get());
            } else {
                logger.debug("Number '{}' not found in phonebook '{}' from provider '{}'", phoneNumber, phonebookName, thingUID);
                newState = new StringType(phoneNumber);
            }
            callback.sendUpdate(newState);
        } catch (IllegalArgumentException e) {
            logger.debug("StringListType does not contain a number at index {}", phoneNumberIndex);
        }
    }
}
Also used : Optional(java.util.Optional) StringType(org.openhab.core.library.types.StringType) UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) StringListType(org.openhab.core.library.types.StringListType)

Example 4 with StringListType

use of org.openhab.core.library.types.StringListType in project openhab-core by openhab.

the class CallItemTest method testSetStringListType.

@Test
public void testSetStringListType() {
    StringListType callType1 = new StringListType("0699222222", "0179999998");
    CallItem callItem1 = new CallItem("testItem");
    callItem1.setState(callType1);
    assertEquals(callItem1.toString(), "testItem (Type=CallItem, State=0699222222,0179999998, Label=null, Category=null)");
    callType1 = new StringListType("0699222222,0179999998");
    callItem1.setState(callType1);
    assertEquals(callItem1.toString(), "testItem (Type=CallItem, State=0699222222,0179999998, Label=null, Category=null)");
}
Also used : StringListType(org.openhab.core.library.types.StringListType) Test(org.junit.jupiter.api.Test)

Example 5 with StringListType

use of org.openhab.core.library.types.StringListType in project openhab-core by openhab.

the class StateUtil method getAllStates.

public static List<State> getAllStates() {
    List<State> states = new LinkedList<>();
    DateTimeType dateTime = new DateTimeType();
    states.add(dateTime);
    DecimalType decimal = new DecimalType(23);
    states.add(decimal);
    PercentType percent = new PercentType(50);
    states.add(percent);
    HSBType hsb = new HSBType("50,75,42");
    states.add(hsb);
    states.add(OnOffType.ON);
    states.add(OnOffType.OFF);
    states.add(OpenClosedType.OPEN);
    states.add(OpenClosedType.CLOSED);
    states.add(PlayPauseType.PLAY);
    states.add(PlayPauseType.PAUSE);
    PointType point = new PointType("42.23,23.5");
    states.add(point);
    RawType raw = new RawType(new byte[0], "application/octet-stream");
    states.add(raw);
    states.add(RewindFastforwardType.REWIND);
    states.add(RewindFastforwardType.FASTFORWARD);
    StringListType stringList = new StringListType(new String[] { "foo", "bar" });
    states.add(stringList);
    StringType string = new StringType("foo");
    states.add(string);
    states.add(UnDefType.NULL);
    states.add(UnDefType.UNDEF);
    states.add(UpDownType.UP);
    states.add(UpDownType.DOWN);
    QuantityType<Temperature> quantityType = new QuantityType<>("12 °C");
    states.add(quantityType);
    return states;
}
Also used : Temperature(javax.measure.quantity.Temperature) StringType(org.openhab.core.library.types.StringType) PercentType(org.openhab.core.library.types.PercentType) LinkedList(java.util.LinkedList) DateTimeType(org.openhab.core.library.types.DateTimeType) QuantityType(org.openhab.core.library.types.QuantityType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) PointType(org.openhab.core.library.types.PointType) RawType(org.openhab.core.library.types.RawType) HSBType(org.openhab.core.library.types.HSBType) StringListType(org.openhab.core.library.types.StringListType)

Aggregations

StringListType (org.openhab.core.library.types.StringListType)8 StringType (org.openhab.core.library.types.StringType)6 State (org.openhab.core.types.State)5 DateTimeType (org.openhab.core.library.types.DateTimeType)4 DecimalType (org.openhab.core.library.types.DecimalType)4 PercentType (org.openhab.core.library.types.PercentType)4 PointType (org.openhab.core.library.types.PointType)4 HSBType (org.openhab.core.library.types.HSBType)3 BigDecimal (java.math.BigDecimal)2 Optional (java.util.Optional)2 Nullable (org.eclipse.jdt.annotation.Nullable)2 CallItem (org.openhab.core.library.items.CallItem)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DateTimeItem (org.openhab.core.library.items.DateTimeItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 LocationItem (org.openhab.core.library.items.LocationItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 QuantityType (org.openhab.core.library.types.QuantityType)2