Search in sources :

Example 41 with DateTimeType

use of org.openhab.core.library.types.DateTimeType 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 42 with DateTimeType

use of org.openhab.core.library.types.DateTimeType 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 43 with DateTimeType

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

the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeWithStringItem.

@Test
public void testDateTimeTypeWithStringItem() 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 StringItem("foo"), new StringType("2016-05-01T13:46:00.050Z"));
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) StringItem(org.openhab.core.library.items.StringItem) Test(org.junit.Test)

Example 44 with DateTimeType

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

the class AbstractDynamoDBItemSerializationTest method testDateTimeTypeLocalWithStringItem.

@Test
public void testDateTimeTypeLocalWithStringItem() 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");
    testAsHistoricGeneric(dbitem, new StringItem("foo"), new StringType("2016-07-17T16:38:07.050Z"));
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) StringItem(org.openhab.core.library.items.StringItem) Test(org.junit.Test)

Example 45 with DateTimeType

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

the class MongoDBPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    if (!initialized) {
        return Collections.emptyList();
    }
    if (!isConnected()) {
        connectToDatabase();
    }
    if (!isConnected()) {
        return Collections.emptyList();
    }
    String name = filter.getItemName();
    Item item = getItem(name);
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    DBObject query = new BasicDBObject();
    if (filter.getItemName() != null) {
        query.put(FIELD_ITEM, filter.getItemName());
    }
    if (filter.getState() != null && filter.getOperator() != null) {
        String op = convertOperator(filter.getOperator());
        Object value = convertValue(filter.getState());
        query.put(FIELD_VALUE, new BasicDBObject(op, value));
    }
    if (filter.getBeginDate() != null) {
        query.put(FIELD_TIMESTAMP, new BasicDBObject("$gte", filter.getBeginDate()));
    }
    if (filter.getEndDate() != null) {
        query.put(FIELD_TIMESTAMP, new BasicDBObject("$lte", filter.getEndDate()));
    }
    Integer sortDir = (filter.getOrdering() == Ordering.ASCENDING) ? 1 : -1;
    DBCursor cursor = this.mongoCollection.find(query).sort(new BasicDBObject(FIELD_TIMESTAMP, sortDir)).skip(filter.getPageNumber() * filter.getPageSize()).limit(filter.getPageSize());
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        final State state;
        if (item instanceof NumberItem) {
            state = new DecimalType(obj.getDouble(FIELD_VALUE));
        } else if (item instanceof DimmerItem) {
            state = new PercentType(obj.getInt(FIELD_VALUE));
        } else if (item instanceof SwitchItem) {
            state = OnOffType.valueOf(obj.getString(FIELD_VALUE));
        } else if (item instanceof ContactItem) {
            state = OpenClosedType.valueOf(obj.getString(FIELD_VALUE));
        } else if (item instanceof RollershutterItem) {
            state = new PercentType(obj.getInt(FIELD_VALUE));
        } else if (item instanceof ColorItem) {
            state = new HSBType(obj.getString(FIELD_VALUE));
        } else if (item instanceof DateTimeItem) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(obj.getDate(FIELD_VALUE));
            state = new DateTimeType(cal);
        } else {
            state = new StringType(obj.getString(FIELD_VALUE));
        }
        items.add(new MongoDBItem(name, state, obj.getDate(FIELD_TIMESTAMP)));
    }
    return items;
}
Also used : StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) ColorItem(org.openhab.core.library.items.ColorItem) DateTimeItem(org.openhab.core.library.items.DateTimeItem) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) DimmerItem(org.openhab.core.library.items.DimmerItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) HistoricItem(org.openhab.core.persistence.HistoricItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) 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) DecimalType(org.openhab.core.library.types.DecimalType) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

DateTimeType (org.openhab.core.library.types.DateTimeType)49 Calendar (java.util.Calendar)37 DecimalType (org.openhab.core.library.types.DecimalType)30 StringType (org.openhab.core.library.types.StringType)29 State (org.openhab.core.types.State)16 DateTimeItem (org.openhab.core.library.items.DateTimeItem)12 PercentType (org.openhab.core.library.types.PercentType)12 NumberItem (org.openhab.core.library.items.NumberItem)11 BigDecimal (java.math.BigDecimal)10 OnOffType (org.openhab.core.library.types.OnOffType)9 ContactItem (org.openhab.core.library.items.ContactItem)8 DimmerItem (org.openhab.core.library.items.DimmerItem)8 StringItem (org.openhab.core.library.items.StringItem)8 HSBType (org.openhab.core.library.types.HSBType)8 Test (org.junit.Test)7 SwitchItem (org.openhab.core.library.items.SwitchItem)7 Date (java.util.Date)6 ColorItem (org.openhab.core.library.items.ColorItem)6 RollershutterItem (org.openhab.core.library.items.RollershutterItem)6 ArrayList (java.util.ArrayList)5