Search in sources :

Example 41 with BindingConfigParseException

use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.

the class ExecGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    ExecBindingConfig config = new ExecBindingConfig();
    config.acceptedDataTypes = new ArrayList<Class<? extends State>>(item.getAcceptedDataTypes());
    Matcher matcher = BASE_CONFIG_PATTERN.matcher(bindingConfig);
    if (!matcher.matches()) {
        if (bindingConfig.startsWith("<") || bindingConfig.startsWith(">")) {
            throw new BindingConfigParseException("Exec binding legacy format cannot start with '<' or '>' ");
        }
        // backward compatibility for old format
        parseLegacyOutBindingConfig(item, bindingConfig, config);
    } else {
        matcher.reset();
        while (matcher.find()) {
            String direction = matcher.group(1);
            String bindingConfigPart = matcher.group(2);
            if (direction.equals("<")) {
                config = parseInBindingConfig(item, bindingConfigPart, config);
            } else if (direction.equals(">")) {
                config = parseOutBindingConfig(item, bindingConfigPart, config);
            } else {
                throw new BindingConfigParseException("Unknown command given! Configuration must start with '<' or '>' ");
            }
        }
    }
    addBindingConfig(item, config);
}
Also used : Matcher(java.util.regex.Matcher) State(org.openhab.core.types.State) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 42 with BindingConfigParseException

use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.

the class KNXGenericBindingProvider method parseBindingConfigString.

/**
     * This is the main method that takes care of parsing a binding configuration
     * string for a given item. It returns a collection of {@link BindingConfig}
     * instances, which hold all relevant data about the binding to KNX of an item.
     * 
     * @param item the item for which the binding configuration string is provided
     * @param bindingConfig a string which holds the binding information
     * @return a knx binding config, a collection of {@link KNXBindingConfigItem}
     *         instances, which hold all relevant data about the binding
     * @throws BindingConfigParseException if the configuration string has no valid syntax
     */
