Search in sources :

Example 6 with BindingConfigParseException

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

the class SnmpGenericBindingProvider method parseAddress.

private Address parseAddress(String s) throws BindingConfigParseException {
    String addressString = s.contains("/") ? s : s + "/161";
    Address address = GenericAddress.parse("udp:" + addressString);
    if (address == null) {
        throw new BindingConfigParseException(getBindingType() + " binding configuration address is invalid: " + s);
    }
    return address;
}
Also used : Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) OctetString(org.snmp4j.smi.OctetString)

Example 7 with BindingConfigParseException

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

the class SnmpGenericBindingProvider method parseBindingConfig.

/**
     * Parses a SNMP-OUT configuration by using the regular expression
     * <code>([0-9.a-zA-Z/]+):([0-9.a-zA-Z]+):([0-9.a-zA-Z]+):([0-9]+)</code>.
     * Where the groups should contain the following content:
     * <ul>
     * <li>Command</li>
     * <li>url: ip[/port], port is optional, default: 161</li>
     * <li>[Optional]Version: v1, v2c, v3</li>
     * <li>SNMP community</li>
     * <li>OID</li>
     * <li>Value</li>
     * </ul>
     *
     * Parses a SNMP-IN configuration by using the regular expression
     * <code>([0-9.a-zA-Z/]+):([0-9.a-zA-Z]+):([0-9.a-zA-Z]+):([0-9]+)</code>.
     * Where the groups should contain the following content:
     * <ul>
     * <li>url: ip[/port], port is optional, default: 161</li>
     * <li>[Optional]Version: v1, v2c, v3</li>
     * <li>SNMP community</li>
     * <li>OID</li>
     * <li>Refresh interval (ms)</li>
     * <li>[Optional]transformation rule</li>
     * </ul>
     *
     * Setting refresh interval to 0 will only receive SNMP traps
     *
     * @param config
     *            - the Configuration that needs to be updated with the parsing
     *            results
     * @param item
     *            - the Item that this configuration is intended for
     * @param bindingConfig
     *            - the configuration string that will be parsed
     * @throws BindingConfigParseException
     */
