Search in sources :

Example 1 with DataType

use of org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType in project org.openhab.binding.zwave by openhab.

the class ZWaveThingHandler method initialiseNode.

void initialiseNode() {
    logger.debug("NODE {}: Initialising Thing Node...", nodeId);
    // Note that for dynamic channels, it seems that defaults can either be not set, or set with the incorrect
    // type. So, we read back as an Object to avoid casting problems.
    pollingPeriod = POLLING_PERIOD_DEFAULT;
    final Object pollParm = getConfig().get(ZWaveBindingConstants.CONFIGURATION_POLLPERIOD);
    if (pollParm instanceof BigDecimal) {
        try {
            pollingPeriod = ((BigDecimal) pollParm).intValue();
        } catch (final NumberFormatException ex) {
            logger.debug("NODE {}: pollingPeriod ({}) cannot be parsed - using default", nodeId, pollParm);
        }
    }
    final Object repollParm = getConfig().get(ZWaveBindingConstants.CONFIGURATION_CMDREPOLLPERIOD);
    if (repollParm instanceof BigDecimal) {
        try {
            commandPollDelay = ((BigDecimal) repollParm).intValue();
        } catch (final NumberFormatException ex) {
            logger.debug("NODE {}: commandPollDelay ({}) cannot be parsed - using default", nodeId, repollParm);
        }
    }
    // Create the channels list to simplify processing incoming events
    thingChannelsCmd = new ArrayList<ZWaveThingChannel>();
    thingChannelsState = new ArrayList<ZWaveThingChannel>();
    for (Channel channel : getThing().getChannels()) {
        logger.trace("NODE {}: Processing channel: {} == {}", nodeId, channel.getUID(), channel.getChannelTypeUID());
        // Process the channel properties and configuration
        Map<String, String> properties = channel.getProperties();
        Configuration configuration = channel.getConfiguration();
        for (String key : properties.keySet()) {
            logger.trace("NODE {}: Processing channel: {} == {}", nodeId, key, properties.get(key));
            String[] bindingType = key.split(":");
            if (bindingType.length != 3) {
                logger.trace("NODE {}: binding string != 3", nodeId);
                continue;
            }
            if (!ZWaveBindingConstants.CHANNEL_CFG_BINDING.equals(bindingType[0])) {
                logger.trace("NODE {}: binding string != CFG_BINDING", nodeId);
                continue;
            }
            String[] bindingProperties = properties.get(key).split(";");
            // TODO: Check length???
            // Get the command classes - comma separated
            String[] cmdClasses = bindingProperties[0].split(",");
            // Convert the arguments to a map
            // - comma separated list of arguments "arg1=val1, arg2=val2"
            Map<String, String> argumentMap = new HashMap<String, String>();
            if (bindingProperties.length == 2) {
                argumentMap = getZWaveProperties(bindingProperties[1]);
            }
            // Process the user configuration and add it to the argument map
            for (String configName : configuration.getProperties().keySet()) {
                argumentMap.put(configName, configuration.get(configName).toString());
            }
            // Add all the command classes...
            boolean first = true;
            for (String cc : cmdClasses) {
                String[] ccSplit = cc.split(":");
                int endpoint = 0;
                if (ccSplit.length == 2) {
                    endpoint = Integer.parseInt(ccSplit[1]);
                }
                // Get the data type
                DataType dataType = DataType.DecimalType;
                try {
                    dataType = DataType.valueOf(bindingType[2]);
                } catch (IllegalArgumentException e) {
                    logger.warn("NODE {}: Invalid item type defined {} for {}. Assuming DecimalType.", nodeId, bindingType[2], channel.getUID());
                }
                ZWaveThingChannel chan = new ZWaveThingChannel(controllerHandler, channel.getChannelTypeUID(), channel.getUID(), dataType, ccSplit[0], endpoint, argumentMap);
                // First time round, and this is a command - then add the command
                if (first && ("*".equals(bindingType[1]) || "Command".equals(bindingType[1]))) {
                    thingChannelsCmd.add(chan);
                    logger.debug("NODE {}: Initialising cmd channel {} for {}", nodeId, channel.getUID(), dataType);
                }
                // Add the state and polling handlers
                if ("*".equals(bindingType[1]) || "State".equals(bindingType[1])) {
                    logger.debug("NODE {}: Initialising state channel {} for {}", nodeId, channel.getUID(), dataType);
                    thingChannelsState.add(chan);
                }
                first = false;
            }
        }
        // if the channel is already linked, add it to our list of channels to poll for
        if (isLinked(channel.getUID().getId())) {
            thingChannelsPoll.add(channel.getUID());
        }
    }
    startPolling();
}
Also used : Configuration(org.openhab.core.config.core.Configuration) HashMap(java.util.HashMap) Channel(org.openhab.core.thing.Channel) BigDecimal(java.math.BigDecimal) DataType(org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType)

