Search in sources :

Example 16 with PercentType

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

the class AbstractDynamoDBItem method asHistoricItem.

/*
     * (non-Javadoc)
     *
     * @see org.openhab.persistence.dynamodb.internal.DynamoItem#asHistoricItem(org.openhab.core.items.Item)
     */
@Override
public HistoricItem asHistoricItem(final Item item) {
    final State[] state = new State[1];
    accept(new DynamoDBItemVisitor() {

        @Override
        public void visit(DynamoDBStringItem dynamoStringItem) {
            if (item instanceof ColorItem) {
                state[0] = new HSBType(dynamoStringItem.getState());
            } else if (item instanceof LocationItem) {
                state[0] = new PointType(dynamoStringItem.getState());
            } else if (item instanceof DateTimeItem) {
                Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                try {
                    cal.setTime(DATEFORMATTER.parse(dynamoStringItem.getState()));
                } catch (ParseException e) {
                    LOGGER.error("Failed to parse {} as date. Outputting UNDEF instead", dynamoStringItem.getState());
                    state[0] = UnDefType.UNDEF;
                }
                state[0] = new DateTimeType(cal);
            } else if (dynamoStringItem.getState().equals(UNDEFINED_PLACEHOLDER)) {
                state[0] = UnDefType.UNDEF;
            } else if (item instanceof CallItem) {
                String parts = dynamoStringItem.getState();
                String[] strings = parts.split("##");
                String dest = strings[0];
                String orig = strings[1];
                state[0] = new CallType(orig, dest);
            } else {
                state[0] = new StringType(dynamoStringItem.getState());
            }
        }

        @Override
        public void visit(DynamoDBBigDecimalItem dynamoBigDecimalItem) {
            if (item instanceof NumberItem) {
                state[0] = new DecimalType(dynamoBigDecimalItem.getState());
            } else if (item instanceof DimmerItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else if (item instanceof SwitchItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OnOffType.ON : OnOffType.OFF;
            } else if (item instanceof ContactItem) {
                state[0] = dynamoBigDecimalItem.getState().compareTo(BigDecimal.ONE) == 0 ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            } else if (item instanceof RollershutterItem) {
                state[0] = new PercentType(dynamoBigDecimalItem.getState());
            } else {
                LOGGER.warn("Not sure how to convert big decimal item {} to type {}. Using StringType as fallback", dynamoBigDecimalItem.getName(), item.getClass());
                state[0] = new StringType(dynamoBigDecimalItem.getState().toString());
            }
        }
    });
    return new DynamoDBHistoricItem(getName(), state[0], getTime());
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) StringType(org.openhab.core.library.types.StringType) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) Calendar(java.util.Calendar) ContactItem(org.openhab.core.library.items.ContactItem) PercentType(org.openhab.core.library.types.PercentType) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) State(org.openhab.core.types.State) PointType(org.openhab.core.library.types.PointType) DecimalType(org.openhab.core.library.types.DecimalType) CallItem(org.openhab.library.tel.items.CallItem) ParseException(java.text.ParseException)

Example 17 with PercentType

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

the class AbstractDynamoDBItemSerializationTest method testPercentTypeWithDimmerItem.

@Test
public void testPercentTypeWithDimmerItem() throws IOException {
    DynamoDBItem<?> dbitem = testStateGeneric(new PercentType(new BigDecimal(3.2)), new BigDecimal(3.2));
    testAsHistoricGeneric(dbitem, new DimmerItem("foo"), new PercentType(new BigDecimal(3.2)));
}
Also used : DimmerItem(org.openhab.core.library.items.DimmerItem) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 18 with PercentType

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

the class AbstractDynamoDBItemSerializationTest method testPercentTypeWithColorItem.

@Test
public void testPercentTypeWithColorItem() throws IOException {
    DynamoDBItem<?> dbitem = testStateGeneric(new PercentType(new BigDecimal(3.2)), new BigDecimal(3.2));
    testAsHistoricGeneric(dbitem, new ColorItem("foo"), new PercentType(new BigDecimal(3.2)));
}
Also used : ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 19 with PercentType

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

the class AbstractDynamoDBItemSerializationTest method testPercentTypeWithNumberItem.

@Test
public void testPercentTypeWithNumberItem() throws IOException {
    DynamoDBItem<?> dbitem = testStateGeneric(new PercentType(new BigDecimal(3.2)), new BigDecimal(3.2));
    // note: comes back as DecimalType instead of the original PercentType
    testAsHistoricGeneric(dbitem, new NumberItem("foo"), new DecimalType(new BigDecimal(3.2)));
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 20 with PercentType

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

the class AbstractDynamoDBItemSerializationTest method testOnOffTypeWithColorItem.

@Test
public void testOnOffTypeWithColorItem() throws IOException {
    DynamoDBItem<?> dbitemOff = testStateGeneric(OnOffType.OFF, BigDecimal.ZERO);
    testAsHistoricGeneric(dbitemOff, new ColorItem("foo"), new PercentType(BigDecimal.ZERO));
    DynamoDBItem<?> dbitemOn = testStateGeneric(OnOffType.ON, BigDecimal.ONE);
    testAsHistoricGeneric(dbitemOn, new ColorItem("foo"), new PercentType(BigDecimal.ONE));
}
Also used : ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) Test(org.junit.Test)

Aggregations

PercentType (org.openhab.core.library.types.PercentType)105 DecimalType (org.openhab.core.library.types.DecimalType)43 State (org.openhab.core.types.State)29 StringType (org.openhab.core.library.types.StringType)27 DimmerItem (org.openhab.core.library.items.DimmerItem)22 Test (org.junit.Test)21 HSBType (org.openhab.core.library.types.HSBType)19 OnOffType (org.openhab.core.library.types.OnOffType)19 RollershutterItem (org.openhab.core.library.items.RollershutterItem)17 NumberItem (org.openhab.core.library.items.NumberItem)16 BigDecimal (java.math.BigDecimal)14 SwitchItem (org.openhab.core.library.items.SwitchItem)13 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)13 ColorItem (org.openhab.core.library.items.ColorItem)11 DateTimeType (org.openhab.core.library.types.DateTimeType)11 Calendar (java.util.Calendar)9 ContactItem (org.openhab.core.library.items.ContactItem)9 UpDownType (org.openhab.core.library.types.UpDownType)9 StopMoveType (org.openhab.core.library.types.StopMoveType)7 DateTimeItem (org.openhab.core.library.items.DateTimeItem)6