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());
}
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);
}
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);
}
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());
}
}
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);
}
Aggregations