Search in sources :

Example 1 with ObjectType

use of org.openhab.binding.satel.internal.types.ObjectType in project openhab1-addons by openhab.

the class IntegraStateBindingConfig method parseConfig.

/**
     * Parses given binding configuration and creates configuration object.
     *
     * @param bindingConfig
     *            config to parse
     * @return parsed config object or <code>null</code> if config does not
     *         match
     * @throws BindingConfigParseException
     *             in case of parse errors
     */
public static IntegraStateBindingConfig parseConfig(String bindingConfig) throws BindingConfigParseException {
    ConfigIterator iterator = new ConfigIterator(bindingConfig);
    ObjectType objectType;
    // parse object type, mandatory
    try {
        objectType = iterator.nextOfType(ObjectType.class, "object type");
    } catch (Exception e) {
        // wrong config type, skip parsing
        return null;
    }
    // parse state type, mandatory except for output
    StateType stateType = null;
    int[] objectNumbers = {};
    switch(objectType) {
        case ZONE:
            stateType = iterator.nextOfType(ZoneState.class, "zone state type");
            break;
        case PARTITION:
            stateType = iterator.nextOfType(PartitionState.class, "partition state type");
            break;
        case OUTPUT:
            stateType = OutputState.OUTPUT;
            break;
        case DOORS:
            stateType = iterator.nextOfType(DoorsState.class, "doors state type");
            break;
    }
    // parse object number, if provided
    if (iterator.hasNext()) {
        try {
            String[] objectNumbersStr = iterator.next().split(",");
            objectNumbers = new int[objectNumbersStr.length];
            for (int i = 0; i < objectNumbersStr.length; ++i) {
                int objectNumber = Integer.parseInt(objectNumbersStr[i]);
                if (objectNumber < 1 || objectNumber > 256) {
                    throw new BindingConfigParseException(String.format("Invalid object number: %s", bindingConfig));
                }
                objectNumbers[i] = objectNumber;
            }
        } catch (NumberFormatException e) {
            throw new BindingConfigParseException(String.format("Invalid object number: %s", bindingConfig));
        }
    }
    return new IntegraStateBindingConfig(stateType, objectNumbers, iterator.parseOptions());
}
Also used : ZoneState(org.openhab.binding.satel.internal.types.ZoneState) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) DoorsState(org.openhab.binding.satel.internal.types.DoorsState) ObjectType(org.openhab.binding.satel.internal.types.ObjectType) StateType(org.openhab.binding.satel.internal.types.StateType) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) PartitionState(org.openhab.binding.satel.internal.types.PartitionState)

Aggregations

DoorsState (org.openhab.binding.satel.internal.types.DoorsState)1 ObjectType (org.openhab.binding.satel.internal.types.ObjectType)1 PartitionState (org.openhab.binding.satel.internal.types.PartitionState)1 StateType (org.openhab.binding.satel.internal.types.StateType)1 ZoneState (org.openhab.binding.satel.internal.types.ZoneState)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1