private void parseBindingConfig(SnmpBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    config.itemType = item.getClass();
    if (bindingConfig != null) {
        // try in without version first
        Matcher inMatcher = IN_BINDING_PATTERN.matcher(bindingConfig);
        if (!inMatcher.matches()) {
            inMatcher = IN_BINDING_PATTERN_TRANSFORM.matcher(bindingConfig);
        }
        if (inMatcher.matches()) {
            SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
            newElement.address = parseAddress(inMatcher.group(1).toString());
            newElement.snmpVersion = SnmpConstants.version1;
            newElement.community = new OctetString(inMatcher.group(2).toString());
            newElement.oid = new OID(inMatcher.group(3).toString());
            newElement.refreshInterval = Integer.valueOf(inMatcher.group(4)).intValue();
            if (inMatcher.groupCount() == 5) {
                newElement.setTransformationRule(inMatcher.group(5));
            }
            config.put(IN_BINDING_KEY, newElement);
        } else {
            // not matched, try with version
            inMatcher = IN_BINDING_PATTERN_VERSION.matcher(bindingConfig);
            if (!inMatcher.matches()) {
                inMatcher = IN_BINDING_PATTERN_VERSION_TRANSFORM.matcher(bindingConfig);
            }
            if (inMatcher.matches()) {
                SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
                newElement.address = parseAddress(inMatcher.group(1).toString());
                String version = inMatcher.group(2).toString();
                if (version.equals("v3")) {
                    newElement.snmpVersion = SnmpConstants.version3;
                } else if (version.equals("v2c")) {
                    newElement.snmpVersion = SnmpConstants.version2c;
                } else {
                    newElement.snmpVersion = SnmpConstants.version1;
                }
                newElement.community = new OctetString(inMatcher.group(3).toString());
                newElement.oid = new OID(inMatcher.group(4).toString());
                newElement.refreshInterval = Integer.valueOf(inMatcher.group(5)).intValue();
                if (inMatcher.groupCount() == 6) {
                    newElement.setTransformationRule(inMatcher.group(6));
                }
                config.put(IN_BINDING_KEY, newElement);
            }
        }
        Matcher outMatcher = OUT_BINDING_PATTERN.matcher(bindingConfig);
        if (outMatcher.matches()) {
            SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
            String commandAsString = outMatcher.group(1).toString();
            newElement.address = parseAddress(outMatcher.group(2).toString());
            newElement.snmpVersion = SnmpConstants.version1;
            newElement.community = new OctetString(outMatcher.group(3).toString());
            newElement.oid = new OID(outMatcher.group(4).toString());
            // Only Integer commands accepted at this time.
            newElement.value = new Integer32(Integer.parseInt(outMatcher.group(5).toString()));
            Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandAsString);
            if (command == null) {
                logger.error("SNMP can't resolve command {} for item {}", commandAsString, item);
            } else {
                config.put(command, newElement);
            }
        } else {
            outMatcher = OUT_BINDING_PATTERN_VERSION.matcher(bindingConfig);
            if (outMatcher.matches()) {
                SnmpBindingConfigElement newElement = new SnmpBindingConfigElement();
                String commandAsString = outMatcher.group(1).toString();
                newElement.address = parseAddress(outMatcher.group(2).toString());
                String version = inMatcher.group(3).toString();
                if (version.equals("v3")) {
                    newElement.snmpVersion = SnmpConstants.version3;
                } else if (version.equals("v2c")) {
                    newElement.snmpVersion = SnmpConstants.version2c;
                } else {
                    newElement.snmpVersion = SnmpConstants.version1;
                }
                newElement.community = new OctetString(outMatcher.group(4).toString());
                newElement.oid = new OID(outMatcher.group(5).toString());
                // Only Integer commands accepted at this time.
                newElement.value = new Integer32(Integer.parseInt(outMatcher.group(6).toString()));
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandAsString);
                if (command == null) {
                    logger.error("SNMP can't resolve command {} for item {}", commandAsString, item);
                } else {
                    config.put(command, newElement);
                }
            }
        }
        // have we found any matches?
        if (!outMatcher.matches() && !inMatcher.matches()) {
            throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of four/five/six [config=" + inMatcher + "] or five/six parts [config=" + outMatcher + "]");
        }
    } else {
        return;
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) Integer32(org.snmp4j.smi.Integer32) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString)

Example 8 with BindingConfigParseException

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

the class SonosBinding method processVariableMap.

