use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class PlugwiseBinding method createStateForType.
@SuppressWarnings("unchecked")
private State createStateForType(PlugwiseCommandType ctype, Object value) throws BindingConfigParseException {
Class<? extends Type> typeClass = ctype.getTypeClass();
// the logic below covers all possible command types and value types
if (typeClass == DecimalType.class && value instanceof Float) {
return new DecimalType((Float) value);
} else if (typeClass == DecimalType.class && value instanceof Double) {
return new DecimalType((Double) value);
} else if (typeClass == OnOffType.class && value instanceof Boolean) {
return ((Boolean) value).booleanValue() ? OnOffType.ON : OnOffType.OFF;
} else if (typeClass == DateTimeType.class && value instanceof Calendar) {
return new DateTimeType((Calendar) value);
} else if (typeClass == DateTimeType.class && value instanceof DateTime) {
return new DateTimeType(((DateTime) value).toCalendar(Locale.getDefault()));
} else if (typeClass == StringType.class && value instanceof String) {
return new StringType((String) value);
}
logger.debug("less efficient (generic) logic is applied for converting a Plugwise value " + "(command type class: {}, value class {})", typeClass.getName(), value.getClass().getName());
List<Class<? extends State>> stateTypeList = new ArrayList<Class<? extends State>>();
stateTypeList.add((Class<? extends State>) typeClass);
return TypeParser.parseState(stateTypeList, value.toString());
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class IhcDataConverter method convertCommandToResourceValue.
/**
* Convert openHAB data type to IHC data type.
*
* @param type
* openHAB data type
* @param value
*
* @param enumValues
*
* @return IHC data type
*/
public static WSResourceValue convertCommandToResourceValue(Type type, WSResourceValue value, ArrayList<IhcEnumValue> enumValues) {
if (type instanceof DecimalType) {
if (value instanceof WSFloatingPointValue) {
double newVal = ((DecimalType) type).doubleValue();
double max = ((WSFloatingPointValue) value).getMaximumValue();
double min = ((WSFloatingPointValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSFloatingPointValue) value).setFloatingPointValue(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(((DecimalType) type).intValue() > 0 ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = ((DecimalType) type).intValue();
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else if (value instanceof WSTimerValue) {
((WSTimerValue) value).setMilliseconds(((DecimalType) type).longValue());
} else if (value instanceof WSWeekdayValue) {
((WSWeekdayValue) value).setWeekdayNumber(((DecimalType) type).intValue());
} else {
throw new NumberFormatException("Can't convert DecimalType to " + value.getClass());
}
} else if (type instanceof OnOffType) {
if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(type == OnOffType.ON ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = type == OnOffType.ON ? 100 : 0;
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert OnOffType to " + value.getClass());
}
} else if (type instanceof OpenClosedType) {
((WSBooleanValue) value).setValue(type == OpenClosedType.OPEN ? true : false);
} else if (type instanceof DateTimeType) {
if (value instanceof WSDateValue) {
Calendar c = ((DateTimeType) type).getCalendar();
short year = (short) c.get(Calendar.YEAR);
byte month = (byte) (c.get(Calendar.MONTH) + 1);
byte day = (byte) c.get(Calendar.DAY_OF_MONTH);
((WSDateValue) value).setYear(year);
((WSDateValue) value).setMonth(month);
((WSDateValue) value).setDay(day);
} else if (value instanceof WSTimeValue) {
Calendar c = ((DateTimeType) type).getCalendar();
int hours = c.get(Calendar.HOUR_OF_DAY);
int minutes = c.get(Calendar.MINUTE);
int seconds = c.get(Calendar.SECOND);
((WSTimeValue) value).setHours(hours);
((WSTimeValue) value).setMinutes(minutes);
((WSTimeValue) value).setSeconds(seconds);
} else {
throw new NumberFormatException("Can't convert DateTimeItem to " + value.getClass());
}
} else if (type instanceof StringType) {
if (value instanceof WSEnumValue) {
boolean found = false;
for (IhcEnumValue item : enumValues) {
if (item.name.equals(type.toString())) {
((WSEnumValue) value).setEnumValueID(item.id);
((WSEnumValue) value).setEnumName(type.toString());
found = true;
break;
}
}
if (found == false) {
throw new NumberFormatException("Can't find enum value for string " + type.toString());
}
} else {
throw new NumberFormatException("Can't convert StringType to " + value.getClass());
}
} else if (type instanceof PercentType) {
if (value instanceof WSIntegerValue) {
int newVal = ((DecimalType) type).intValue();
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert PercentType to " + value.getClass());
}
} else if (type instanceof UpDownType) {
if (value instanceof WSBooleanValue) {
((WSBooleanValue) value).setValue(type == UpDownType.DOWN ? true : false);
} else if (value instanceof WSIntegerValue) {
int newVal = type == UpDownType.DOWN ? 100 : 0;
int max = ((WSIntegerValue) value).getMaximumValue();
int min = ((WSIntegerValue) value).getMinimumValue();
if (newVal >= min && newVal <= max) {
((WSIntegerValue) value).setInteger(newVal);
} else {
throw new NumberFormatException("Value is not between accetable limits (min=" + min + ", max=" + max + ")");
}
} else {
throw new NumberFormatException("Can't convert UpDownType to " + value.getClass());
}
} else {
throw new NumberFormatException("Can't convert " + type.getClass().toString());
}
return value;
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class IhcDataConverter method convertResourceValueToState.
/**
* Convert IHC data type to openHAB data type.
*
* @param itemType
* OpenHAB data type class
* @param value
* IHC data value
*
* @return openHAB {@link State}
*/
public static State convertResourceValueToState(Class<? extends Item> itemType, WSResourceValue value) throws NumberFormatException {
org.openhab.core.types.State state = UnDefType.UNDEF;
if (itemType == NumberItem.class) {
if (value.getClass() == WSFloatingPointValue.class) {
// state = new
// DecimalType(((WSFloatingPointValue)value).getFloatingPointValue());
// Controller might send floating point value with >10 decimals
// (22.299999237060546875), so round value to have max 2
// decimals
double d = ((WSFloatingPointValue) value).getFloatingPointValue();
BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
state = new DecimalType(bd);
} else if (value.getClass() == WSBooleanValue.class) {
state = new DecimalType(((WSBooleanValue) value).isValue() ? 1 : 0);
} else if (value.getClass() == WSIntegerValue.class) {
state = new DecimalType(((WSIntegerValue) value).getInteger());
} else if (value.getClass() == WSTimerValue.class) {
state = new DecimalType(((WSTimerValue) value).getMilliseconds());
} else if (value.getClass() == WSEnumValue.class) {
state = new DecimalType(((WSEnumValue) value).getEnumValueID());
} else if (value.getClass() == WSWeekdayValue.class) {
state = new DecimalType(((WSWeekdayValue) value).getWeekdayNumber());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
} else if (itemType == DimmerItem.class) {
if (value.getClass() == WSIntegerValue.class) {
state = new PercentType(((WSIntegerValue) value).getInteger());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
} else if (itemType == SwitchItem.class) {
if (value.getClass() == WSBooleanValue.class) {
if (((WSBooleanValue) value).isValue()) {
state = OnOffType.ON;
} else {
state = OnOffType.OFF;
}
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to SwitchItem");
}
} else if (itemType == ContactItem.class) {
if (value.getClass() == WSBooleanValue.class) {
if (((WSBooleanValue) value).isValue()) {
state = OpenClosedType.OPEN;
} else {
state = OpenClosedType.CLOSED;
}
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to ContactItem");
}
} else if (itemType == DateTimeItem.class) {
if (value.getClass() == WSDateValue.class) {
Calendar cal = WSDateTimeToCalendar((WSDateValue) value, null);
state = new DateTimeType(cal);
} else if (value.getClass() == WSTimeValue.class) {
Calendar cal = WSDateTimeToCalendar(null, (WSTimeValue) value);
state = new DateTimeType(cal);
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to DateTimeItem");
}
} else if (itemType == StringItem.class) {
if (value.getClass() == WSEnumValue.class) {
state = new StringType(((WSEnumValue) value).getEnumName());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to StringItem");
}
} else if (itemType == RollershutterItem.class) {
if (value.getClass() == WSIntegerValue.class) {
state = new PercentType(((WSIntegerValue) value).getInteger());
} else {
throw new NumberFormatException("Can't convert " + value.getClass().toString() + " to NumberItem");
}
}
return state;
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class LightwaveRfWifiLinkStatusMessage method getState.
@Override
public State getState(LightwaveRfType type) {
switch(type) {
case WIFILINK_IP:
return new StringType(ip);
case WIFILINK_FIRMWARE:
return new StringType(firmware);
case WIFILINK_DUSK_TIME:
Calendar calDusk = Calendar.getInstance();
calDusk.setTime(duskTime);
return new DateTimeType(calDusk);
case WIFILINK_DAWN_TIME:
Calendar calDawn = Calendar.getInstance();
calDawn.setTime(dawnTime);
return new DateTimeType(calDawn);
case WIFILINK_UPTIME:
return new DecimalType(uptime);
case WIFILINK_LONGITUDE:
return new StringType(longitude);
case WIFILINK_LATITUDE:
return new StringType(latitude);
case UPDATETIME:
Calendar cal = Calendar.getInstance();
cal.setTime(getTime());
return new DateTimeType(cal);
default:
return null;
}
}
use of org.openhab.core.library.types.DateTimeType in project openhab1-addons by openhab.
the class LightwaveRfWifiLinkStatusMessageTest method testGetState.
@Test
public void testGetState() throws Exception {
LightwaveRfWifiLinkStatusMessage message = new LightwaveRfWifiLinkStatusMessage(messageString);
Calendar dawnTime = Calendar.getInstance();
dawnTime.setTime(new Date(1447659083000L));
Calendar duskTime = Calendar.getInstance();
duskTime.setTime(new Date(1447690400000L));
assertEquals(new DateTimeType(dawnTime), message.getState(LightwaveRfType.WIFILINK_DAWN_TIME));
assertEquals(new DateTimeType(duskTime), message.getState(LightwaveRfType.WIFILINK_DUSK_TIME));
assertEquals(new StringType("52.48"), message.getState(LightwaveRfType.WIFILINK_LATITUDE));
assertEquals(new StringType("-87.89"), message.getState(LightwaveRfType.WIFILINK_LONGITUDE));
assertEquals(new StringType("U2.91Y"), message.getState(LightwaveRfType.WIFILINK_FIRMWARE));
assertEquals(new StringType("192.168.0.1"), message.getState(LightwaveRfType.WIFILINK_IP));
assertEquals(new DecimalType(1386309), message.getState(LightwaveRfType.WIFILINK_UPTIME));
}
Aggregations