protected KNXBindingConfig parseBindingConfigString(Item item, String bindingConfig) throws BindingConfigParseException {
    KNXBindingConfig config = new KNXBindingConfig();
    String[] datapointConfigs = bindingConfig.trim().split(",");
    // we can have one datapoint per accepted command type of this item
    for (int i = 0; i < datapointConfigs.length; i++) {
        try {
            String datapointConfig = datapointConfigs[i].trim();
            KNXBindingConfigItem configItem = new KNXBindingConfigItem();
            configItem.itemName = item.getName();
            if (datapointConfig.split("<").length > 2) {
                throw new BindingConfigParseException("Only one readable GA allowed.");
            }
            String[] dataPoints = datapointConfig.split("\\+");
            for (int j = 0; j < dataPoints.length; ++j) {
                String dataPoint = dataPoints[j].trim();
                // Just skip it, it will be handle in the next iteration.
                if (dataPoint.isEmpty()) {
                    continue;
                }
                boolean isReadable = false;
                int autoRefreshTimeInSecs = 0;
                // check for the readable flag
                if (dataPoint.startsWith("<")) {
                    isReadable = true;
                    dataPoint = dataPoint.substring(1);
                    // check for the auto refresh parameter
                    if (dataPoint.startsWith("(")) {
                        int endIndex = dataPoint.indexOf(")");
                        if (endIndex > -1) {
                            dataPoint = dataPoint.substring(1);
                            if (endIndex > 1) {
                                try {
                                    autoRefreshTimeInSecs = Integer.parseInt(dataPoint.substring(0, endIndex - 1));
                                    dataPoint = dataPoint.substring(endIndex);
                                    if (autoRefreshTimeInSecs == 0) {
                                        throw new BindingConfigParseException("Autorefresh time cannot be 0.");
                                    }
                                } catch (NumberFormatException nfe) {
                                    throw new BindingConfigParseException("Autorefresh time must be a number, but was '" + dataPoint.substring(1, endIndex) + "'.");
                                }
                            } else {
                                throw new BindingConfigParseException("Autorefresh time parameter: missing time. Empty brackets are not allowed.");
                            }
                        } else {
                            throw new BindingConfigParseException("Closing ')' missing on autorefresh time parameter.");
                        }
                    }
                }
                // find the DPT for this entry
                String[] segments = dataPoint.split(":");
                Class<? extends Type> typeClass = null;
                String dptID = null;
                if (segments.length == 1) {
                    //DatapointID NOT specified in binding config, so try to guess it
                    typeClass = item.getAcceptedCommandTypes().size() > 0 ? item.getAcceptedCommandTypes().get(i) : item.getAcceptedDataTypes().size() > 1 ? item.getAcceptedDataTypes().get(i) : item.getAcceptedDataTypes().get(0);
                    dptID = getDefaultDPTId(typeClass);
                } else {
                    //DatapointID specified in binding config, so use it
                    dptID = segments[0];
                }
                if (dptID == null || dptID.trim().isEmpty()) {
                    throw new BindingConfigParseException("No DPT could be determined for the type '" + typeClass.getSimpleName() + "'.");
                }
                // check if this DPT is supported
                if (KNXCoreTypeMapper.toTypeClass(dptID) == null) {
                    throw new BindingConfigParseException("DPT " + dptID + " is not supported by the KNX binding.");
                }
                String ga = (segments.length == 1) ? segments[0].trim() : segments[1].trim();
                // determine start/stop behavior
                Boolean startStopBehavior = Boolean.FALSE;
                if (ga.endsWith(START_STOP_MARKER_SUFFIX)) {
                    startStopBehavior = Boolean.TRUE;
                    ga = ga.substring(0, ga.length() - START_STOP_MARKER_SUFFIX.length());
                }
                // create group address and datapoint
                GroupAddress groupAddress = new GroupAddress(ga);
                configItem.startStopMap.put(groupAddress, startStopBehavior);
                Datapoint dp;
                if (j != 0 || item.getAcceptedCommandTypes().size() == 0) {
                    dp = new StateDP(groupAddress, item.getName(), 0, dptID);
                } else {
                    dp = new CommandDP(groupAddress, item.getName(), 0, dptID);
                }
                // assign datapoint to configuration item
                if (configItem.mainDataPoint == null) {
                    configItem.mainDataPoint = dp;
                }
                if (isReadable) {
                    configItem.readableDataPoint = dp;
                    if (autoRefreshTimeInSecs > 0) {
                        configItem.autoRefreshInSecs = autoRefreshTimeInSecs;
                    }
                }
                if (!configItem.allDataPoints.contains(dp)) {
                    configItem.allDataPoints.add(dp);
                } else {
                    throw new BindingConfigParseException("Datapoint '" + dp.getDPT() + "' already exists for item '" + item.getName() + "'.");
                }
            }
            config.add(configItem);
        } catch (IndexOutOfBoundsException e) {
            throw new BindingConfigParseException("No more than " + i + " datapoint definitions are allowed for this item.");
        } catch (KNXFormatException e) {
            throw new BindingConfigParseException(e.getMessage());
        }
    }
    return config;
}
Also used : CommandDP(tuwien.auto.calimero.datapoint.CommandDP) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) Datapoint(tuwien.auto.calimero.datapoint.Datapoint) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) StateDP(tuwien.auto.calimero.datapoint.StateDP) GroupAddress(tuwien.auto.calimero.GroupAddress) KNXFormatException(tuwien.auto.calimero.exception.KNXFormatException)

Example 43 with BindingConfigParseException

use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.

the class KM200GenericBindingProvider method parseBindingConfig.

/**
     * Checks if the bindingConfig contains a valid binding type and returns an appropriate instance.
     *
     * @param item
     * @param bindingConfig
     *
     * @throws BindingConfigParseException if bindingConfig is no valid binding type
     */
