Search in sources :

Example 1 with WSIntegerValue

use of org.openhab.binding.ihc.ws.datatypes.WSIntegerValue 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;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) DateTimeType(org.openhab.core.library.types.DateTimeType) OnOffType(org.openhab.core.library.types.OnOffType) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) WSDateValue(org.openhab.binding.ihc.ws.datatypes.WSDateValue) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Example 2 with WSIntegerValue

use of org.openhab.binding.ihc.ws.datatypes.WSIntegerValue 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;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) ContactItem(org.openhab.core.library.items.ContactItem) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) PercentType(org.openhab.core.library.types.PercentType) StringItem(org.openhab.core.library.items.StringItem) BigDecimal(java.math.BigDecimal) DateTimeType(org.openhab.core.library.types.DateTimeType) DimmerItem(org.openhab.core.library.items.DimmerItem) DecimalType(org.openhab.core.library.types.DecimalType) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Example 3 with WSIntegerValue

use of org.openhab.binding.ihc.ws.datatypes.WSIntegerValue in project openhab1-addons by openhab.

the class IhcResourceInteractionService method parseResourceValue.

private WSResourceValue parseResourceValue(Node n, int index) throws XPathExpressionException {
    // parse resource id
    String resourceId = getValue(n, "ns1:resourceID");
    if (StringUtils.isNotBlank(resourceId)) {
        int id = Integer.parseInt(resourceId);
        // Parse floating point value
        String value = getValue(n, "ns1:value/ns" + index + ":floatingPointValue");
        if (StringUtils.isNotBlank(value)) {
            WSFloatingPointValue val = new WSFloatingPointValue();
            val.setResourceID(id);
            val.setFloatingPointValue(Double.valueOf(value));
            value = getValue(n, "ns1:value/ns" + index + ":maximumValue");
            if (StringUtils.isNotBlank(value)) {
                val.setMaximumValue(Double.valueOf(value));
            }
            value = getValue(n, "ns1:value/ns" + index + ":minimumValue");
            if (StringUtils.isNotBlank(value)) {
                val.setMinimumValue(Double.valueOf(value));
            }
            return val;
        }
        // Parse boolean value
        value = getValue(n, "ns1:value/ns" + index + ":value");
        if (StringUtils.isNotBlank(value)) {
            WSBooleanValue val = new WSBooleanValue();
            val.setResourceID(id);
            val.setValue(Boolean.valueOf(value));
            return val;
        }
        // Parse integer value
        value = getValue(n, "ns1:value/ns" + index + ":integer");
        if (StringUtils.isNotBlank(value)) {
            WSIntegerValue val = new WSIntegerValue();
            val.setResourceID(id);
            val.setInteger(Integer.valueOf(value));
            value = getValue(n, "ns1:value/ns" + index + ":maximumValue");
            if (StringUtils.isNotBlank(value)) {
                val.setMaximumValue(Integer.valueOf(value));
            }
            value = getValue(n, "ns1:value/ns" + index + ":minimumValue");
            if (StringUtils.isNotBlank(value)) {
                val.setMinimumValue(Integer.valueOf(value));
            }
            return val;
        }
        // Parse timer value
        value = getValue(n, "ns1:value/ns" + index + ":milliseconds");
        if (StringUtils.isNotBlank(value)) {
            WSTimerValue val = new WSTimerValue();
            val.setResourceID(id);
            val.setMilliseconds(Integer.valueOf(value));
            return val;
        }
        // Parse time value
        value = getValue(n, "ns1:value/ns" + index + ":hours");
        if (StringUtils.isNotBlank(value)) {
            WSTimeValue val = new WSTimeValue();
            val.setResourceID(id);
            val.setHours(Integer.valueOf(value));
            value = getValue(n, "ns1:value/ns" + index + ":minutes");
            if (StringUtils.isNotBlank(value)) {
                val.setMinutes(Integer.valueOf(value));
            }
            value = getValue(n, "ns1:value/ns" + index + ":seconds");
            if (StringUtils.isNotBlank(value)) {
                val.setSeconds(Integer.valueOf(value));
            }
            return val;
        }
        // Parse date value
        value = getValue(n, "ns1:value/ns" + index + ":day");
        if (StringUtils.isNotBlank(value)) {
            WSDateValue val = new WSDateValue();
            val.setResourceID(id);
            val.setDay(Byte.valueOf(value));
            value = getValue(n, "ns1:value/ns" + index + ":month");
            if (StringUtils.isNotBlank(value)) {
                val.setMonth(Byte.valueOf(value));
            }
            value = getValue(n, "ns1:value/ns" + index + ":year");
            if (StringUtils.isNotBlank(value)) {
                val.setYear(Short.valueOf(value));
            }
            return val;
        }
        // Parse enum value
        value = getValue(n, "ns1:value/ns" + index + ":definitionTypeID");
        if (StringUtils.isNotBlank(value)) {
            WSEnumValue val = new WSEnumValue();
            val.setResourceID(id);
            val.setDefinitionTypeID(Integer.valueOf(value));
            value = getValue(n, "ns1:value/ns" + index + ":enumValueID");
            if (StringUtils.isNotBlank(value)) {
                val.setEnumValueID(Integer.valueOf(value));
            }
            value = getValue(n, "ns1:value/ns" + index + ":enumName");
            if (StringUtils.isNotBlank(value)) {
                val.setEnumName(value);
            }
            return val;
        }
        // Parse week day value
        value = getValue(n, "ns1:value/ns" + index + ":weekdayNumber");
        if (StringUtils.isNotBlank(value)) {
            WSWeekdayValue val = new WSWeekdayValue();
            val.setResourceID(id);
            val.setWeekdayNumber(Integer.valueOf(value));
            return val;
        }
        throw new IllegalArgumentException("Unsupported value type");
    }
    return null;
}
Also used : WSIntegerValue(org.openhab.binding.ihc.ws.datatypes.WSIntegerValue) WSFloatingPointValue(org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue) WSTimeValue(org.openhab.binding.ihc.ws.datatypes.WSTimeValue) WSDateValue(org.openhab.binding.ihc.ws.datatypes.WSDateValue) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) WSWeekdayValue(org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue) WSBooleanValue(org.openhab.binding.ihc.ws.datatypes.WSBooleanValue) WSTimerValue(org.openhab.binding.ihc.ws.datatypes.WSTimerValue)

Aggregations

WSBooleanValue (org.openhab.binding.ihc.ws.datatypes.WSBooleanValue)3 WSEnumValue (org.openhab.binding.ihc.ws.datatypes.WSEnumValue)3 WSFloatingPointValue (org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue)3 WSIntegerValue (org.openhab.binding.ihc.ws.datatypes.WSIntegerValue)3 WSTimeValue (org.openhab.binding.ihc.ws.datatypes.WSTimeValue)3 WSTimerValue (org.openhab.binding.ihc.ws.datatypes.WSTimerValue)3 WSWeekdayValue (org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue)3 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 WSDateValue (org.openhab.binding.ihc.ws.datatypes.WSDateValue)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2 DecimalType (org.openhab.core.library.types.DecimalType)2 PercentType (org.openhab.core.library.types.PercentType)2 StringType (org.openhab.core.library.types.StringType)2 BigDecimal (java.math.BigDecimal)1 IhcEnumValue (org.openhab.binding.ihc.ws.IhcEnumValue)1 ContactItem (org.openhab.core.library.items.ContactItem)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 StringItem (org.openhab.core.library.items.StringItem)1 OnOffType (org.openhab.core.library.types.OnOffType)1