Search in sources :

Example 1 with Direction

use of org.openhab.binding.tcp.Direction in project openhab1-addons by openhab.

the class ProtocolGenericBindingProvider method parseBindingConfig.

/**
     * Parses the configuration string and update the provided config
     *
     * @param config - the Configuration that needs to be updated with the parsing results
     * @param item - the Item that this configuration is intented for
     * @param bindingConfig - the configuration string that will be parsed
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(ProtocolBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String direction = null;
    Direction directionType = null;
    String commandAsString = null;
    String host = null;
    String port = null;
    String protocolCommand = null;
    if (bindingConfig != null) {
        Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
        Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);
        if ((!actionMatcher.matches() && !statusMatcher.matches())) {
            throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of four [config=" + statusMatcher + "] or five parts [config=" + actionMatcher + "]");
        } else {
            if (actionMatcher.matches()) {
                direction = actionMatcher.group(1);
                commandAsString = actionMatcher.group(2);
                host = actionMatcher.group(3);
                port = actionMatcher.group(4);
                protocolCommand = actionMatcher.group(5) != null ? actionMatcher.group(5) : actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                direction = statusMatcher.group(1);
                commandAsString = null;
                host = statusMatcher.group(2);
                port = statusMatcher.group(3);
                protocolCommand = statusMatcher.group(4) != null ? statusMatcher.group(4) : statusMatcher.group(5);
            }
            if (direction.equals(">")) {
                directionType = Direction.OUT;
            } else if (direction.equals("<")) {
                directionType = Direction.IN;
            }
            ProtocolBindingConfigElement newElement = new ProtocolBindingConfigElement(host, port, directionType, protocolCommand, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                // for those configuration strings that are not really linked to a openHAB command we
                // create a dummy Command to be able to store the configuration information
                // I have choosen to do that with NumberItems
                NumberItem dummy = new NumberItem(Integer.toString(counter));
                command = createCommandFromString(dummy, Integer.toString(counter));
                counter++;
            } else {
                command = createCommandFromString(item, commandAsString);
            }
            config.put(command, newElement);
        }
    } else {
        return;
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Direction(org.openhab.binding.tcp.Direction)

Aggregations

Matcher (java.util.regex.Matcher)1 Direction (org.openhab.binding.tcp.Direction)1 NumberItem (org.openhab.core.library.items.NumberItem)1 Command (org.openhab.core.types.Command)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1