Search in sources :

Example 91 with BindingConfigParseException

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

the class SatelBindingConfigFactory method createBindingConfig.

/**
     * Creates binding configuration class basing on given configuration string.
     * 
     * @param bindingConfig
     *            configuration string
     * @return an instance of {@link SatelBindingConfig}
     * @throws BindingConfigParseException
     *             on any parsing error
     */
public SatelBindingConfig createBindingConfig(String bindingConfig) throws BindingConfigParseException {
    try {
        for (Class<? extends SatelBindingConfig> c : bindingConfigurationClasses) {
            Method parseConfigMethod = c.getMethod("parseConfig", String.class);
            SatelBindingConfig bc = (SatelBindingConfig) parseConfigMethod.invoke(null, bindingConfig);
            if (bc != null) {
                return bc;
            }
        }
    // no more options, throw parse exception
    } catch (Exception e) {
    // throw parse exception in case of any error
    }
    throw new BindingConfigParseException(String.format("Invalid binding configuration: %s", bindingConfig));
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) Method(java.lang.reflect.Method) SatelBindingConfig(org.openhab.binding.satel.SatelBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 92 with BindingConfigParseException

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

the class SerialBinding method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    int indexOf = bindingConfig.indexOf(',');
    String serialPart = bindingConfig;
    String pattern = null;
    boolean base64 = false;
    if (indexOf != -1) {
        String substring = bindingConfig.substring(indexOf + 1);
        serialPart = bindingConfig.substring(0, indexOf);
        if (substring.startsWith("REGEX(")) {
            pattern = substring.substring(6, substring.length() - 1);
        }
        if (substring.equals("BASE64")) {
            base64 = true;
        }
    }
    String[] portConfig = serialPart.split("@");
    String port = portConfig[0];
    int baudRate = 0;
    if (portConfig.length > 1) {
        baudRate = Integer.parseInt(portConfig[1]);
    }
    SerialDevice serialDevice = serialDevices.get(port);
    if (serialDevice == null) {
        if (baudRate > 0) {
            serialDevice = new SerialDevice(port, baudRate);
        } else {
            serialDevice = new SerialDevice(port);
        }
        serialDevice.setTransformationService(transformationService);
        serialDevice.setEventPublisher(eventPublisher);
        try {
            serialDevice.initialize();
        } catch (InitializationException e) {
            throw new BindingConfigParseException("Could not open serial port " + port + ": " + e.getMessage());
        } catch (Throwable e) {
            throw new BindingConfigParseException("Could not open serial port " + port + ": " + e.getMessage());
        }
        itemMap.put(item.getName(), port);
        serialDevices.put(port, serialDevice);
    }
    serialDevice.addConfig(item.getName(), item.getClass(), pattern, base64);
    Set<String> itemNames = contextMap.get(context);
    if (itemNames == null) {
        itemNames = new HashSet<String>();
        contextMap.put(context, itemNames);
    }
    itemNames.add(item.getName());
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 93 with BindingConfigParseException

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

the class SappBindingConfigSwitchItem method parseSappAddressControl.

private SappAddressOnOffControl parseSappAddressControl(String bindingStringAddress) throws BindingConfigParseException {
    String pnmasId;
    SappAddressType addressType;
    int address;
    String subAddress;
    int onValue = 1;
    int offValue = 0;
    String[] bindingAddress = bindingStringAddress.split(":");
    if (bindingAddress.length != 4 && bindingAddress.length != 6) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // pnmasId
    pnmasId = bindingAddress[0];
    if (pnmasId.length() == 0) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // addressType
    addressType = SappAddressType.fromString(bindingAddress[1].toUpperCase());
    if (!validAddresses.keySet().contains(addressType)) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    if (addressType != SappAddressType.VIRTUAL) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // address
    try {
        address = Integer.parseInt(bindingAddress[2]);
        if (address < validAddresses.get(addressType).getLoRange() || address > validAddresses.get(addressType).getHiRange()) {
            throw new BindingConfigParseException(errorMessage(bindingStringAddress));
        }
    } catch (NumberFormatException e) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // subaddress
    subAddress = bindingAddress[3].toUpperCase();
    if (!ArrayUtils.contains(validSubAddresses, subAddress)) {
        throw new BindingConfigParseException(errorMessage(bindingStringAddress));
    }
    // onvalue, offvalue
    if (bindingAddress.length == 6) {
        try {
            onValue = Integer.parseInt(bindingAddress[4]);
            offValue = Integer.parseInt(bindingAddress[5]);
        } catch (NumberFormatException e) {
            throw new BindingConfigParseException(errorMessage(bindingStringAddress));
        }
    }
    return new SappAddressOnOffControl(pnmasId, addressType, address, subAddress, onValue, offValue);
}
Also used : SappAddressOnOffControl(org.openhab.binding.sapp.internal.model.SappAddressOnOffControl) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SappAddressType(org.openhab.binding.sapp.internal.model.SappAddressType)

Example 94 with BindingConfigParseException

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

the class SamsungAcGenericBindingProvider method parseBindingConfig.

private SamsungAcBindingConfig parseBindingConfig(Item item, String bindingConfig) throws BindingConfigParseException {
    Matcher matcher = CONFIG_PATTERN.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException("Config for item '" + item.getName() + "' could not be parsed.");
    }
    String acInstance = matcher.group(1);
    CommandEnum property = CommandEnum.valueOf(matcher.group(2));
    return new SamsungAcBindingConfig(acInstance, item.getName(), property);
}
Also used : Matcher(java.util.regex.Matcher) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 95 with BindingConfigParseException

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

the class SysteminfoGenericBindingProvider method processBindingConfiguration.

/**
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    super.processBindingConfiguration(context, item, bindingConfig);
    if (item == null) {
        throw new BindingConfigParseException("item is not permitted to be null");
    } else if (bindingConfig == null) {
        throw new BindingConfigParseException("bindingConfig is not permitted to be null");
    }
    SysteminfoBindingConfig config = new SysteminfoBindingConfig();
    String[] configParts = bindingConfig.trim().split(":");
    if (configParts.length < 2) {
        throw new BindingConfigParseException("Systeminfo binding must contain at least 2 parts separated by ':'");
    }
    String commandType = configParts[0].trim();
    if (configParts.length > 3) {
        try {
            int index1 = bindingConfig.indexOf(":");
            int index2 = bindingConfig.indexOf(":", index1 + 1);
            if (index1 > 0 && index2 > index1 + 1) {
                config.target = bindingConfig.substring(index2 + 1);
            } else {
                throw new BindingConfigParseException("Systeminfo binding must contain 2-3 parts separated by ':'");
            }
        } catch (Exception e) {
            throw new BindingConfigParseException("Systeminfo binding must contain 2-3 parts separated by ':'");
        }
    }
    try {
        config.commandType = SysteminfoCommandType.getCommandType(commandType);
    } catch (IllegalArgumentException e) {
        throw new BindingConfigParseException("'" + commandType + "' is not a valid command type");
    }
    try {
        config.refreshInterval = Integer.valueOf(configParts[1]);
    } catch (NumberFormatException e) {
        throw new BindingConfigParseException("'" + configParts[1] + "' is not a valid refresh interval");
    }
    if (config.target == null) {
        if (configParts.length > 2) {
            config.target = configParts[2].trim();
        }
    }
    addBindingConfig(item, config);
}
Also used : BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) 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