Search in sources :

Example 41 with Command

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

the class SonosBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    SonosBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    String commandAsString = command.toString();
    if (command != null) {
        List<Command> commands = new ArrayList<Command>();
        if (command instanceof StringType || command instanceof DecimalType) {
            commands = provider.getVariableCommands(itemName);
        } else {
            commands.add(command);
        }
        for (Command someCommand : commands) {
            String sonosID = provider.getSonosID(itemName, someCommand);
            String sonosCommand = provider.getSonosCommand(itemName, someCommand);
            SonosCommandType sonosCommandType = null;
            try {
                sonosCommandType = SonosCommandType.getCommandType(sonosCommand, Direction.OUT);
            } catch (Exception e) {
                logger.error("An exception occured while verifying command compatibility ({})", e.getMessage());
            }
            if (sonosID != null) {
                if (sonosCommandType != null) {
                    logger.debug("Executing command: item:{}, command:{}, ID:{}, CommandType:{}, commandString:{}", new Object[] { itemName, someCommand, sonosID, sonosCommandType, commandAsString });
                    executeCommand(itemName, someCommand, sonosID, sonosCommandType, commandAsString);
                } else {
                    logger.error("wrong command type for binding [Item={}, command={}]", itemName, commandAsString);
                }
            } else {
                logger.error("{} is an unrecognised command for Item {}", commandAsString, itemName);
            }
        }
    }
}
Also used : SonosBindingProvider(org.openhab.binding.sonos.SonosBindingProvider) Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) SonosCommandType(org.openhab.binding.sonos.SonosCommandType) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType) ConfigurationException(org.osgi.service.cm.ConfigurationException) IllegalClassException(org.apache.commons.lang.IllegalClassException) JobExecutionException(org.quartz.JobExecutionException) SAXException(org.xml.sax.SAXException) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException) SchedulerException(org.quartz.SchedulerException)

Example 42 with Command

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

the class SonosGenericBindingProvider method getVariableCommands.

@Override
public List<Command> getVariableCommands(String anItem) {
    List<Command> commands = new ArrayList<Command>();
    SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(anItem);
    for (Command aCommand : aConfig.keySet()) {
        if (aCommand instanceof StringType || aCommand instanceof DecimalType) {
            commands.add(aCommand);
        }
    }
    return commands;
}
Also used : Command(org.openhab.core.types.Command) StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) DecimalType(org.openhab.core.library.types.DecimalType)

Example 43 with Command

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

the class SonosGenericBindingProvider method getCommands.

@Override
public List<Command> getCommands(String anItem, String sonosCommand) {
    List<Command> commands = new ArrayList<Command>();
    SonosBindingConfig aConfig = (SonosBindingConfig) bindingConfigs.get(anItem);
    for (Command aCommand : aConfig.keySet()) {
        SonosBindingConfigElement anElement = aConfig.get(aCommand);
        if (anElement.getSonosCommand().equals(sonosCommand)) {
            commands.add(aCommand);
        }
    }
    return commands;
}
Also used : Command(org.openhab.core.types.Command) ArrayList(java.util.ArrayList)

Example 44 with Command

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

the class SonosGenericBindingProvider method parseBindingConfig.

private void parseBindingConfig(SonosBindingConfig config, Item item, String bindingConfig) throws BindingConfigParseException {
    String sonosID = null;
    String commandAsString = null;
    String sonosCommand = null;
    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("Sonos binding configuration must consist of either two [config=" + statusMatcher + "] or three parts [config=" + actionMatcher + "]");
        } else {
            if (actionMatcher.matches()) {
                commandAsString = actionMatcher.group(1);
                sonosID = actionMatcher.group(2);
                sonosCommand = actionMatcher.group(3);
            } else if (statusMatcher.matches()) {
                commandAsString = null;
                sonosID = statusMatcher.group(1);
                sonosCommand = statusMatcher.group(2);
            }
            SonosBindingConfigElement newElement = new SonosBindingConfigElement(sonosCommand, sonosID, item.getAcceptedDataTypes());
            Command command = null;
            if (commandAsString == null) {
                command = createCommandFromString(null, Integer.toString(counter));
                counter++;
                config.put(command, newElement);
            } else {
                command = createCommandFromString(item, commandAsString);
                config.put(command, newElement);
            }
        }
    } else {
        return;
    }
}
Also used : Matcher(java.util.regex.Matcher) Command(org.openhab.core.types.Command) BindingConfigParseException(org.openhab.model.item.binding.BindingConfigParseException)