Example 2 with DataType

use of org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType in project org.openhab.binding.zwave by openhab.

the class ZWaveThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command commandParam) {
    Command command = commandParam;
    logger.debug("NODE {}: Command received {} --> {} [{}]", nodeId, channelUID, command, command.getClass().getSimpleName());
    if (controllerHandler == null) {
        logger.debug("NODE {}: Controller handler not found. Cannot handle command without ZWave controller.", nodeId);
        return;
    }
    if (command == RefreshType.REFRESH) {
        startPolling(REFRESH_POLL_DELAY);
        return;
    }
    DataType dataType;
    try {
        dataType = DataType.fromTypeClass(command.getClass());
    } catch (IllegalArgumentException e) {
        logger.warn("NODE {}: Command received with no implementation ({}).", nodeId, command.getClass().getSimpleName());
        return;
    }
    // Find the channel
    Map<DataType, ZWaveThingChannel> cmdChannels = new HashMap<>();
    for (ZWaveThingChannel channel : thingChannelsCmd) {
        if (channel.getUID().equals(channelUID)) {
            cmdChannels.put(channel.getDataType(), channel);
        }
    }
    // first try to get a channel by the expected datatype
    ZWaveThingChannel cmdChannel = cmdChannels.get(dataType);
    if (cmdChannel == null && !cmdChannels.isEmpty()) {
        // nothing by expected datatype found, try to find one where the datatype can be converted to
        for (ZWaveThingChannel channel : cmdChannels.values()) {
            command = convertCommandToDataType(channelUID, channel.getDataType(), command, dataType);
            if (command != null) {
                cmdChannel = channel;
                logger.debug("NODE {}: Received command {} was converted --> {} [{}]", nodeId, channelUID, command, command.getClass().getSimpleName());
                break;
            }
        }
    }
    if (cmdChannel == null) {
        logger.debug("NODE {}: Command for unknown channel {} with {}", nodeId, channelUID, dataType);
        return;
    }
    ZWaveNode node = controllerHandler.getNode(nodeId);
    if (node == null) {
        logger.debug("NODE {}: Node is not found for {}", nodeId, channelUID);
        return;
    }
    if (cmdChannel.getConverter() == null) {
        logger.warn("NODE {}: No command converter set for command {} type {}", nodeId, channelUID, dataType);
        return;
    }
    List<ZWaveCommandClassTransactionPayload> messages = cmdChannel.getConverter().receiveCommand(cmdChannel, node, command);
    if (messages == null) {
        logger.debug("NODE {}: No messages returned from converter", nodeId);
        return;
    }
    // Send all the messages
    for (ZWaveCommandClassTransactionPayload message : messages) {
        controllerHandler.sendData(message);
    }
    // Restart the polling so we get an update on the channel shortly after this command is sent
    if (commandPollDelay != 0) {
        startPolling(commandPollDelay);
    }
}
Also used : ZWaveNode(org.openhab.binding.zwave.internal.protocol.ZWaveNode) ZWaveCommandClassTransactionPayload(org.openhab.binding.zwave.internal.protocol.transaction.ZWaveCommandClassTransactionPayload) Command(org.openhab.core.types.Command) HashMap(java.util.HashMap) DataType(org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType)

Aggregations

HashMap (java.util.HashMap)2 DataType (org.openhab.binding.zwave.handler.ZWaveThingChannel.DataType)2 BigDecimal (java.math.BigDecimal)1 ZWaveNode (org.openhab.binding.zwave.internal.protocol.ZWaveNode)1 ZWaveCommandClassTransactionPayload (org.openhab.binding.zwave.internal.protocol.transaction.ZWaveCommandClassTransactionPayload)1 Configuration (org.openhab.core.config.core.Configuration)1 Channel (org.openhab.core.thing.Channel)1 Command (org.openhab.core.types.Command)1