Search in sources :

Example 6 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-addons by openhab.

the class InfluxDBPersistenceService method objectToState.

/**
   * Converts a value to a {@link State} which is suitable for the given {@link Item}. This is
   * needed for querying a {@link HistoricState}.
   * 
   * @param value to be converted to a {@link State}
   * @param itemName name of the {@link Item} to get the {@link State} for
   * @return the state of the item represented by the itemName parameter, 
   *         else the string value of the Object parameter
   */
private State objectToState(Object value, String itemName) {
    String valueStr = String.valueOf(value);
    if (itemRegistry != null) {
        try {
            Item item = itemRegistry.getItem(itemName);
            if (item instanceof NumberItem) {
                return new DecimalType(valueStr);
            } else if (item instanceof ColorItem) {
                return new HSBType(valueStr);
            } else if (item instanceof DimmerItem) {
                return new PercentType(valueStr);
            } else if (item instanceof SwitchItem) {
                return string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF) ? OnOffType.OFF : OnOffType.ON;
            } else if (item instanceof ContactItem) {
                return (string2DigitalValue(valueStr).equals(DIGITAL_VALUE_OFF)) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
            } else if (item instanceof RollershutterItem) {
                return new PercentType(valueStr);
            } else if (item instanceof DateTimeItem) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(new BigDecimal(valueStr).longValue());
                return new DateTimeType(calendar);
            } else {
                return new StringType(valueStr);
            }
        } catch (ItemNotFoundException e) {
            logger.warn("Could not find item '{}' in registry", itemName);
        }
    }
    // just return a StringType as a fallback
    return new StringType(valueStr);
}
Also used : StringType(org.openhab.core.library.types.StringType) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) HistoricItem(org.openhab.core.persistence.HistoricItem) NumberItem(org.openhab.core.library.items.NumberItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException)

Example 7 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-addons by openhab.

the class JdbcBaseDAO method getState.

/*****************
     * H E L P E R S *
     *****************/
