use of org.openhab.core.library.types.OpenClosedType 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.OpenClosedType in project openhab1-addons by openhab.
the class OpenSprinklerBinding method internalReceiveCommand.
/**
* @{inheritDoc}
*/
@Override
protected void internalReceiveCommand(String itemName, Command command) {
if (command instanceof OnOffType) {
final OnOffType switchCommand = (OnOffType) command;
final OpenSprinklerBindingProvider bindingProvider = findFirstMatchingBindingProvider(itemName, command);
final int station = bindingProvider.getStationNumber(itemName);
if (station < 0 || station >= numberOfStations) {
logger.warn("Station " + station + " is not in the valid [" + 0 + ".." + numberOfStations + "] range");
return;
}
switch(switchCommand) {
case ON:
openSprinkler.openStation(station);
break;
case OFF:
openSprinkler.closeStation(station);
break;
}
return;
}
if (command instanceof OpenClosedType) {
// abort processing
return;
}
logger.debug("Provided command " + command + " is not of type 'OnOffType' or 'OpenClosedType'");
}
Aggregations