Search in sources :

Example 1 with IhcEnumValue

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

the class IhcBinding method updateResource.

/**
     * Update resource value to IHC controller.
     */
private void updateResource(String itemName, Type type, boolean updateOnlyExclusiveOutBinding) {
    if (itemName != null) {
        Command cmd = null;
        try {
            cmd = (Command) type;
        } catch (Exception e) {
        }
        IhcBindingProvider provider = findFirstMatchingBindingProvider(itemName, cmd);
        if (provider == null) {
            // command not configured, skip
            return;
        }
        if (updateOnlyExclusiveOutBinding && provider.hasInBinding(itemName)) {
            logger.trace("Ignore in binding update for item '{}'", itemName);
            return;
        }
        logger.debug("Received update/command (item='{}', state='{}', class='{}')", new Object[] { itemName, type.toString(), type.getClass().toString() });
        if (ihc == null) {
            logger.warn("Controller is not initialized, abort resource value update for item '{}'!", itemName);
            return;
        }
        if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
            logger.warn("Connection to controller is not ok, abort resource value update for item '{}'!", itemName);
            return;
        }
        try {
            int resourceId = provider.getResourceId(itemName, (Command) type);
            logger.trace("found resourceId {} (item='{}', state='{}', class='{}')", new Object[] { new Integer(resourceId).toString(), itemName, type.toString(), type.getClass().toString() });
            if (resourceId > 0) {
                WSResourceValue value = ihc.getResourceValueInformation(resourceId);
                ArrayList<IhcEnumValue> enumValues = null;
                if (value instanceof WSEnumValue) {
                    enumValues = ihc.getEnumValues(((WSEnumValue) value).getDefinitionTypeID());
                }
                // check if configuration has a custom value defined
                // (0->OFF, 1->ON, >1->trigger)
                // if that is the case, the type will be overridden with a
                // new type
                Integer val = provider.getValue(itemName, (Command) type);
                boolean trigger = false;
                if (val != null) {
                    if (val == 0) {
                        type = OnOffType.OFF;
                    } else if (val == 1) {
                        type = OnOffType.ON;
                    } else {
                        trigger = true;
                    }
                } else {
                // the original type is kept
                }
                if (!trigger) {
                    value = IhcDataConverter.convertCommandToResourceValue(type, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item updated '{}' succesfully sent", itemName);
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                } else {
                    value = IhcDataConverter.convertCommandToResourceValue(OnOffType.ON, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item '{}' trigger started", itemName);
                        Thread.sleep(val);
                        value = IhcDataConverter.convertCommandToResourceValue(OnOffType.OFF, value, enumValues);
                        result = updateResource(value);
                        if (result == true) {
                            logger.debug("Item '{}' trigger completed", itemName);
                        } else {
                            logger.error("Item '{}' trigger stop failed", itemName);
                        }
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                }
            } else {
                logger.error("resourceId invalid");
            }
        } catch (IhcExecption e) {
            logger.error("Can't update Item '{}' value ", itemName, e);
        } catch (Exception e) {
            logger.error("Error occured during item update", e);
        }
    }
}
Also used : IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) Command(org.openhab.core.types.Command) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 2 with IhcEnumValue

use of org.openhab.binding.ihc.ws.IhcEnumValue 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)

Aggregations

IhcEnumValue (org.openhab.binding.ihc.ws.IhcEnumValue)2 WSEnumValue (org.openhab.binding.ihc.ws.datatypes.WSEnumValue)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 IhcBindingProvider (org.openhab.binding.ihc.IhcBindingProvider)1 IhcExecption (org.openhab.binding.ihc.ws.IhcExecption)1 WSBooleanValue (org.openhab.binding.ihc.ws.datatypes.WSBooleanValue)1 WSDateValue (org.openhab.binding.ihc.ws.datatypes.WSDateValue)1 WSFloatingPointValue (org.openhab.binding.ihc.ws.datatypes.WSFloatingPointValue)1 WSIntegerValue (org.openhab.binding.ihc.ws.datatypes.WSIntegerValue)1 WSResourceValue (org.openhab.binding.ihc.ws.datatypes.WSResourceValue)1 WSTimeValue (org.openhab.binding.ihc.ws.datatypes.WSTimeValue)1 WSTimerValue (org.openhab.binding.ihc.ws.datatypes.WSTimerValue)1 WSWeekdayValue (org.openhab.binding.ihc.ws.datatypes.WSWeekdayValue)1 DateTimeType (org.openhab.core.library.types.DateTimeType)1 DecimalType (org.openhab.core.library.types.DecimalType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 OpenClosedType (org.openhab.core.library.types.OpenClosedType)1 PercentType (org.openhab.core.library.types.PercentType)1 StringType (org.openhab.core.library.types.StringType)1