Search in sources :

Example 61 with BindingConfigParseException

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

the class BindingConfigParser method parse.

/**
     * Parses the bindingConfig of an item and returns a HomematicBindingConfig.
     */
public HomematicBindingConfig parse(Item item, String bindingConfig) throws BindingConfigParseException {
    bindingConfig = StringUtils.trimToEmpty(bindingConfig);
    bindingConfig = StringUtils.removeStart(bindingConfig, "{");
    bindingConfig = StringUtils.removeEnd(bindingConfig, "}");
    String[] entries = bindingConfig.split("[,]");
    HomematicBindingConfigHelper helper = new HomematicBindingConfigHelper();
    for (String entry : entries) {
        String[] entryParts = StringUtils.trimToEmpty(entry).split("[=]");
        if (entryParts.length != 2) {
            throw new BindingConfigParseException("Each entry must have a key and a value");
        }
        String key = StringUtils.trim(entryParts[0]);
        // convert entry id to device if necessary
        if ("id".equalsIgnoreCase(key)) {
            logger.info("Please change the Homematic binding with the attribute 'id' to 'address' in entry: " + entry + " -> " + StringUtils.replace(entry, "id=", "address="));
            key = "address";
        }
        String value = StringUtils.trim(entryParts[1]);
        value = StringUtils.removeStart(value, "\"");
        value = StringUtils.removeEnd(value, "\"");
        try {
            helper.getClass().getDeclaredField(key).set(helper, value);
        } catch (Exception e) {
            throw new BindingConfigParseException("Could not set value " + value + " for attribute " + key);
        }
    }
    Converter<?> converter = null;
    // if (helper.isValidDatapoint() || helper.isValidVariable()) {
    // converter = instantiateConverter(helper.converter);
    // }
    BindingAction bindingAction = getBindingAction(item, helper.action);
    if (helper.isValidDatapoint()) {
        return new DatapointConfig(helper.address, helper.channel, helper.parameter, converter, bindingAction, helper.isForceUpdate(), NumberUtils.toDouble(helper.delay));
    } else if (helper.isValidVariable()) {
        return new VariableConfig(helper.variable, converter, bindingAction, helper.isForceUpdate(), NumberUtils.toDouble(helper.delay));
    } else if (helper.isValidProgram()) {
        if (!acceptsOnOffType(item)) {
            throw new BindingConfigParseException("Programs can only be attached to items which accepts OnOffType commands, ignoring item " + item.getName());
        }
        return new ProgramConfig(helper.program, bindingAction);
    } else if (bindingAction != null) {
        return new ActionConfig(bindingAction);
    } else {
        throw new BindingConfigParseException("Invalid binding: " + bindingConfig);
    }
}
Also used : VariableConfig(org.openhab.binding.homematic.internal.config.binding.VariableConfig) ActionConfig(org.openhab.binding.homematic.internal.config.binding.ActionConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DatapointConfig(org.openhab.binding.homematic.internal.config.binding.DatapointConfig) BindingAction(org.openhab.binding.homematic.internal.config.BindingAction) ProgramConfig(org.openhab.binding.homematic.internal.config.binding.ProgramConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 62 with BindingConfigParseException

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

the class k8055GenericBindingProvider method parseBindingConfig.

protected k8055BindingConfig parseBindingConfig(String bindingConfig) throws BindingConfigParseException {
    k8055BindingConfig config = new k8055BindingConfig();
    String[] configParts = bindingConfig.split(":");
    if (configParts.length != 2) {
        throw new BindingConfigParseException("Unable to parse k8055 binding string: " + bindingConfig + ".  Incorrect number of colons.");
    }
    try {
        config.ioNumber = Integer.parseInt(configParts[1]);
        config.ioType = IOType.valueOf(configParts[0]);
    } catch (NumberFormatException e) {
        throw new BindingConfigParseException("Unable to parse k8055 binding string: " + bindingConfig + ". Could not parse input number: " + configParts[1]);
    } catch (IllegalArgumentException e) {
        throw new BindingConfigParseException("Unable to parse k8055 binding string: " + bindingConfig + ". Invalid input type: " + configParts[0]);
    }
    // Verify config is actually valid given the hardware
    if (config.ioNumber < 1) {
        throw new BindingConfigParseException("Unable to parse k8055 binding string: " + bindingConfig + ". IO channel must be greater than equal to 1 ");
    } else if ((config.ioNumber > NUM_DIGITAL_INPUTS && config.ioType.equals(IOType.DIGITAL_IN)) || (config.ioNumber > NUM_DIGITAL_OUTPUTS && config.ioType.equals(IOType.DIGITAL_OUT)) || (config.ioNumber > NUM_ANALOG_INPUTS && config.ioType.equals(IOType.ANALOG_IN)) || (config.ioNumber > NUM_ANALOG_OUTPUTS && config.ioType.equals(IOType.ANALOG_OUT))) {
        throw new BindingConfigParseException("Unable to parse k8055 binding string: " + bindingConfig + ". IO channel number was greater than the number of physical channels ");
    }
    return config;
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 63 with BindingConfigParseException

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

the class IRtransGenericBindingProvider method parseBindingConfig.

/**
     * Parses the binding config.
     *
     * @param config the config
     * @param item the item
     * @param bindingConfig the binding config
     * @throws BindingConfigParseException the binding config parse exception
     */
private void parseBindingConfig(IRtransBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String host = null;
    String port = null;
    String led = null;
    String remote = null;
    String irCommand = null;
    Leds ledType = Leds.DEFAULT;
    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 five [config=" + statusMatcher.pattern() + "] or six parts [config=" + actionMatcher.pattern() + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                host = actionMatcher.group(2);
                port = actionMatcher.group(3);
                led = actionMatcher.group(4);
                remote = actionMatcher.group(5);
                irCommand = actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                host = statusMatcher.group(1);
                port = statusMatcher.group(2);
                led = statusMatcher.group(3);
                remote = statusMatcher.group(4);
                irCommand = statusMatcher.group(5);
            }
            if (led.equals("*")) {
                ledType = Leds.ALL;
            } else {
                ledType = Leds.valueOf(led);
            }
            IRtransBindingConfigElement newElement = new IRtransBindingConfigElement(host, port, ledType, remote, irCommand, 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++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }
            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)

Example 64 with BindingConfigParseException

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

the class MilightGenericBindingProvider method processBindingConfiguration.

@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    try {
        if (bindingConfig != null) {
            String[] configParts = bindingConfig.split(";");
            if (configParts.length > 4) {
                throw new BindingConfigParseException("milight binding configuration must not have more than four parts");
            }
            if (item instanceof ColorItem) {
                BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], BindingType.rgb.name(), null);
                addBindingConfig(item, milightBindingConfig);
            } else if (item instanceof DimmerItem || item instanceof SwitchItem) {
                BindingConfig milightBindingConfig = new MilightBindingConfig(configParts[0], configParts[1], configParts.length < 3 ? null : configParts[2], configParts.length < 4 ? null : configParts[3]);
                addBindingConfig(item, milightBindingConfig);
            }
        } else {
            logger.warn("bindingConfig is NULL (item=" + item + ") -> processing bindingConfig aborted!");
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        logger.warn("bindingConfig is invalid (item=" + item + ") -> processing bindingConfig aborted!");
    }
}
Also used : BindingConfig(org.openhab.core.binding.BindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DimmerItem(org.openhab.core.library.items.DimmerItem) ColorItem(org.openhab.core.library.items.ColorItem) SwitchItem(org.openhab.core.library.items.SwitchItem)

Example 65 with BindingConfigParseException

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

the class MCP23017GenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    MCP23017BindingConfig config = new MCP23017BindingConfig();
    // parse bindingConfig here ...
    /*
         * Configuration string should be a json in the form:
         * Contact Test1 "Test 1" (Tests) { mcp23017="{ address:20, pin:'A0', mode:'DIGITAL_INPUT'}" }
         * Switch Test2 "Test 2" (Tests) { mcp23017="{ address:20, pin:'B0', mode:'DIGITAL_OUTPUT', defaultState:'LOW'}"
         * }
         */
    JsonParserFactory factory = JsonParserFactory.getInstance();
    JSONParser parser = factory.newJsonParser();
    @SuppressWarnings("unchecked") Map<String, Object> jsonData = parser.parseJson(bindingConfig);
    try {
        logger.debug("Process binding configuration in context {}", context);
        config.setBusAddress(Integer.parseInt((String) jsonData.get("address"), 16));
        config.setPin((Pin) MCP23017Pin.class.getField("GPIO_" + (String) jsonData.get("pin")).get(null));
        config.setPinMode(PinMode.valueOf((String) jsonData.get("mode")));
        if (item instanceof SwitchItem) {
            config.setDefaultState(PinState.valueOf((String) jsonData.get("defaultState")));
        }
    } catch (IllegalArgumentException exception) {
        final String message = "Illegal argument exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (IllegalAccessException exception) {
        final String message = "Illegal access exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (NoSuchFieldException exception) {
        final String message = "No such field exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    } catch (SecurityException exception) {
        final String message = "Security exception in configuration string ";
        logger.error("{} '{}': {}", message, bindingConfig, exception.getMessage());
        throw new BindingConfigParseException(message + "'" + bindingConfig + "'");
    }
    addBindingConfig(item, config);
}
Also used : MCP23017Pin(com.pi4j.gpio.extension.mcp.MCP23017Pin) JsonParserFactory(com.json.parsers.JsonParserFactory) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) JSONParser(com.json.parsers.JSONParser) SwitchItem(org.openhab.core.library.items.SwitchItem)

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