Search in sources :

Example 51 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class WindowHandleProfile method valueChanged.

@Override
public void valueChanged(ParameterAddress parameterAddress, Value valueObject) {
    Command command = null;
    if (valueObject.getValue().equals(Positions.DOWN.toString())) {
        command = new StringType("CLOSED");
    } else if (valueObject.getValue().equals(Positions.MIDDLE.toString())) {
        command = new StringType("OPEN");
    } else if (valueObject.getValue().equals(Positions.UP.toString())) {
        command = new StringType("AJAR");
    }
    postCommand(command);
}
Also used : Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType)

Example 52 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class KNXBinding method readFromKNX.

/**
     * Handles the given {@link ProcessEvent}. After finding the corresponding
     * Item (by iterating through all known group addresses) this Item is updated.
     * Each item is added to a special list to identify and avoid echo's in
     * the <code>receiveUpdate</code> and <code>receiveCommand</code> methods.
     *
     * @param e the {@link ProcessEvent} to handle.
     */
private void readFromKNX(ProcessEvent e) {
    try {
        GroupAddress destination = e.getDestination();
        byte[] asdu = e.getASDU();
        if (asdu.length == 0) {
            return;
        }
        String[] itemList = getItemNames(destination);
        if (itemList.length == 0) {
            logger.debug("Received telegram for unknown group address {}", destination.toString());
        }
        for (String itemName : itemList) {
            Iterable<Datapoint> datapoints = getDatapoints(itemName, destination);
            if (datapoints != null) {
                for (Datapoint datapoint : datapoints) {
                    Type type = getType(datapoint, asdu);
                    if (type != null) {
                        if (type instanceof Command && isStartStopEnabled(itemName, destination, datapoint)) {
                            if (isDimmerThreadRunning(itemName) && type == IncreaseDecreaseType.INCREASE) {
                                stopDimmerThread(itemName);
                            } else {
                                startDimmerThread(destination, itemName, (Command) type);
                            }
                        } else {
                            sendTypeToItemButNotToKnx(destination, itemName, type);
                        }
                    } else {
                        final char[] hexCode = "0123456789ABCDEF".toCharArray();
                        StringBuilder sb = new StringBuilder(2 + asdu.length * 2);
                        sb.append("0x");
                        for (byte b : asdu) {
                            sb.append(hexCode[(b >> 4) & 0xF]);
                            sb.append(hexCode[(b & 0xF)]);
                        }
                        logger.debug("Ignoring KNX bus data: couldn't transform to an openHAB type (not supported). Destination='{}', datapoint='{}', data='{}'", new Object[] { destination.toString(), datapoint.toString(), sb.toString() });
                    }
                }
            }
        }
    } catch (RuntimeException re) {
        logger.error("Error while receiving event from KNX bus: " + re.toString());
    }
}
Also used : Datapoint(tuwien.auto.calimero.datapoint.Datapoint) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) Type(org.openhab.core.types.Type) Command(org.openhab.core.types.Command) GroupAddress(tuwien.auto.calimero.GroupAddress)

Example 53 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class IRtransGenericBindingProvider method parseBindingConfig.

/**
     * Parses the binding config.
     *
     * @param config the config
     * @param item the item
     * @param bindingConfig the binding config
     * @throws BindingConfigParseException the binding config parse exception
     */
private void parseBindingConfig(IRtransBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String commandAsString = null;
    String host = null;
    String port = null;
    String led = null;
    String remote = null;
    String irCommand = null;
    Leds ledType = Leds.DEFAULT;
    if (bindingConfig != null) {
        Matcher actionMatcher = ACTION_CONFIG_PATTERN.matcher(bindingConfig);
        Matcher statusMatcher = STATUS_CONFIG_PATTERN.matcher(bindingConfig);
        if ((!actionMatcher.matches() && !statusMatcher.matches())) {
            throw new BindingConfigParseException(getBindingType() + " binding configuration must consist of five [config=" + statusMatcher.pattern() + "] or six parts [config=" + actionMatcher.pattern() + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                host = actionMatcher.group(2);
                port = actionMatcher.group(3);
                led = actionMatcher.group(4);
                remote = actionMatcher.group(5);
                irCommand = actionMatcher.group(6);
            } else if (statusMatcher.matches()) {
                host = statusMatcher.group(1);
                port = statusMatcher.group(2);
                led = statusMatcher.group(3);
                remote = statusMatcher.group(4);
                irCommand = statusMatcher.group(5);
            }
            if (led.equals("*")) {
                ledType = Leds.ALL;
            } else {
                ledType = Leds.valueOf(led);
            }
            IRtransBindingConfigElement newElement = new IRtransBindingConfigElement(host, port, ledType, remote, irCommand, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                // for those configuration strings that are not really linked to a openHAB command we
                // create a dummy Command to be able to store the configuration information
                // I have choosen to do that with NumberItems
                NumberItem dummy = new NumberItem(Integer.toString(counter));
                command = createCommandFromString(dummy, Integer.toString(counter));
                counter++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }
            config.put(command, newElement);
        }
    } else {
        return;
    }
}
Also used : NumberItem(org.openhab.core.library.items.NumberItem) Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 54 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class ExecGenericBindingProvider method parseLegacyOutBindingConfig.

