Search in sources :

Example 1 with DateTimeType

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

the class AbstractDevice method getObjState.

/**
     * Given the context kept in the {@link GaradgetSubscriber}, return the most
     * appropriate {@link State} for the given value.
     *
     * @param value
     *            the value to convert into a {@link State}
     * @param subscriber
     *            the context of the subscriber to use to return an accepted {@link State}
     * @return
     *         an accepted {@link State} for the given value
     */
public State getObjState(Object value, GaradgetSubscriber subscriber) {
    if (value == null) {
        return UnDefType.NULL;
    } else if (value instanceof Boolean) {
        for (Class<? extends State> dataType : subscriber.getAcceptedDataTypes()) {
            if (dataType == OnOffType.class) {
                return (Boolean) value ? OnOffType.ON : OnOffType.OFF;
            } else if (dataType == OpenClosedType.class) {
                return (Boolean) value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
            }
        }
    } else if (value instanceof Date) {
        for (Class<? extends State> dataType : subscriber.getAcceptedDataTypes()) {
            if (dataType == DateTimeType.class) {
                Calendar cal = Calendar.getInstance();
                cal.setTime((Date) value);
                return new DateTimeType(cal);
            }
        }
    }
    return TypeParser.parseState(subscriber.getAcceptedDataTypes(), value.toString());
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) Calendar(java.util.Calendar) Date(java.util.Date)

Example 2 with DateTimeType

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

the class HeatmiserThermostat method getHolidayTime.

public State getHolidayTime(Class<? extends Item> itemType) {
    if (itemType == SwitchItem.class) {
        return dcbHolidayTime > 0 ? OnOffType.ON : OnOffType.OFF;
    }
    // Return a date with the end time
    Calendar now = Calendar.getInstance();
    now.set(Calendar.MINUTE, 0);
    now.set(Calendar.SECOND, 0);
    now.set(Calendar.MILLISECOND, 0);
    now.add(Calendar.HOUR, dcbHolidayTime);
    return new DateTimeType(now);
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar)

Example 3 with DateTimeType

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

the class HeatmiserThermostat method getHoldTime.

public State getHoldTime(Class<? extends Item> itemType) {
    if (itemType == SwitchItem.class) {
        return dcbHoldTime > 0 ? OnOffType.ON : OnOffType.OFF;
    }
    // Return a date with the end time
    Calendar now = Calendar.getInstance();
    now.add(Calendar.MINUTE, dcbHoldTime);
    return new DateTimeType(now);
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar)

Example 4 with DateTimeType

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

the class NestBinding method createState.

/**
     * Creates an openHAB {@link State} in accordance to the class of the given {@code propertyValue}. Currently
     * {@link Date}, {@link BigDecimal}, {@link Temperature} and {@link Boolean} are handled explicitly. All other
     * {@code dataTypes} are mapped to {@link StringType}.
     * <p>
     * If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be returned.
     *
     * Copied/adapted from the Koubachi binding.
     *
     * @param propertyValue
     *
     * @return the new {@link State} in accordance with {@code dataType}. Will never be {@code null}.
     */
private State createState(Object propertyValue) {
    if (propertyValue == null) {
        return UnDefType.NULL;
    }
    Class<?> dataType = propertyValue.getClass();
    if (Date.class.isAssignableFrom(dataType)) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime((Date) propertyValue);
        return new DateTimeType(calendar);
    } else if (Integer.class.isAssignableFrom(dataType)) {
        return new DecimalType((Integer) propertyValue);
    } else if (BigDecimal.class.isAssignableFrom(dataType)) {
        return new DecimalType((BigDecimal) propertyValue);
    } else if (Boolean.class.isAssignableFrom(dataType)) {
        if ((Boolean) propertyValue) {
            return OnOffType.ON;
        } else {
            return OnOffType.OFF;
        }
    } else {
        return new StringType(propertyValue.toString());
    }
}
Also used : 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)

Example 5 with DateTimeType

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

the class FatekDateTimeItem method getState.

@Override
public State getState(Map<Reg, RegValue> response) {
    Calendar cal = Calendar.getInstance();
    if (regSec == null) {
        // calculate some value
        int y = year.getValue(response);
        if (y < 50) {
            y += 2000;
        } else if (y < 100) {
            y += 1900;
        }
        int m = month.getValue(response) - 1;
        cal.clear();
        cal.set(y, m, day.getValue(response), hour.getValue(response), minute.getValue(response), second.getValue(response));
    } else {
        long value = response.get(regSec).longValue() * 1000;
        if (factor != null) {
            value = factor.multiply(BigDecimal.valueOf(value)).longValue();
        }
        if (relative) {
            value = System.currentTimeMillis() - value;
        }
        cal.setTimeInMillis(value);
        cal.set(Calendar.MILLISECOND, 0);
    }
    return new DateTimeType(cal);
}
Also used : DateTimeType(org.openhab.core.library.types.DateTimeType) Calendar(java.util.Calendar)

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