Search in sources :

Example 6 with DecimalType

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

the class MpdBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
public void internalReceiveCommand(String itemName, Command command) {
    MpdBindingProvider provider;
    String matchingPlayerCommand;
    // nothing by default
    Object params = new Object();
    if (command instanceof PercentType) {
        // we have received volume adjustment request
        matchingPlayerCommand = "PERCENT";
        params = command;
    } else if (command instanceof DecimalType) {
        // we have received play song id request
        matchingPlayerCommand = "NUMBER";
        params = command;
    } else {
        matchingPlayerCommand = command.toString();
    }
    provider = findFirstMatchingBindingProvider(itemName, matchingPlayerCommand);
    if (provider == null) {
        logger.warn("Cannot find matching binding provider [itemName={}, command={}]", itemName, command);
        return;
    }
    String playerCommand = provider.getPlayerCommand(itemName, matchingPlayerCommand);
    if (StringUtils.isNotBlank(playerCommand)) {
        String playerCommandParam = provider.getPlayerCommandParam(itemName, matchingPlayerCommand);
        if (playerCommandParam != null) {
            params = playerCommandParam;
        }
        executePlayerCommand(playerCommand, params);
    }
}
Also used : DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) MpdBindingProvider(org.openhab.binding.mpd.MpdBindingProvider)

Example 7 with DecimalType

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

the class TACmiBinding method execute.

/**
     * @{inheritDoc
     */
@Override
protected void execute() {
    logger.trace("execute() method is called!");
    try {
        clientSocket.setBroadcast(true);
        clientSocket.setSoTimeout(120000);
        byte[] receiveData = new byte[14];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        clientSocket.receive(receivePacket);
        byte[] data = receivePacket.getData();
        Message message;
        if (data[1] > 0) {
            logger.debug("Processing analog message");
            message = new AnalogMessage(data);
        } else if (data[1] == 0) {
            logger.debug("Processing digital message");
            message = new DigitalMessage(data);
        } else {
            logger.debug("Invalid message received");
            return;
        }
        logger.debug(message.toString());
        for (TACmiBindingProvider provider : providers) {
            for (String itemName : provider.getItemNames()) {
                logger.debug("Processing item: " + itemName);
                int portNumber = provider.getPortNumber(itemName);
                if (provider.getCanNode(itemName) == message.canNode && provider.getPortType(itemName).equals(message.getType().toString().toLowerCase())) {
                    if (message.hasPortnumber(portNumber)) {
                        if (message.getType() == MessageType.A) {
                            AnalogValue value = ((AnalogMessage) message).getAnalogValue(portNumber);
                            if (value.value != null) {
                                logger.debug("Updating item: " + itemName + " with value: " + value.value);
                                eventPublisher.postUpdate(itemName, new DecimalType(value.value));
                            }
                        } else {
                            OnOffType state = ((DigitalMessage) message).getPortStateAsOnOffType(portNumber);
                            logger.debug("Updating item {} with state {}", itemName, state);
                            eventPublisher.postUpdate(itemName, state);
                        }
                    } else {
                        logger.debug("Portnumber {} not included in message", portNumber);
                    }
                } else {
                    logger.debug("CAN Node does not match");
                }
            }
        }
    } catch (SocketTimeoutException te) {
        logger.info("Receive timeout on CoE socket, retrying ...");
    } catch (Exception e) {
        logger.error("Error in execute: ", e);
    }
    logger.trace("TACmi execute() finished");
}
Also used : AnalogMessage(org.openhab.binding.tacmi.internal.message.AnalogMessage) Message(org.openhab.binding.tacmi.internal.message.Message) DigitalMessage(org.openhab.binding.tacmi.internal.message.DigitalMessage) DigitalMessage(org.openhab.binding.tacmi.internal.message.DigitalMessage) AnalogValue(org.openhab.binding.tacmi.internal.message.AnalogValue) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) AnalogMessage(org.openhab.binding.tacmi.internal.message.AnalogMessage) SocketTimeoutException(java.net.SocketTimeoutException) TACmiBindingProvider(org.openhab.binding.tacmi.TACmiBindingProvider) OnOffType(org.openhab.core.library.types.OnOffType) DatagramPacket(java.net.DatagramPacket) DecimalType(org.openhab.core.library.types.DecimalType)

