Search in sources :

Example 71 with BindingConfigParseException

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

the class PulseaudioGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    PulseaudioBindingConfig config = new PulseaudioBindingConfig();
    String[] configParts = bindingConfig.split(":");
    if (item instanceof StringItem) {
        if (configParts.length != 1 && configParts.length != 3) {
            throw new BindingConfigParseException("Pulseaudio binding configuration for StringItems must consist of one or three part [config=" + configParts + "]");
        }
        config.serverId = StringUtils.trim(configParts[0]);
        config.itemType = item.getClass();
        addBindingConfig(item, config);
        return;
    } else if (configParts.length < 2 || configParts.length > 3) {
        throw new BindingConfigParseException("Pulseaudio binding configuration must consist of two or three parts [config=" + configParts + "]");
    }
    config.serverId = StringUtils.trim(configParts[0]);
    config.itemName = StringUtils.trim(configParts[1]);
    if (configParts.length == 3) {
        config.command = StringUtils.trim(configParts[2]);
    }
    config.itemType = item.getClass();
    addBindingConfig(item, config);
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) StringItem(org.openhab.core.library.items.StringItem)

Example 72 with BindingConfigParseException

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

the class RFXComGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    RFXComBindingConfig config = new RFXComBindingConfig();
    String valueSelectorString = null;
    if (bindingConfig.startsWith("<")) {
        String[] configParts = bindingConfig.trim().split(":");
        if (configParts.length != 2) {
            throw new BindingConfigParseException("RFXCOM binding must contain two parts separated by ':'");
        }
        config.id = configParts[0].trim().replace("<", "");
        config.inBinding = true;
        valueSelectorString = configParts[1].trim();
    } else if (bindingConfig.startsWith(">")) {
        String[] configParts = bindingConfig.trim().split(":");
        config.id = configParts[0].trim().replace(">", "");
        config.inBinding = false;
        String[] types = configParts[1].trim().split("\\.");
        if (types.length != 2) {
            throw new BindingConfigParseException("RFXCOM out binding second field should contain 2 parts separated by '.'");
        }
        try {
            config.packetType = RFXComMessageFactory.convertPacketType(types[0].trim());
        } catch (IllegalArgumentException e) {
            throw new BindingConfigParseException("Invalid packet type '" + types[0] + "'!");
        }
        try {
            config.subType = RFXComMessageFactory.getMessageInterface(config.packetType).convertSubType(types[1].trim());
        } catch (Exception e) {
            throw new BindingConfigParseException("Invalid sub type '" + types[1] + "' in type '" + config.packetType + "'!");
        }
        valueSelectorString = configParts[2].trim();
    } else {
        throw new BindingConfigParseException("RFXCOM binding should start < or > character!");
    }
    try {
        RFXComValueSelector.validateBinding(valueSelectorString, item.getClass());
        config.valueSelector = RFXComValueSelector.getValueSelector(valueSelectorString);
    } catch (IllegalArgumentException e1) {
        throw new BindingConfigParseException("Invalid value selector '" + valueSelectorString + "'!");
    } catch (InvalidClassException e1) {
        throw new BindingConfigParseException("Invalid item type for value selector '" + valueSelectorString + "'!");
    }
    addBindingConfig(item, config);
}
Also used : InvalidClassException(java.io.InvalidClassException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) InvalidClassException(java.io.InvalidClassException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 73 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 74 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 75 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)

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