Search in sources :

Example 66 with BindingConfigParseException

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

the class PlugwiseBinding method postUpdate.

/**
     * Method to post updates to the OH runtime.
     *
     *
     * @param MAC of the Plugwise device concerned
     * @param ctype is the Plugwise Command type
     * @param value is the value (to be converted) to post
     */
public void postUpdate(String MAC, PlugwiseCommandType ctype, Object value) {
    if (MAC != null && ctype != null && value != null) {
        for (PlugwiseBindingProvider provider : providers) {
            Set<String> qualifiedItems = provider.getItemNames(MAC, ctype);
            // Make sure we also capture those devices that were pre-defined with a friendly name in a .cfg or alike
            Set<String> qualifiedItemsFriendly = provider.getItemNames(stick.getDevice(MAC).getName(), ctype);
            qualifiedItems.addAll(qualifiedItemsFriendly);
            State type = null;
            try {
                type = createStateForType(ctype, value);
            } catch (BindingConfigParseException e) {
                logger.error("Error parsing a value {} to a state variable of type {}", value.toString(), ctype.getTypeClass().toString());
            }
            for (String item : qualifiedItems) {
                if (type instanceof State) {
                    eventPublisher.postUpdate(item, type);
                } else {
                    throw new IllegalClassException("Cannot process update of type " + (type == null ? "null" : type.toString()));
                }
            }
        }
    }
}
Also used : PlugwiseBindingProvider(org.openhab.binding.plugwise.PlugwiseBindingProvider) State(org.openhab.core.types.State) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) IllegalClassException(org.apache.commons.lang.IllegalClassException)

Example 67 with BindingConfigParseException

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

the class PlugwiseGenericBindingProvider method parseBindingConfig.

/**
     * Parses the configuration string and update the provided config
     *
     * @param config
     * @param item
     * @param bindingConfig
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(PlugwiseBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String plugwiseID = null;
    String plugwiseCommand = null;
    int interval = 60;
    if (bindingConfig == null) {
        logger.warn("bindingConfig for item '{}' is null", item.getName());
        return;
    }
    Matcher actionWithJobMatcher = ACTION_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithJobMatcher = STATUS_CONFIG_WITH_JOB_PATTERN.matcher(bindingConfig);
    Matcher actionWithoutJobMatcher = ACTION_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    Matcher statusWithoutJobMatcher = STATUS_CONFIG_WITHOUT_JOB_PATTERN.matcher(bindingConfig);
    if (!actionWithJobMatcher.matches() && !statusWithJobMatcher.matches() && !actionWithoutJobMatcher.matches() && !statusWithoutJobMatcher.matches()) {
        throw new //
        BindingConfigParseException(//
        "Plugwise binding configuration must consist of either:\n" + "* 2 parts: [config=" + statusWithoutJobMatcher + //
        "]\n" + "* 3 parts: [config=" + statusWithJobMatcher + //
        "]\n" + "           [config=" + actionWithoutJobMatcher + //
        "]\n" + "* 4 parts: [config=" + actionWithJobMatcher + "]");
    }
    if (actionWithJobMatcher.matches()) {
        commandAsString = actionWithJobMatcher.group(1);
        plugwiseID = actionWithJobMatcher.group(2);
        plugwiseCommand = actionWithJobMatcher.group(3);
        interval = Integer.valueOf(actionWithJobMatcher.group(4));
    } else if (statusWithJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithJobMatcher.group(1);
        plugwiseCommand = statusWithJobMatcher.group(2);
        interval = Integer.valueOf(statusWithJobMatcher.group(3));
    } else if (actionWithoutJobMatcher.matches()) {
        commandAsString = actionWithoutJobMatcher.group(1);
        plugwiseID = actionWithoutJobMatcher.group(2);
        plugwiseCommand = actionWithoutJobMatcher.group(3);
        interval = -1;
    } else if (statusWithoutJobMatcher.matches()) {
        commandAsString = null;
        plugwiseID = statusWithoutJobMatcher.group(1);
        plugwiseCommand = statusWithoutJobMatcher.group(2);
        interval = -1;
    }
    PlugwiseCommandType type = PlugwiseCommandType.getCommandType(plugwiseCommand);
    if (PlugwiseCommandType.validateBinding(type, item)) {
        PlugwiseBindingConfigElement newElement = new PlugwiseBindingConfigElement(plugwiseID, type, interval);
        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++;
            config.put(command, newElement);
        } else {
            command = createCommandFromString(item, commandAsString);
            config.put(command, newElement);
        }
    } else {
        String validItemType = PlugwiseCommandType.getValidItemTypes(plugwiseCommand);
        if (StringUtils.isEmpty(validItemType)) {
            throw new BindingConfigParseException("'" + bindingConfig + "' is no valid binding type");
        } else {
            throw new BindingConfigParseException("'" + bindingConfig + "' is not bound to a valid item type. Valid item type(s): " + validItemType);
        }
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) PlugwiseCommandType(org.openhab.binding.plugwise.PlugwiseCommandType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 68 with BindingConfigParseException

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

the class SnmpGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    if (bindingConfig != null) {
        SnmpBindingConfig newConfig = new SnmpBindingConfig();
        Matcher matcher = BASE_CONFIG_PATTERN.matcher(bindingConfig);
        if (!matcher.matches()) {
            throw new BindingConfigParseException("bindingConfig '" + bindingConfig + "' doesn't contain a valid binding configuration");
        }
        matcher.reset();
        while (matcher.find()) {
            String bindingConfigPart = matcher.group(1);
            if (StringUtils.isNotBlank(bindingConfigPart)) {
                parseBindingConfig(newConfig, item, bindingConfigPart);
            }
        }
        addBindingConfig(item, newConfig);
    } else {
        logger.warn("bindingConfig is NULL (item={}) -> processing bindingConfig aborted!", item);
    }
}
Also used : Matcher(java.util.regex.Matcher) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) OctetString(org.snmp4j.smi.OctetString)

Example 69 with BindingConfigParseException

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

the class SwegonVentilationGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    SwegonVentilationBindingConfig config = new SwegonVentilationBindingConfig();
    config.itemType = item.getClass();
    String commandType = bindingConfig.trim();
    try {
        SwegonVentilationCommandType.validateBinding(commandType, config.itemType);
        config.commandType = SwegonVentilationCommandType.getCommandType(commandType);
    } catch (IllegalArgumentException e) {
        throw new BindingConfigParseException("'" + commandType + "' is not a valid command type");
    } catch (InvalidClassException e) {
        throw new BindingConfigParseException("Not valid class for command type '" + commandType + "'");
    }
    addBindingConfig(item, config);
}
Also used : InvalidClassException(java.io.InvalidClassException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 70 with BindingConfigParseException

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

the class SonosGenericBindingProvider method parseBindingConfig.

private void parseBindingConfig(SonosBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String sonosID = null;
    String commandAsString = null;
    String sonosCommand = 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("Sonos binding configuration must consist of either two [config=" + statusMatcher + "] or three parts [config=" + actionMatcher + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                sonosID = actionMatcher.group(2);
                sonosCommand = actionMatcher.group(3);
            } else if (statusMatcher.matches()) {
                commandAsString = null;
                sonosID = statusMatcher.group(1);
                sonosCommand = statusMatcher.group(2);
            }
            SonosBindingConfigElement newElement = new SonosBindingConfigElement(sonosCommand, sonosID, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                command = createCommandFromString(null, Integer.toString(counter));
                counter++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }
        }
    } else {
        return;
    }
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) 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