Example 8 with DecimalType

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

the class SonanceBinding method sendVolumeCommand.

/**
     * Send volume commands to groups (music zones)
     *
     * @param itemName
     *            item name to send update to
     * @param command
     *            Sonance IP code to execute
     * @param outToServer
     *            date output stream we can write to
     * @param i
     *            bufered reader where we can read from
     * @throws IOException
     *             throws an exception when we can't reach to amplifier
     */
private void sendVolumeCommand(String itemName, String command, DataOutputStream outToServer, BufferedReader i) throws IOException {
    // Response is always 50 characters
    char[] cbuf = new char[50];
    logger.debug("Sending volume command {}", command);
    outToServer.write(hexStringToByteArray(command));
    i.read(cbuf, 0, 50);
    Matcher m = volumePattern.matcher(new String(cbuf));
    if (m.find()) {
        String volume = m.group(1);
        eventPublisher.postUpdate(itemName, new DecimalType(volume));
        logger.debug("Setting volume for item {} on {}", itemName, volume);
    } else {
        logger.error("Error sending regular volume command {}, received this: {}", command, new String(cbuf));
    }
}
Also used : Matcher(java.util.regex.Matcher) DecimalType(org.openhab.core.library.types.DecimalType)

Example 9 with DecimalType

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

the class SonanceBinding method setVolumeCommand.

/**
     * Sets the group to the specified target volume. Amplifier doesn't support
     * direct volume commands, so a loop is needed
     *
     * @param itemName
     *            item to publish result to
     * @param group
     *            target group
     * @param targetVolume
     *            target volume
     * @param outToServer
     *            data output stream where we can write to
     * @param i
     *            buffered reader where we can read from
     * @param endpoint
     *            ip:port
     * @throws IOException
     *             throws an IOException when we can't reach the amplifier
     */
private void setVolumeCommand(String itemName, String group, int targetVolume, DataOutputStream outToServer, BufferedReader i, String endpoint) throws IOException {
    // Response is always 50 characters
    char[] cbuf = new char[50];
    String question = String.format("%s%s%s", SonanceConsts.DIRECT_VOLUME_QUERY, Integer.toHexString(183 + targetVolume), group);
    logger.trace("Sending this to amplifier: {}", question);
    outToServer.write(hexStringToByteArray(question));
    i.read(cbuf, 0, 50);
    String result = new String(cbuf);
    logger.trace("Received this as response : {}", result);
    Matcher m = volumePattern.matcher(result);
    if (m.find()) {
        double currentVolume = Integer.parseInt(m.group(1));
        eventPublisher.postUpdate(itemName, new DecimalType(currentVolume));
        logger.debug("Updating {} with new volume {}", itemName, currentVolume);
    } else {
        logger.error("Error sending volume command, received this: {}", new String(cbuf));
    }
}
Also used : Matcher(java.util.regex.Matcher) DecimalType(org.openhab.core.library.types.DecimalType)

Example 10 with DecimalType

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

the class SqueezeboxBinding method internalReceiveCommand.

/**
     * @{inheritDoc}
     */
