Search in sources :

Example 81 with BindingConfigParseException

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

the class HttpGenericBindingProvider method parseOutBindingConfig.

/**
     * Parses an http-out configuration by using the regular expression
     * <code>(.*?):([A-Z]*):(.*)</code>. Where the groups should contain the
     * following content:
     * <ul>
     * <li>1 - command</li>
     * <li>2 - http method</li>
     * <li>3 - url</li>
     * <li>4 - post body</li>
     * </ul>
     *
     * @param item
     * @param bindingConfig the config string to parse
     * @param config
     * @return the filled {@link HttpBindingConfig}
     *
     * @throws BindingConfigParseException if the regular expression doesn't match
     *             the given <code>bindingConfig</code>
     */
protected HttpBindingConfig parseOutBindingConfig(Item item, String bindingConfig, HttpBindingConfig config) throws BindingConfigParseException {
    logger.debug("parsing this as an http out binding: {}", bindingConfig);
    Matcher matcher = OUT_BINDING_PATTERN.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException("bindingConfig '" + bindingConfig + "' doesn't contain a valid out-binding-configuration. A valid configuration is matched by the RegExp '" + OUT_BINDING_PATTERN + "'");
    }
    matcher.reset();
    HttpBindingConfigElement configElement;
    while (matcher.find()) {
        configElement = new HttpBindingConfigElement();
        Command command = createCommandFromString(item, matcher.group(1));
        configElement.httpMethod = matcher.group(2);
        String lastPart = matcher.group(3).replaceAll("\\\\\"", "");
        logger.debug("URL portion of binding config to be processed: {}", lastPart);
        Matcher urlMatcher = URL_PARSING_PATTERN.matcher(lastPart);
        urlMatcher.find();
        if (logger.isDebugEnabled()) {
            for (int i = 0; i <= urlMatcher.groupCount(); i++) {
                logger.debug("Group {}: {}", i, urlMatcher.group(i));
            }
        }
        if (urlMatcher.group(1).endsWith("}")) {
            String g1 = urlMatcher.group(1);
            int beginIdx = g1.indexOf("{");
            int endIdx = g1.indexOf("}");
            configElement.url = g1.substring(0, beginIdx);
            configElement.headers = parseHttpHeaders(g1.substring(beginIdx + 1, endIdx));
        } else {
            configElement.url = urlMatcher.group(1);
        }
        if (configElement.httpMethod.equals("POST") && urlMatcher.group(11) != null) {
            configElement.body = urlMatcher.group(11).substring(1);
        }
        config.put(command, configElement);
    }
    return config;
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 82 with BindingConfigParseException

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

the class IhcGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    IhcBindingConfig config = new IhcBindingConfig();
    config.itemType = item.getClass();
    config.outCommandMap = new HashMap<Command, IhcOutCommandConfig>();
    String[] splittedCommands = bindingConfig.split(",");
    for (String split : splittedCommands) {
        if (split.startsWith(">")) {
            try {
                // out binding
                String resourceCommand = split.substring(1);
                Matcher m = commandPattern.matcher(resourceCommand);
                IhcOutCommandConfig outConfig = new IhcOutCommandConfig();
                if (!m.matches()) {
                    if (splittedCommands.length < 2) {
                        // assume old style out command
                        // wildcard
                        outConfig.command = null;
                        outConfig.resourceId = getResourceIdFromString(resourceCommand);
                    } else {
                        throw new BindingConfigParseException("Item '" + item.getName() + "' has invalid out binding config");
                    }
                } else {
                    // new style out binding
                    Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), m.group(1));
                    if (command == null && !m.group(1).equals("*")) {
                        throw new BindingConfigParseException("Item '" + item.getName() + " invalid Command: " + m.group(1));
                    }
                    outConfig.command = command;
                    outConfig.resourceId = getResourceIdFromString(m.group(2));
                    if (m.groupCount() == 3 && m.group(3) != null) {
                        outConfig.value = Integer.parseInt(m.group(3));
                        if (outConfig.value > 2000) {
                            throw new BindingConfigParseException("Item '" + item.getName() + "' exceeds maximum value of 2000 ms for trigger command");
                        }
                    }
                }
                config.outCommandMap.put(outConfig.command, outConfig);
            } catch (Exception e) {
                logger.warn("Error in output config for item: " + item.getName(), e);
            }
        } else if (split.startsWith("<")) {
            if (config.resourceId < 1) {
                config.resourceId = getResourceIdFromString(split.substring(1));
            } else {
                throw new BindingConfigParseException("Multiple In-Bindings found, only one In-Binding allowed.");
            }
        } else {
            if (splittedCommands.length < 2) {
                String[] configParts = bindingConfig.trim().split(":");
                if (configParts.length > 2) {
                    throw new BindingConfigParseException("IHC / ELKO LS binding must contain of max two two parts separated by ':'");
                }
                String resourceId = configParts[0];
                config.resourceId = getResourceIdFromString(resourceId);
                IhcOutCommandConfig outConfig = new IhcOutCommandConfig();
                // wildcard
                outConfig.command = null;
                outConfig.resourceId = config.resourceId;
                config.outCommandMap.put(null, outConfig);
                if (configParts.length == 2) {
                    config.refreshInterval = Integer.parseInt(configParts[1]);
                }
            } else {
                throw new BindingConfigParseException("Only a sum of out bindings (+In-Only Binding) or a normal binding is supported.");
            }
        }
    }
    addBindingConfig(item, config);
}
Also used : Command(org.openhab.core.types.Command) Matcher(java.util.regex.Matcher) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 83 with BindingConfigParseException

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

