Search in sources :

Example 6 with ZWaveBindingConfig

use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.

the class ZWaveGenericBindingProvider method processBindingConfiguration.

/**
     * Processes Z-Wave binding configuration string.
     * {@inheritDoc}
     */
@Override
public void processBindingConfiguration(String context, Item item, String bindingConfig) throws BindingConfigParseException {
    logger.trace("processBindingConfiguration({}, {})", item.getName(), bindingConfig);
    super.processBindingConfiguration(context, item, bindingConfig);
    String[] segments = bindingConfig.split(":");
    if (segments.length < 1 || segments.length > 3) {
        throw new BindingConfigParseException("Invalid number of segments in binding: " + bindingConfig);
    }
    int nodeId;
    try {
        nodeId = Integer.parseInt(segments[0]);
    } catch (Exception e) {
        logger.error("{}: Invalid node ID '{}'", item.getName(), segments[0]);
        throw new BindingConfigParseException(segments[0] + " is not a valid node id.");
    }
    if (nodeId <= 0 || nodeId > 232) {
        logger.error("{}: Invalid node ID '{}'", item.getName(), nodeId);
        throw new BindingConfigParseException(nodeId + " is not a valid node number.");
    }
    int endpoint = 0;
    Integer refreshInterval = null;
    Map<String, String> arguments = new HashMap<String, String>();
    for (int i = 1; i < segments.length; i++) {
        try {
            if (segments[i].contains("=")) {
                for (String keyValuePairString : segments[i].split(",")) {
                    String[] pair = keyValuePairString.split("=");
                    String key = pair[0].trim().toLowerCase();
                    String value = pair[1].trim().toLowerCase();
                    if (key.equals("refresh_interval")) {
                        refreshInterval = Integer.parseInt(value);
                    } else {
                        arguments.put(key, value);
                    }
                    // Sanity check the command class
                    if (key.equals("command")) {
                        if (ZWaveCommandClass.CommandClass.getCommandClass(pair[1]) == null && value.equals("info") == false) {
                            logger.error("{}: Invalid command class '{}'", item.getName(), pair[1].toUpperCase());
                            throw new BindingConfigParseException("Invalid command class " + pair[1].toUpperCase());
                        }
                    }
                }
            } else {
                try {
                    endpoint = Integer.parseInt(segments[i]);
                } catch (Exception e) {
                    logger.error("{}: Invalid endpoint ID '{}'", item.getName(), segments[i]);
                    throw new BindingConfigParseException(segments[i] + " is not a valid endpoint.");
                }
            }
        } catch (Exception e) {
            throw new BindingConfigParseException(segments[i] + " is not a valid argument.");
        }
    }
    ZWaveBindingConfig config = new ZWaveBindingConfig(nodeId, endpoint, refreshInterval, arguments);
    addBindingConfig(item, config);
    items.put(item.getName(), item);
}
Also used : HashMap(java.util.HashMap) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) ZWaveBindingConfig(org.openhab.binding.zwave.ZWaveBindingConfig) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 7 with ZWaveBindingConfig

use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.

the class ZWaveConverterHandler method getRefreshInterval.

/**
     * Get the refresh interval for an item binding
     *
     * @param provider
     *            the {@link ZWaveBindingProvider} that provides the item
     * @param itemName
     *            the name of the item to poll.
     */