@Override
public void internalReceiveCommand(String itemName, Command command) {
    if (squeezeServer == null) {
        logger.warn("Squeeze Server not initialised or configured yet, ignoring command '{}' for item '{}'", command.toString(), itemName);
        return;
    }
    logger.trace("internalReceiveCommand(itemname = {}, command = {})", itemName, command.toString());
    for (SqueezeboxBindingProvider provider : providers) {
        SqueezeboxBindingConfig bindingConfig = provider.getSqueezeboxBindingConfig(itemName);
        String playerId = bindingConfig.getPlayerId();
        try {
            switch(bindingConfig.getCommandType()) {
                case POWER:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.powerOn(playerId);
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.powerOff(playerId);
                    }
                    break;
                case MUTE:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.mute(playerId);
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.unMute(playerId);
                    }
                    break;
                case VOLUME:
                    if (command.equals(IncreaseDecreaseType.INCREASE)) {
                        squeezeServer.volumeUp(playerId);
                    } else if (command.equals(IncreaseDecreaseType.DECREASE)) {
                        squeezeServer.volumeDown(playerId);
                    } else if (command.equals(UpDownType.UP)) {
                        squeezeServer.volumeUp(playerId);
                    } else if (command.equals(UpDownType.DOWN)) {
                        squeezeServer.volumeDown(playerId);
                    } else if (command instanceof DecimalType) {
                        squeezeServer.setVolume(playerId, ((DecimalType) command).intValue());
                    }
                    break;
                case PLAY:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.play(playerId);
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.stop(playerId);
                    }
                    break;
                case PAUSE:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.pause(playerId);
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.unPause(playerId);
                    }
                    break;
                case STOP:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.stop(playerId);
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.play(playerId);
                    }
                    break;
                case NEXT:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.next(playerId);
                    }
                    break;
                case PREV:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.prev(playerId);
                    }
                    break;
                case HTTP:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.playUrl(playerId, "http://" + bindingConfig.getExtra());
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.stop(playerId);
                    }
                    break;
                case FILE:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.playUrl(playerId, "file://" + bindingConfig.getExtra());
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.stop(playerId);
                    }
                    break;
                case SYNC:
                    if (command.equals(OnOffType.ON)) {
                        squeezeServer.syncPlayer(playerId, bindingConfig.getExtra());
                    } else if (command.equals(OnOffType.OFF)) {
                        squeezeServer.unSyncPlayer(bindingConfig.getExtra());
                    }
                    break;
                case COMMAND:
                    if (command instanceof StringType) {
                        squeezeServer.playerCommand(playerId, command.toString());
                    } else {
                        squeezeServer.playerCommand(playerId, bindingConfig.getExtra());
                    }
                    break;
                default:
                    logger.warn("Unsupported command type '{}'", bindingConfig.getCommandType());
            }
        } catch (Exception e) {
            logger.warn("Error executing command type '{}'", bindingConfig.getCommandType(), e);
        }
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) SqueezeboxBindingProvider(org.openhab.binding.squeezebox.SqueezeboxBindingProvider) DecimalType(org.openhab.core.library.types.DecimalType) SqueezeboxBindingConfig(org.openhab.binding.squeezebox.SqueezeboxBindingConfig)

Aggregations

DecimalType (org.openhab.core.library.types.DecimalType)222 Test (org.junit.Test)70 StringType (org.openhab.core.library.types.StringType)69 OnOffType (org.openhab.core.library.types.OnOffType)63 PercentType (org.openhab.core.library.types.PercentType)43 State (org.openhab.core.types.State)40 Type (org.openhab.core.types.Type)39 NumberItem (org.openhab.core.library.items.NumberItem)38 SHCMessage (org.openhab.binding.smarthomatic.internal.SHCMessage)37 DateTimeType (org.openhab.core.library.types.DateTimeType)29 BigDecimal (java.math.BigDecimal)23 Calendar (java.util.Calendar)23 HSBType (org.openhab.core.library.types.HSBType)20 SwitchItem (org.openhab.core.library.items.SwitchItem)18 IOException (java.io.IOException)16 StringItem (org.openhab.core.library.items.StringItem)16 ConfigurationException (org.osgi.service.cm.ConfigurationException)16 RollershutterItem (org.openhab.core.library.items.RollershutterItem)15 ContactItem (org.openhab.core.library.items.ContactItem)14 DimmerItem (org.openhab.core.library.items.DimmerItem)14