Search in sources :

Example 21 with OnOffType

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

the class PulseaudioBinding method internalReceiveCommand.

@Override
public void internalReceiveCommand(String itemName, Command command) {
    PulseaudioBindingProvider provider = findFirstMatchingBindingProvider(itemName, command);
    if (provider == null) {
        logger.warn("doesn't find matching binding provider [itemName={}, command={}]", itemName, command);
        return;
    }
    String audioItemName = provider.getItemName(itemName);
    String serverId = provider.getServerId(itemName);
    // Item item = provider.getItem(itemName);
    String paCommand = provider.getCommand(itemName);
    PulseaudioCommandTypeMapping pulseaudioCommandType = null;
    if (paCommand != null && !paCommand.isEmpty()) {
        try {
            pulseaudioCommandType = PulseaudioCommandTypeMapping.valueOf(paCommand.toUpperCase());
        } catch (IllegalArgumentException e) {
            logger.warn("unknown command specified for the given itemName [itemName={}, audio-item-name={}, serverId={}, command={}] => querying for values aborted!", new Object[] { itemName, audioItemName, serverId, command });
        }
    }
    PulseaudioClient client = clients.get(serverId);
    if (client == null) {
        // try to reconnect if the server is configured
        if (serverConfigCache.containsKey(serverId)) {
            connect(serverId, serverConfigCache.get(serverId));
            client = clients.get(serverId);
        }
    }
    if (client == null) {
        logger.warn("does't find matching pulseaudio client [itemName={}, serverId={}]", itemName, serverId);
        return;
    }
    if (audioItemName != null && !audioItemName.isEmpty()) {
        AbstractAudioDeviceConfig audioItem = client.getGenericAudioItem(audioItemName);
        if (audioItem == null) {
            logger.warn("no corresponding audio-item found [audioItemName={}]", audioItemName);
            return;
        }
        State updateState = UnDefType.UNDEF;
        if (command instanceof IncreaseDecreaseType) {
            int volume = audioItem.getVolume();
            logger.debug(audioItemName + " volume is " + volume);
            if (command.equals(IncreaseDecreaseType.INCREASE)) {
                volume = Math.min(100, volume + 5);
            }
            if (command.equals(IncreaseDecreaseType.DECREASE)) {
                volume = Math.max(0, volume - 5);
            }
            logger.debug("setting " + audioItemName + " volume to " + volume);
            client.setVolumePercent(audioItem, volume);
            updateState = new PercentType(volume);
        } else if (command instanceof PercentType) {
            client.setVolumePercent(audioItem, Integer.valueOf(command.toString()));
            updateState = (PercentType) command;
        } else if (command instanceof DecimalType) {
            if (pulseaudioCommandType == null || pulseaudioCommandType.equals(PulseaudioCommandTypeMapping.VOLUME)) {
                // set volume
                client.setVolume(audioItem, Integer.valueOf(command.toString()));
                updateState = (DecimalType) command;
            }
        // all other pulseaudioCommandType's for DecimalTypes are
        // read-only and
        // therefore we do nothing here
        } else if (command instanceof OnOffType) {
            if (pulseaudioCommandType == null) {
                // Default behaviour when no command is specified => mute
                client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
                updateState = (OnOffType) command;
            } else {
                switch(pulseaudioCommandType) {
                    case EXISTS:
                        // we better do nothing here
                        break;
                    case MUTED:
                        client.setMute(audioItem, ((OnOffType) command).equals(OnOffType.ON));
                        updateState = (OnOffType) command;
                        break;
                    case RUNNING:
                    case CORKED:
                    case SUSPENDED:
                    case IDLE:
                        // the state of an audio-item cannot be changed
                        break;
                    case ID:
                    case MODULE_ID:
                        // changed
                        break;
                    case VOLUME:
                        if (((OnOffType) command).equals(OnOffType.ON)) {
                            // Set Volume to 100%
                            client.setVolume(audioItem, 100);
                        } else {
                            // set volume to 0
                            client.setVolume(audioItem, 100);
                        }
                        updateState = (OnOffType) command;
                        break;
                    case SLAVE_SINKS:
                        // also an read-only field
                        break;
                }
            }
        } else if (command instanceof StringType) {
            if (pulseaudioCommandType != null) {
                switch(pulseaudioCommandType) {
                    case CORKED:
                    case EXISTS:
                    case ID:
                    case IDLE:
                    case MODULE_ID:
                    case MUTED:
                    case RUNNING:
                    case SUSPENDED:
                    case VOLUME:
                        // no action here
                        break;
                    case SLAVE_SINKS:
                        if (audioItem instanceof Sink && ((Sink) audioItem).isCombinedSink()) {
                            // change the slave sinks of the given combined sink
                            // to the new value
                            Sink mainSink = (Sink) audioItem;
                            ArrayList<Sink> slaveSinks = new ArrayList<Sink>();
                            for (String slaveSinkName : StringUtils.split(command.toString(), ",")) {
                                Sink slaveSink = client.getSink(slaveSinkName);
                                if (slaveSink != null) {
                                    slaveSinks.add(slaveSink);
                                }
                            }
                            logger.debug(slaveSinks.size() + " slave sinks");
                            if (slaveSinks.size() > 0) {
                                client.setCombinedSinkSlaves(mainSink, slaveSinks);
                            }
                        }
                        break;
                }
            }
        }
        if (!updateState.equals(UnDefType.UNDEF)) {
            eventPublisher.postUpdate(itemName, updateState);
        }
    } else if (command instanceof StringType) {
        // send the command directly to the pulseaudio server
        client.sendCommand(command.toString());
        eventPublisher.postUpdate(itemName, (StringType) command);
    }
}
Also used : PulseaudioBindingProvider(org.openhab.binding.pulseaudio.PulseaudioBindingProvider) StringType(org.openhab.core.library.types.StringType) ArrayList(java.util.ArrayList) PercentType(org.openhab.core.library.types.PercentType) Sink(org.openhab.binding.pulseaudio.internal.items.Sink) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) AbstractAudioDeviceConfig(org.openhab.binding.pulseaudio.internal.items.AbstractAudioDeviceConfig)

