Search in sources :

Example 96 with RefreshType

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

the class YamahaZoneThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (zoneControl == null) {
        return;
    }
    String id = channelUID.getIdWithoutGroup();
    try {
        if (command instanceof RefreshType) {
            refreshFromState(channelUID);
            return;
        }
        switch(id) {
            case CHANNEL_POWER:
                zoneControl.setPower(((OnOffType) command) == OnOffType.ON);
                break;
            case CHANNEL_INPUT:
                zoneControl.setInput(((StringType) command).toString());
                break;
            case CHANNEL_SURROUND:
                zoneControl.setSurroundProgram(((StringType) command).toString());
                break;
            case CHANNEL_VOLUME_DB:
                zoneControl.setVolumeDB(((DecimalType) command).floatValue());
                break;
            case CHANNEL_VOLUME:
                if (command instanceof DecimalType) {
                    zoneControl.setVolume(((DecimalType) command).floatValue());
                } else if (command instanceof IncreaseDecreaseType) {
                    zoneControl.setVolumeRelative(zoneState, (((IncreaseDecreaseType) command) == IncreaseDecreaseType.INCREASE ? 1 : -1) * zoneConfig.getVolumeRelativeChangeFactor());
                }
                break;
            case CHANNEL_MUTE:
                zoneControl.setMute(((OnOffType) command) == OnOffType.ON);
                break;
            case CHANNEL_SCENE:
                zoneControl.setScene(((StringType) command).toString());
                break;
            case CHANNEL_DIALOGUE_LEVEL:
                zoneControl.setDialogueLevel(((DecimalType) command).intValue());
                break;
            case CHANNEL_HDMI1OUT:
                zoneControl.setHDMI1Out(((OnOffType) command) == OnOffType.ON);
                break;
            case CHANNEL_HDMI2OUT:
                zoneControl.setHDMI2Out(((OnOffType) command) == OnOffType.ON);
                break;
            case CHANNEL_NAVIGATION_MENU:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                String path = ((StringType) command).toFullString();
                inputWithNavigationControl.selectItemFullPath(path);
                break;
            case CHANNEL_NAVIGATION_UPDOWN:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                if (((UpDownType) command) == UpDownType.UP) {
                    inputWithNavigationControl.goUp();
                } else {
                    inputWithNavigationControl.goDown();
                }
                break;
            case CHANNEL_NAVIGATION_LEFTRIGHT:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                if (((UpDownType) command) == UpDownType.UP) {
                    inputWithNavigationControl.goLeft();
                } else {
                    inputWithNavigationControl.goRight();
                }
                break;
            case CHANNEL_NAVIGATION_SELECT:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                inputWithNavigationControl.selectCurrentItem();
                break;
            case CHANNEL_NAVIGATION_BACK:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                inputWithNavigationControl.goBack();
                break;
            case CHANNEL_NAVIGATION_BACKTOROOT:
                if (inputWithNavigationControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                inputWithNavigationControl.goToRoot();
                break;
            case CHANNEL_PLAYBACK_PRESET:
                if (inputWithPresetControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                if (command instanceof DecimalType) {
                    inputWithPresetControl.selectItemByPresetNumber(((DecimalType) command).intValue());
                } else if (command instanceof StringType) {
                    try {
                        int v = Integer.valueOf(((StringType) command).toString());
                        inputWithPresetControl.selectItemByPresetNumber(v);
                    } catch (NumberFormatException e) {
                        logger.warn("Provide a number for {}", id);
                    }
                }
                break;
            case CHANNEL_TUNER_BAND:
                if (inputWithDabBandControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                if (command instanceof StringType) {
                    inputWithDabBandControl.selectBandByName(command.toString());
                } else {
                    logger.warn("Provide a string for {}", id);
                }
                break;
            case CHANNEL_PLAYBACK:
                if (inputWithPlayControl == null) {
                    logger.warn("Channel {} not working with {} input!", id, zoneState.inputID);
                    return;
                }
                if (command instanceof PlayPauseType) {
                    PlayPauseType t = ((PlayPauseType) command);
                    switch(t) {
                        case PAUSE:
                            inputWithPlayControl.pause();
                            break;
                        case PLAY:
                            inputWithPlayControl.play();
                            break;
                    }
                } else if (command instanceof NextPreviousType) {
                    NextPreviousType t = ((NextPreviousType) command);
                    switch(t) {
                        case NEXT:
                            inputWithPlayControl.nextTrack();
                            break;
                        case PREVIOUS:
                            inputWithPlayControl.previousTrack();
                            break;
                    }
                } else if (command instanceof DecimalType) {
                    int v = ((DecimalType) command).intValue();
                    if (v < 0) {
                        inputWithPlayControl.skipREV();
                    } else if (v > 0) {
                        inputWithPlayControl.skipFF();
                    }
                } else if (command instanceof StringType) {
                    String v = ((StringType) command).toFullString();
                    switch(v) {
                        case "Play":
                            inputWithPlayControl.play();
                            break;
                        case "Pause":
                            inputWithPlayControl.pause();
                            break;
                        case "Stop":
                            inputWithPlayControl.stop();
                            break;
                        case "Rewind":
                            inputWithPlayControl.skipREV();
                            break;
                        case "FastForward":
                            inputWithPlayControl.skipFF();
                            break;
                        case "Next":
                            inputWithPlayControl.nextTrack();
                            break;
                        case "Previous":
                            inputWithPlayControl.previousTrack();
                            break;
                    }
                }
                break;
            default:
                logger.warn("Channel {} not supported!", id);
        }
    } catch (IOException e) {
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
    } catch (ReceivedMessageParseException e) {
        // Some AVRs send unexpected responses. We log parser exceptions therefore.
        logger.debug("Parse error!", e);
    }
}
Also used : ReceivedMessageParseException(org.openhab.binding.yamahareceiver.internal.protocol.ReceivedMessageParseException) StringType(org.openhab.core.library.types.StringType) PlayPauseType(org.openhab.core.library.types.PlayPauseType) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) UpDownType(org.openhab.core.library.types.UpDownType) IOException(java.io.IOException) RefreshType(org.openhab.core.types.RefreshType) NextPreviousType(org.openhab.core.library.types.NextPreviousType)

Example 97 with RefreshType

use of org.openhab.core.types.RefreshType in project addons by smarthomej.

the class LightThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (channelUID.getId().equals(CHANNEL_ONTIME)) {
        if (command instanceof QuantityType<?>) {
            QuantityType<?> onTimeSeconds = ((QuantityType<?>) command).toUnit(Units.SECOND);
            if (onTimeSeconds != null) {
                onTime = 10 * onTimeSeconds.intValue();
            } else {
                logger.warn("Channel '{}' received command '{}', could not be converted to seconds.", channelUID, command);
            }
        }
        return;
    }
    if (command instanceof RefreshType) {
        valueUpdated(channelUID.getId(), lightStateCache);
        return;
    }
    LightState newLightState = new LightState();
    Boolean currentOn = lightStateCache.on;
    Integer currentBri = lightStateCache.bri;
    switch(channelUID.getId()) {
        case CHANNEL_ALERT:
            if (command instanceof StringType) {
                newLightState.alert = command.toString();
            } else {
                return;
            }
            break;
        case CHANNEL_EFFECT:
            if (command instanceof StringType) {
                // effect command only allowed for lights that are turned on
                newLightState.on = true;
                newLightState.effect = command.toString();
            } else {
                return;
            }
            break;
        case CHANNEL_EFFECT_SPEED:
            if (command instanceof DecimalType) {
                newLightState.on = true;
                newLightState.effectSpeed = Util.constrainToRange(((DecimalType) command).intValue(), 0, 10);
            } else {
                return;
            }
            break;
        case CHANNEL_SWITCH:
        case CHANNEL_LOCK:
            if (command instanceof OnOffType) {
                newLightState.on = (command == OnOffType.ON);
            } else {
                return;
            }
            break;
        case CHANNEL_BRIGHTNESS:
        case CHANNEL_COLOR:
            if (command instanceof OnOffType) {
                newLightState.on = (command == OnOffType.ON);
            } else if (command instanceof IncreaseDecreaseType) {
                // try to get best value for current brightness
                int oldBri = currentBri != null ? currentBri : (Boolean.TRUE.equals(currentOn) ? BRIGHTNESS_MAX : BRIGHTNESS_MIN);
                if (command.equals(IncreaseDecreaseType.INCREASE)) {
                    newLightState.bri = Util.constrainToRange(oldBri + BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
                } else {
                    newLightState.bri = Util.constrainToRange(oldBri - BRIGHTNESS_DIM_STEP, BRIGHTNESS_MIN, BRIGHTNESS_MAX);
                }
            } else if (command instanceof HSBType) {
                HSBType hsbCommand = (HSBType) command;
                // is in CT mode or iii) already in XY mode. Only if the bulb is in HS mode, use this one.
                if ("hs".equals(colorMode)) {
                    newLightState.hue = (int) (hsbCommand.getHue().doubleValue() * HUE_FACTOR);
                    newLightState.sat = Util.fromPercentType(hsbCommand.getSaturation());
                    newLightState.bri = Util.fromPercentType(hsbCommand.getBrightness());
                } else {
                    double[] xy = ColorUtil.hsbToXY(hsbCommand);
                    newLightState.xy = new double[] { xy[0], xy[1] };
                    newLightState.bri = (int) (xy[2] * BRIGHTNESS_MAX);
                }
            } else if (command instanceof PercentType) {
                newLightState.bri = Util.fromPercentType((PercentType) command);
            } else if (command instanceof DecimalType) {
                newLightState.bri = ((DecimalType) command).intValue();
            } else {
                return;
            }
            // send on/off state together with brightness if not already set or unknown
            Integer newBri = newLightState.bri;
            if (newBri != null) {
                newLightState.on = (newBri > 0);
            }
            // fix sending bri=0 when light is already off
            if (newBri != null && newBri == 0 && currentOn != null && !currentOn) {
                return;
            }
            Double transitiontime = config.transitiontime;
            if (transitiontime != null) {
                // value is in 1/10 seconds
                newLightState.transitiontime = (int) Math.round(10 * transitiontime);
            }
            break;
        case CHANNEL_COLOR_TEMPERATURE:
            if (command instanceof DecimalType) {
                int miredValue = kelvinToMired(((DecimalType) command).intValue());
                newLightState.ct = constrainToRange(miredValue, ctMin, ctMax);
                newLightState.on = true;
            }
            break;
        case CHANNEL_POSITION:
            if (command instanceof UpDownType) {
                newLightState.open = (command == UpDownType.UP);
            } else if (command == StopMoveType.STOP) {
                newLightState.stop = true;
            } else if (command instanceof PercentType) {
                newLightState.lift = ((PercentType) command).intValue();
            } else {
                return;
            }
            break;
        default:
            // no supported command
            return;
    }
    Boolean newOn = newLightState.on;
    if (newOn != null && !newOn) {
        // if light shall be off, no other commands are allowed, so reset the new light state
        newLightState.clear();
        newLightState.on = false;
    } else if (newOn != null && newOn) {
        newLightState.ontime = onTime;
    }
    sendCommand(newLightState, command, channelUID, () -> {
        Integer transitionTime = newLightState.transitiontime;
        lastCommandExpireTimestamp = System.currentTimeMillis() + (transitionTime != null ? transitionTime : DEFAULT_COMMAND_EXPIRY_TIME);
        lastCommand = newLightState;
    });
}
Also used : LightState(org.smarthomej.binding.deconz.internal.dto.LightState) StringType(org.openhab.core.library.types.StringType) UpDownType(org.openhab.core.library.types.UpDownType) PercentType(org.openhab.core.library.types.PercentType) RefreshType(org.openhab.core.types.RefreshType) QuantityType(org.openhab.core.library.types.QuantityType) OnOffType(org.openhab.core.library.types.OnOffType) DecimalType(org.openhab.core.library.types.DecimalType) IncreaseDecreaseType(org.openhab.core.library.types.IncreaseDecreaseType) HSBType(org.openhab.core.library.types.HSBType)

Example 98 with RefreshType

use of org.openhab.core.types.RefreshType in project addons by smarthomej.

the class SensorThermostatThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (command instanceof RefreshType) {
        sensorState.buttonevent = null;
        valueUpdated(channelUID, sensorState, false);
        return;
    }
    ThermostatUpdateConfig newConfig = new ThermostatUpdateConfig();
    switch(channelUID.getId()) {
        case CHANNEL_THERMOSTAT_LOCKED:
            newConfig.locked = OnOffType.ON.equals(command);
            break;
        case CHANNEL_HEATSETPOINT:
            Integer newHeatsetpoint = getTemperatureFromCommand(command);
            if (newHeatsetpoint == null) {
                logger.warn("Heatsetpoint must not be null.");
                return;
            }
            newConfig.heatsetpoint = newHeatsetpoint;
            break;
        case CHANNEL_TEMPERATURE_OFFSET:
            Integer newOffset = getTemperatureFromCommand(command);
            if (newOffset == null) {
                logger.warn("Offset must not be null.");
                return;
            }
            newConfig.offset = newOffset;
            break;
        case CHANNEL_THERMOSTAT_MODE:
            if (command instanceof StringType) {
                String thermostatMode = ((StringType) command).toString();
                try {
                    newConfig.mode = ThermostatMode.valueOf(thermostatMode);
                } catch (IllegalArgumentException ex) {
                    logger.warn("Invalid thermostat mode: {}. Valid values: {}", thermostatMode, ThermostatMode.values());
                    return;
                }
                if (newConfig.mode == ThermostatMode.UNKNOWN) {
                    logger.warn("Invalid thermostat mode: {}. Valid values: {}", thermostatMode, ThermostatMode.values());
                    return;
                }
            } else {
                return;
            }
            break;
        case CHANNEL_EXTERNAL_WINDOW_OPEN:
            newConfig.externalwindowopen = OpenClosedType.OPEN.equals(command);
            break;
        default:
            // no supported command
            return;
    }
    sendCommand(newConfig, command, channelUID, null);
}
Also used : StringType(org.openhab.core.library.types.StringType) ThermostatUpdateConfig(org.smarthomej.binding.deconz.internal.dto.ThermostatUpdateConfig) RefreshType(org.openhab.core.types.RefreshType)