Example 45 with Command

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

the class IhcBinding method updateResource.

/**
     * Update resource value to IHC controller.
     */
private void updateResource(String itemName, Type type, boolean updateOnlyExclusiveOutBinding) {
    if (itemName != null) {
        Command cmd = null;
        try {
            cmd = (Command) type;
        } catch (Exception e) {
        }
        IhcBindingProvider provider = findFirstMatchingBindingProvider(itemName, cmd);
        if (provider == null) {
            // command not configured, skip
            return;
        }
        if (updateOnlyExclusiveOutBinding && provider.hasInBinding(itemName)) {
            logger.trace("Ignore in binding update for item '{}'", itemName);
            return;
        }
        logger.debug("Received update/command (item='{}', state='{}', class='{}')", new Object[] { itemName, type.toString(), type.getClass().toString() });
        if (ihc == null) {
            logger.warn("Controller is not initialized, abort resource value update for item '{}'!", itemName);
            return;
        }
        if (ihc.getConnectionState() != ConnectionState.CONNECTED) {
            logger.warn("Connection to controller is not ok, abort resource value update for item '{}'!", itemName);
            return;
        }
        try {
            int resourceId = provider.getResourceId(itemName, (Command) type);
            logger.trace("found resourceId {} (item='{}', state='{}', class='{}')", new Object[] { new Integer(resourceId).toString(), itemName, type.toString(), type.getClass().toString() });
            if (resourceId > 0) {
                WSResourceValue value = ihc.getResourceValueInformation(resourceId);
                ArrayList<IhcEnumValue> enumValues = null;
                if (value instanceof WSEnumValue) {
                    enumValues = ihc.getEnumValues(((WSEnumValue) value).getDefinitionTypeID());
                }
                // check if configuration has a custom value defined
                // (0->OFF, 1->ON, >1->trigger)
                // if that is the case, the type will be overridden with a
                // new type
                Integer val = provider.getValue(itemName, (Command) type);
                boolean trigger = false;
                if (val != null) {
                    if (val == 0) {
                        type = OnOffType.OFF;
                    } else if (val == 1) {
                        type = OnOffType.ON;
                    } else {
                        trigger = true;
                    }
                } else {
                // the original type is kept
                }
                if (!trigger) {
                    value = IhcDataConverter.convertCommandToResourceValue(type, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item updated '{}' succesfully sent", itemName);
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                } else {
                    value = IhcDataConverter.convertCommandToResourceValue(OnOffType.ON, value, enumValues);
                    boolean result = updateResource(value);
                    if (result == true) {
                        logger.debug("Item '{}' trigger started", itemName);
                        Thread.sleep(val);
                        value = IhcDataConverter.convertCommandToResourceValue(OnOffType.OFF, value, enumValues);
                        result = updateResource(value);
                        if (result == true) {
                            logger.debug("Item '{}' trigger completed", itemName);
                        } else {
                            logger.error("Item '{}' trigger stop failed", itemName);
                        }
                    } else {
                        logger.error("Item '{}' update failed", itemName);
                    }
                }
            } else {
                logger.error("resourceId invalid");
            }
        } catch (IhcExecption e) {
            logger.error("Can't update Item '{}' value ", itemName, e);
        } catch (Exception e) {
            logger.error("Error occured during item update", e);
        }
    }
}
Also used : IhcEnumValue(org.openhab.binding.ihc.ws.IhcEnumValue) IhcBindingProvider(org.openhab.binding.ihc.IhcBindingProvider) WSResourceValue(org.openhab.binding.ihc.ws.datatypes.WSResourceValue) IhcExecption(org.openhab.binding.ihc.ws.IhcExecption) Command(org.openhab.core.types.Command) WSEnumValue(org.openhab.binding.ihc.ws.datatypes.WSEnumValue) ConfigurationException(org.osgi.service.cm.ConfigurationException)

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