Search in sources :

Example 1 with IRtransBindingProvider

use of org.openhab.binding.irtrans.IRtransBindingProvider in project openhab1-addons by openhab.

the class IRtransBinding method internalReceiveChanneledCommand.

/**
     * {@inheritDoc}
     */
@Override
protected boolean internalReceiveChanneledCommand(String itemName, Command command, Channel sChannel, String commandAsString) {
    IRtransBindingProvider provider = findFirstMatchingBindingProvider(itemName);
    String remoteName = null;
    String irCommandName = null;
    if (command != null && provider != null) {
        if (command instanceof DecimalType) {
            remoteName = StringUtils.substringBefore(commandAsString, ",");
            irCommandName = StringUtils.substringAfter(commandAsString, ",");
            IrCommand firstCommand = new IrCommand();
            firstCommand.remote = remoteName;
            firstCommand.command = irCommandName;
            IrCommand secondCommand = new IrCommand();
            secondCommand.remote = provider.getRemote(itemName, command);
            secondCommand.command = provider.getIrCommand(itemName, command);
            if (!firstCommand.matches(secondCommand)) {
                remoteName = null;
                irCommandName = null;
            }
        } else {
            remoteName = provider.getRemote(itemName, command);
            irCommandName = provider.getIrCommand(itemName, command);
        }
    }
    if (remoteName != null && irCommandName != null) {
        Leds led = provider.getLed(itemName, command);
        IrCommand theCommand = new IrCommand();
        theCommand.remote = remoteName;
        theCommand.command = irCommandName;
        // construct the string we need to send to the IRtrans device
        String output = packIRDBCommand(led, theCommand);
        ByteBuffer outputBuffer = ByteBuffer.allocate(output.getBytes().length);
        ByteBuffer response = null;
        try {
            outputBuffer.put(output.getBytes("ASCII"));
            response = writeBuffer(outputBuffer, sChannel, true, timeOut);
        } catch (UnsupportedEncodingException e) {
            logger.error("An exception occurred while encoding an infrared command: {}", e.getMessage());
        }
        if (response != null) {
            String message = stripByteCount(response);
            if (message != null) {
                if (message.contains("RESULT OK")) {
                    List<Class<? extends State>> stateTypeList = provider.getAcceptedDataTypes(itemName, command);
                    State newState = createStateFromString(stateTypeList, commandAsString);
                    if (newState != null) {
                        eventPublisher.postUpdate(itemName, newState);
                    } else {
                        logger.warn("Can not parse " + commandAsString + " to match command {} on item {}  ", command, itemName);
                    }
                } else {
                    logger.warn("Received an unexpected response {}", StringUtils.substringAfter(message, "RESULT "));
                }
            }
        } else {
            logger.warn("Did not receive an answer from the IRtrans device - Parsing is skipped");
        }
    } else {
        logger.warn("Invalid command {} for Item {} - Transmission is skipped", commandAsString, itemName);
    }
    // not deal with that anymore.
    return false;
}
Also used : State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IRtransBindingProvider(org.openhab.binding.irtrans.IRtransBindingProvider) ByteBuffer(java.nio.ByteBuffer)

Example 2 with IRtransBindingProvider

use of org.openhab.binding.irtrans.IRtransBindingProvider in project openhab1-addons by openhab.

the class IRtransBinding method parseDecodedCommand.

protected void parseDecodedCommand(String itemName, IrCommand theCommand, Command ohCommand) {
    if (theCommand != null) {
        // traverse the providers, for each provider, check each binding if it matches theCommand
        for (IRtransBindingProvider provider : providers) {
            if (provider.providesBindingFor(itemName)) {
                List<org.openhab.core.types.Command> commands = provider.getAllCommands(itemName);
                // first check if commands are defined, and that they have the correct DirectionType
                Iterator<org.openhab.core.types.Command> listIterator = commands.listIterator();
                while (listIterator.hasNext()) {
                    org.openhab.core.types.Command aCommand = listIterator.next();
                    IrCommand providerCommand = new IrCommand();
                    providerCommand.remote = provider.getRemote(itemName, aCommand);
                    providerCommand.command = provider.getIrCommand(itemName, aCommand);
                    if (aCommand == ohCommand) {
                        if (providerCommand.matches(theCommand)) {
                            List<Class<? extends State>> stateTypeList = provider.getAcceptedDataTypes(itemName, aCommand);
                            State newState = null;
                            if (aCommand instanceof DecimalType) {
                                newState = createStateFromString(stateTypeList, theCommand.remote + "," + theCommand.command);
                            } else {
                                newState = createStateFromString(stateTypeList, aCommand.toString());
                            }
                            if (newState != null) {
                                eventPublisher.postUpdate(itemName, newState);
                            } else {
                                logger.warn("Can not create an Item State to match command {} on item {}  ", aCommand, itemName);
                            }
                        } else {
                            logger.info("The IRtrans command '{},{}' does not match the command '{}' of the binding configuration for item '{}'", new Object[] { theCommand.remote, theCommand.command, ohCommand, itemName });
                        }
                    }
                }
            }
        }
    }
}
Also used : Command(org.openhab.core.types.Command) IRtransBindingProvider(org.openhab.binding.irtrans.IRtransBindingProvider) Command(org.openhab.core.types.Command) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType)

Aggregations

IRtransBindingProvider (org.openhab.binding.irtrans.IRtransBindingProvider)2 DecimalType (org.openhab.core.library.types.DecimalType)2 State (org.openhab.core.types.State)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 Command (org.openhab.core.types.Command)1