Search in sources :

Example 1 with Zone

use of org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone in project openhab1-addons by openhab.

the class YamahaReceiverBinding method sendUpdates.

private void sendUpdates(YamahaReceiverProxy receiverProxy, String deviceUid) {
    // Get all item configurations belonging to this proxy
    Collection<YamahaReceiverBindingConfig> configs = getDeviceConfigs(deviceUid);
    try {
        for (Zone zone : Zone.values()) {
            // Poll the state from the device
            YamahaReceiverState state = receiverProxy.getState(zone);
            // Create state updates
            State powerUpdate = state.isPower() ? OnOffType.ON : OnOffType.OFF;
            State muteUpdate = state.isMute() ? OnOffType.ON : OnOffType.OFF;
            State inputUpdate = new StringType(state.getInput());
            State surroundUpdate = new StringType(state.getSurroundProgram());
            State updateVolumeDb = new DecimalType(state.getVolume());
            State updateVolumePercent = new PercentType((int) dbToPercent(state.getVolume()));
            // Send updates
            sendUpdate(configs, zone, BindingType.power, powerUpdate);
            sendUpdate(configs, zone, BindingType.mute, muteUpdate);
            sendUpdate(configs, zone, BindingType.input, inputUpdate);
            sendUpdate(configs, zone, BindingType.surroundProgram, surroundUpdate);
            sendUpdate(configs, zone, BindingType.volumePercent, updateVolumePercent);
            sendUpdate(configs, zone, BindingType.volumeDb, updateVolumeDb);
        }
    } catch (IOException e) {
        logger.warn("Cannot communicate with " + receiverProxy.getHost());
    }
}
Also used : StringType(org.openhab.core.library.types.StringType) Zone(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState)

Example 2 with Zone

use of org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone in project openhab1-addons by openhab.

the class YamahaReceiverBinding method internalReceiveCommand.

@Override
protected void internalReceiveCommand(String itemName, Command command) {
    YamahaReceiverBindingConfig config = getConfigForItemName(itemName);
    if (config == null) {
        logger.error("Received command for unknown item '" + itemName + "'");
        return;
    }
    YamahaReceiverProxy proxy = proxies.get(config.getDeviceUid());
    if (proxy == null) {
        logger.error("Received command for unknown device uid '" + config.getDeviceUid() + "'");
        return;
    }
    if (logger.isDebugEnabled()) {
        logger.debug(BINDING_NAME + " processing command '" + command + "' of type '" + command.getClass().getSimpleName() + "' for item '" + itemName + "'");
    }
    try {
        Zone zone = config.getZone();
        BindingType type = config.getBindingType();
        if (type == BindingType.power) {
            if (command instanceof OnOffType) {
                proxy.setPower(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.volumePercent || type == BindingType.volumeDb) {
            if (command instanceof IncreaseDecreaseType || command instanceof UpDownType) {
                // increase/decrease dB by .5 dB
                float db = proxy.getState(zone).getVolume();
                float adjAmt;
                if (command == IncreaseDecreaseType.INCREASE || command == UpDownType.UP) {
                    adjAmt = .5f;
                } else {
                    adjAmt = -.5f;
                }
                float newDb = db + adjAmt;
                proxy.setVolume(zone, newDb);
                // send new value as update
                State newState = new DecimalType(newDb);
                eventPublisher.postUpdate(itemName, newState);
            } else if (command instanceof PercentType) {
                // set dB from percent
                byte percent = ((PercentType) command).byteValue();
                int db = percentToDB(percent);
                proxy.setVolume(zone, db);
            } else {
                // set dB from value
                float db = Float.parseFloat(command.toString());
                proxy.setVolume(zone, db);
            }
            // Volume updates multiple values => send update now
            sendUpdates(proxy, config.getDeviceUid());
        } else if (type == BindingType.mute) {
            if (command instanceof OnOffType) {
                proxy.setMute(zone, command == OnOffType.ON);
            }
        } else if (type == BindingType.input) {
            proxy.setInput(zone, parseString(command.toString()));
        } else if (type == BindingType.surroundProgram) {
            proxy.setSurroundProgram(zone, parseString(command.toString()));
        } else if (type == BindingType.netRadio) {
            if (command instanceof DecimalType) {
                proxy.setNetRadio(((DecimalType) command).intValue());
            }
        }
    } catch (IOException e) {
        logger.warn("Cannot communicate with " + proxy.getHost() + " (uid: " + config.getDeviceUid() + ")");
    } catch (Throwable t) {
        logger.error("Error processing command '" + command + "' for item '" + itemName + "'", t);
    }
}
Also used : Zone(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) IOException(java.io.IOException) OnOffType(org.openhab.core.library.types.OnOffType) BindingType(org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.BindingType) YamahaReceiverState(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState) State(org.openhab.core.types.State) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) YamahaReceiverProxy(org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverProxy)

Aggregations

IOException (java.io.IOException)2 Zone (org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.Zone)2 YamahaReceiverState (org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverState)2 DecimalType (org.openhab.core.library.types.DecimalType)2 PercentType (org.openhab.core.library.types.PercentType)2 State (org.openhab.core.types.State)2 BindingType (org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConfig.BindingType)1 YamahaReceiverProxy (org.openhab.binding.yamahareceiver.internal.hardware.YamahaReceiverProxy)1 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)1 OnOffType (org.openhab.core.library.types.OnOffType)1 StringType (org.openhab.core.library.types.StringType)1 UpDownType (org.openhab.core.library.types.UpDownType)1