the class LightwaveRfGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    try {
        String roomId = null;
        String deviceId = null;
        LightwaveRfType type = null;
        int poll = -1;
        String serialId = null;
        LightwaveRfItemDirection direction = LightwaveRfItemDirection.IN_AND_OUT;
        if (bindingConfig.startsWith("<")) {
            direction = LightwaveRfItemDirection.IN_ONLY;
        } else if (bindingConfig.startsWith(">")) {
            direction = LightwaveRfItemDirection.OUT_ONLY;
        }
        Matcher roomMatcher = ROOM_REG_EXP.matcher(bindingConfig);
        if (roomMatcher.matches()) {
            roomId = roomMatcher.group(1);
        }
        Matcher deviceMatcher = DEVICE_REG_EXP.matcher(bindingConfig);
        if (deviceMatcher.matches()) {
            deviceId = deviceMatcher.group(1);
        }
        Matcher serialMatcher = SERIAL_REG_EXP.matcher(bindingConfig);
        if (serialMatcher.matches()) {
            serialId = serialMatcher.group(1);
        }
        Matcher typeMatcher = TYPE_REG_EXP.matcher(bindingConfig);
        if (typeMatcher.matches()) {
            type = LightwaveRfType.valueOf(typeMatcher.group(1));
        }
        Matcher pollMatcher = POLL_REG_EXP.matcher(bindingConfig);
        if (pollMatcher.matches()) {
            poll = Integer.valueOf(pollMatcher.group(1));
        }
        LightwaveRfBindingConfig config = new LightwaveRfBindingConfig(roomId, deviceId, serialId, type, poll, direction);
        logger.info("ConfigString[{}] Room[{}] Device[{}] Serial[{}] Type[{}] Poll[{}]", new Object[] { bindingConfig, roomId, deviceId, serialId, type, poll });
        addBindingConfig(item, config);
    } catch (Exception e) {
        throw new BindingConfigParseException("Error parsing binding for Context[" + context + "] Item[" + item + "] BindingConfig[" + bindingConfig + "] ErrorMessage: " + e.getMessage());
    }
}
Also used : Matcher(java.util.regex.Matcher) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 84 with BindingConfigParseException

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

the class MqttGenericBindingProvider method processBindingConfiguration.

@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    final String itemName = item.getName();
    logger.trace("Starting to load MQTT config for item {}", itemName);
    if (StringUtils.isEmpty(bindingConfig)) {
        throw new BindingConfigParseException("Missing mqtt binding configuration for item " + itemName);
    }
    final MqttItemConfig itemConfig = new MqttItemConfig(item, bindingConfig);
    // register all message consumers
    for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
        subscriber.setItemName(item.getName());
        mqttService.registerMessageConsumer(subscriber.getBroker(), subscriber);
    }
    // add binding change listener to clean up message consumers on item removal
    addBindingChangeListener(new BindingChangeListener() {

        @Override
        public void bindingChanged(BindingProvider provider, String changedItemName) {
            if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                logger.debug("Removing message consumers for item {}", itemName);
                for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
                    mqttService.unregisterMessageConsumer(subscriber.getBroker(), subscriber);
                }
                removeBindingChangeListener(this);
            }
        }

        @Override
        public void allBindingsChanged(BindingProvider provider) {
            if (!provider.providesBindingFor(itemName)) {
                logger.debug("Removing message consumers for item {}", itemName);
                for (MqttMessageSubscriber subscriber : itemConfig.getMessageSubscribers()) {
                    mqttService.unregisterMessageConsumer(subscriber.getBroker(), subscriber);
                }
                removeBindingChangeListener(this);
            }
        }
    });
    // register all message producers
    for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
        publisher.setItemName(item.getName());
        mqttService.registerMessageProducer(publisher.getBroker(), publisher);
    }
    // add binding change listener to clean up message publishers on item
    // removal
    addBindingChangeListener(new BindingChangeListener() {

        @Override
        public void bindingChanged(BindingProvider provider, String changedItemName) {
            if (itemName.equals(changedItemName) && !provider.providesBindingFor(itemName)) {
                logger.debug("Removing message publishers for item {}", itemName);
                for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
                    mqttService.unregisterMessageProducer(publisher.getBroker(), publisher);
                }
                removeBindingChangeListener(this);
            }
        }

        @Override
        public void allBindingsChanged(BindingProvider provider) {
            if (!provider.providesBindingFor(itemName)) {
                logger.debug("Removing message publishers for item {}", itemName);
                for (MqttMessagePublisher publisher : itemConfig.getMessagePublishers()) {
                    mqttService.unregisterMessageProducer(publisher.getBroker(), publisher);
                }
                removeBindingChangeListener(this);
            }
        }
    });
    addBindingConfig(item, itemConfig);
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) BindingChangeListener(org.openhab.core.binding.BindingChangeListener) BindingProvider(org.openhab.core.binding.BindingProvider) MqttBindingProvider(org.openhab.binding.mqtt.MqttBindingProvider) AbstractGenericBindingProvider(org.openhab.model.item.binding.AbstractGenericBindingProvider)

Example 85 with BindingConfigParseException

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

the class PanStampGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    PanStampBindingConfig<?> config;
    try {
        config = parse(item.getName(), bindingConfig);
        addBindingConfig(item, config);
        logger.debug("Created binding config '{}'", config);
    } catch (ValueException e) {
        throw new BindingConfigParseException(e.getMessage());
    }
    super.processBindingConfiguration(context, item, bindingConfig);
}
Also used : 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