Example 22 with OnOffType

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

the class PowerDogLocalApiBinding method internalReceiveCommand.

/**
     * @{inheritDoc
     */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    logger.debug("internalReceiveCommand({},{}) is called!", itemName, command);
    State newState = null;
    // cast Interfaces
    if (command instanceof OnOffType) {
        newState = (OnOffType) command;
    } else if (command instanceof OpenClosedType) {
        newState = (OpenClosedType) command;
    } else if (command instanceof PercentType) {
        newState = (PercentType) command;
    } else if (command instanceof DecimalType) {
        newState = (DecimalType) command;
    }
    if (newState != null) {
        eventPublisher.postUpdate(itemName, newState);
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) OpenClosedType(org.openhab.core.library.types.OpenClosedType) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType)

Example 23 with OnOffType

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

the class CalDavBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    if (!(command instanceof OnOffType)) {
        logger.trace("invalid command for DISABLE (just SwitchItems allowed)");
        return;
    }
    // get binding provider
    CalDavBindingProvider provider = getCalDavBindingProvider();
    if (provider == null) {
        logger.error("cannot find any provider");
        return;
    }
    CalDavNextEventConfig config = provider.getConfig(itemName);
    if (config == null) {
        logger.error("no config found for item {}", itemName);
        return;
    }
    if (config.getType() != CalDavType.DISABLE) {
        logger.trace("can just use commands for type=DISABLE");
        return;
    }
    if (command == OnOffType.ON) {
        logger.info("execution for '{}' disabled", config.getItemNameToListenTo());
        this.disabledItems.add(config.getItemNameToListenTo());
    } else if (command == OnOffType.OFF) {
        logger.info("execution for '{}' enabled", config.getItemNameToListenTo());
        this.disabledItems.remove(config.getItemNameToListenTo());
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) CalDavBindingProvider(org.openhab.binding.caldav_command.CalDavBindingProvider)

Example 24 with OnOffType

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

the class BenqProjectorBinding method sendCommandToProjector.

/**
     * Send the command to the projector via configured transport and return the
     * response string
     *
     * @param cfg
     *            Item binding configuration
     * @param c
     *            command to be sent
     * @return Response string from projector
     */