@SuppressWarnings("rawtypes")
public void processVariableMap(RemoteDevice device, Map<String, StateVariableValue> values) {
    if (device != null && values != null) {
        SonosZonePlayer associatedPlayer = sonosZonePlayerCache.getByDevice(device);
        if (associatedPlayer == null) {
            logger.debug("There is no Sonos Player defined matching the device {}", device);
            return;
        }
        for (String stateVariable : values.keySet()) {
            // find all the CommandTypes that are defined for each
            // StateVariable
            List<SonosCommandType> supportedCommands = SonosCommandType.getCommandByVariable(stateVariable);
            StateVariableValue status = values.get(stateVariable);
            for (SonosCommandType sonosCommandType : supportedCommands) {
                // create a new State based on the type of Sonos Command and
                // the status value in the map
                Type newState = null;
                try {
                    newState = createStateForType((Class<? extends State>) sonosCommandType.getTypeClass(), status.getValue().toString());
                } catch (BindingConfigParseException e) {
                    logger.error("Error parsing a value {} to a state variable of type {}", status.toString(), sonosCommandType.getTypeClass().toString());
                }
                for (SonosBindingProvider provider : providers) {
                    List<String> qualifiedItems = provider.getItemNames(sonosZonePlayerCache.getByDevice(device).getId(), sonosCommandType.getSonosCommand());
                    List<String> qualifiedItemsByUDN = provider.getItemNames(sonosZonePlayerCache.getByDevice(device).getUdn().getIdentifierString(), sonosCommandType.getSonosCommand());
                    for (String item : qualifiedItemsByUDN) {
                        if (!qualifiedItems.contains(item)) {
                            qualifiedItems.add(item);
                        }
                    }
                    for (String anItem : qualifiedItems) {
                        // get the openHAB commands attached to each Item at
                        // this given Provider
                        List<Command> commands = provider.getCommands(anItem, sonosCommandType.getSonosCommand());
                        if (provider.getAcceptedDataTypes(anItem).contains(sonosCommandType.getTypeClass())) {
                            if (newState != null) {
                                eventPublisher.postUpdate(anItem, (State) newState);
                            } else {
                                throw new IllegalClassException("Cannot process update for the command of type " + sonosCommandType.toString());
                            }
                        } else {
                            logger.warn("Cannot cast {} to an accepted state type for item {}", sonosCommandType.getTypeClass().toString(), anItem);
                        }
                    }
                }
            }
        }
    }
}
Also used : StateVariableValue(org.teleal.cling.model.state.StateVariableValue) SonosCommandType(org.openhab.binding.sonos.SonosCommandType) IllegalClassException(org.apache.commons.lang.IllegalClassException) StringType(org.openhab.core.library.types.StringType) SonosCommandType(org.openhab.binding.sonos.SonosCommandType) OnOffType(org.openhab.core.library.types.OnOffType) UDAServiceType(org.teleal.cling.model.types.UDAServiceType) Type(org.openhab.core.types.Type) DecimalType(org.openhab.core.library.types.DecimalType) SonosBindingProvider(org.openhab.binding.sonos.SonosBindingProvider) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 9 with BindingConfigParseException

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

the class SonosGenericBindingProvider method createCommandFromString.

/**
     * Creates a {@link Command} out of the given <code>commandAsString</code>
     * incorporating the {@link TypeParser}.
     * 
     * @param item, or null if the Command has to be of the StringType type
     * @param commandAsString
     * 
     * @return an appropriate Command (see {@link TypeParser} for more
     *         information
     * 
     * @throws BindingConfigParseException if the {@link TypeParser} couldn't
     *             create a command appropriately
     * 
     * @see {@link TypeParser}
     */
private Command createCommandFromString(Item item, String commandAsString) throws BindingConfigParseException {
    List<Class<? extends Command>> acceptedTypes = new ArrayList<Class<? extends Command>>();
    if (item != null) {
        acceptedTypes = item.getAcceptedCommandTypes();
    } else {
        acceptedTypes.add(StringType.class);
    }
    Command command = TypeParser.parseCommand(acceptedTypes, commandAsString);
    if (command == null) {
        throw new BindingConfigParseException("couldn't create Command from '" + commandAsString + "' ");
    }
    return command;
}
Also used : Command(org.openhab.core.types.Command) ArrayList(java.util.ArrayList) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 10 with BindingConfigParseException

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

the class EpsonProjectorGenericBindingProvider method getCommandTypeFromString.

private EpsonProjectorCommandType getCommandTypeFromString(String commandTypeString, Item item) throws BindingConfigParseException {
    EpsonProjectorCommandType commandType = null;
    try {
        EpsonProjectorCommandType.validateBinding(commandTypeString, item.getClass());
        commandType = EpsonProjectorCommandType.getCommandType(commandTypeString);
    } catch (IllegalArgumentException e) {
        throw new BindingConfigParseException("Invalid command type '" + commandTypeString + "'!");
    } catch (InvalidClassException e) {
        throw new BindingConfigParseException("Invalid item type for command type '" + commandTypeString + "'!");
    }
    return commandType;
}
Also used : InvalidClassException(java.io.InvalidClassException) 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