protected void parseLegacyOutBindingConfig(Item item, String bindingConfig, ExecBindingConfig config) throws BindingConfigParseException {
    String command = StringUtils.substringBefore(bindingConfig, ":").trim();
    String tmpCommandLine = StringUtils.substringAfter(bindingConfig, ":").trim();
    if (StringUtils.isBlank(command) && StringUtils.isBlank(tmpCommandLine)) {
        return;
    }
    String commandLine;
    // if commandLine is surrounded by quotes, life is easy ...
    if (tmpCommandLine.startsWith("'")) {
        commandLine = tmpCommandLine.substring(1).split("(?<!\\\\)'")[0];
        // is there another command we have to parse?
        String tail = tmpCommandLine.replaceFirst(".*(?<!\\\\)' ?,", "").trim();
        if (!tail.isEmpty()) {
            parseLegacyOutBindingConfig(item, tail, config);
        }
    } else {
        // if not, we have to search for the next "," (if there are more than
        // one commandLines) or for the end of this line.
        String[] tmpCommandLineElements = tmpCommandLine.split("(?<!\\\\),");
        if (tmpCommandLineElements.length == 0) {
            commandLine = tmpCommandLine;
        } else {
            commandLine = tmpCommandLineElements[0];
            String tail = StringUtils.join(tmpCommandLineElements, ", ", 1, tmpCommandLineElements.length);
            parseLegacyOutBindingConfig(item, tail, config);
        }
    }
    ExecBindingConfigElement configElement = new ExecBindingConfigElement();
    configElement.commandLine = commandLine.replaceAll("(?<!\\\\)\\\\", "");
    Command cmd = createCommandFromString(item, command);
    config.put(cmd, configElement);
}
Also used : Command(org.openhab.core.types.Command)

Example 55 with Command

use of org.openhab.core.types.Command in project openhab1-addons by openhab.

the class ExecGenericBindingProvider method parseOutBindingConfig.

protected ExecBindingConfig parseOutBindingConfig(Item item, String bindingConfig, ExecBindingConfig config) throws BindingConfigParseException {
    Matcher matcher = OUT_BINDING_PATTERN.matcher(bindingConfig);
    if (!matcher.matches()) {
        throw new BindingConfigParseException("bindingConfig '" + bindingConfig + "' doesn't represent a valid in-binding-configuration.");
    }
    matcher.reset();
    ExecBindingConfigElement configElement;
    while (matcher.find()) {
        Command command = createCommandFromString(item, matcher.group(1));
        configElement = new ExecBindingConfigElement();
        configElement.commandLine = matcher.group(2).replaceAll("\\\\\"", "");
        config.put(command, configElement);
    }
    return config;
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Aggregations

Command (org.openhab.core.types.Command)61 ArrayList (java.util.ArrayList)20 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)14 State (org.openhab.core.types.State)12 Matcher (java.util.regex.Matcher)10 DecimalType (org.openhab.core.library.types.DecimalType)8 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)6 NikobusCommand (org.openhab.binding.nikobus.internal.core.NikobusCommand)6 SchedulerException (org.quartz.SchedulerException)6 StringType (org.openhab.core.library.types.StringType)5 JobDataMap (org.quartz.JobDataMap)5 JobDetail (org.quartz.JobDetail)5 Scheduler (org.quartz.Scheduler)5 Trigger (org.quartz.Trigger)5 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 IOException (java.io.IOException)4 SocketChannel (java.nio.channels.SocketChannel)4 IllegalClassException (org.apache.commons.lang.IllegalClassException)4 PercentType (org.openhab.core.library.types.PercentType)4