Example 99 with RefreshType

use of org.openhab.core.types.RefreshType in project addons by smarthomej.

the class SensorThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    if (command instanceof RefreshType) {
        sensorState.buttonevent = null;
        valueUpdated(channelUID, sensorState, false);
        return;
    }
    switch(channelUID.getId()) {
        case CHANNEL_ENABLED:
            if (command instanceof OnOffType) {
                SensorUpdateConfig newConfig = new SensorUpdateConfig();
                newConfig.on = OnOffType.ON.equals(command);
                sendCommand(newConfig, command, channelUID, null);
            }
            break;
    }
}
Also used : OnOffType(org.openhab.core.library.types.OnOffType) SensorUpdateConfig(org.smarthomej.binding.deconz.internal.dto.SensorUpdateConfig) RefreshType(org.openhab.core.types.RefreshType)

Example 100 with RefreshType

use of org.openhab.core.types.RefreshType in project addons by smarthomej.

the class ChaserThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    switch(channelUID.getId()) {
        case CHANNEL_SWITCH:
            if (command instanceof OnOffType) {
                if (((OnOffType) command).equals(OnOffType.ON)) {
                    Integer channelCounter = 0;
                    for (DmxChannel channel : channels) {
                        if (resumeAfter) {
                            channel.suspendAction();
                        } else {
                            channel.clearAction();
                        }
                        for (ValueSet value : values) {
                            channel.addChannelAction(new FadeAction(value.getFadeTime(), value.getValue(channelCounter), value.getHoldTime()));
                        }
                        if (resumeAfter) {
                            channel.addChannelAction(new ResumeAction());
                        }
                        channel.addListener(channelUID, this, ListenerType.ACTION);
                        channelCounter++;
                    }
                } else {
                    for (DmxChannel channel : channels) {
                        if (resumeAfter && channel.isSuspended()) {
                            channel.setChannelAction(new ResumeAction());
                        } else {
                            channel.clearAction();
                        }
                    }
                }
            } else if (command instanceof RefreshType) {
                updateState(channelUID, isRunning);
            } else {
                logger.debug("command {} not supported in channel {}:switch", command.getClass(), this.thing.getUID());
            }
            break;
        case CHANNEL_CONTROL:
            if (command instanceof StringType) {
                List<ValueSet> newValues = ValueSet.parseChaseConfig(((StringType) command).toString());
                if (!newValues.isEmpty()) {
                    values = newValues;
                    logger.debug("updated chase config in {}", this.thing.getUID());
                } else {
                    logger.debug("could not update chase config in {}, malformed: {}", this.thing.getUID(), command);
                }
            } else {
                logger.debug("command {} not supported in channel {}:control", command.getClass(), this.thing.getUID());
            }
            break;
        default:
            logger.debug("Channel {} not supported in thing {}", channelUID.getId(), this.thing.getUID());
    }
}
Also used : FadeAction(org.smarthomej.binding.dmx.internal.action.FadeAction) BaseDmxChannel(org.smarthomej.binding.dmx.internal.multiverse.BaseDmxChannel) DmxChannel(org.smarthomej.binding.dmx.internal.multiverse.DmxChannel) OnOffType(org.openhab.core.library.types.OnOffType) StringType(org.openhab.core.library.types.StringType) ResumeAction(org.smarthomej.binding.dmx.internal.action.ResumeAction) RefreshType(org.openhab.core.types.RefreshType) ValueSet(org.smarthomej.binding.dmx.internal.ValueSet)

Aggregations

RefreshType (org.openhab.core.types.RefreshType)210 OnOffType (org.openhab.core.library.types.OnOffType)81 StringType (org.openhab.core.library.types.StringType)66 DecimalType (org.openhab.core.library.types.DecimalType)60 PercentType (org.openhab.core.library.types.PercentType)50 QuantityType (org.openhab.core.library.types.QuantityType)29 Channel (org.openhab.core.thing.Channel)26 State (org.openhab.core.types.State)26 IncreaseDecreaseType (org.openhab.core.library.types.IncreaseDecreaseType)25 IOException (java.io.IOException)22 Nullable (org.eclipse.jdt.annotation.Nullable)18 Bridge (org.openhab.core.thing.Bridge)18 ChannelUID (org.openhab.core.thing.ChannelUID)16 HSBType (org.openhab.core.library.types.HSBType)15 BigDecimal (java.math.BigDecimal)14 DateTimeType (org.openhab.core.library.types.DateTimeType)11 UpDownType (org.openhab.core.library.types.UpDownType)11 Temperature (javax.measure.quantity.Temperature)8 Thing (org.openhab.core.thing.Thing)8 Command (org.openhab.core.types.Command)8