protected KM200BindingConfig parseBindingConfig(Item item, String bindingConfig) throws BindingConfigParseException {
    String service = null, current = null;
    HashMap<String, String> parameterMap = new HashMap<String, String>();
    /* Check whether some defined services are used */
    logger.info("Bind Config: {}", bindingConfig);
    if (bindingConfig.trim().equals(KM200BindingProvider.DATE_TIME)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.DATE_TIME, "/gateway/DateTime", parameterMap);
    }
    if (bindingConfig.trim().equals(KM200BindingProvider.SYS_BRAND)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_BRAND, "/system/brand", parameterMap);
    }
    if (bindingConfig.trim().equals(KM200BindingProvider.SYS_TYPE)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_TYPE, "/system/appliance/type", parameterMap);
    }
    if (bindingConfig.trim().equals(KM200BindingProvider.SYS_STATE)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.SYS_STATE, "/system/healthStatus", parameterMap);
    }
    if (bindingConfig.trim().equals(KM200BindingProvider.VER_FIRMWARE)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.VER_FIRMWARE, "/gateway/versionFirmware", parameterMap);
    }
    if (bindingConfig.trim().equals(KM200BindingProvider.VER_HARDWARE)) {
        return new KM200BindingConfig(item.getClass(), KM200BindingProvider.VER_HARDWARE, "/gateway/versionHardware", parameterMap);
    }
    /* Maybe it's a configuration */
    /* Configuration string should be in the form "service:/gateway/versionFirmware" */
    /* Available options for a SwitchItem are on:xxx and off:yyy with a allowed value xxx|yyy */
    String[] keyTypeStructure = bindingConfig.split("\\s+");
    logger.debug("Bind Config nbr: {}", keyTypeStructure.length);
    if (keyTypeStructure.length > 3) {
        logger.error("Incorrect number of structures in configuration string '{}'", bindingConfig);
        throw new BindingConfigParseException("Incorrect number of structures in configuration string '" + bindingConfig + "'");
    }
    for (String structure : keyTypeStructure) {
        String[] keyValueStructure = structure.split(":");
        if (keyValueStructure.length != 2) {
            logger.error("Incorrect key:value structure in configuration string '{}'", bindingConfig);
            throw new BindingConfigParseException("Incorrect key:value structure in configuration string '" + bindingConfig + "'");
        }
        String key = keyValueStructure[0];
        String value = keyValueStructure[1];
        if (key.equals("service")) {
            service = value;
            if (!service.contains("/")) {
                logger.error("Wrong service string without / in configuration string '{}'", bindingConfig);
                throw new BindingConfigParseException("Wrong service string without / in configuration string '" + bindingConfig + "'");
            }
        } else if (key.equals("current")) {
            current = value;
            if (!current.contains("/")) {
                logger.error("Wrong current string without / in configuration string '{}'", bindingConfig);
                throw new BindingConfigParseException("Wrong current string without / in configuration string '" + bindingConfig + "'");
            }
            parameterMap.put(key, current);
        } else if (key.equals("on") || key.equals("off")) {
            parameterMap.put(key, value);
        } else {
            logger.error("Unsupported key: ({}) in configuration string: '{}'", key, bindingConfig);
            throw new BindingConfigParseException("Unsupported key (" + key + ") in configuration string '" + bindingConfig + "'");
        }
    }
    if (parameterMap.containsKey("on") || parameterMap.containsKey("off")) {
        if (!(parameterMap.containsKey("on") && parameterMap.containsKey("off"))) {
            throw new BindingConfigParseException("Unsupported combination, 'on' and 'off' have to be configured together '" + bindingConfig + "'");
        }
    }
    return new KM200BindingConfig(item.getClass(), KM200BindingProvider.DIRECT_SERVICE, service, parameterMap);
}
Also used : HashMap(java.util.HashMap) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 44 with BindingConfigParseException