private String sendCommandToProjector(BenqProjectorBindingConfig cfg, Command c) {
    Boolean cmdSent = false;
    String response = "";
    switch(cfg.mode) {
        case POWER:
        case MUTE:
            if (c instanceof OnOffType) {
                if ((OnOffType) c == OnOffType.ON) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("ON"));
                    cmdSent = true;
                } else if ((OnOffType) c == OnOffType.OFF) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("OFF"));
                    cmdSent = true;
                }
            }
            break;
        case VOLUME:
            if (c instanceof DecimalType) {
                /* get current volume */
                State currentVolState = queryProjector(cfg);
                int currentVol = ((DecimalType) currentVolState).intValue();
                int volLevel = ((DecimalType) c).intValue();
                if (volLevel > this.MAX_VOLUME) {
                    volLevel = this.MAX_VOLUME;
                } else if (volLevel < this.MIN_VOLUME) {
                    volLevel = this.MIN_VOLUME;
                }
                if (currentVol == volLevel) {
                    cmdSent = true;
                }
                while (currentVol != volLevel) {
                    if (currentVol < volLevel) {
                        transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
                        currentVol++;
                        cmdSent = true;
                    } else {
                        transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
                        currentVol--;
                        cmdSent = true;
                    }
                }
            } else if (c instanceof IncreaseDecreaseType) {
                if ((IncreaseDecreaseType) c == IncreaseDecreaseType.INCREASE) {
                    transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("+"));
                    cmdSent = true;
                } else if ((IncreaseDecreaseType) c == IncreaseDecreaseType.DECREASE) {
                    transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString("-"));
                    cmdSent = true;
                }
            }
            /* get final volume */
            response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandQueryString());
            break;
        case LAMP_HOURS:
            logger.warn("Cannot send command to set lamp hours - not a valid operation!");
            break;
        case SOURCE_NUMBER:
            if (c instanceof DecimalType) {
                DecimalType sourceIdx = (DecimalType) c;
                String cmd = BenqProjectorSourceMapping.getStringFromMapping(sourceIdx.intValue());
                if (cmd.isEmpty() == false) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(cmd));
                    cmdSent = true;
                }
            }
            break;
        case SOURCE_STRING:
            if (c instanceof StringType) {
                StringType sourceStr = (StringType) c;
                int mappingIdx = BenqProjectorSourceMapping.getMappingFromString(sourceStr.toString());
                if (// double check this is a valid mapping
                mappingIdx != -1) {
                    response = transport.sendCommandExpectResponse(cfg.mode.getItemModeCommandSetString(sourceStr.toString()));
                    cmdSent = true;
                }
            }
            break;
        default:
            logger.error("Unexpected Item Mode!");
            break;
    }
    if (cmdSent == false) {
        logger.error("Unable to convert item command to projector state: Command=" + c);
    }
    return response;
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) StringType(org.openhab.core.library.types.StringType) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType)

Example 25 with OnOffType

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

the class AlarmDecoderBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    if (!m_acceptCommands) {
        logger.warn("sending commands is disabled, enable it in openhab.cfg!");
        return;
    }
    String param = "INVALID";
    if (command instanceof OnOffType) {
        OnOffType cmd = (OnOffType) command;
        param = cmd.equals(OnOffType.ON) ? "ON" : "OFF";
    } else if (command instanceof DecimalType) {
        param = ((DecimalType) command).toString();
    } else {
        logger.error("item {} only accepts DecimalType and OnOffType", itemName);
        return;
    }
    try {
        ArrayList<AlarmDecoderBindingConfig> bcl = getItems(itemName);
        for (AlarmDecoderBindingConfig bc : bcl) {
            String sendStr = bc.getParameters().get(param);
            if (sendStr == null) {
                logger.error("{} has no mapping for command {}!", itemName, param);
            } else {
                String s = sendStr.replace("POUND", "#");
                m_writer.write(s);
                m_writer.flush();
            }
        }
    } catch (IOException e) {
        logger.error("write to serial port failed: ", e);
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) IOException(java.io.IOException)

Aggregations

OnOffType (org.openhab.core.library.types.OnOffType)50 DecimalType (org.openhab.core.library.types.DecimalType)22 PercentType (org.openhab.core.library.types.PercentType)15 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)12 OpenClosedType (org.openhab.core.library.types.OpenClosedType)11 StringType (org.openhab.core.library.types.StringType)11 UpDownType (org.openhab.core.library.types.UpDownType)9 IOException (java.io.IOException)8 State (org.openhab.core.types.State)7 StopMoveType (org.openhab.core.library.types.StopMoveType)6 DateTimeType (org.openhab.core.library.types.DateTimeType)5 HSBType (org.openhab.core.library.types.HSBType)5 UnknownHostException (java.net.UnknownHostException)4 ConfigurationException (org.osgi.service.cm.ConfigurationException)4 RFXComException (org.openhab.binding.rfxcom.internal.RFXComException)3 Item (org.openhab.core.items.Item)3 SwitchItem (org.openhab.core.library.items.SwitchItem)3 Color (java.awt.Color)2 DatagramPacket (java.net.DatagramPacket)2 SocketTimeoutException (java.net.SocketTimeoutException)2