@SuppressWarnings("unchecked")
public Integer getRefreshInterval(ZWaveBindingProvider provider, String itemName) {
    ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
    ZWaveCommandClass commandClass;
    String commandClassName = bindingConfiguration.getArguments().get("command");
    // this binding is configured not to poll.
    if (bindingConfiguration.getRefreshInterval() != null && 0 == bindingConfiguration.getRefreshInterval()) {
        return 0;
    }
    ZWaveNode node = this.controller.getNode(bindingConfiguration.getNodeId());
    // ignore nodes that are not initialized.
    if (node == null) {
        return 0;
    }
    if (commandClassName != null) {
        // this is a report item, handle it with the report info converter.
        if (commandClassName.equalsIgnoreCase("info")) {
            return infoConverter.getRefreshInterval();
        }
        if (node.getNodeId() == this.controller.getOwnNodeId() && commandClassName.equalsIgnoreCase("switch_all")) {
            return 0;
        }
        commandClass = node.resolveCommandClass(CommandClass.getCommandClass(commandClassName), bindingConfiguration.getEndpoint());
        if (commandClass == null) {
            logger.warn("No command class found for item = {}, command class name = {}, using 0 refresh interval.", itemName, commandClassName);
            return 0;
        }
    } else {
        commandClass = resolveConverter(provider.getItem(itemName), node, bindingConfiguration.getEndpoint());
    }
    if (commandClass == null) {
        logger.warn("No converter found for item = {}, using 0 refresh interval.", itemName);
        return 0;
    }
    ZWaveCommandClassConverter<ZWaveCommandClass> converter = (ZWaveCommandClassConverter<ZWaveCommandClass>) getConverter(commandClass.getCommandClass());
    if (converter == null) {
        logger.warn("No converter found for item = {}, using 0 refresh interval.", itemName);
        return 0;
    }
    if (bindingConfiguration.getRefreshInterval() == null) {
        bindingConfiguration.setRefreshInterval(converter.getRefreshInterval());
    }
    return bindingConfiguration.getRefreshInterval();
}
Also used : ZWaveCommandClass(org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass) ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveBindingConfig(org.openhab.binding.zwave.ZWaveBindingConfig)

Example 8 with ZWaveBindingConfig

use of org.openhab.binding.zwave.ZWaveBindingConfig in project openhab1-addons by openhab.

the class ZWaveConverterHandler method handleEvent.

/**
     * Handles an incoming {@link ZWaveCommandClassValueEvent}. Implement this
     * message in derived classes to convert the value and post an update on the
     * openHAB bus.
     *
     * @param provider
     *            the {@link ZWaveBindingProvider} that provides the item
     * @param itemName
     *            the name of the item that will receive the event.
     * @param event
     *            the received {@link ZWaveCommandClassValueEvent}.
     */
public void handleEvent(ZWaveBindingProvider provider, String itemName, ZWaveCommandClassValueEvent event) {
    ZWaveBindingConfig bindingConfiguration = provider.getZwaveBindingConfig(itemName);
    Item item = provider.getItem(itemName);
    String commandClassName = bindingConfiguration.getArguments().get("command");
    boolean respondToBasic = "true".equalsIgnoreCase(bindingConfiguration.getArguments().get("respond_to_basic"));
    logger.trace("Getting converter for item = {}, command class = {}, item command class = {}", itemName, event.getCommandClass().getLabel(), commandClassName);
    if (item == null) {
        return;
    }
    if (commandClassName != null && !commandClassName.equalsIgnoreCase(event.getCommandClass().getLabel().toLowerCase()) && !(respondToBasic && event.getCommandClass() == CommandClass.BASIC)) {
        return;
    }
    ZWaveCommandClassConverter<?> converter = this.getConverter(event.getCommandClass());
    if (converter == null) {
        logger.warn("No converter found for command class = {}, ignoring event.", event.getCommandClass().toString());
        return;
    }
    converter.handleEvent(event, item, bindingConfiguration.getArguments());
}
Also used : DimmerItem(org.openhab.core.library.items.DimmerItem) SwitchItem(org.openhab.core.library.items.SwitchItem) RollershutterItem(org.openhab.core.library.items.RollershutterItem) Item(org.openhab.core.items.Item) ContactItem(org.openhab.core.library.items.ContactItem) NumberItem(org.openhab.core.library.items.NumberItem) ZWaveBindingConfig(org.openhab.binding.zwave.ZWaveBindingConfig)

Aggregations

ZWaveBindingConfig (org.openhab.binding.zwave.ZWaveBindingConfig)8 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)4 ZWaveBindingProvider (org.openhab.binding.zwave.ZWaveBindingProvider)3 ZWaveCommandClass (org.openhab.binding.zwave.internal.protocol.commandclass.ZWaveCommandClass)3 HashMap (java.util.HashMap)1 SerialMessage (org.openhab.binding.zwave.internal.protocol.SerialMessage)1 Item (org.openhab.core.items.Item)1 ContactItem (org.openhab.core.library.items.ContactItem)1 DimmerItem (org.openhab.core.library.items.DimmerItem)1 NumberItem (org.openhab.core.library.items.NumberItem)1 RollershutterItem (org.openhab.core.library.items.RollershutterItem)1 SwitchItem (org.openhab.core.library.items.SwitchItem)1 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)1