Search in sources :

Example 1 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem in project openhab1-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 ColorItem) {
        state = new HSBType(pItem.getValue());
    } else if (item instanceof DateTimeItem) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date(Long.valueOf(pItem.getValue())));
        state = new DateTimeType(cal);
    } 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 CallItem) {
        state = new CallType(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) Calendar(java.util.Calendar) CallType(org.openhab.library.tel.types.CallType) ColorItem(org.openhab.core.library.items.ColorItem) PercentType(org.openhab.core.library.types.PercentType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) 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) CallItem(org.openhab.library.tel.items.CallItem) HSBType(org.openhab.core.library.types.HSBType) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 2 with DateTimeItem

use of org.openhab.core.library.items.DateTimeItem 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 3 with DateTimeItem

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

the class DateTimeItemIntegrationTest method storeData.

@BeforeClass
public static void storeData() throws InterruptedException {
    DateTimeItem item = (DateTimeItem) items.get(name);
    item.setState(state1);
    beforeStore = new Date();
    Thread.sleep(10);
    service.store(item);
    afterStore1 = new Date();
    Thread.sleep(10);
    item.setState(state2);
    service.store(item);
    Thread.sleep(10);
    afterStore2 = new Date();
    logger.info("Created item between {} and {}", AbstractDynamoDBItem.DATEFORMATTER.format(beforeStore), AbstractDynamoDBItem.DATEFORMATTER.format(afterStore1));
}
Also used : DateTimeItem(org.openhab.core.library.items.DateTimeItem) Date(java.util.Date) BeforeClass(org.junit.BeforeClass)

Example 4 with DateTimeItem

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

the class LightwaveRfBindingFunctionalTest method testWifiLinkStatusReceived.

@Test
public void testWifiLinkStatusReceived() throws Exception {
    String message = "*!{\"trans\":452,\"mac\":\"ab:cd:ef\",\"time\":1447712274,\"type\":\"hub\",\"prod\":\"wfl\",\"fw\":\"U2.91Y\"," + "\"uptime\":1386309,\"timeZone\":0,\"lat\":52.48,\"long\":-87.89,\"duskTime\":1447690400," + "\"dawnTime\":1447659083,\"tmrs\":0,\"evns\":1,\"run\":0,\"macs\":8,\"ip\":\"192.168.0.1\",\"devs\":0}";
    List<ItemConfigAndExpectedState> itemConfigAndExpectedStates = new LinkedList<ItemConfigAndExpectedState>();
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_IP"), "serial=wifilink,type=WIFILINK_IP", new StringType("192.168.0.1")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_FIRMWARE"), "serial=wifilink,type=WIFILINK_FIRMWARE", new StringType("U2.91Y")));
    Calendar duskCal = Calendar.getInstance();
    duskCal.setTime(new Date(1447690400000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("WIFILINK_DUSK_TIME"), "serial=wifilink,type=WIFILINK_DUSK_TIME", new DateTimeType(duskCal)));
    Calendar dawnCal = Calendar.getInstance();
    dawnCal.setTime(new Date(1447659083000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("WIFILINK_DAWN_TIME"), "serial=wifilink,type=WIFILINK_DAWN_TIME", new DateTimeType(dawnCal)));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("WIFILINK_UPTIME"), "serial=wifilink,type=WIFILINK_UPTIME", new DecimalType("1386309")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_LONGITUDE"), "serial=wifilink,type=WIFILINK_LONGITUDE", new StringType("-87.89")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("WIFILINK_LATITUDE"), "serial=wifilink,type=WIFILINK_LATITUDE", new StringType("52.48")));
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1447712274000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("TIME"), "serial=wifilink,type=UPDATETIME", new DateTimeType(cal)));
    testReceivingACommandAndVerify(itemConfigAndExpectedStates, message);
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) DecimalType(org.openhab.core.library.types.DecimalType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 5 with DateTimeItem

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

the class LightwaveRfBindingFunctionalTest method testHeatingInfoResponseReceived.

@Test
public void testHeatingInfoResponseReceived() throws Exception {
    String message = "*!{\"trans\":1506,\"mac\":\"03:02:71\",\"time\":1423850746,\"prod\":\"valve\",\"serial\":\"064402\",\"signal\":54,\"type\":\"temp\",\"batt\":2.99,\"ver\":56,\"state\":\"boost\",\"cTemp\":22.3,\"cTarg\":24.0,\"output\":100,\"nTarg\":20.0,\"nSlot\":\"18:45\",\"prof\":5}";
    List<ItemConfigAndExpectedState> itemConfigAndExpectedStates = new LinkedList<ItemConfigAndExpectedState>();
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("BATTERY"), "serial=064402,type=HEATING_BATTERY", new DecimalType("2.99")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("CURRENT_TEMP"), "serial=064402,type=HEATING_CURRENT_TEMP", new DecimalType("22.3")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("MODE"), "serial=064402,type=HEATING_MODE", new StringType("boost")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new StringItem("VERSION"), "serial=064402,type=VERSION", new StringType("56")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("SIGNAL"), "serial=064402,type=SIGNAL", new DecimalType("54")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("SET_TEMP"), "serial=064402,type=HEATING_SET_TEMP", new DecimalType("24.0")));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new NumberItem("OUTPUT"), "serial=064402,type=HEATING_OUTPUT", new DecimalType("100")));
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date(1423850746000L));
    itemConfigAndExpectedStates.add(new ItemConfigAndExpectedState(new DateTimeItem("TIME"), "serial=064402,type=UPDATETIME", new DateTimeType(cal)));
    testReceivingACommandAndVerify(itemConfigAndExpectedStates, message);
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) DateTimeType(org.openhab.core.library.types.DateTimeType) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) DecimalType(org.openhab.core.library.types.DecimalType) DateTimeItem(org.openhab.core.library.items.DateTimeItem) StringItem(org.openhab.core.library.items.StringItem) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

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