protected State getState(Item item, Object v) {
    String clazz = v.getClass().getSimpleName();
    logger.debug("JDBC::ItemResultHandler::handleResult getState value = '{}', getClass = '{}', clazz = '{}'", v.toString(), v.getClass(), clazz);
    if (item instanceof NumberItem) {
        String it = getSqlTypes().get("NUMBERITEM");
        if (it.toUpperCase().contains("DOUBLE")) {
            return new DecimalType(((Number) v).doubleValue());
        } else if (it.toUpperCase().contains("DECIMAL") || it.toUpperCase().contains("NUMERIC")) {
            return new DecimalType((BigDecimal) v);
        } else if (it.toUpperCase().contains("INT")) {
            return new DecimalType(((Integer) v).intValue());
        }
        return DecimalType.valueOf(((String) v).toString());
    } else if (item instanceof ColorItem) {
        return HSBType.valueOf(((String) v).toString());
    } else if (item instanceof DimmerItem) {
        return new PercentType(objectAsInteger(v));
    } else if (item instanceof SwitchItem) {
        return OnOffType.valueOf(((String) v).toString().trim());
    } else if (item instanceof ContactItem) {
        return OpenClosedType.valueOf(((String) v).toString().trim());
    } else if (item instanceof RollershutterItem) {
        return new PercentType(objectAsInteger(v));
    } else if (item instanceof DateTimeItem) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(objectAsLong(v));
        return new DateTimeType(calendar);
    } else if (item instanceof StringItem) {
        return StringType.valueOf(((String) v).toString());
    } else {
        // Call, Location, String
        return StringType.valueOf(((String) v).toString());
    }
}
Also used : ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) DecimalType(org.openhab.core.library.types.DecimalType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 8 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-addons by openhab.

the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeLocalWithDateTimeItem.

@Test
public void testDateTimeTypeLocalWithDateTimeItem() throws IOException {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeZone(TimeZone.getTimeZone("GMT+03:00"));
    // GMT: Sun, 17 Jul 2016 16:38:07.050 GMT
    calendar.setTimeInMillis(1468773487050L);
    DynamoDBItem<?> dbitem = testStateGeneric(new DateTimeType(calendar), "2016-07-17T16:38:07.050Z");
    // when deserializing data, we get the date in UTC Calendar
    Calendar expectedCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    expectedCal.setTimeInMillis(1468773487050L);
    testAsHistoricGeneric(dbitem, new DateTimeItem("foo"), new DateTimeType(expectedCal));
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Test(org.junit.Test)

Example 9 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-addons by openhab.

the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeWithDateTimeItem.

@Test
public void testDateTimeTypeWithDateTimeItem() throws IOException {
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    calendar.set(2016, Calendar.MAY, 1, 13, 46, 0);
    calendar.set(Calendar.MILLISECOND, 50);
    DynamoDBItem<?> dbitem = testStateGeneric(new DateTimeType(calendar), "2016-05-01T13:46:00.050Z");
    testAsHistoricGeneric(dbitem, new DateTimeItem("foo"), new DateTimeType(calendar));
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Test(org.junit.Test)

Example 10 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-addons by openhab.

the class BaseIntegrationTest method initService.

@BeforeClass
public static void initService() throws InterruptedException {
    items.put("dimmer", new DimmerItem("dimmer"));
    items.put("number", new NumberItem("number"));
    items.put("string", new StringItem("string"));
    items.put("switch", new SwitchItem("switch"));
    items.put("contact", new ContactItem("contact"));
    items.put("color", new ColorItem("color"));
    items.put("rollershutter", new RollershutterItem("rollershutter"));
    items.put("datetime", new DateTimeItem("datetime"));
    items.put("call", new CallItem("call"));
    items.put("location", new LocationItem("location"));
    service = new DynamoDBPersistenceService();
    service.setItemRegistry(new ItemRegistry() {

        @Override
        public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }

        @Override
        public boolean isValidItemName(String itemName) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems(String pattern) {
            throw new NotImplementedException();
        }

        @Override
        public Collection<Item> getItems() {
            throw new NotImplementedException();
        }

        @Override
        public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
            throw new NotImplementedException();
        }

        @Override
        public Item getItem(String name) throws ItemNotFoundException {
            Item item = items.get(name);
            if (item == null) {
                throw new ItemNotFoundException(name);
            }
            return item;
        }

        @Override
        public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
            throw new NotImplementedException();
        }
    });
    HashMap<String, Object> config = new HashMap<>();
    config.put("region", System.getProperty("DYNAMODBTEST_REGION"));
    config.put("accessKey", System.getProperty("DYNAMODBTEST_ACCESS"));
    config.put("secretKey", System.getProperty("DYNAMODBTEST_SECRET"));
    config.put("tablePrefix", "dynamodb-integration-tests-");
    for (Entry<String, Object> entry : config.entrySet()) {
        if (entry.getValue() == null) {
            logger.warn(String.format("Expecting %s to have value for integration tests. Integration tests will be skipped", entry.getKey()));
            service = null;
            return;
        }
    }
    service.activate(null, config);
    // Clear data
    for (String table : new String[] { "dynamodb-integration-tests-bigdecimal", "dynamodb-integration-tests-string" }) {
        try {
            service.getDb().getDynamoClient().deleteTable(table);
            service.getDb().getDynamoDB().getTable(table).waitForDelete();
        } catch (ResourceNotFoundException e) {
        }
    }
}
Also used : LocationItem(org.openhab.core.library.items.LocationItem) HashMap(java.util.HashMap) NotImplementedException(org.apache.commons.lang.NotImplementedException) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) ItemRegistry(org.openhab.core.items.ItemRegistry) DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) CallItem(org.openhab.library.tel.items.CallItem) Item(org.openhab.core.items.Item) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) LocationItem(org.openhab.core.library.items.LocationItem) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) ItemRegistryChangeListener(org.openhab.core.items.ItemRegistryChangeListener) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) ItemNotUniqueException(org.openhab.core.items.ItemNotUniqueException) StringItem(org.openhab.core.library.items.StringItem) NumberItem(org.openhab.core.library.items.NumberItem) Collection(java.util.Collection) CallItem(org.openhab.library.tel.items.CallItem) ItemNotFoundException(org.openhab.core.items.ItemNotFoundException) BeforeClass(org.junit.BeforeClass)

Aggregations

DateTimeItem (org.openhab.core.library.items.DateTimeItem)12 Calendar (java.util.Calendar)10 DateTimeType (org.openhab.core.library.types.DateTimeType)10 NumberItem (org.openhab.core.library.items.NumberItem)9 DecimalType (org.openhab.core.library.types.DecimalType)8 ColorItem (org.openhab.core.library.items.ColorItem)7 ContactItem (org.openhab.core.library.items.ContactItem)7 DimmerItem (org.openhab.core.library.items.DimmerItem)7 RollershutterItem (org.openhab.core.library.items.RollershutterItem)7 SwitchItem (org.openhab.core.library.items.SwitchItem)7 StringType (org.openhab.core.library.types.StringType)7 PercentType (org.openhab.core.library.types.PercentType)6 HSBType (org.openhab.core.library.types.HSBType)5 Date (java.util.Date)4 Test (org.junit.Test)4 Item (org.openhab.core.items.Item)4 StringItem (org.openhab.core.library.items.StringItem)4 State (org.openhab.core.types.State)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)3 HistoricItem (org.openhab.core.persistence.HistoricItem)3