use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.

the class KoubachiGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    String[] configParts = bindingConfig.split(":");
    if (configParts.length < 3 || configParts.length > 4) {
        throw new BindingConfigParseException("A Koubachi binding configuration for a property must consist of three or four parts - please verify your *.items file");
    } else if (configParts[2].equals("action") && configParts.length != 4) {
        throw new BindingConfigParseException("A Koubachi binding configuration for an action  must consist of four parts - please verify your *.items file");
    }
    KoubachiBindingConfig config = new KoubachiBindingConfig();
    config.resourceType = KoubachiResourceType.valueOf(configParts[0].toUpperCase());
    config.resourceId = configParts[1];
    if (configParts.length == 3) {
        // this is a binding for a property
        config.propertyName = configParts[2];
    } else {
        // this is a binding for a care action
        config.actionType = configParts[3];
    }
    addBindingConfig(item, config);
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 45 with BindingConfigParseException

use of org.openhab.model.item.binding.BindingConfigParseException in project openhab1-addons by openhab.

the class SmarthomaticGenericBindingProvider method parseConfig.

private SmarthomaticBindingConfig parseConfig(Item item, String bindingConfig) throws BindingConfigParseException {
    SmarthomaticBindingConfig config = new SmarthomaticBindingConfig();
    Matcher matcher = CONFIG_PATTERN.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException("Config for item '" + item.getName() + "' could not be parsed.");
    }
    bindingConfig = matcher.group(1);
    config.setItemName(item.getName());
    config.setItem(item);
    matcher = TRANSFORMATION_PATTERN.matcher(bindingConfig);
    if (matcher.matches()) {
        bindingConfig = matcher.group(1);
        String transformation = matcher.group(2);
        config.getConfigParams().put("transformation", transformation);
    }
    // parse bindingconfig here ...
    StringTokenizer confItems = new StringTokenizer(bindingConfig, ",");
    while (confItems.hasMoreTokens()) {
        String[] token = confItems.nextToken().split("=");
        String key = token[0];
        String value = token[1];
        config.getConfigParams().put(key, value);
        // Strip all whitespaces from token[0]
        key = key.replaceAll("\\s", "");
        if ("deviceId".equals(key)) {
            config.setDeviceId(value.replaceAll("\\s", ""));
        } else if ("messageGroupId".equals(key)) {
            config.setMessageGroupId(value.replaceAll("\\s", ""));
        } else if ("messageId".equals(key)) {
            config.setMessageId(value.replaceAll("\\s", ""));
        } else if ("messagePart".equals(key)) {
            config.setMessagePartId(value.replaceAll("\\s", ""));
        } else if ("messageItem".equals(key)) {
            config.setMessageItemId(value.replaceAll("\\s", ""));
        }
    }
    return config;
}
Also used : StringTokenizer(java.util.StringTokenizer) Matcher(java.util.regex.Matcher) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Aggregations

BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)112 Matcher (java.util.regex.Matcher)37 Command (org.openhab.core.types.Command)11 NumberItem (org.openhab.core.library.items.NumberItem)9 SwitchItem (org.openhab.core.library.items.SwitchItem)9 SappAddressType (org.openhab.binding.sapp.internal.model.SappAddressType)7 InvalidClassException (java.io.InvalidClassException)6 HashMap (java.util.HashMap)4 ContactItem (org.openhab.core.library.items.ContactItem)4 S7Client (Moka7.S7Client)2 JSONParser (com.json.parsers.JSONParser)2 JsonParserFactory (com.json.parsers.JsonParserFactory)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 IllegalClassException (org.apache.commons.lang.IllegalClassException)2 BindingConfig (org.openhab.core.binding.BindingConfig)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 StringItem (org.openhab.core.library.items.StringItem)2 StringType (org.openhab.core.library.types.StringType)2 State (org.openhab.core.types.State)2